blob: 8281279bec685a780a6e7f623a47f498c3d71c08 [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);
468
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800469 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
Robert Greenwalta029ea12013-09-25 16:38:12 -0700471 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
472
Dianne Hackborn617f8772009-03-31 15:04:46 -0700473 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700474 * Note that these must match the constants in android.os.PowerManager.
475 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
476 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700477 */
478 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700479 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700480 };
481
Jeff Browndf693de2012-07-27 12:03:38 -0700482 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700483
Dianne Hackborn617f8772009-03-31 15:04:46 -0700484 public abstract void noteUserActivityLocked(int type);
485 public abstract boolean hasUserActivity();
486 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700487
488 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800489 public abstract long getNetworkActivityBytes(int type, int which);
490 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800491 public abstract long getMobileRadioActiveTime(int which);
492 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700493
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700494 /**
495 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
496 */
497 public abstract long getUserCpuTimeUs(int which);
498
499 /**
500 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
501 */
502 public abstract long getSystemCpuTimeUs(int which);
503
504 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700505 * Get the total cpu power consumed (in milli-ampere-microseconds).
506 */
507 public abstract long getCpuPowerMaUs(int which);
508
509 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700510 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
511 * given CPU cluster.
512 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700513 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700514 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700515 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
516 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700517 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700518 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800521 /*
522 * FIXME: it's not correct to use this magic value because it
523 * could clash with a sensor handle (which are defined by
524 * the sensor HAL, and therefore out of our control
525 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 // Magic sensor number for the GPS.
527 public static final int GPS = -10000;
528
529 public abstract int getHandle();
530
531 public abstract Timer getSensorTime();
532 }
533
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700534 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800535 public int mWakeNesting;
536 public long mWakeSumMs;
537 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700538 }
539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 /**
541 * The statistics associated with a particular process.
542 */
543 public static abstract class Proc {
544
Dianne Hackborn287952c2010-09-22 22:34:31 -0700545 public static class ExcessivePower {
546 public static final int TYPE_WAKE = 1;
547 public static final int TYPE_CPU = 2;
548
549 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700550 public long overTime;
551 public long usedTime;
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800555 * Returns true if this process is still active in the battery stats.
556 */
557 public abstract boolean isActive();
558
559 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700560 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700562 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 */
564 public abstract long getUserTime(int which);
565
566 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700567 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700569 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 */
571 public abstract long getSystemTime(int which);
572
573 /**
574 * Returns the number of times the process has been started.
575 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700576 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 */
578 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700579
580 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800581 * Returns the number of times the process has crashed.
582 *
583 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
584 */
585 public abstract int getNumCrashes(int which);
586
587 /**
588 * Returns the number of times the process has ANRed.
589 *
590 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
591 */
592 public abstract int getNumAnrs(int which);
593
594 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700595 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700596 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700597 * @return foreground cpu time in microseconds
598 */
599 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700600
Dianne Hackborn287952c2010-09-22 22:34:31 -0700601 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700602
Dianne Hackborn287952c2010-09-22 22:34:31 -0700603 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
606 /**
607 * The statistics associated with a particular package.
608 */
609 public static abstract class Pkg {
610
611 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700612 * Returns information about all wakeup alarms that have been triggered for this
613 * package. The mapping keys are tag names for the alarms, the counter contains
614 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700616 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
618 /**
619 * Returns a mapping containing service statistics.
620 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700621 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622
623 /**
624 * The statistics associated with a particular service.
625 */
Joe Onoratoabded112016-02-08 16:49:39 -0800626 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 /**
629 * Returns the amount of time spent started.
630 *
631 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700632 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * @return
634 */
635 public abstract long getStartTime(long batteryUptime, int which);
636
637 /**
638 * Returns the total number of times startService() has been called.
639 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700640 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 */
642 public abstract int getStarts(int which);
643
644 /**
645 * Returns the total number times the service has been launched.
646 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700647 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 */
649 public abstract int getLaunches(int which);
650 }
651 }
652 }
653
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800654 public static final class LevelStepTracker {
655 public long mLastStepTime = -1;
656 public int mNumStepDurations;
657 public final long[] mStepDurations;
658
659 public LevelStepTracker(int maxLevelSteps) {
660 mStepDurations = new long[maxLevelSteps];
661 }
662
663 public LevelStepTracker(int numSteps, long[] steps) {
664 mNumStepDurations = numSteps;
665 mStepDurations = new long[numSteps];
666 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
667 }
668
669 public long getDurationAt(int index) {
670 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
671 }
672
673 public int getLevelAt(int index) {
674 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
675 >> STEP_LEVEL_LEVEL_SHIFT);
676 }
677
678 public int getInitModeAt(int index) {
679 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
680 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
681 }
682
683 public int getModModeAt(int index) {
684 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
685 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
686 }
687
688 private void appendHex(long val, int topOffset, StringBuilder out) {
689 boolean hasData = false;
690 while (topOffset >= 0) {
691 int digit = (int)( (val>>topOffset) & 0xf );
692 topOffset -= 4;
693 if (!hasData && digit == 0) {
694 continue;
695 }
696 hasData = true;
697 if (digit >= 0 && digit <= 9) {
698 out.append((char)('0' + digit));
699 } else {
700 out.append((char)('a' + digit - 10));
701 }
702 }
703 }
704
705 public void encodeEntryAt(int index, StringBuilder out) {
706 long item = mStepDurations[index];
707 long duration = item & STEP_LEVEL_TIME_MASK;
708 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
709 >> STEP_LEVEL_LEVEL_SHIFT);
710 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
711 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
712 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
713 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
714 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
715 case Display.STATE_OFF: out.append('f'); break;
716 case Display.STATE_ON: out.append('o'); break;
717 case Display.STATE_DOZE: out.append('d'); break;
718 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
719 }
720 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
721 out.append('p');
722 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700723 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
724 out.append('i');
725 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800726 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
727 case Display.STATE_OFF: out.append('F'); break;
728 case Display.STATE_ON: out.append('O'); break;
729 case Display.STATE_DOZE: out.append('D'); break;
730 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
731 }
732 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
733 out.append('P');
734 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700735 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
736 out.append('I');
737 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800738 out.append('-');
739 appendHex(level, 4, out);
740 out.append('-');
741 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
742 }
743
744 public void decodeEntryAt(int index, String value) {
745 final int N = value.length();
746 int i = 0;
747 char c;
748 long out = 0;
749 while (i < N && (c=value.charAt(i)) != '-') {
750 i++;
751 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800752 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800753 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800754 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800755 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800756 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800757 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800758 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
759 << STEP_LEVEL_INITIAL_MODE_SHIFT);
760 break;
761 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
762 << STEP_LEVEL_INITIAL_MODE_SHIFT);
763 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700764 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
765 << STEP_LEVEL_INITIAL_MODE_SHIFT);
766 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800767 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
768 break;
769 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
770 break;
771 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
772 break;
773 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
774 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
775 break;
776 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
777 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800778 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700779 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
780 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
781 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800782 }
783 }
784 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800785 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800786 while (i < N && (c=value.charAt(i)) != '-') {
787 i++;
788 level <<= 4;
789 if (c >= '0' && c <= '9') {
790 level += c - '0';
791 } else if (c >= 'a' && c <= 'f') {
792 level += c - 'a' + 10;
793 } else if (c >= 'A' && c <= 'F') {
794 level += c - 'A' + 10;
795 }
796 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800797 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800798 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
799 long duration = 0;
800 while (i < N && (c=value.charAt(i)) != '-') {
801 i++;
802 duration <<= 4;
803 if (c >= '0' && c <= '9') {
804 duration += c - '0';
805 } else if (c >= 'a' && c <= 'f') {
806 duration += c - 'a' + 10;
807 } else if (c >= 'A' && c <= 'F') {
808 duration += c - 'A' + 10;
809 }
810 }
811 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
812 }
813
814 public void init() {
815 mLastStepTime = -1;
816 mNumStepDurations = 0;
817 }
818
819 public void clearTime() {
820 mLastStepTime = -1;
821 }
822
823 public long computeTimePerLevel() {
824 final long[] steps = mStepDurations;
825 final int numSteps = mNumStepDurations;
826
827 // For now we'll do a simple average across all steps.
828 if (numSteps <= 0) {
829 return -1;
830 }
831 long total = 0;
832 for (int i=0; i<numSteps; i++) {
833 total += steps[i] & STEP_LEVEL_TIME_MASK;
834 }
835 return total / numSteps;
836 /*
837 long[] buckets = new long[numSteps];
838 int numBuckets = 0;
839 int numToAverage = 4;
840 int i = 0;
841 while (i < numSteps) {
842 long totalTime = 0;
843 int num = 0;
844 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
845 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
846 num++;
847 }
848 buckets[numBuckets] = totalTime / num;
849 numBuckets++;
850 numToAverage *= 2;
851 i += num;
852 }
853 if (numBuckets < 1) {
854 return -1;
855 }
856 long averageTime = buckets[numBuckets-1];
857 for (i=numBuckets-2; i>=0; i--) {
858 averageTime = (averageTime + buckets[i]) / 2;
859 }
860 return averageTime;
861 */
862 }
863
864 public long computeTimeEstimate(long modesOfInterest, long modeValues,
865 int[] outNumOfInterest) {
866 final long[] steps = mStepDurations;
867 final int count = mNumStepDurations;
868 if (count <= 0) {
869 return -1;
870 }
871 long total = 0;
872 int numOfInterest = 0;
873 for (int i=0; i<count; i++) {
874 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
875 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
876 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
877 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
878 // If the modes of interest didn't change during this step period...
879 if ((modMode&modesOfInterest) == 0) {
880 // And the mode values during this period match those we are measuring...
881 if ((initMode&modesOfInterest) == modeValues) {
882 // Then this can be used to estimate the total time!
883 numOfInterest++;
884 total += steps[i] & STEP_LEVEL_TIME_MASK;
885 }
886 }
887 }
888 if (numOfInterest <= 0) {
889 return -1;
890 }
891
892 if (outNumOfInterest != null) {
893 outNumOfInterest[0] = numOfInterest;
894 }
895
896 // The estimated time is the average time we spend in each level, multipled
897 // by 100 -- the total number of battery levels
898 return (total / numOfInterest) * 100;
899 }
900
901 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
902 int stepCount = mNumStepDurations;
903 final long lastStepTime = mLastStepTime;
904 if (lastStepTime >= 0 && numStepLevels > 0) {
905 final long[] steps = mStepDurations;
906 long duration = elapsedRealtime - lastStepTime;
907 for (int i=0; i<numStepLevels; i++) {
908 System.arraycopy(steps, 0, steps, 1, steps.length-1);
909 long thisDuration = duration / (numStepLevels-i);
910 duration -= thisDuration;
911 if (thisDuration > STEP_LEVEL_TIME_MASK) {
912 thisDuration = STEP_LEVEL_TIME_MASK;
913 }
914 steps[0] = thisDuration | modeBits;
915 }
916 stepCount += numStepLevels;
917 if (stepCount > steps.length) {
918 stepCount = steps.length;
919 }
920 }
921 mNumStepDurations = stepCount;
922 mLastStepTime = elapsedRealtime;
923 }
924
925 public void readFromParcel(Parcel in) {
926 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -0700927 if (N > mStepDurations.length) {
928 throw new ParcelFormatException("more step durations than available: " + N);
929 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800930 mNumStepDurations = N;
931 for (int i=0; i<N; i++) {
932 mStepDurations[i] = in.readLong();
933 }
934 }
935
936 public void writeToParcel(Parcel out) {
937 final int N = mNumStepDurations;
938 out.writeInt(N);
939 for (int i=0; i<N; i++) {
940 out.writeLong(mStepDurations[i]);
941 }
942 }
943 }
944
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700945 public static final class PackageChange {
946 public String mPackageName;
947 public boolean mUpdate;
948 public int mVersionCode;
949 }
950
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800951 public static final class DailyItem {
952 public long mStartTime;
953 public long mEndTime;
954 public LevelStepTracker mDischargeSteps;
955 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700956 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800957 }
958
959 public abstract DailyItem getDailyItemLocked(int daysAgo);
960
961 public abstract long getCurrentDailyStartTime();
962
963 public abstract long getNextMinDailyDeadline();
964
965 public abstract long getNextMaxDailyDeadline();
966
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800967 public final static class HistoryTag {
968 public String string;
969 public int uid;
970
971 public int poolIdx;
972
973 public void setTo(HistoryTag o) {
974 string = o.string;
975 uid = o.uid;
976 poolIdx = o.poolIdx;
977 }
978
979 public void setTo(String _string, int _uid) {
980 string = _string;
981 uid = _uid;
982 poolIdx = -1;
983 }
984
985 public void writeToParcel(Parcel dest, int flags) {
986 dest.writeString(string);
987 dest.writeInt(uid);
988 }
989
990 public void readFromParcel(Parcel src) {
991 string = src.readString();
992 uid = src.readInt();
993 poolIdx = -1;
994 }
995
996 @Override
997 public boolean equals(Object o) {
998 if (this == o) return true;
999 if (o == null || getClass() != o.getClass()) return false;
1000
1001 HistoryTag that = (HistoryTag) o;
1002
1003 if (uid != that.uid) return false;
1004 if (!string.equals(that.string)) return false;
1005
1006 return true;
1007 }
1008
1009 @Override
1010 public int hashCode() {
1011 int result = string.hashCode();
1012 result = 31 * result + uid;
1013 return result;
1014 }
1015 }
1016
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001017 /**
1018 * Optional detailed information that can go into a history step. This is typically
1019 * generated each time the battery level changes.
1020 */
1021 public final static class HistoryStepDetails {
1022 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1023 public int userTime;
1024 public int systemTime;
1025
1026 // Top three apps using CPU in the last step, with times in 1/100 second.
1027 public int appCpuUid1;
1028 public int appCpuUTime1;
1029 public int appCpuSTime1;
1030 public int appCpuUid2;
1031 public int appCpuUTime2;
1032 public int appCpuSTime2;
1033 public int appCpuUid3;
1034 public int appCpuUTime3;
1035 public int appCpuSTime3;
1036
1037 // Information from /proc/stat
1038 public int statUserTime;
1039 public int statSystemTime;
1040 public int statIOWaitTime;
1041 public int statIrqTime;
1042 public int statSoftIrqTime;
1043 public int statIdlTime;
1044
1045 public HistoryStepDetails() {
1046 clear();
1047 }
1048
1049 public void clear() {
1050 userTime = systemTime = 0;
1051 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1052 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1053 = appCpuUTime3 = appCpuSTime3 = 0;
1054 }
1055
1056 public void writeToParcel(Parcel out) {
1057 out.writeInt(userTime);
1058 out.writeInt(systemTime);
1059 out.writeInt(appCpuUid1);
1060 out.writeInt(appCpuUTime1);
1061 out.writeInt(appCpuSTime1);
1062 out.writeInt(appCpuUid2);
1063 out.writeInt(appCpuUTime2);
1064 out.writeInt(appCpuSTime2);
1065 out.writeInt(appCpuUid3);
1066 out.writeInt(appCpuUTime3);
1067 out.writeInt(appCpuSTime3);
1068 out.writeInt(statUserTime);
1069 out.writeInt(statSystemTime);
1070 out.writeInt(statIOWaitTime);
1071 out.writeInt(statIrqTime);
1072 out.writeInt(statSoftIrqTime);
1073 out.writeInt(statIdlTime);
1074 }
1075
1076 public void readFromParcel(Parcel in) {
1077 userTime = in.readInt();
1078 systemTime = in.readInt();
1079 appCpuUid1 = in.readInt();
1080 appCpuUTime1 = in.readInt();
1081 appCpuSTime1 = in.readInt();
1082 appCpuUid2 = in.readInt();
1083 appCpuUTime2 = in.readInt();
1084 appCpuSTime2 = in.readInt();
1085 appCpuUid3 = in.readInt();
1086 appCpuUTime3 = in.readInt();
1087 appCpuSTime3 = in.readInt();
1088 statUserTime = in.readInt();
1089 statSystemTime = in.readInt();
1090 statIOWaitTime = in.readInt();
1091 statIrqTime = in.readInt();
1092 statSoftIrqTime = in.readInt();
1093 statIdlTime = in.readInt();
1094 }
1095 }
1096
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001097 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001098 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001099
1100 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001101 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001102
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001103 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001104 public static final byte CMD_NULL = -1;
1105 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001106 public static final byte CMD_CURRENT_TIME = 5;
1107 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001108 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001109 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001110
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001111 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001112
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001113 /**
1114 * Return whether the command code is a delta data update.
1115 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001116 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001117 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001118 }
1119
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001120 public byte batteryLevel;
1121 public byte batteryStatus;
1122 public byte batteryHealth;
1123 public byte batteryPlugType;
1124
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001125 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001126 public char batteryVoltage;
1127
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001128 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001129 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001130 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001131 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001132 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1133 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001134 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001135 public static final int STATE_PHONE_STATE_SHIFT = 6;
1136 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001137 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001138 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001139 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001140
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001141 // These states always appear directly in the first int token
1142 // of a delta change; they should be ones that change relatively
1143 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001144 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1145 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001146 public static final int STATE_GPS_ON_FLAG = 1<<29;
1147 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001148 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001149 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001150 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001151 // These are on the lower bits used for the command; if they change
1152 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001153 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001154 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001155 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001156 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1157 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1158 // empty slot
1159 // empty slot
1160 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001161
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001162 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001163 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1164
1165 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001166
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001167 public int states;
1168
Dianne Hackborn3251b902014-06-20 14:40:53 -07001169 // Constants from WIFI_SUPPL_STATE_*
1170 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1171 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1172 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1173 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1174 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1175 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1176
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001177 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001178 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1179 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1180 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001181 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001182 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1183 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1184 public static final int STATE2_CHARGING_FLAG = 1<<24;
1185 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1186 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1187 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001188 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001189
1190 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001191 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001192 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1193
1194 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001195
Dianne Hackborn40c87252014-03-19 16:55:40 -07001196 public int states2;
1197
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001198 // The wake lock that was acquired at this point.
1199 public HistoryTag wakelockTag;
1200
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001201 // Kernel wakeup reason at this point.
1202 public HistoryTag wakeReasonTag;
1203
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001204 // Non-null when there is more detailed information at this step.
1205 public HistoryStepDetails stepDetails;
1206
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001207 public static final int EVENT_FLAG_START = 0x8000;
1208 public static final int EVENT_FLAG_FINISH = 0x4000;
1209
1210 // No event in this item.
1211 public static final int EVENT_NONE = 0x0000;
1212 // Event is about a process that is running.
1213 public static final int EVENT_PROC = 0x0001;
1214 // Event is about an application package that is in the foreground.
1215 public static final int EVENT_FOREGROUND = 0x0002;
1216 // Event is about an application package that is at the top of the screen.
1217 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001218 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001219 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001220 // Events for all additional wake locks aquired/release within a wake block.
1221 // These are not generated by default.
1222 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001223 // Event is about an application executing a scheduled job.
1224 public static final int EVENT_JOB = 0x0006;
1225 // Events for users running.
1226 public static final int EVENT_USER_RUNNING = 0x0007;
1227 // Events for foreground user.
1228 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001229 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001230 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001231 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001232 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001233 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001234 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001235 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001236 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001237 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001238 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001239 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001240 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001241 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001242 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001243 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001244 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001245 // Event for a package being on the temporary whitelist.
1246 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001247 // Event for the screen waking up.
1248 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Amith Yamasani67768492015-06-09 12:23:58 -07001249
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001250 // Number of event types.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001251 public static final int EVENT_COUNT = 0x0013;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001252 // Mask to extract out only the type part of the event.
1253 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001254
1255 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1256 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1257 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1258 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1259 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1260 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001261 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1262 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001263 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1264 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001265 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1266 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1267 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1268 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1269 public static final int EVENT_USER_FOREGROUND_START =
1270 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1271 public static final int EVENT_USER_FOREGROUND_FINISH =
1272 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001273 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1274 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001275 public static final int EVENT_TEMP_WHITELIST_START =
1276 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1277 public static final int EVENT_TEMP_WHITELIST_FINISH =
1278 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001279
1280 // For CMD_EVENT.
1281 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001282 public HistoryTag eventTag;
1283
Dianne Hackborn9a755432014-05-15 17:05:22 -07001284 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001285 public long currentTime;
1286
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001287 // Meta-data when reading.
1288 public int numReadInts;
1289
1290 // Pre-allocated objects.
1291 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001292 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001293 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001294
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001295 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001296 }
1297
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001298 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001299 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001300 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001301 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001302 }
1303
1304 public int describeContents() {
1305 return 0;
1306 }
1307
1308 public void writeToParcel(Parcel dest, int flags) {
1309 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001310 int bat = (((int)cmd)&0xff)
1311 | ((((int)batteryLevel)<<8)&0xff00)
1312 | ((((int)batteryStatus)<<16)&0xf0000)
1313 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001314 | ((((int)batteryPlugType)<<24)&0xf000000)
1315 | (wakelockTag != null ? 0x10000000 : 0)
1316 | (wakeReasonTag != null ? 0x20000000 : 0)
1317 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001318 dest.writeInt(bat);
1319 bat = (((int)batteryTemperature)&0xffff)
1320 | ((((int)batteryVoltage)<<16)&0xffff0000);
1321 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001322 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001323 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001324 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001325 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001326 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001327 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001328 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001329 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001330 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001331 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001332 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001333 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001334 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001335 dest.writeLong(currentTime);
1336 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001337 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001338
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001339 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001340 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001341 int bat = src.readInt();
1342 cmd = (byte)(bat&0xff);
1343 batteryLevel = (byte)((bat>>8)&0xff);
1344 batteryStatus = (byte)((bat>>16)&0xf);
1345 batteryHealth = (byte)((bat>>20)&0xf);
1346 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001347 int bat2 = src.readInt();
1348 batteryTemperature = (short)(bat2&0xffff);
1349 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001350 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001351 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001352 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001353 wakelockTag = localWakelockTag;
1354 wakelockTag.readFromParcel(src);
1355 } else {
1356 wakelockTag = null;
1357 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001358 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001359 wakeReasonTag = localWakeReasonTag;
1360 wakeReasonTag.readFromParcel(src);
1361 } else {
1362 wakeReasonTag = null;
1363 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001364 if ((bat&0x40000000) != 0) {
1365 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001366 eventTag = localEventTag;
1367 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001368 } else {
1369 eventCode = EVENT_NONE;
1370 eventTag = null;
1371 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001372 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001373 currentTime = src.readLong();
1374 } else {
1375 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001376 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001377 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001378 }
1379
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001380 public void clear() {
1381 time = 0;
1382 cmd = CMD_NULL;
1383 batteryLevel = 0;
1384 batteryStatus = 0;
1385 batteryHealth = 0;
1386 batteryPlugType = 0;
1387 batteryTemperature = 0;
1388 batteryVoltage = 0;
1389 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001390 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001391 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001392 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001393 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001394 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001395 }
1396
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001397 public void setTo(HistoryItem o) {
1398 time = o.time;
1399 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001400 setToCommon(o);
1401 }
1402
1403 public void setTo(long time, byte cmd, HistoryItem o) {
1404 this.time = time;
1405 this.cmd = cmd;
1406 setToCommon(o);
1407 }
1408
1409 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001410 batteryLevel = o.batteryLevel;
1411 batteryStatus = o.batteryStatus;
1412 batteryHealth = o.batteryHealth;
1413 batteryPlugType = o.batteryPlugType;
1414 batteryTemperature = o.batteryTemperature;
1415 batteryVoltage = o.batteryVoltage;
1416 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001417 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001418 if (o.wakelockTag != null) {
1419 wakelockTag = localWakelockTag;
1420 wakelockTag.setTo(o.wakelockTag);
1421 } else {
1422 wakelockTag = null;
1423 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001424 if (o.wakeReasonTag != null) {
1425 wakeReasonTag = localWakeReasonTag;
1426 wakeReasonTag.setTo(o.wakeReasonTag);
1427 } else {
1428 wakeReasonTag = null;
1429 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001430 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001431 if (o.eventTag != null) {
1432 eventTag = localEventTag;
1433 eventTag.setTo(o.eventTag);
1434 } else {
1435 eventTag = null;
1436 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001437 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001438 }
1439
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001440 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001441 return batteryLevel == o.batteryLevel
1442 && batteryStatus == o.batteryStatus
1443 && batteryHealth == o.batteryHealth
1444 && batteryPlugType == o.batteryPlugType
1445 && batteryTemperature == o.batteryTemperature
1446 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001447 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001448 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001449 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001450 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001451
1452 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001453 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454 return false;
1455 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001456 if (wakelockTag != o.wakelockTag) {
1457 if (wakelockTag == null || o.wakelockTag == null) {
1458 return false;
1459 }
1460 if (!wakelockTag.equals(o.wakelockTag)) {
1461 return false;
1462 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001464 if (wakeReasonTag != o.wakeReasonTag) {
1465 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1466 return false;
1467 }
1468 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1469 return false;
1470 }
1471 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001472 if (eventTag != o.eventTag) {
1473 if (eventTag == null || o.eventTag == null) {
1474 return false;
1475 }
1476 if (!eventTag.equals(o.eventTag)) {
1477 return false;
1478 }
1479 }
1480 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001481 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001482 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001483
1484 public final static class HistoryEventTracker {
1485 private final HashMap<String, SparseIntArray>[] mActiveEvents
1486 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1487
1488 public boolean updateState(int code, String name, int uid, int poolIdx) {
1489 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1490 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1491 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1492 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001493 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001494 mActiveEvents[idx] = active;
1495 }
1496 SparseIntArray uids = active.get(name);
1497 if (uids == null) {
1498 uids = new SparseIntArray();
1499 active.put(name, uids);
1500 }
1501 if (uids.indexOfKey(uid) >= 0) {
1502 // Already set, nothing to do!
1503 return false;
1504 }
1505 uids.put(uid, poolIdx);
1506 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1507 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1508 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1509 if (active == null) {
1510 // not currently active, nothing to do.
1511 return false;
1512 }
1513 SparseIntArray uids = active.get(name);
1514 if (uids == null) {
1515 // not currently active, nothing to do.
1516 return false;
1517 }
1518 idx = uids.indexOfKey(uid);
1519 if (idx < 0) {
1520 // not currently active, nothing to do.
1521 return false;
1522 }
1523 uids.removeAt(idx);
1524 if (uids.size() <= 0) {
1525 active.remove(name);
1526 }
1527 }
1528 return true;
1529 }
1530
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001531 public void removeEvents(int code) {
1532 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1533 mActiveEvents[idx] = null;
1534 }
1535
Dianne Hackborn37de0982014-05-09 09:32:18 -07001536 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1537 return mActiveEvents[code];
1538 }
1539 }
1540
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001541 public static final class BitDescription {
1542 public final int mask;
1543 public final int shift;
1544 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001545 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001546 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001547 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001548
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001549 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001550 this.mask = mask;
1551 this.shift = -1;
1552 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001553 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001554 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001555 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001556 }
1557
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001558 public BitDescription(int mask, int shift, String name, String shortName,
1559 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001560 this.mask = mask;
1561 this.shift = shift;
1562 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001563 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001564 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001565 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001566 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001567 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001568
Dianne Hackbornfc064132014-06-02 12:42:12 -07001569 /**
1570 * Don't allow any more batching in to the current history event. This
1571 * is called when printing partial histories, so to ensure that the next
1572 * history event will go in to a new batch after what was printed in the
1573 * last partial history.
1574 */
1575 public abstract void commitCurrentHistoryBatchLocked();
1576
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001577 public abstract int getHistoryTotalSize();
1578
1579 public abstract int getHistoryUsedSize();
1580
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001581 public abstract boolean startIteratingHistoryLocked();
1582
Dianne Hackborn099bc622014-01-22 13:39:16 -08001583 public abstract int getHistoryStringPoolSize();
1584
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001585 public abstract int getHistoryStringPoolBytes();
1586
1587 public abstract String getHistoryTagPoolString(int index);
1588
1589 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001590
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001591 public abstract boolean getNextHistoryLocked(HistoryItem out);
1592
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001593 public abstract void finishIteratingHistoryLocked();
1594
1595 public abstract boolean startIteratingOldHistoryLocked();
1596
1597 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1598
1599 public abstract void finishIteratingOldHistoryLocked();
1600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001602 * Return the base time offset for the battery history.
1603 */
1604 public abstract long getHistoryBaseTime();
1605
1606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 * Returns the number of times the device has been started.
1608 */
1609 public abstract int getStartCount();
1610
1611 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001612 * 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 -08001613 * running on battery.
1614 *
1615 * {@hide}
1616 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001617 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001619 /**
1620 * Returns the number of times the screen was turned on.
1621 *
1622 * {@hide}
1623 */
1624 public abstract int getScreenOnCount(int which);
1625
Jeff Browne95c3cd2014-05-02 16:59:26 -07001626 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1627
Dianne Hackborn617f8772009-03-31 15:04:46 -07001628 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1629 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1630 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1631 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1632 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1633
1634 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1635 "dark", "dim", "medium", "light", "bright"
1636 };
1637
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001638 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1639 "0", "1", "2", "3", "4"
1640 };
1641
Dianne Hackborn617f8772009-03-31 15:04:46 -07001642 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001643
Dianne Hackborn617f8772009-03-31 15:04:46 -07001644 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001645 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001646 * the given brightness
1647 *
1648 * {@hide}
1649 */
1650 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001651 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001654 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001655 * running on battery.
1656 *
1657 * {@hide}
1658 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001659 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001660
1661 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001662 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001663 *
1664 * {@hide}
1665 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001666 public abstract int getPowerSaveModeEnabledCount(int which);
1667
1668 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001669 * Constant for device idle mode: not active.
1670 */
1671 public static final int DEVICE_IDLE_MODE_OFF = 0;
1672
1673 /**
1674 * Constant for device idle mode: active in lightweight mode.
1675 */
1676 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1677
1678 /**
1679 * Constant for device idle mode: active in full mode.
1680 */
1681 public static final int DEVICE_IDLE_MODE_FULL = 2;
1682
1683 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001684 * Returns the time in microseconds that device has been in idle mode while
1685 * running on battery.
1686 *
1687 * {@hide}
1688 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001689 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001690
1691 /**
1692 * Returns the number of times that the devie has gone in to idle mode.
1693 *
1694 * {@hide}
1695 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001696 public abstract int getDeviceIdleModeCount(int mode, int which);
1697
1698 /**
1699 * Return the longest duration we spent in a particular device idle mode (fully in the
1700 * mode, not in idle maintenance etc).
1701 */
1702 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001703
1704 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001705 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001706 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001707 * counts all of the time that we consider the device to be idle, whether or not
1708 * it is currently in the actual device idle mode.
1709 *
1710 * {@hide}
1711 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001712 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001713
1714 /**
1715 * Returns the number of times that the devie has started idling.
1716 *
1717 * {@hide}
1718 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001719 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001720
1721 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001722 * Returns the number of times that connectivity state changed.
1723 *
1724 * {@hide}
1725 */
1726 public abstract int getNumConnectivityChange(int which);
1727
1728 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001729 * 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 -08001730 * running on battery.
1731 *
1732 * {@hide}
1733 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001734 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001735
1736 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001737 * Returns the number of times a phone call was activated.
1738 *
1739 * {@hide}
1740 */
1741 public abstract int getPhoneOnCount(int which);
1742
1743 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001744 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001745 * the given signal strength.
1746 *
1747 * {@hide}
1748 */
1749 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001750 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001751
Dianne Hackborn617f8772009-03-31 15:04:46 -07001752 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001753 * Returns the time in microseconds that the phone has been trying to
1754 * acquire a signal.
1755 *
1756 * {@hide}
1757 */
1758 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001759 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001760
1761 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001762 * Returns the number of times the phone has entered the given signal strength.
1763 *
1764 * {@hide}
1765 */
1766 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1767
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001768 /**
1769 * Returns the time in microseconds that the mobile network has been active
1770 * (in a high power state).
1771 *
1772 * {@hide}
1773 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001774 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001775
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001776 /**
1777 * Returns the number of times that the mobile network has transitioned to the
1778 * active state.
1779 *
1780 * {@hide}
1781 */
1782 public abstract int getMobileRadioActiveCount(int which);
1783
1784 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001785 * Returns the time in microseconds that is the difference between the mobile radio
1786 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1787 * from the radio.
1788 *
1789 * {@hide}
1790 */
1791 public abstract long getMobileRadioActiveAdjustedTime(int which);
1792
1793 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001794 * Returns the time in microseconds that the mobile network has been active
1795 * (in a high power state) but not being able to blame on an app.
1796 *
1797 * {@hide}
1798 */
1799 public abstract long getMobileRadioActiveUnknownTime(int which);
1800
1801 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001802 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001803 *
1804 * {@hide}
1805 */
1806 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001807
Dianne Hackborn627bba72009-03-24 22:32:56 -07001808 public static final int DATA_CONNECTION_NONE = 0;
1809 public static final int DATA_CONNECTION_GPRS = 1;
1810 public static final int DATA_CONNECTION_EDGE = 2;
1811 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001812 public static final int DATA_CONNECTION_CDMA = 4;
1813 public static final int DATA_CONNECTION_EVDO_0 = 5;
1814 public static final int DATA_CONNECTION_EVDO_A = 6;
1815 public static final int DATA_CONNECTION_1xRTT = 7;
1816 public static final int DATA_CONNECTION_HSDPA = 8;
1817 public static final int DATA_CONNECTION_HSUPA = 9;
1818 public static final int DATA_CONNECTION_HSPA = 10;
1819 public static final int DATA_CONNECTION_IDEN = 11;
1820 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001821 public static final int DATA_CONNECTION_LTE = 13;
1822 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001823 public static final int DATA_CONNECTION_HSPAP = 15;
1824 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001825
Dianne Hackborn627bba72009-03-24 22:32:56 -07001826 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001827 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001828 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001829 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001830 };
1831
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001832 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001833
1834 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001835 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001836 * the given data connection.
1837 *
1838 * {@hide}
1839 */
1840 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001841 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 * Returns the number of times the phone has entered the given data
1845 * connection type.
1846 *
1847 * {@hide}
1848 */
1849 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001850
Dianne Hackborn3251b902014-06-20 14:40:53 -07001851 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1852 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1853 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1854 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1855 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1856 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1857 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1858 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1859 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1860 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1861 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1862 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1863 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1864
1865 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1866
1867 static final String[] WIFI_SUPPL_STATE_NAMES = {
1868 "invalid", "disconn", "disabled", "inactive", "scanning",
1869 "authenticating", "associating", "associated", "4-way-handshake",
1870 "group-handshake", "completed", "dormant", "uninit"
1871 };
1872
1873 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1874 "inv", "dsc", "dis", "inact", "scan",
1875 "auth", "ascing", "asced", "4-way",
1876 "group", "compl", "dorm", "uninit"
1877 };
1878
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001879 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1880 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001881 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001882 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1883 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001884 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001885 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1886 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1887 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001888 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001889 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001890 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001891 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001892 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1893 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001894 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1895 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1896 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1897 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1898 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1899 new String[] {"in", "out", "emergency", "off"},
1900 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001901 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1902 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1903 SignalStrength.SIGNAL_STRENGTH_NAMES,
1904 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001905 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1906 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1907 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001908 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001909
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001910 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1911 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001912 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001913 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001914 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001915 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001916 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001917 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
1918 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
1919 new String[] { "off", "light", "full", "???" },
1920 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001921 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1922 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1923 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001924 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1925 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1926 new String[] { "0", "1", "2", "3", "4" },
1927 new String[] { "0", "1", "2", "3", "4" }),
1928 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1929 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1930 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001931 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001932 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001933 };
1934
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001935 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001936 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001937 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
1938 "screenwake",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001939 };
1940
1941 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001942 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001943 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
1944 "Esw",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001945 };
1946
Dianne Hackborn617f8772009-03-31 15:04:46 -07001947 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001948 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001949 * running on battery.
1950 *
1951 * {@hide}
1952 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001953 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001954
1955 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001956 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001957 * been in the running state while the device was running on battery.
1958 *
1959 * {@hide}
1960 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001961 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001962
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001963 public static final int WIFI_STATE_OFF = 0;
1964 public static final int WIFI_STATE_OFF_SCANNING = 1;
1965 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1966 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1967 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1968 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1969 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1970 public static final int WIFI_STATE_SOFT_AP = 7;
1971
1972 static final String[] WIFI_STATE_NAMES = {
1973 "off", "scanning", "no_net", "disconn",
1974 "sta", "p2p", "sta_p2p", "soft_ap"
1975 };
1976
1977 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1978
1979 /**
1980 * Returns the time in microseconds that WiFi has been running in the given state.
1981 *
1982 * {@hide}
1983 */
1984 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001985 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001986
1987 /**
1988 * Returns the number of times that WiFi has entered the given state.
1989 *
1990 * {@hide}
1991 */
1992 public abstract int getWifiStateCount(int wifiState, int which);
1993
The Android Open Source Project10592532009-03-18 17:39:46 -07001994 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001995 * Returns the time in microseconds that the wifi supplicant has been
1996 * in a given state.
1997 *
1998 * {@hide}
1999 */
2000 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2001
2002 /**
2003 * Returns the number of times that the wifi supplicant has transitioned
2004 * to a given state.
2005 *
2006 * {@hide}
2007 */
2008 public abstract int getWifiSupplStateCount(int state, int which);
2009
2010 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2011
2012 /**
2013 * Returns the time in microseconds that WIFI has been running with
2014 * the given signal strength.
2015 *
2016 * {@hide}
2017 */
2018 public abstract long getWifiSignalStrengthTime(int strengthBin,
2019 long elapsedRealtimeUs, int which);
2020
2021 /**
2022 * Returns the number of times WIFI has entered the given signal strength.
2023 *
2024 * {@hide}
2025 */
2026 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2027
2028 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002029 * Returns the time in microseconds that the flashlight has been on while the device was
2030 * running on battery.
2031 *
2032 * {@hide}
2033 */
2034 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2035
2036 /**
2037 * Returns the number of times that the flashlight has been turned on while the device was
2038 * running on battery.
2039 *
2040 * {@hide}
2041 */
2042 public abstract long getFlashlightOnCount(int which);
2043
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002044 /**
2045 * Returns the time in microseconds that the camera has been on while the device was
2046 * running on battery.
2047 *
2048 * {@hide}
2049 */
2050 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2051
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002052 /**
2053 * Returns the time in microseconds that bluetooth scans were running while the device was
2054 * on battery.
2055 *
2056 * {@hide}
2057 */
2058 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002059
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002060 public static final int NETWORK_MOBILE_RX_DATA = 0;
2061 public static final int NETWORK_MOBILE_TX_DATA = 1;
2062 public static final int NETWORK_WIFI_RX_DATA = 2;
2063 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002064 public static final int NETWORK_BT_RX_DATA = 4;
2065 public static final int NETWORK_BT_TX_DATA = 5;
2066 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002067
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002068 public abstract long getNetworkActivityBytes(int type, int which);
2069 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002070
Adam Lesinskie08af192015-03-25 16:42:59 -07002071 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002072 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002073 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002074 * actual power data.
2075 */
2076 public abstract boolean hasWifiActivityReporting();
2077
2078 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002079 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2080 * in various radio controller states, such as transmit, receive, and idle.
2081 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002082 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002083 public abstract ControllerActivityCounter getWifiControllerActivity();
2084
2085 /**
2086 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2087 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2088 * actual power data.
2089 */
2090 public abstract boolean hasBluetoothActivityReporting();
2091
2092 /**
2093 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2094 * in various radio controller states, such as transmit, receive, and idle.
2095 * @return non-null {@link ControllerActivityCounter}
2096 */
2097 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2098
2099 /**
2100 * Returns true if the BatteryStats object has detailed modem power reports.
2101 * When true, calling {@link #getModemControllerActivity()} will yield the
2102 * actual power data.
2103 */
2104 public abstract boolean hasModemActivityReporting();
2105
2106 /**
2107 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2108 * in various radio controller states, such as transmit, receive, and idle.
2109 * @return non-null {@link ControllerActivityCounter}
2110 */
2111 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002112
The Android Open Source Project10592532009-03-18 17:39:46 -07002113 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002114 * Return the wall clock time when battery stats data collection started.
2115 */
2116 public abstract long getStartClockTime();
2117
2118 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002119 * Return platform version tag that we were running in when the battery stats started.
2120 */
2121 public abstract String getStartPlatformVersion();
2122
2123 /**
2124 * Return platform version tag that we were running in when the battery stats ended.
2125 */
2126 public abstract String getEndPlatformVersion();
2127
2128 /**
2129 * Return the internal version code of the parcelled format.
2130 */
2131 public abstract int getParcelVersion();
2132
2133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 * Return whether we are currently running on battery.
2135 */
2136 public abstract boolean getIsOnBattery();
2137
2138 /**
2139 * Returns a SparseArray containing the statistics for each uid.
2140 */
2141 public abstract SparseArray<? extends Uid> getUidStats();
2142
2143 /**
2144 * Returns the current battery uptime in microseconds.
2145 *
2146 * @param curTime the amount of elapsed realtime in microseconds.
2147 */
2148 public abstract long getBatteryUptime(long curTime);
2149
2150 /**
2151 * Returns the current battery realtime in microseconds.
2152 *
2153 * @param curTime the amount of elapsed realtime in microseconds.
2154 */
2155 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002156
2157 /**
Evan Millar633a1742009-04-02 16:36:33 -07002158 * Returns the battery percentage level at the last time the device was unplugged from power, or
2159 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002160 */
Evan Millar633a1742009-04-02 16:36:33 -07002161 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002162
2163 /**
Evan Millar633a1742009-04-02 16:36:33 -07002164 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2165 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002166 */
Evan Millar633a1742009-04-02 16:36:33 -07002167 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168
2169 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002170 * Get the amount the battery has discharged since the stats were
2171 * last reset after charging, as a lower-end approximation.
2172 */
2173 public abstract int getLowDischargeAmountSinceCharge();
2174
2175 /**
2176 * Get the amount the battery has discharged since the stats were
2177 * last reset after charging, as an upper-end approximation.
2178 */
2179 public abstract int getHighDischargeAmountSinceCharge();
2180
2181 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002182 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2183 */
2184 public abstract int getDischargeAmount(int which);
2185
2186 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002187 * Get the amount the battery has discharged while the screen was on,
2188 * since the last time power was unplugged.
2189 */
2190 public abstract int getDischargeAmountScreenOn();
2191
2192 /**
2193 * Get the amount the battery has discharged while the screen was on,
2194 * since the last time the device was charged.
2195 */
2196 public abstract int getDischargeAmountScreenOnSinceCharge();
2197
2198 /**
2199 * Get the amount the battery has discharged while the screen was off,
2200 * since the last time power was unplugged.
2201 */
2202 public abstract int getDischargeAmountScreenOff();
2203
2204 /**
2205 * Get the amount the battery has discharged while the screen was off,
2206 * since the last time the device was charged.
2207 */
2208 public abstract int getDischargeAmountScreenOffSinceCharge();
2209
2210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * Returns the total, last, or current battery uptime in microseconds.
2212 *
2213 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002214 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 */
2216 public abstract long computeBatteryUptime(long curTime, int which);
2217
2218 /**
2219 * Returns the total, last, or current battery realtime in microseconds.
2220 *
2221 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002222 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 */
2224 public abstract long computeBatteryRealtime(long curTime, int which);
2225
2226 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002227 * Returns the total, last, or current battery screen off uptime in microseconds.
2228 *
2229 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002230 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002231 */
2232 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2233
2234 /**
2235 * Returns the total, last, or current battery screen off realtime in microseconds.
2236 *
2237 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002238 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002239 */
2240 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2241
2242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 * Returns the total, last, or current uptime in microseconds.
2244 *
2245 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002246 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 */
2248 public abstract long computeUptime(long curTime, int which);
2249
2250 /**
2251 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002254 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 */
2256 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002257
2258 /**
2259 * Compute an approximation for how much run time (in microseconds) is remaining on
2260 * the battery. Returns -1 if no time can be computed: either there is not
2261 * enough current data to make a decision, or the battery is currently
2262 * charging.
2263 *
2264 * @param curTime The current elepsed realtime in microseconds.
2265 */
2266 public abstract long computeBatteryTimeRemaining(long curTime);
2267
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002268 // The part of a step duration that is the actual time.
2269 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2270
2271 // Bits in a step duration that are the new battery level we are at.
2272 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002273 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002274
2275 // Bits in a step duration that are the initial mode we were in at that step.
2276 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002277 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002278
2279 // Bits in a step duration that indicate which modes changed during that step.
2280 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002281 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002282
2283 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2284 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2285
2286 // Step duration mode: power save is on.
2287 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2288
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002289 // Step duration mode: device is currently in idle mode.
2290 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2291
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002292 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2293 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002294 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2295 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002296 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2297 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,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002301 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2302 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002303 };
2304 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2305 (Display.STATE_OFF-1),
2306 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002307 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002308 (Display.STATE_ON-1),
2309 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2310 (Display.STATE_DOZE-1),
2311 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2312 (Display.STATE_DOZE_SUSPEND-1),
2313 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002314 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002315 };
2316 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2317 "screen off",
2318 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002319 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002320 "screen on",
2321 "screen on power save",
2322 "screen doze",
2323 "screen doze power save",
2324 "screen doze-suspend",
2325 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002326 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002327 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002328
2329 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002330 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002331 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002332 public abstract LevelStepTracker getDischargeLevelStepTracker();
2333
2334 /**
2335 * Return the array of daily discharge step durations.
2336 */
2337 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002338
2339 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002340 * Compute an approximation for how much time (in microseconds) remains until the battery
2341 * is fully charged. Returns -1 if no time can be computed: either there is not
2342 * enough current data to make a decision, or the battery is currently
2343 * discharging.
2344 *
2345 * @param curTime The current elepsed realtime in microseconds.
2346 */
2347 public abstract long computeChargeTimeRemaining(long curTime);
2348
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002349 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002350 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002351 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002352 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002353
2354 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002355 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002356 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002357 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002358
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002359 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2360
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002361 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002362
Evan Millarc64edde2009-04-18 12:26:32 -07002363 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002365 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2366
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002367 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 long days = seconds / (60 * 60 * 24);
2369 if (days != 0) {
2370 out.append(days);
2371 out.append("d ");
2372 }
2373 long used = days * 60 * 60 * 24;
2374
2375 long hours = (seconds - used) / (60 * 60);
2376 if (hours != 0 || used != 0) {
2377 out.append(hours);
2378 out.append("h ");
2379 }
2380 used += hours * 60 * 60;
2381
2382 long mins = (seconds-used) / 60;
2383 if (mins != 0 || used != 0) {
2384 out.append(mins);
2385 out.append("m ");
2386 }
2387 used += mins * 60;
2388
2389 if (seconds != 0 || used != 0) {
2390 out.append(seconds-used);
2391 out.append("s ");
2392 }
2393 }
2394
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002395 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002397 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 sb.append(time - (sec * 1000));
2399 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002402 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002403 long sec = time / 1000;
2404 formatTimeRaw(sb, sec);
2405 sb.append(time - (sec * 1000));
2406 sb.append("ms");
2407 }
2408
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002409 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002411 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413 float perc = ((float)num) / ((float)den) * 100;
2414 mFormatBuilder.setLength(0);
2415 mFormatter.format("%.1f%%", perc);
2416 return mFormatBuilder.toString();
2417 }
2418
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002419 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002420 mFormatBuilder.setLength(0);
2421
2422 if (bytes < BYTES_PER_KB) {
2423 return bytes + "B";
2424 } else if (bytes < BYTES_PER_MB) {
2425 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2426 return mFormatBuilder.toString();
2427 } else if (bytes < BYTES_PER_GB){
2428 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2429 return mFormatBuilder.toString();
2430 } else {
2431 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2432 return mFormatBuilder.toString();
2433 }
2434 }
2435
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002436 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002437 if (timer != null) {
2438 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002439 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002440 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2441 return totalTimeMillis;
2442 }
2443 return 0;
2444 }
2445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 /**
2447 *
2448 * @param sb a StringBuilder object.
2449 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002450 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002452 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 * @param linePrefix a String to be prepended to each line of output.
2454 * @return the line prefix
2455 */
2456 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002457 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002460 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002461
Evan Millarc64edde2009-04-18 12:26:32 -07002462 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 if (totalTimeMillis != 0) {
2464 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002465 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002466 if (name != null) {
2467 sb.append(name);
2468 sb.append(' ');
2469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 sb.append('(');
2471 sb.append(count);
2472 sb.append(" times)");
2473 return ", ";
2474 }
2475 }
2476 return linePrefix;
2477 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002478
2479 /**
2480 *
2481 * @param pw a PrintWriter object to print to.
2482 * @param sb a StringBuilder object.
2483 * @param timer a Timer object contining the wakelock times.
2484 * @param rawRealtime the current on-battery time in microseconds.
2485 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2486 * @param prefix a String to be prepended to each line of output.
2487 * @param type the name of the timer.
2488 */
2489 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2490 long rawRealtime, int which, String prefix, String type) {
2491 if (timer != null) {
2492 // Convert from microseconds to milliseconds with rounding
2493 final long totalTime = (timer.getTotalTimeLocked(
2494 rawRealtime, which) + 500) / 1000;
2495 final int count = timer.getCountLocked(which);
2496 if (totalTime != 0) {
2497 sb.setLength(0);
2498 sb.append(prefix);
2499 sb.append(" ");
2500 sb.append(type);
2501 sb.append(": ");
2502 formatTimeMs(sb, totalTime);
2503 sb.append("realtime (");
2504 sb.append(count);
2505 sb.append(" times)");
2506 pw.println(sb.toString());
2507 return true;
2508 }
2509 }
2510 return false;
2511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512
2513 /**
2514 * Checkin version of wakelock printer. Prints simple comma-separated list.
2515 *
2516 * @param sb a StringBuilder object.
2517 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002518 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002520 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 * @param linePrefix a String to be prepended to each line of output.
2522 * @return the line prefix
2523 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002524 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2525 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 long totalTimeMicros = 0;
2527 int count = 0;
2528 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002529 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002530 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
2532 sb.append(linePrefix);
2533 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2534 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002535 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 sb.append(count);
2537 return ",";
2538 }
2539
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002540 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2541 String type) {
2542 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2543 pw.print(',');
2544 pw.print(uid);
2545 pw.print(',');
2546 pw.print(category);
2547 pw.print(',');
2548 pw.print(type);
2549 }
2550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 /**
2552 * Dump a comma-separated line of values for terse checkin mode.
2553 *
2554 * @param pw the PageWriter to dump log to
2555 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2556 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2557 * @param args type-dependent data arguments
2558 */
2559 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2560 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002561 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002562 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002563 pw.print(',');
2564 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002566 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002568
2569 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002570 * Dump a given timer stat for terse checkin mode.
2571 *
2572 * @param pw the PageWriter to dump log to
2573 * @param uid the UID to log
2574 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2575 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2576 * @param timer a {@link Timer} to dump stats for
2577 * @param rawRealtime the current elapsed realtime of the system in microseconds
2578 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2579 */
2580 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2581 Timer timer, long rawRealtime, int which) {
2582 if (timer != null) {
2583 // Convert from microseconds to milliseconds with rounding
2584 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2585 / 1000;
2586 final int count = timer.getCountLocked(which);
2587 if (totalTime != 0) {
2588 dumpLine(pw, uid, category, type, totalTime, count);
2589 }
2590 }
2591 }
2592
2593 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002594 * Checks if the ControllerActivityCounter has any data worth dumping.
2595 */
2596 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2597 if (counter == null) {
2598 return false;
2599 }
2600
2601 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2602 || counter.getRxTimeCounter().getCountLocked(which) != 0
2603 || counter.getPowerCounter().getCountLocked(which) != 0) {
2604 return true;
2605 }
2606
2607 for (LongCounter c : counter.getTxTimeCounters()) {
2608 if (c.getCountLocked(which) != 0) {
2609 return true;
2610 }
2611 }
2612 return false;
2613 }
2614
2615 /**
2616 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2617 * The order of the arguments in the final check in line is:
2618 *
2619 * idle, rx, power, tx...
2620 *
2621 * where tx... is one or more transmit level times.
2622 */
2623 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2624 String type,
2625 ControllerActivityCounter counter,
2626 int which) {
2627 if (!controllerActivityHasData(counter, which)) {
2628 return;
2629 }
2630
2631 dumpLineHeader(pw, uid, category, type);
2632 pw.print(",");
2633 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2634 pw.print(",");
2635 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2636 pw.print(",");
2637 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2638 for (LongCounter c : counter.getTxTimeCounters()) {
2639 pw.print(",");
2640 pw.print(c.getCountLocked(which));
2641 }
2642 pw.println();
2643 }
2644
2645 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2646 String prefix, String controllerName,
2647 ControllerActivityCounter counter,
2648 int which) {
2649 if (controllerActivityHasData(counter, which)) {
2650 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2651 }
2652 }
2653
2654 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2655 String controllerName,
2656 ControllerActivityCounter counter, int which) {
2657 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2658 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2659 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2660 long totalTxTimeMs = 0;
2661 for (LongCounter txState : counter.getTxTimeCounters()) {
2662 totalTxTimeMs += txState.getCountLocked(which);
2663 }
2664
2665 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2666
2667 sb.setLength(0);
2668 sb.append(prefix);
2669 sb.append(" ");
2670 sb.append(controllerName);
2671 sb.append(" Idle time: ");
2672 formatTimeMs(sb, idleTimeMs);
2673 sb.append("(");
2674 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2675 sb.append(")");
2676 pw.println(sb.toString());
2677
2678 sb.setLength(0);
2679 sb.append(prefix);
2680 sb.append(" ");
2681 sb.append(controllerName);
2682 sb.append(" Rx time: ");
2683 formatTimeMs(sb, rxTimeMs);
2684 sb.append("(");
2685 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2686 sb.append(")");
2687 pw.println(sb.toString());
2688
2689 sb.setLength(0);
2690 sb.append(prefix);
2691 sb.append(" ");
2692 sb.append(controllerName);
2693 sb.append(" Tx time: ");
2694 formatTimeMs(sb, totalTxTimeMs);
2695 sb.append("(");
2696 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2697 sb.append(")");
2698 pw.println(sb.toString());
2699
2700 final int numTxLvls = counter.getTxTimeCounters().length;
2701 if (numTxLvls > 1) {
2702 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2703 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2704 sb.setLength(0);
2705 sb.append(prefix);
2706 sb.append(" [");
2707 sb.append(lvl);
2708 sb.append("] ");
2709 formatTimeMs(sb, txLvlTimeMs);
2710 sb.append("(");
2711 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2712 sb.append(")");
2713 pw.println(sb.toString());
2714 }
2715 }
2716
2717 sb.setLength(0);
2718 sb.append(prefix);
2719 sb.append(" ");
2720 sb.append(controllerName);
2721 sb.append(" Power drain: ").append(
2722 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2723 sb.append("mAh");
2724 pw.println(sb.toString());
2725 }
2726
2727 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002728 * Temporary for settings.
2729 */
2730 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2731 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2732 }
2733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 /**
2735 * Checkin server version of dump to produce more compact, computer-readable log.
2736 *
2737 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002739 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2740 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2742 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2743 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2745 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002746 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2747 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2748 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 final long totalRealtime = computeRealtime(rawRealtime, which);
2750 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002751 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002752 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002753 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002754 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2755 rawRealtime, which);
2756 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
2757 rawRealtime, which);
2758 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2759 rawRealtime, which);
2760 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
2761 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002762 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002763 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002764
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002765 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002767 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002768 final int NU = uidStats.size();
2769
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002770 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 // Dump "battery" stat
2773 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002774 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002775 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002776 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002777 getStartClockTime(),
2778 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002780 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002781 long fullWakeLockTimeTotal = 0;
2782 long partialWakeLockTimeTotal = 0;
2783
2784 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002785 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002786
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002787 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2788 = u.getWakelockStats();
2789 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2790 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002791
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002792 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2793 if (fullWakeTimer != null) {
2794 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2795 which);
2796 }
2797
2798 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2799 if (partialWakeTimer != null) {
2800 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2801 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002802 }
2803 }
2804 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002805
2806 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002807 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2808 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2809 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2810 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2811 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2812 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2813 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2814 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002815 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
2816 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002817 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2818 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002819 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
2820 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002821
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002822 // Dump Modem controller stats
2823 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
2824 getModemControllerActivity(), which);
2825
Adam Lesinskie283d332015-04-16 12:29:25 -07002826 // Dump Wifi controller stats
2827 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2828 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002829 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08002830 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002831
2832 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
2833 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002834
2835 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002836 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
2837 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 // Dump misc stats
2840 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002841 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002842 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002843 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002844 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002845 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
2846 getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which), deviceIdlingTime / 1000,
2847 getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07002848 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002849 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
2850 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
2851 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
2852 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
2853 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002854
2855 // Dump screen brightness stats
2856 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2857 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002858 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002859 }
2860 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002861
Dianne Hackborn627bba72009-03-24 22:32:56 -07002862 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002863 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2864 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002865 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002866 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002867 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002868 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002869 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002870 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002871 args[i] = getPhoneSignalStrengthCount(i, which);
2872 }
2873 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002874
Dianne Hackborn627bba72009-03-24 22:32:56 -07002875 // Dump network type stats
2876 args = new Object[NUM_DATA_CONNECTION_TYPES];
2877 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002878 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002879 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002880 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2881 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2882 args[i] = getPhoneDataConnectionCount(i, which);
2883 }
2884 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002885
2886 // Dump wifi state stats
2887 args = new Object[NUM_WIFI_STATES];
2888 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002889 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002890 }
2891 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2892 for (int i=0; i<NUM_WIFI_STATES; i++) {
2893 args[i] = getWifiStateCount(i, which);
2894 }
2895 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2896
Dianne Hackborn3251b902014-06-20 14:40:53 -07002897 // Dump wifi suppl state stats
2898 args = new Object[NUM_WIFI_SUPPL_STATES];
2899 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2900 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2901 }
2902 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2903 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2904 args[i] = getWifiSupplStateCount(i, which);
2905 }
2906 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2907
2908 // Dump wifi signal strength stats
2909 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2910 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2911 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2912 }
2913 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2914 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2915 args[i] = getWifiSignalStrengthCount(i, which);
2916 }
2917 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2918
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002919 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002920 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002921 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002924 if (which == STATS_SINCE_UNPLUGGED) {
2925 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2926 getDischargeStartLevel()-getDischargeCurrentLevel(),
2927 getDischargeStartLevel()-getDischargeCurrentLevel(),
2928 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2929 } else {
2930 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2931 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002932 getDischargeAmountScreenOnSinceCharge(),
2933 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002934 }
2935
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002936 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002937 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002938 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002939 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002940 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002941 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002942 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002943 sb.toString());
2944 }
Evan Millarc64edde2009-04-18 12:26:32 -07002945 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002946 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002947 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002948 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2949 // Not doing the regular wake lock formatting to remain compatible
2950 // with the old checkin format.
2951 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2952 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002953 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002954 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002955 }
2956 }
Evan Millarc64edde2009-04-18 12:26:32 -07002957 }
2958
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002959 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002960 helper.create(this);
2961 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002962 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002963 if (sippers != null && sippers.size() > 0) {
2964 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2965 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002966 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002967 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2968 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2969 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002970 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002971 int uid = 0;
2972 String label;
2973 switch (bs.drainType) {
2974 case IDLE:
2975 label="idle";
2976 break;
2977 case CELL:
2978 label="cell";
2979 break;
2980 case PHONE:
2981 label="phone";
2982 break;
2983 case WIFI:
2984 label="wifi";
2985 break;
2986 case BLUETOOTH:
2987 label="blue";
2988 break;
2989 case SCREEN:
2990 label="scrn";
2991 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002992 case FLASHLIGHT:
2993 label="flashlight";
2994 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002995 case APP:
2996 uid = bs.uidObj.getUid();
2997 label = "uid";
2998 break;
2999 case USER:
3000 uid = UserHandle.getUid(bs.userId, 0);
3001 label = "user";
3002 break;
3003 case UNACCOUNTED:
3004 label = "unacc";
3005 break;
3006 case OVERCOUNTED:
3007 label = "over";
3008 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003009 case CAMERA:
3010 label = "camera";
3011 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003012 default:
3013 label = "???";
3014 }
3015 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003016 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003017 }
3018 }
3019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 for (int iu = 0; iu < NU; iu++) {
3021 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003022 if (reqUid >= 0 && uid != reqUid) {
3023 continue;
3024 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003025 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003028 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3029 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3030 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3031 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3032 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3033 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3034 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3035 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
3036 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3037 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003038 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3039 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003040 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3041 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003042 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
3043 || btBytesRx > 0 || btBytesTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003044 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3045 wifiBytesRx, wifiBytesTx,
3046 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003047 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003048 mobileActiveTime, mobileActiveCount,
3049 btBytesRx, btBytesTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003050 }
3051
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003052 // Dump modem controller data, per UID.
3053 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3054 u.getModemControllerActivity(), which);
3055
3056 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003057 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3058 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3059 final int wifiScanCount = u.getWifiScanCount(which);
3060 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003061 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003062 || uidWifiRunningTime != 0) {
3063 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3064 uidWifiRunningTime, wifiScanCount,
3065 /* legacy fields follow, keep at 0 */ 0, 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003068 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3069 u.getWifiControllerActivity(), which);
3070
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003071 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3072 u.getBluetoothControllerActivity(), which);
3073
Dianne Hackborn617f8772009-03-31 15:04:46 -07003074 if (u.hasUserActivity()) {
3075 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3076 boolean hasData = false;
3077 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3078 int val = u.getUserActivityCount(i, which);
3079 args[i] = val;
3080 if (val != 0) hasData = true;
3081 }
3082 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003083 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003084 }
3085 }
3086
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003087 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3088 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3089 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3090 String linePrefix = "";
3091 sb.setLength(0);
3092 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3093 rawRealtime, "f", which, linePrefix);
3094 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3095 rawRealtime, "p", which, linePrefix);
3096 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3097 rawRealtime, "w", which, linePrefix);
3098
3099 // Only log if we had at lease one wakelock...
3100 if (sb.length() > 0) {
3101 String name = wakelocks.keyAt(iw);
3102 if (name.indexOf(',') >= 0) {
3103 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003105 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 }
3107 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003108
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003109 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3110 for (int isy=syncs.size()-1; isy>=0; isy--) {
3111 final Timer timer = syncs.valueAt(isy);
3112 // Convert from microseconds to milliseconds with rounding
3113 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3114 final int count = timer.getCountLocked(which);
3115 if (totalTime != 0) {
3116 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003117 }
3118 }
3119
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003120 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3121 for (int ij=jobs.size()-1; ij>=0; ij--) {
3122 final Timer timer = jobs.valueAt(ij);
3123 // Convert from microseconds to milliseconds with rounding
3124 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3125 final int count = timer.getCountLocked(which);
3126 if (totalTime != 0) {
3127 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003128 }
3129 }
3130
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003131 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3132 rawRealtime, which);
3133 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3134 rawRealtime, which);
3135 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3136 rawRealtime, which);
3137 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3138 rawRealtime, which);
3139
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003140 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3141 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003142 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003143 final Uid.Sensor se = sensors.valueAt(ise);
3144 final int sensorNumber = sensors.keyAt(ise);
3145 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003146 if (timer != null) {
3147 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003148 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3149 / 1000;
3150 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003151 if (totalTime != 0) {
3152 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
3153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155 }
3156
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003157 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3158 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003159
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003160 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3161 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003162
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003163 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003164 long totalStateTime = 0;
3165 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003166 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3167 totalStateTime += time;
3168 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003169 }
3170 if (totalStateTime > 0) {
3171 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3172 }
3173
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003174 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3175 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3176 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3177 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3178 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3179 powerCpuMaUs / 1000);
3180 }
3181
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003182 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3183 = u.getProcessStats();
3184 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3185 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003186
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003187 final long userMillis = ps.getUserTime(which);
3188 final long systemMillis = ps.getSystemTime(which);
3189 final long foregroundMillis = ps.getForegroundTime(which);
3190 final int starts = ps.getStarts(which);
3191 final int numCrashes = ps.getNumCrashes(which);
3192 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003193
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003194 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3195 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3196 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3197 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
3199 }
3200
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003201 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3202 = u.getPackageStats();
3203 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3204 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3205 int wakeups = 0;
3206 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3207 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3208 wakeups += alarms.valueAt(iwa).getCountLocked(which);
3209 }
3210 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3211 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3212 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3213 final long startTime = ss.getStartTime(batteryUptime, which);
3214 final int starts = ss.getStarts(which);
3215 final int launches = ss.getLaunches(which);
3216 if (startTime != 0 || starts != 0 || launches != 0) {
3217 dumpLine(pw, uid, category, APK_DATA,
3218 wakeups, // wakeup alarms
3219 packageStats.keyAt(ipkg), // Apk
3220 serviceStats.keyAt(isvc), // service
3221 startTime / 1000, // time spent started, in ms
3222 starts,
3223 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 }
3226 }
3227 }
3228 }
3229
Dianne Hackborn81038902012-11-26 17:04:09 -08003230 static final class TimerEntry {
3231 final String mName;
3232 final int mId;
3233 final BatteryStats.Timer mTimer;
3234 final long mTime;
3235 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3236 mName = name;
3237 mId = id;
3238 mTimer = timer;
3239 mTime = time;
3240 }
3241 }
3242
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003243 private void printmAh(PrintWriter printer, double power) {
3244 printer.print(BatteryStatsHelper.makemAh(power));
3245 }
3246
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003247 private void printmAh(StringBuilder sb, double power) {
3248 sb.append(BatteryStatsHelper.makemAh(power));
3249 }
3250
Dianne Hackbornd953c532014-08-16 18:17:38 -07003251 /**
3252 * Temporary for settings.
3253 */
3254 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3255 int reqUid) {
3256 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3257 }
3258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003260 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003261 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3263 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3264 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265
3266 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3267 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3268 final long totalRealtime = computeRealtime(rawRealtime, which);
3269 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003270 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3271 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3272 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003273 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3274 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003275
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003276 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003277
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003278 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003279 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003281 sb.setLength(0);
3282 sb.append(prefix);
3283 sb.append(" Time on battery: ");
3284 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3285 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3286 sb.append(") realtime, ");
3287 formatTimeMs(sb, whichBatteryUptime / 1000);
3288 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3289 sb.append(") uptime");
3290 pw.println(sb.toString());
3291 sb.setLength(0);
3292 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003293 sb.append(" Time on battery screen off: ");
3294 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3295 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3296 sb.append(") realtime, ");
3297 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3298 sb.append("(");
3299 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3300 sb.append(") uptime");
3301 pw.println(sb.toString());
3302 sb.setLength(0);
3303 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003304 sb.append(" Total run time: ");
3305 formatTimeMs(sb, totalRealtime / 1000);
3306 sb.append("realtime, ");
3307 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003308 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003309 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003310 if (batteryTimeRemaining >= 0) {
3311 sb.setLength(0);
3312 sb.append(prefix);
3313 sb.append(" Battery time remaining: ");
3314 formatTimeMs(sb, batteryTimeRemaining / 1000);
3315 pw.println(sb.toString());
3316 }
3317 if (chargeTimeRemaining >= 0) {
3318 sb.setLength(0);
3319 sb.append(prefix);
3320 sb.append(" Charge time remaining: ");
3321 formatTimeMs(sb, chargeTimeRemaining / 1000);
3322 pw.println(sb.toString());
3323 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003324 pw.print(" Start clock time: ");
3325 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3326
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003327 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003328 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003329 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003330 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3331 rawRealtime, which);
3332 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
3333 rawRealtime, which);
3334 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3335 rawRealtime, which);
3336 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
3337 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003338 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3339 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3340 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003341 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003342 sb.append(prefix);
3343 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3344 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003345 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003346 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3347 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003348 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003349 pw.println(sb.toString());
3350 sb.setLength(0);
3351 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003352 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003353 boolean didOne = false;
3354 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003355 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003356 if (time == 0) {
3357 continue;
3358 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003359 sb.append("\n ");
3360 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003361 didOne = true;
3362 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3363 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003364 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003365 sb.append("(");
3366 sb.append(formatRatioLocked(time, screenOnTime));
3367 sb.append(")");
3368 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003369 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003370 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003371 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003372 sb.setLength(0);
3373 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003374 sb.append(" Power save mode enabled: ");
3375 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003376 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003377 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003378 sb.append(")");
3379 pw.println(sb.toString());
3380 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003381 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003382 sb.setLength(0);
3383 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003384 sb.append(" Device light idling: ");
3385 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003386 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003387 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3388 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003389 sb.append("x");
3390 pw.println(sb.toString());
3391 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003392 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003393 sb.setLength(0);
3394 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003395 sb.append(" Idle mode light time: ");
3396 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003397 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003398 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3399 sb.append(") ");
3400 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003401 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003402 sb.append(" -- longest ");
3403 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3404 pw.println(sb.toString());
3405 }
3406 if (deviceIdlingTime != 0) {
3407 sb.setLength(0);
3408 sb.append(prefix);
3409 sb.append(" Device full idling: ");
3410 formatTimeMs(sb, deviceIdlingTime / 1000);
3411 sb.append("(");
3412 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3413 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which));
3414 sb.append("x");
3415 pw.println(sb.toString());
3416 }
3417 if (deviceIdleModeFullTime != 0) {
3418 sb.setLength(0);
3419 sb.append(prefix);
3420 sb.append(" Idle mode full time: ");
3421 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3422 sb.append("(");
3423 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3424 sb.append(") ");
3425 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which));
3426 sb.append("x");
3427 sb.append(" -- longest ");
3428 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003429 pw.println(sb.toString());
3430 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003431 if (phoneOnTime != 0) {
3432 sb.setLength(0);
3433 sb.append(prefix);
3434 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3435 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003436 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003437 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003438 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003439 if (connChanges != 0) {
3440 pw.print(prefix);
3441 pw.print(" Connectivity changes: "); pw.println(connChanges);
3442 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003443
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003444 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003445 long fullWakeLockTimeTotalMicros = 0;
3446 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003447
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003448 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003449
Evan Millar22ac0432009-03-31 11:33:18 -07003450 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003451 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003452
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003453 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3454 = u.getWakelockStats();
3455 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3456 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003457
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003458 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3459 if (fullWakeTimer != null) {
3460 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3461 rawRealtime, which);
3462 }
3463
3464 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3465 if (partialWakeTimer != null) {
3466 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3467 rawRealtime, which);
3468 if (totalTimeMicros > 0) {
3469 if (reqUid < 0) {
3470 // Only show the ordered list of all wake
3471 // locks if the caller is not asking for data
3472 // about a specific uid.
3473 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3474 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003475 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003476 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003477 }
3478 }
3479 }
3480 }
3481
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003482 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3483 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3484 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3485 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3486 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3487 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3488 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3489 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003490 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3491 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003492
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003493 if (fullWakeLockTimeTotalMicros != 0) {
3494 sb.setLength(0);
3495 sb.append(prefix);
3496 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3497 (fullWakeLockTimeTotalMicros + 500) / 1000);
3498 pw.println(sb.toString());
3499 }
3500
3501 if (partialWakeLockTimeTotalMicros != 0) {
3502 sb.setLength(0);
3503 sb.append(prefix);
3504 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3505 (partialWakeLockTimeTotalMicros + 500) / 1000);
3506 pw.println(sb.toString());
3507 }
3508
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003509 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003510 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3511 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3512 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3513 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003514 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003515 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003516 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003517 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003518 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003519 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003520 if (time == 0) {
3521 continue;
3522 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003523 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003524 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003525 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003526 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003527 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003528 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003529 sb.append("(");
3530 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003531 sb.append(") ");
3532 sb.append(getPhoneSignalStrengthCount(i, which));
3533 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003534 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003535 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003536 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003537
3538 sb.setLength(0);
3539 sb.append(prefix);
3540 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003541 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003542 pw.println(sb.toString());
3543
Dianne Hackborn627bba72009-03-24 22:32:56 -07003544 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003545 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003546 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003547 didOne = false;
3548 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003549 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003550 if (time == 0) {
3551 continue;
3552 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003553 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003554 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003555 didOne = true;
3556 sb.append(DATA_CONNECTION_NAMES[i]);
3557 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003558 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003559 sb.append("(");
3560 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003561 sb.append(") ");
3562 sb.append(getPhoneDataConnectionCount(i, which));
3563 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003564 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003565 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003566 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003567
3568 sb.setLength(0);
3569 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003570 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003571 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003572 formatTimeMs(sb, mobileActiveTime / 1000);
3573 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3574 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3575 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003576 pw.println(sb.toString());
3577
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003578 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3579 if (mobileActiveUnknownTime != 0) {
3580 sb.setLength(0);
3581 sb.append(prefix);
3582 sb.append(" Mobile radio active unknown time: ");
3583 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3584 sb.append("(");
3585 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3586 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3587 sb.append("x");
3588 pw.println(sb.toString());
3589 }
3590
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003591 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3592 if (mobileActiveAdjustedTime != 0) {
3593 sb.setLength(0);
3594 sb.append(prefix);
3595 sb.append(" Mobile radio active adjusted time: ");
3596 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3597 sb.append("(");
3598 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3599 sb.append(")");
3600 pw.println(sb.toString());
3601 }
3602
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003603 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3604
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003605 pw.print(prefix);
3606 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3607 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3608 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3609 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003610 sb.setLength(0);
3611 sb.append(prefix);
3612 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3613 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3614 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3615 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003616 sb.append(")");
3617 pw.println(sb.toString());
3618
3619 sb.setLength(0);
3620 sb.append(prefix);
3621 sb.append(" Wifi states:");
3622 didOne = false;
3623 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003624 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003625 if (time == 0) {
3626 continue;
3627 }
3628 sb.append("\n ");
3629 didOne = true;
3630 sb.append(WIFI_STATE_NAMES[i]);
3631 sb.append(" ");
3632 formatTimeMs(sb, time/1000);
3633 sb.append("(");
3634 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3635 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003636 sb.append(getWifiStateCount(i, which));
3637 sb.append("x");
3638 }
3639 if (!didOne) sb.append(" (no activity)");
3640 pw.println(sb.toString());
3641
3642 sb.setLength(0);
3643 sb.append(prefix);
3644 sb.append(" Wifi supplicant states:");
3645 didOne = false;
3646 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3647 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3648 if (time == 0) {
3649 continue;
3650 }
3651 sb.append("\n ");
3652 didOne = true;
3653 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3654 sb.append(" ");
3655 formatTimeMs(sb, time/1000);
3656 sb.append("(");
3657 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3658 sb.append(") ");
3659 sb.append(getWifiSupplStateCount(i, which));
3660 sb.append("x");
3661 }
3662 if (!didOne) sb.append(" (no activity)");
3663 pw.println(sb.toString());
3664
3665 sb.setLength(0);
3666 sb.append(prefix);
3667 sb.append(" Wifi signal levels:");
3668 didOne = false;
3669 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3670 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3671 if (time == 0) {
3672 continue;
3673 }
3674 sb.append("\n ");
3675 sb.append(prefix);
3676 didOne = true;
3677 sb.append("level(");
3678 sb.append(i);
3679 sb.append(") ");
3680 formatTimeMs(sb, time/1000);
3681 sb.append("(");
3682 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3683 sb.append(") ");
3684 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003685 sb.append("x");
3686 }
3687 if (!didOne) sb.append(" (no activity)");
3688 pw.println(sb.toString());
3689
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003690 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003691
Adam Lesinski50e47602015-12-04 17:04:54 -08003692 pw.print(prefix);
3693 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3694 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3695
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003696 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
3697 sb.setLength(0);
3698 sb.append(prefix);
3699 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
3700 pw.println(sb.toString());
3701
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003702 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3703 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003704
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003705 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003706
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003707 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003708 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003709 pw.print(prefix); pw.println(" Device is currently unplugged");
3710 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3711 pw.println(getDischargeStartLevel());
3712 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3713 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003714 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003715 pw.print(prefix); pw.println(" Device is currently plugged into power");
3716 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3717 pw.println(getDischargeStartLevel());
3718 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3719 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003720 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003721 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3722 pw.println(getDischargeAmountScreenOn());
3723 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3724 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003725 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003726 } else {
3727 pw.print(prefix); pw.println(" Device battery use since last full charge");
3728 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3729 pw.println(getLowDischargeAmountSinceCharge());
3730 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3731 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003732 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3733 pw.println(getDischargeAmountScreenOnSinceCharge());
3734 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3735 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003736 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003737 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003738
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003739 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003740 helper.create(this);
3741 helper.refreshStats(which, UserHandle.USER_ALL);
3742 List<BatterySipper> sippers = helper.getUsageList();
3743 if (sippers != null && sippers.size() > 0) {
3744 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3745 pw.print(prefix); pw.print(" Capacity: ");
3746 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003747 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003748 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3749 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3750 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3751 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003752 pw.println();
3753 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003754 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003755 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003756 switch (bs.drainType) {
3757 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003758 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003759 break;
3760 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003761 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003762 break;
3763 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003764 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003765 break;
3766 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003767 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003768 break;
3769 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003770 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003771 break;
3772 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003773 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003774 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003775 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003776 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003777 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003778 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003779 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003780 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003781 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003782 break;
3783 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003784 pw.print(" User "); pw.print(bs.userId);
3785 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003786 break;
3787 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003788 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003789 break;
3790 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003791 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003792 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003793 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003794 pw.print(" Camera: ");
3795 break;
3796 default:
3797 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003798 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003799 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003800 printmAh(pw, bs.totalPowerMah);
3801
Adam Lesinski57123002015-06-12 16:12:07 -07003802 if (bs.usagePowerMah != bs.totalPowerMah) {
3803 // If the usage (generic power) isn't the whole amount, we list out
3804 // what components are involved in the calculation.
3805
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003806 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003807 if (bs.usagePowerMah != 0) {
3808 pw.print(" usage=");
3809 printmAh(pw, bs.usagePowerMah);
3810 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003811 if (bs.cpuPowerMah != 0) {
3812 pw.print(" cpu=");
3813 printmAh(pw, bs.cpuPowerMah);
3814 }
3815 if (bs.wakeLockPowerMah != 0) {
3816 pw.print(" wake=");
3817 printmAh(pw, bs.wakeLockPowerMah);
3818 }
3819 if (bs.mobileRadioPowerMah != 0) {
3820 pw.print(" radio=");
3821 printmAh(pw, bs.mobileRadioPowerMah);
3822 }
3823 if (bs.wifiPowerMah != 0) {
3824 pw.print(" wifi=");
3825 printmAh(pw, bs.wifiPowerMah);
3826 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003827 if (bs.bluetoothPowerMah != 0) {
3828 pw.print(" bt=");
3829 printmAh(pw, bs.bluetoothPowerMah);
3830 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003831 if (bs.gpsPowerMah != 0) {
3832 pw.print(" gps=");
3833 printmAh(pw, bs.gpsPowerMah);
3834 }
3835 if (bs.sensorPowerMah != 0) {
3836 pw.print(" sensor=");
3837 printmAh(pw, bs.sensorPowerMah);
3838 }
3839 if (bs.cameraPowerMah != 0) {
3840 pw.print(" camera=");
3841 printmAh(pw, bs.cameraPowerMah);
3842 }
3843 if (bs.flashlightPowerMah != 0) {
3844 pw.print(" flash=");
3845 printmAh(pw, bs.flashlightPowerMah);
3846 }
3847 pw.print(" )");
3848 }
3849 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003850 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003851 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003852 }
3853
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003854 sippers = helper.getMobilemsppList();
3855 if (sippers != null && sippers.size() > 0) {
3856 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003857 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003858 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003859 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003860 sb.setLength(0);
3861 sb.append(prefix); sb.append(" Uid ");
3862 UserHandle.formatUid(sb, bs.uidObj.getUid());
3863 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3864 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3865 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003866 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003867 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003868 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003869 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003870 sb.setLength(0);
3871 sb.append(prefix);
3872 sb.append(" TOTAL TIME: ");
3873 formatTimeMs(sb, totalTime);
3874 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3875 sb.append(")");
3876 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003877 pw.println();
3878 }
3879
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003880 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3881 @Override
3882 public int compare(TimerEntry lhs, TimerEntry rhs) {
3883 long lhsTime = lhs.mTime;
3884 long rhsTime = rhs.mTime;
3885 if (lhsTime < rhsTime) {
3886 return 1;
3887 }
3888 if (lhsTime > rhsTime) {
3889 return -1;
3890 }
3891 return 0;
3892 }
3893 };
3894
3895 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003896 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3897 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003898 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003899 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3900 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3901 : kernelWakelocks.entrySet()) {
3902 final BatteryStats.Timer timer = ent.getValue();
3903 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003904 if (totalTimeMillis > 0) {
3905 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3906 }
3907 }
3908 if (ktimers.size() > 0) {
3909 Collections.sort(ktimers, timerComparator);
3910 pw.print(prefix); pw.println(" All kernel wake locks:");
3911 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003912 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003913 String linePrefix = ": ";
3914 sb.setLength(0);
3915 sb.append(prefix);
3916 sb.append(" Kernel Wake lock ");
3917 sb.append(timer.mName);
3918 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3919 which, linePrefix);
3920 if (!linePrefix.equals(": ")) {
3921 sb.append(" realtime");
3922 // Only print out wake locks that were held
3923 pw.println(sb.toString());
3924 }
3925 }
3926 pw.println();
3927 }
3928 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003929
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003930 if (timers.size() > 0) {
3931 Collections.sort(timers, timerComparator);
3932 pw.print(prefix); pw.println(" All partial wake locks:");
3933 for (int i=0; i<timers.size(); i++) {
3934 TimerEntry timer = timers.get(i);
3935 sb.setLength(0);
3936 sb.append(" Wake lock ");
3937 UserHandle.formatUid(sb, timer.mId);
3938 sb.append(" ");
3939 sb.append(timer.mName);
3940 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3941 sb.append(" realtime");
3942 pw.println(sb.toString());
3943 }
3944 timers.clear();
3945 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003946 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003947
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003948 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003949 if (wakeupReasons.size() > 0) {
3950 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003951 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003952 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003953 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003954 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3955 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003956 }
3957 Collections.sort(reasons, timerComparator);
3958 for (int i=0; i<reasons.size(); i++) {
3959 TimerEntry timer = reasons.get(i);
3960 String linePrefix = ": ";
3961 sb.setLength(0);
3962 sb.append(prefix);
3963 sb.append(" Wakeup reason ");
3964 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003965 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3966 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003967 pw.println(sb.toString());
3968 }
3969 pw.println();
3970 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003971 }
Evan Millar22ac0432009-03-31 11:33:18 -07003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 for (int iu=0; iu<NU; iu++) {
3974 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003975 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003976 continue;
3977 }
3978
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003979 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003980
3981 pw.print(prefix);
3982 pw.print(" ");
3983 UserHandle.formatUid(pw, uid);
3984 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003986
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003987 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3988 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3989 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3990 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003991 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3992 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
3993
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003994 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3995 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003996 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3997 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003998
3999 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4000 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4001
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004002 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4003 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4004 final int wifiScanCount = u.getWifiScanCount(which);
4005 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004006
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004007 if (mobileRxBytes > 0 || mobileTxBytes > 0
4008 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004009 pw.print(prefix); pw.print(" Mobile network: ");
4010 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004011 pw.print(formatBytesLocked(mobileTxBytes));
4012 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4013 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004015 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4016 sb.setLength(0);
4017 sb.append(prefix); sb.append(" Mobile radio active: ");
4018 formatTimeMs(sb, uidMobileActiveTime / 1000);
4019 sb.append("(");
4020 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4021 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4022 long packets = mobileRxPackets + mobileTxPackets;
4023 if (packets == 0) {
4024 packets = 1;
4025 }
4026 sb.append(" @ ");
4027 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4028 sb.append(" mspp");
4029 pw.println(sb.toString());
4030 }
4031
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004032 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4033 u.getModemControllerActivity(), which);
4034
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004035 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004036 pw.print(prefix); pw.print(" Wi-Fi network: ");
4037 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004038 pw.print(formatBytesLocked(wifiTxBytes));
4039 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4040 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004041 }
4042
Dianne Hackborn62793e42015-03-09 11:15:41 -07004043 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004044 || uidWifiRunningTime != 0) {
4045 sb.setLength(0);
4046 sb.append(prefix); sb.append(" Wifi Running: ");
4047 formatTimeMs(sb, uidWifiRunningTime / 1000);
4048 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4049 whichBatteryRealtime)); sb.append(")\n");
4050 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4051 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4052 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4053 whichBatteryRealtime)); sb.append(")\n");
4054 sb.append(prefix); sb.append(" Wifi Scan: ");
4055 formatTimeMs(sb, wifiScanTime / 1000);
4056 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004057 whichBatteryRealtime)); sb.append(") ");
4058 sb.append(wifiScanCount);
4059 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004060 pw.println(sb.toString());
4061 }
4062
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004063 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4064 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004065
Adam Lesinski50e47602015-12-04 17:04:54 -08004066 if (btRxBytes > 0 || btTxBytes > 0) {
4067 pw.print(prefix); pw.print(" Bluetooth network: ");
4068 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4069 pw.print(formatBytesLocked(btTxBytes));
4070 pw.println(" sent");
4071 }
4072
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004073 uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
4074 "Bluetooth Scan");
4075
Dianne Hackborn617f8772009-03-31 15:04:46 -07004076 if (u.hasUserActivity()) {
4077 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004078 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004079 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004080 if (val != 0) {
4081 if (!hasData) {
4082 sb.setLength(0);
4083 sb.append(" User activity: ");
4084 hasData = true;
4085 } else {
4086 sb.append(", ");
4087 }
4088 sb.append(val);
4089 sb.append(" ");
4090 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4091 }
4092 }
4093 if (hasData) {
4094 pw.println(sb.toString());
4095 }
4096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004098 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4099 = u.getWakelockStats();
4100 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004101 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004102 int countWakelock = 0;
4103 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4104 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4105 String linePrefix = ": ";
4106 sb.setLength(0);
4107 sb.append(prefix);
4108 sb.append(" Wake lock ");
4109 sb.append(wakelocks.keyAt(iw));
4110 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4111 "full", which, linePrefix);
4112 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4113 "partial", which, linePrefix);
4114 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4115 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004116 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4117 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004118 sb.append(" realtime");
4119 pw.println(sb.toString());
4120 uidActivity = true;
4121 countWakelock++;
4122
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004123 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4124 rawRealtime, which);
4125 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4126 rawRealtime, which);
4127 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4128 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004129 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004130 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004131 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004132 if (countWakelock > 1) {
4133 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4134 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004135 sb.setLength(0);
4136 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004137 sb.append(" TOTAL wake: ");
4138 boolean needComma = false;
4139 if (totalFullWakelock != 0) {
4140 needComma = true;
4141 formatTimeMs(sb, totalFullWakelock);
4142 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004143 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004144 if (totalPartialWakelock != 0) {
4145 if (needComma) {
4146 sb.append(", ");
4147 }
4148 needComma = true;
4149 formatTimeMs(sb, totalPartialWakelock);
4150 sb.append("partial");
4151 }
4152 if (totalWindowWakelock != 0) {
4153 if (needComma) {
4154 sb.append(", ");
4155 }
4156 needComma = true;
4157 formatTimeMs(sb, totalWindowWakelock);
4158 sb.append("window");
4159 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004160 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004161 if (needComma) {
4162 sb.append(",");
4163 }
4164 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004165 formatTimeMs(sb, totalDrawWakelock);
4166 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004167 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004168 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004169 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004170 }
4171 }
4172
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004173 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4174 for (int isy=syncs.size()-1; isy>=0; isy--) {
4175 final Timer timer = syncs.valueAt(isy);
4176 // Convert from microseconds to milliseconds with rounding
4177 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4178 final int count = timer.getCountLocked(which);
4179 sb.setLength(0);
4180 sb.append(prefix);
4181 sb.append(" Sync ");
4182 sb.append(syncs.keyAt(isy));
4183 sb.append(": ");
4184 if (totalTime != 0) {
4185 formatTimeMs(sb, totalTime);
4186 sb.append("realtime (");
4187 sb.append(count);
4188 sb.append(" times)");
4189 } else {
4190 sb.append("(not used)");
4191 }
4192 pw.println(sb.toString());
4193 uidActivity = true;
4194 }
4195
4196 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4197 for (int ij=jobs.size()-1; ij>=0; ij--) {
4198 final Timer timer = jobs.valueAt(ij);
4199 // Convert from microseconds to milliseconds with rounding
4200 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4201 final int count = timer.getCountLocked(which);
4202 sb.setLength(0);
4203 sb.append(prefix);
4204 sb.append(" Job ");
4205 sb.append(jobs.keyAt(ij));
4206 sb.append(": ");
4207 if (totalTime != 0) {
4208 formatTimeMs(sb, totalTime);
4209 sb.append("realtime (");
4210 sb.append(count);
4211 sb.append(" times)");
4212 } else {
4213 sb.append("(not used)");
4214 }
4215 pw.println(sb.toString());
4216 uidActivity = true;
4217 }
4218
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004219 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4220 prefix, "Flashlight");
4221 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4222 prefix, "Camera");
4223 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4224 prefix, "Video");
4225 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4226 prefix, "Audio");
4227
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004228 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4229 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004230 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004231 final Uid.Sensor se = sensors.valueAt(ise);
4232 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004233 sb.setLength(0);
4234 sb.append(prefix);
4235 sb.append(" Sensor ");
4236 int handle = se.getHandle();
4237 if (handle == Uid.Sensor.GPS) {
4238 sb.append("GPS");
4239 } else {
4240 sb.append(handle);
4241 }
4242 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004244 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004245 if (timer != null) {
4246 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004247 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004248 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004249 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004250 //timer.logState();
4251 if (totalTime != 0) {
4252 formatTimeMs(sb, totalTime);
4253 sb.append("realtime (");
4254 sb.append(count);
4255 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 } else {
4257 sb.append("(not used)");
4258 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004259 } else {
4260 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004262
4263 pw.println(sb.toString());
4264 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 }
4266
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004267 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4268 "Vibrator");
4269 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4270 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004271
Dianne Hackborn61659e52014-07-09 16:13:01 -07004272 long totalStateTime = 0;
4273 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4274 long time = u.getProcessStateTime(ips, rawRealtime, which);
4275 if (time > 0) {
4276 totalStateTime += time;
4277 sb.setLength(0);
4278 sb.append(prefix);
4279 sb.append(" ");
4280 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4281 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004282 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004283 pw.println(sb.toString());
4284 uidActivity = true;
4285 }
4286 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004287 if (totalStateTime > 0) {
4288 sb.setLength(0);
4289 sb.append(prefix);
4290 sb.append(" Total running: ");
4291 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4292 pw.println(sb.toString());
4293 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004294
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004295 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4296 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004297 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4298 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004299 sb.setLength(0);
4300 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004301 sb.append(" Total cpu time: u=");
4302 formatTimeMs(sb, userCpuTimeUs / 1000);
4303 sb.append("s=");
4304 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004305 sb.append("p=");
4306 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4307 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004308 pw.println(sb.toString());
4309 }
4310
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004311 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4312 = u.getProcessStats();
4313 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4314 final Uid.Proc ps = processStats.valueAt(ipr);
4315 long userTime;
4316 long systemTime;
4317 long foregroundTime;
4318 int starts;
4319 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004321 userTime = ps.getUserTime(which);
4322 systemTime = ps.getSystemTime(which);
4323 foregroundTime = ps.getForegroundTime(which);
4324 starts = ps.getStarts(which);
4325 final int numCrashes = ps.getNumCrashes(which);
4326 final int numAnrs = ps.getNumAnrs(which);
4327 numExcessive = which == STATS_SINCE_CHARGED
4328 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004330 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4331 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4332 sb.setLength(0);
4333 sb.append(prefix); sb.append(" Proc ");
4334 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4335 sb.append(prefix); sb.append(" CPU: ");
4336 formatTimeMs(sb, userTime); sb.append("usr + ");
4337 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4338 formatTimeMs(sb, foregroundTime); sb.append("fg");
4339 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4340 sb.append("\n"); sb.append(prefix); sb.append(" ");
4341 boolean hasOne = false;
4342 if (starts != 0) {
4343 hasOne = true;
4344 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004345 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004346 if (numCrashes != 0) {
4347 if (hasOne) {
4348 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004349 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004350 hasOne = true;
4351 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004352 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004353 if (numAnrs != 0) {
4354 if (hasOne) {
4355 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004357 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004360 pw.println(sb.toString());
4361 for (int e=0; e<numExcessive; e++) {
4362 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4363 if (ew != null) {
4364 pw.print(prefix); pw.print(" * Killed for ");
4365 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4366 pw.print("wake lock");
4367 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4368 pw.print("cpu");
4369 } else {
4370 pw.print("unknown");
4371 }
4372 pw.print(" use: ");
4373 TimeUtils.formatDuration(ew.usedTime, pw);
4374 pw.print(" over ");
4375 TimeUtils.formatDuration(ew.overTime, pw);
4376 if (ew.overTime != 0) {
4377 pw.print(" (");
4378 pw.print((ew.usedTime*100)/ew.overTime);
4379 pw.println("%)");
4380 }
4381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 }
4383 uidActivity = true;
4384 }
4385 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004386
4387 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4388 = u.getPackageStats();
4389 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4390 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4391 pw.println(":");
4392 boolean apkActivity = false;
4393 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4394 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4395 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4396 pw.print(prefix); pw.print(" Wakeup alarm ");
4397 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4398 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4399 pw.println(" times");
4400 apkActivity = true;
4401 }
4402 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4403 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4404 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4405 final long startTime = ss.getStartTime(batteryUptime, which);
4406 final int starts = ss.getStarts(which);
4407 final int launches = ss.getLaunches(which);
4408 if (startTime != 0 || starts != 0 || launches != 0) {
4409 sb.setLength(0);
4410 sb.append(prefix); sb.append(" Service ");
4411 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4412 sb.append(prefix); sb.append(" Created for: ");
4413 formatTimeMs(sb, startTime / 1000);
4414 sb.append("uptime\n");
4415 sb.append(prefix); sb.append(" Starts: ");
4416 sb.append(starts);
4417 sb.append(", launches: "); sb.append(launches);
4418 pw.println(sb.toString());
4419 apkActivity = true;
4420 }
4421 }
4422 if (!apkActivity) {
4423 pw.print(prefix); pw.println(" (nothing executed)");
4424 }
4425 uidActivity = true;
4426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004428 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 }
4430 }
4431 }
4432
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004433 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004434 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004435 int diff = oldval ^ newval;
4436 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004437 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004438 for (int i=0; i<descriptions.length; i++) {
4439 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004440 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004441 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004442 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004443 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004444 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004445 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4446 didWake = true;
4447 pw.print("=");
4448 if (longNames) {
4449 UserHandle.formatUid(pw, wakelockTag.uid);
4450 pw.print(":\"");
4451 pw.print(wakelockTag.string);
4452 pw.print("\"");
4453 } else {
4454 pw.print(wakelockTag.poolIdx);
4455 }
4456 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004457 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004458 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004459 pw.print("=");
4460 int val = (newval&bd.mask)>>bd.shift;
4461 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004462 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004463 } else {
4464 pw.print(val);
4465 }
4466 }
4467 }
4468 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004469 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004470 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004471 if (longNames) {
4472 UserHandle.formatUid(pw, wakelockTag.uid);
4473 pw.print(":\"");
4474 pw.print(wakelockTag.string);
4475 pw.print("\"");
4476 } else {
4477 pw.print(wakelockTag.poolIdx);
4478 }
4479 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004480 }
4481
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004482 public void prepareForDumpLocked() {
4483 }
4484
4485 public static class HistoryPrinter {
4486 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004487 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004488 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004489 int oldStatus = -1;
4490 int oldHealth = -1;
4491 int oldPlug = -1;
4492 int oldTemp = -1;
4493 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004494 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004495
Dianne Hackborn3251b902014-06-20 14:40:53 -07004496 void reset() {
4497 oldState = oldState2 = 0;
4498 oldLevel = -1;
4499 oldStatus = -1;
4500 oldHealth = -1;
4501 oldPlug = -1;
4502 oldTemp = -1;
4503 oldVolt = -1;
4504 }
4505
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004506 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004507 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004508 if (!checkin) {
4509 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004510 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004511 pw.print(" (");
4512 pw.print(rec.numReadInts);
4513 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004514 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004515 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4516 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004517 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004518 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004519 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004520 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004521 }
4522 lastTime = rec.time;
4523 }
4524 if (rec.cmd == HistoryItem.CMD_START) {
4525 if (checkin) {
4526 pw.print(":");
4527 }
4528 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004529 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004530 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4531 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004532 if (checkin) {
4533 pw.print(":");
4534 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004535 if (rec.cmd == HistoryItem.CMD_RESET) {
4536 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004537 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004538 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004539 pw.print("TIME:");
4540 if (checkin) {
4541 pw.println(rec.currentTime);
4542 } else {
4543 pw.print(" ");
4544 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4545 rec.currentTime).toString());
4546 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004547 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4548 if (checkin) {
4549 pw.print(":");
4550 }
4551 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004552 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4553 if (checkin) {
4554 pw.print(":");
4555 }
4556 pw.println("*OVERFLOW*");
4557 } else {
4558 if (!checkin) {
4559 if (rec.batteryLevel < 10) pw.print("00");
4560 else if (rec.batteryLevel < 100) pw.print("0");
4561 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004562 if (verbose) {
4563 pw.print(" ");
4564 if (rec.states < 0) ;
4565 else if (rec.states < 0x10) pw.print("0000000");
4566 else if (rec.states < 0x100) pw.print("000000");
4567 else if (rec.states < 0x1000) pw.print("00000");
4568 else if (rec.states < 0x10000) pw.print("0000");
4569 else if (rec.states < 0x100000) pw.print("000");
4570 else if (rec.states < 0x1000000) pw.print("00");
4571 else if (rec.states < 0x10000000) pw.print("0");
4572 pw.print(Integer.toHexString(rec.states));
4573 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004574 } else {
4575 if (oldLevel != rec.batteryLevel) {
4576 oldLevel = rec.batteryLevel;
4577 pw.print(",Bl="); pw.print(rec.batteryLevel);
4578 }
4579 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004580 if (oldStatus != rec.batteryStatus) {
4581 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004582 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004583 switch (oldStatus) {
4584 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004585 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004586 break;
4587 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004588 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004589 break;
4590 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004591 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004592 break;
4593 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004594 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004595 break;
4596 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004597 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004598 break;
4599 default:
4600 pw.print(oldStatus);
4601 break;
4602 }
4603 }
4604 if (oldHealth != rec.batteryHealth) {
4605 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004606 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004607 switch (oldHealth) {
4608 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004609 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004610 break;
4611 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004612 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004613 break;
4614 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004615 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004616 break;
4617 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004618 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004619 break;
4620 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004621 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004622 break;
4623 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004624 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004625 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004626 case BatteryManager.BATTERY_HEALTH_COLD:
4627 pw.print(checkin ? "c" : "cold");
4628 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004629 default:
4630 pw.print(oldHealth);
4631 break;
4632 }
4633 }
4634 if (oldPlug != rec.batteryPlugType) {
4635 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004636 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004637 switch (oldPlug) {
4638 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004639 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004640 break;
4641 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004642 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004643 break;
4644 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004645 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004646 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004647 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004648 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004649 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004650 default:
4651 pw.print(oldPlug);
4652 break;
4653 }
4654 }
4655 if (oldTemp != rec.batteryTemperature) {
4656 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004657 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004658 pw.print(oldTemp);
4659 }
4660 if (oldVolt != rec.batteryVoltage) {
4661 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004662 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004663 pw.print(oldVolt);
4664 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004665 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004666 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004667 printBitDescriptions(pw, oldState2, rec.states2, null,
4668 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004669 if (rec.wakeReasonTag != null) {
4670 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004671 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004672 pw.print(rec.wakeReasonTag.poolIdx);
4673 } else {
4674 pw.print(" wake_reason=");
4675 pw.print(rec.wakeReasonTag.uid);
4676 pw.print(":\"");
4677 pw.print(rec.wakeReasonTag.string);
4678 pw.print("\"");
4679 }
4680 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004681 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004682 pw.print(checkin ? "," : " ");
4683 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4684 pw.print("+");
4685 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4686 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004687 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004688 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4689 : HISTORY_EVENT_NAMES;
4690 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4691 | HistoryItem.EVENT_FLAG_FINISH);
4692 if (idx >= 0 && idx < eventNames.length) {
4693 pw.print(eventNames[idx]);
4694 } else {
4695 pw.print(checkin ? "Ev" : "event");
4696 pw.print(idx);
4697 }
4698 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004699 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004700 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004701 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004702 UserHandle.formatUid(pw, rec.eventTag.uid);
4703 pw.print(":\"");
4704 pw.print(rec.eventTag.string);
4705 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004706 }
4707 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004708 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004709 if (rec.stepDetails != null) {
4710 if (!checkin) {
4711 pw.print(" Details: cpu=");
4712 pw.print(rec.stepDetails.userTime);
4713 pw.print("u+");
4714 pw.print(rec.stepDetails.systemTime);
4715 pw.print("s");
4716 if (rec.stepDetails.appCpuUid1 >= 0) {
4717 pw.print(" (");
4718 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4719 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4720 if (rec.stepDetails.appCpuUid2 >= 0) {
4721 pw.print(", ");
4722 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4723 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4724 }
4725 if (rec.stepDetails.appCpuUid3 >= 0) {
4726 pw.print(", ");
4727 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4728 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4729 }
4730 pw.print(')');
4731 }
4732 pw.println();
4733 pw.print(" /proc/stat=");
4734 pw.print(rec.stepDetails.statUserTime);
4735 pw.print(" usr, ");
4736 pw.print(rec.stepDetails.statSystemTime);
4737 pw.print(" sys, ");
4738 pw.print(rec.stepDetails.statIOWaitTime);
4739 pw.print(" io, ");
4740 pw.print(rec.stepDetails.statIrqTime);
4741 pw.print(" irq, ");
4742 pw.print(rec.stepDetails.statSoftIrqTime);
4743 pw.print(" sirq, ");
4744 pw.print(rec.stepDetails.statIdlTime);
4745 pw.print(" idle");
4746 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4747 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4748 + rec.stepDetails.statSoftIrqTime;
4749 int total = totalRun + rec.stepDetails.statIdlTime;
4750 if (total > 0) {
4751 pw.print(" (");
4752 float perc = ((float)totalRun) / ((float)total) * 100;
4753 pw.print(String.format("%.1f%%", perc));
4754 pw.print(" of ");
4755 StringBuilder sb = new StringBuilder(64);
4756 formatTimeMsNoSpace(sb, total*10);
4757 pw.print(sb);
4758 pw.print(")");
4759 }
4760 pw.println();
4761 } else {
4762 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4763 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4764 pw.print(rec.stepDetails.userTime);
4765 pw.print(":");
4766 pw.print(rec.stepDetails.systemTime);
4767 if (rec.stepDetails.appCpuUid1 >= 0) {
4768 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4769 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4770 if (rec.stepDetails.appCpuUid2 >= 0) {
4771 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4772 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4773 }
4774 if (rec.stepDetails.appCpuUid3 >= 0) {
4775 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4776 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4777 }
4778 }
4779 pw.println();
4780 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4781 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4782 pw.print(rec.stepDetails.statUserTime);
4783 pw.print(',');
4784 pw.print(rec.stepDetails.statSystemTime);
4785 pw.print(',');
4786 pw.print(rec.stepDetails.statIOWaitTime);
4787 pw.print(',');
4788 pw.print(rec.stepDetails.statIrqTime);
4789 pw.print(',');
4790 pw.print(rec.stepDetails.statSoftIrqTime);
4791 pw.print(',');
4792 pw.print(rec.stepDetails.statIdlTime);
4793 pw.println();
4794 }
4795 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004796 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004797 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004798 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004799 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004800
4801 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4802 UserHandle.formatUid(pw, uid);
4803 pw.print("=");
4804 pw.print(utime);
4805 pw.print("u+");
4806 pw.print(stime);
4807 pw.print("s");
4808 }
4809
4810 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4811 pw.print('/');
4812 pw.print(uid);
4813 pw.print(":");
4814 pw.print(utime);
4815 pw.print(":");
4816 pw.print(stime);
4817 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004818 }
4819
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004820 private void printSizeValue(PrintWriter pw, long size) {
4821 float result = size;
4822 String suffix = "";
4823 if (result >= 10*1024) {
4824 suffix = "KB";
4825 result = result / 1024;
4826 }
4827 if (result >= 10*1024) {
4828 suffix = "MB";
4829 result = result / 1024;
4830 }
4831 if (result >= 10*1024) {
4832 suffix = "GB";
4833 result = result / 1024;
4834 }
4835 if (result >= 10*1024) {
4836 suffix = "TB";
4837 result = result / 1024;
4838 }
4839 if (result >= 10*1024) {
4840 suffix = "PB";
4841 result = result / 1024;
4842 }
4843 pw.print((int)result);
4844 pw.print(suffix);
4845 }
4846
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004847 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4848 String label3, long estimatedTime) {
4849 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004850 return false;
4851 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004852 pw.print(label1);
4853 pw.print(label2);
4854 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004855 StringBuilder sb = new StringBuilder(64);
4856 formatTimeMs(sb, estimatedTime);
4857 pw.print(sb);
4858 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004859 return true;
4860 }
4861
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004862 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4863 LevelStepTracker steps, boolean checkin) {
4864 if (steps == null) {
4865 return false;
4866 }
4867 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004868 if (count <= 0) {
4869 return false;
4870 }
4871 if (!checkin) {
4872 pw.println(header);
4873 }
Kweku Adams030980a2015-04-01 16:07:48 -07004874 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004875 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004876 long duration = steps.getDurationAt(i);
4877 int level = steps.getLevelAt(i);
4878 long initMode = steps.getInitModeAt(i);
4879 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004880 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004881 lineArgs[0] = Long.toString(duration);
4882 lineArgs[1] = Integer.toString(level);
4883 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4884 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4885 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4886 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4887 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4888 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004889 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004890 }
4891 } else {
4892 lineArgs[2] = "";
4893 }
4894 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4895 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4896 } else {
4897 lineArgs[3] = "";
4898 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004899 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004900 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004901 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004902 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004903 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004904 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4905 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004906 pw.print(prefix);
4907 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004908 TimeUtils.formatDuration(duration, pw);
4909 pw.print(" to "); pw.print(level);
4910 boolean haveModes = false;
4911 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4912 pw.print(" (");
4913 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4914 case Display.STATE_OFF: pw.print("screen-off"); break;
4915 case Display.STATE_ON: pw.print("screen-on"); break;
4916 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4917 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004918 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004919 }
4920 haveModes = true;
4921 }
4922 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4923 pw.print(haveModes ? ", " : " (");
4924 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4925 ? "power-save-on" : "power-save-off");
4926 haveModes = true;
4927 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004928 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4929 pw.print(haveModes ? ", " : " (");
4930 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4931 ? "device-idle-on" : "device-idle-off");
4932 haveModes = true;
4933 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004934 if (haveModes) {
4935 pw.print(")");
4936 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004937 pw.println();
4938 }
4939 }
4940 return true;
4941 }
4942
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004943 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004944 public static final int DUMP_DAILY_ONLY = 1<<2;
4945 public static final int DUMP_HISTORY_ONLY = 1<<3;
4946 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4947 public static final int DUMP_VERBOSE = 1<<5;
4948 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004949
Dianne Hackborn37de0982014-05-09 09:32:18 -07004950 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4951 final HistoryPrinter hprinter = new HistoryPrinter();
4952 final HistoryItem rec = new HistoryItem();
4953 long lastTime = -1;
4954 long baseTime = -1;
4955 boolean printed = false;
4956 HistoryEventTracker tracker = null;
4957 while (getNextHistoryLocked(rec)) {
4958 lastTime = rec.time;
4959 if (baseTime < 0) {
4960 baseTime = lastTime;
4961 }
4962 if (rec.time >= histStart) {
4963 if (histStart >= 0 && !printed) {
4964 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004965 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004966 || rec.cmd == HistoryItem.CMD_START
4967 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004968 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004969 hprinter.printNextItem(pw, rec, baseTime, checkin,
4970 (flags&DUMP_VERBOSE) != 0);
4971 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004972 } else if (rec.currentTime != 0) {
4973 printed = true;
4974 byte cmd = rec.cmd;
4975 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004976 hprinter.printNextItem(pw, rec, baseTime, checkin,
4977 (flags&DUMP_VERBOSE) != 0);
4978 rec.cmd = cmd;
4979 }
4980 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004981 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4982 hprinter.printNextItem(pw, rec, baseTime, checkin,
4983 (flags&DUMP_VERBOSE) != 0);
4984 rec.cmd = HistoryItem.CMD_UPDATE;
4985 }
4986 int oldEventCode = rec.eventCode;
4987 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004988 rec.eventTag = new HistoryTag();
4989 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4990 HashMap<String, SparseIntArray> active
4991 = tracker.getStateForEvent(i);
4992 if (active == null) {
4993 continue;
4994 }
4995 for (HashMap.Entry<String, SparseIntArray> ent
4996 : active.entrySet()) {
4997 SparseIntArray uids = ent.getValue();
4998 for (int j=0; j<uids.size(); j++) {
4999 rec.eventCode = i;
5000 rec.eventTag.string = ent.getKey();
5001 rec.eventTag.uid = uids.keyAt(j);
5002 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005003 hprinter.printNextItem(pw, rec, baseTime, checkin,
5004 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005005 rec.wakeReasonTag = null;
5006 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005007 }
5008 }
5009 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005010 rec.eventCode = oldEventCode;
5011 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005012 tracker = null;
5013 }
5014 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005015 hprinter.printNextItem(pw, rec, baseTime, checkin,
5016 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005017 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5018 // This is an attempt to aggregate the previous state and generate
5019 // fake events to reflect that state at the point where we start
5020 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005021 if (tracker == null) {
5022 tracker = new HistoryEventTracker();
5023 }
5024 tracker.updateState(rec.eventCode, rec.eventTag.string,
5025 rec.eventTag.uid, rec.eventTag.poolIdx);
5026 }
5027 }
5028 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005029 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005030 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5031 }
5032 }
5033
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005034 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5035 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5036 if (steps == null) {
5037 return;
5038 }
5039 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5040 if (timeRemaining >= 0) {
5041 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5042 tmpSb.setLength(0);
5043 formatTimeMs(tmpSb, timeRemaining);
5044 pw.print(tmpSb);
5045 pw.print(" (from "); pw.print(tmpOutInt[0]);
5046 pw.println(" steps)");
5047 }
5048 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5049 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5050 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5051 if (estimatedTime > 0) {
5052 pw.print(prefix); pw.print(label); pw.print(" ");
5053 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5054 pw.print(" time: ");
5055 tmpSb.setLength(0);
5056 formatTimeMs(tmpSb, estimatedTime);
5057 pw.print(tmpSb);
5058 pw.print(" (from "); pw.print(tmpOutInt[0]);
5059 pw.println(" steps)");
5060 }
5061 }
5062 }
5063
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005064 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5065 ArrayList<PackageChange> changes) {
5066 if (changes == null) {
5067 return;
5068 }
5069 pw.print(prefix); pw.println("Package changes:");
5070 for (int i=0; i<changes.size(); i++) {
5071 PackageChange pc = changes.get(i);
5072 if (pc.mUpdate) {
5073 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5074 pw.print(" vers="); pw.println(pc.mVersionCode);
5075 } else {
5076 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5077 }
5078 }
5079 }
5080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 /**
5082 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5083 *
5084 * @param pw a Printer to receive the dump output.
5085 */
5086 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005087 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005088 prepareForDumpLocked();
5089
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005090 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005091 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005092
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005093 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005094 final long historyTotalSize = getHistoryTotalSize();
5095 final long historyUsedSize = getHistoryUsedSize();
5096 if (startIteratingHistoryLocked()) {
5097 try {
5098 pw.print("Battery History (");
5099 pw.print((100*historyUsedSize)/historyTotalSize);
5100 pw.print("% used, ");
5101 printSizeValue(pw, historyUsedSize);
5102 pw.print(" used of ");
5103 printSizeValue(pw, historyTotalSize);
5104 pw.print(", ");
5105 pw.print(getHistoryStringPoolSize());
5106 pw.print(" strings using ");
5107 printSizeValue(pw, getHistoryStringPoolBytes());
5108 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005109 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005110 pw.println();
5111 } finally {
5112 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005113 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005114 }
5115
5116 if (startIteratingOldHistoryLocked()) {
5117 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005118 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005119 pw.println("Old battery History:");
5120 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005121 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005122 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005123 if (baseTime < 0) {
5124 baseTime = rec.time;
5125 }
5126 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005127 }
5128 pw.println();
5129 } finally {
5130 finishIteratingOldHistoryLocked();
5131 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005132 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005133 }
5134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005135 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005136 return;
5137 }
5138
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005139 if (!filtering) {
5140 SparseArray<? extends Uid> uidStats = getUidStats();
5141 final int NU = uidStats.size();
5142 boolean didPid = false;
5143 long nowRealtime = SystemClock.elapsedRealtime();
5144 for (int i=0; i<NU; i++) {
5145 Uid uid = uidStats.valueAt(i);
5146 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5147 if (pids != null) {
5148 for (int j=0; j<pids.size(); j++) {
5149 Uid.Pid pid = pids.valueAt(j);
5150 if (!didPid) {
5151 pw.println("Per-PID Stats:");
5152 didPid = true;
5153 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005154 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5155 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005156 pw.print(" PID "); pw.print(pids.keyAt(j));
5157 pw.print(" wake time: ");
5158 TimeUtils.formatDuration(time, pw);
5159 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005160 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005161 }
5162 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005163 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005164 pw.println();
5165 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005166 }
5167
5168 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005169 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5170 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005171 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5172 if (timeRemaining >= 0) {
5173 pw.print(" Estimated discharge time remaining: ");
5174 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5175 pw.println();
5176 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005177 final LevelStepTracker steps = getDischargeLevelStepTracker();
5178 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5179 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5180 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5181 STEP_LEVEL_MODE_VALUES[i], null));
5182 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005183 pw.println();
5184 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005185 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5186 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005187 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5188 if (timeRemaining >= 0) {
5189 pw.print(" Estimated charge time remaining: ");
5190 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5191 pw.println();
5192 }
5193 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005194 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005195 }
5196 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5197 pw.println("Daily stats:");
5198 pw.print(" Current start time: ");
5199 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5200 getCurrentDailyStartTime()).toString());
5201 pw.print(" Next min deadline: ");
5202 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5203 getNextMinDailyDeadline()).toString());
5204 pw.print(" Next max deadline: ");
5205 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5206 getNextMaxDailyDeadline()).toString());
5207 StringBuilder sb = new StringBuilder(64);
5208 int[] outInt = new int[1];
5209 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5210 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005211 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5212 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005213 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005214 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5215 dsteps, false)) {
5216 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5217 sb, outInt);
5218 }
5219 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5220 csteps, false)) {
5221 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5222 sb, outInt);
5223 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005224 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005225 } else {
5226 pw.println(" Current daily steps:");
5227 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5228 sb, outInt);
5229 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5230 sb, outInt);
5231 }
5232 }
5233 DailyItem dit;
5234 int curIndex = 0;
5235 while ((dit=getDailyItemLocked(curIndex)) != null) {
5236 curIndex++;
5237 if ((flags&DUMP_DAILY_ONLY) != 0) {
5238 pw.println();
5239 }
5240 pw.print(" Daily from ");
5241 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5242 pw.print(" to ");
5243 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5244 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005245 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005246 if (dumpDurationSteps(pw, " ",
5247 " Discharge step durations:", dit.mDischargeSteps, false)) {
5248 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5249 sb, outInt);
5250 }
5251 if (dumpDurationSteps(pw, " ",
5252 " Charge step durations:", dit.mChargeSteps, false)) {
5253 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5254 sb, outInt);
5255 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005256 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005257 } else {
5258 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5259 sb, outInt);
5260 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5261 sb, outInt);
5262 }
5263 }
5264 pw.println();
5265 }
5266 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005267 pw.println("Statistics since last charge:");
5268 pw.println(" System starts: " + getStartCount()
5269 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005270 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5271 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005272 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 }
5275
5276 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005277 public void dumpCheckinLocked(Context context, PrintWriter pw,
5278 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005279 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005280
5281 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005282 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5283 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005284
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005285 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5286
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005287 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005288 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005289
5290 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005291 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005292 try {
5293 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5294 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5295 pw.print(HISTORY_STRING_POOL); pw.print(',');
5296 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005297 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005298 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005299 pw.print(",\"");
5300 String str = getHistoryTagPoolString(i);
5301 str = str.replace("\\", "\\\\");
5302 str = str.replace("\"", "\\\"");
5303 pw.print(str);
5304 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005305 pw.println();
5306 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005307 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005308 } finally {
5309 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005310 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005311 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005312 }
5313
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005314 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005315 return;
5316 }
5317
Dianne Hackborne4a59512010-12-07 11:08:07 -08005318 if (apps != null) {
5319 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5320 for (int i=0; i<apps.size(); i++) {
5321 ApplicationInfo ai = apps.get(i);
5322 ArrayList<String> pkgs = uids.get(ai.uid);
5323 if (pkgs == null) {
5324 pkgs = new ArrayList<String>();
5325 uids.put(ai.uid, pkgs);
5326 }
5327 pkgs.add(ai.packageName);
5328 }
5329 SparseArray<? extends Uid> uidStats = getUidStats();
5330 final int NU = uidStats.size();
5331 String[] lineArgs = new String[2];
5332 for (int i=0; i<NU; i++) {
5333 int uid = uidStats.keyAt(i);
5334 ArrayList<String> pkgs = uids.get(uid);
5335 if (pkgs != null) {
5336 for (int j=0; j<pkgs.size(); j++) {
5337 lineArgs[0] = Integer.toString(uid);
5338 lineArgs[1] = pkgs.get(j);
5339 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5340 (Object[])lineArgs);
5341 }
5342 }
5343 }
5344 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005345 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005346 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005347 String[] lineArgs = new String[1];
5348 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5349 if (timeRemaining >= 0) {
5350 lineArgs[0] = Long.toString(timeRemaining);
5351 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5352 (Object[])lineArgs);
5353 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005354 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005355 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5356 if (timeRemaining >= 0) {
5357 lineArgs[0] = Long.toString(timeRemaining);
5358 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5359 (Object[])lineArgs);
5360 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005361 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5362 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365}