blob: 52fa2ed162f18a3b4b597467015fe60d3d65531e [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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * Include all of the data in the stats, including previously saved data.
151 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700152 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Include only the current run in the stats.
156 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700157 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 /**
160 * Include only the run since the last time the device was unplugged in the stats.
161 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700162 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700163
164 // NOTE: Update this list if you add/change any stats above.
165 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700166 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700167 private static final String[] STAT_NAMES = { "l", "c", "u" };
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700170 * Current version of checkin data format.
171 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800172 static final String CHECKIN_VERSION = "17";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700173
174 /**
175 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700177 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700178
Evan Millar22ac0432009-03-31 11:33:18 -0700179 private static final long BYTES_PER_KB = 1024;
180 private static final long BYTES_PER_MB = 1048576; // 1024^2
181 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700183 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800184 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700186 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700187 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700188 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800189 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700190 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700191 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700192 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700193 private static final String SYNC_DATA = "sy";
194 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700195 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700196 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700197 private static final String NETWORK_DATA = "nt";
198 private static final String USER_ACTIVITY_DATA = "ua";
199 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800200 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700201 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700202 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700203 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800204 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
205 private static final String WIFI_CONTROLLER_DATA = "wfcd";
206 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
207 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Evan Millare84de8d2009-04-02 22:16:12 -0700208 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800209 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800210 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
211 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800212 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700213 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700214 private static final String SCREEN_BRIGHTNESS_DATA = "br";
215 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700216 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700217 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
218 private static final String DATA_CONNECTION_TIME_DATA = "dct";
219 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800220 private static final String WIFI_STATE_TIME_DATA = "wst";
221 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700222 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
223 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
224 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
225 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800226 private static final String POWER_USE_SUMMARY_DATA = "pws";
227 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700228 private static final String DISCHARGE_STEP_DATA = "dsd";
229 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700230 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
231 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700232 private static final String FLASHLIGHT_DATA = "fla";
233 private static final String CAMERA_DATA = "cam";
234 private static final String VIDEO_DATA = "vid";
235 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700237 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 private final Formatter mFormatter = new Formatter(mFormatBuilder);
239
240 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700241 * State for keeping track of counting information.
242 */
243 public static abstract class Counter {
244
245 /**
246 * Returns the count associated with this Counter for the
247 * selected type of statistics.
248 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700249 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700250 */
Evan Millarc64edde2009-04-18 12:26:32 -0700251 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700252
253 /**
254 * Temporary for debugging.
255 */
256 public abstract void logState(Printer pw, String prefix);
257 }
258
259 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700260 * State for keeping track of long counting information.
261 */
262 public static abstract class LongCounter {
263
264 /**
265 * Returns the count associated with this Counter for the
266 * selected type of statistics.
267 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700268 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700269 */
270 public abstract long getCountLocked(int which);
271
272 /**
273 * Temporary for debugging.
274 */
275 public abstract void logState(Printer pw, String prefix);
276 }
277
278 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800279 * Container class that aggregates counters for transmit, receive, and idle state of a
280 * radio controller.
281 */
282 public static abstract class ControllerActivityCounter {
283 /**
284 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
285 * idle state.
286 */
287 public abstract LongCounter getIdleTimeCounter();
288
289 /**
290 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
291 * receive state.
292 */
293 public abstract LongCounter getRxTimeCounter();
294
295 /**
296 * An array of {@link LongCounter}, representing various transmit levels, where each level
297 * may draw a different amount of power. The levels themselves are controller-specific.
298 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
299 * various transmit level states.
300 */
301 public abstract LongCounter[] getTxTimeCounters();
302
303 /**
304 * @return a non-null {@link LongCounter} representing the power consumed by the controller
305 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
306 * yield a value of 0 if the device doesn't support power calculations.
307 */
308 public abstract LongCounter getPowerCounter();
309 }
310
311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 * State for keeping track of timing information.
313 */
314 public static abstract class Timer {
315
316 /**
317 * Returns the count associated with this Timer for the
318 * selected type of statistics.
319 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700320 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 */
Evan Millarc64edde2009-04-18 12:26:32 -0700322 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
324 /**
325 * Returns the total time in microseconds associated with this Timer for the
326 * selected type of statistics.
327 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800328 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700329 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * @return a time in microseconds
331 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800332 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700335 * Returns the total time in microseconds associated with this Timer since the
336 * 'mark' was last set.
337 *
338 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
339 * @return a time in microseconds
340 */
341 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
342
343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * Temporary for debugging.
345 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700346 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
348
349 /**
350 * The statistics associated with a particular uid.
351 */
352 public static abstract class Uid {
353
354 /**
355 * Returns a mapping containing wakelock statistics.
356 *
357 * @return a Map from Strings to Uid.Wakelock objects.
358 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700359 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
361 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700362 * Returns a mapping containing sync statistics.
363 *
364 * @return a Map from Strings to Timer objects.
365 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700366 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700367
368 /**
369 * Returns a mapping containing scheduled job statistics.
370 *
371 * @return a Map from Strings to Timer objects.
372 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700373 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700374
375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * The statistics associated with a particular wake lock.
377 */
378 public static abstract class Wakelock {
379 public abstract Timer getWakeTime(int type);
380 }
381
382 /**
383 * Returns a mapping containing sensor statistics.
384 *
385 * @return a Map from Integer sensor ids to Uid.Sensor objects.
386 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700387 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
389 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700390 * Returns a mapping containing active process data.
391 */
392 public abstract SparseArray<? extends Pid> getPidStats();
393
394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * Returns a mapping containing process statistics.
396 *
397 * @return a Map from Strings to Uid.Proc objects.
398 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700399 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
401 /**
402 * Returns a mapping containing package statistics.
403 *
404 * @return a Map from Strings to Uid.Pkg objects.
405 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700406 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700407
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800408 public abstract ControllerActivityCounter getWifiControllerActivity();
409 public abstract ControllerActivityCounter getBluetoothControllerActivity();
410 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800411
412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * {@hide}
414 */
415 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700416
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800417 public abstract void noteWifiRunningLocked(long elapsedRealtime);
418 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
419 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
420 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
421 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
422 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
423 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
424 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
425 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
426 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800427 public abstract void noteActivityResumedLocked(long elapsedRealtime);
428 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800429 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
430 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
431 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700432 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800433 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700434 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800435 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700436 public abstract Timer getAudioTurnedOnTimer();
437 public abstract Timer getVideoTurnedOnTimer();
438 public abstract Timer getFlashlightTurnedOnTimer();
439 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700440 public abstract Timer getForegroundActivityTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700441
Dianne Hackborna8d10942015-11-19 17:55:19 -0800442 // Time this uid has any processes in the top state.
443 public static final int PROCESS_STATE_TOP = 0;
444 // Time this uid has any process with a started out bound foreground service.
445 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
446 // Time this uid has any process that is top while the device is sleeping.
447 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
448 // Time this uid has any process in an active foreground state.
449 public static final int PROCESS_STATE_FOREGROUND = 3;
450 // Time this uid has any process in an active background state.
451 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700452 // Time this uid has any processes running at all.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800453 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700454 // Total number of process states we track.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800455 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700456
457 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800458 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700459 };
460
461 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
462
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800463 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464
Robert Greenwalta029ea12013-09-25 16:38:12 -0700465 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
466
Dianne Hackborn617f8772009-03-31 15:04:46 -0700467 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700468 * Note that these must match the constants in android.os.PowerManager.
469 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
470 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700471 */
472 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700473 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700474 };
475
Jeff Browndf693de2012-07-27 12:03:38 -0700476 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700477
Dianne Hackborn617f8772009-03-31 15:04:46 -0700478 public abstract void noteUserActivityLocked(int type);
479 public abstract boolean hasUserActivity();
480 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700481
482 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800483 public abstract long getNetworkActivityBytes(int type, int which);
484 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800485 public abstract long getMobileRadioActiveTime(int which);
486 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700487
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700488 /**
489 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
490 */
491 public abstract long getUserCpuTimeUs(int which);
492
493 /**
494 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
495 */
496 public abstract long getSystemCpuTimeUs(int which);
497
498 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700499 * Get the total cpu power consumed (in milli-ampere-microseconds).
500 */
501 public abstract long getCpuPowerMaUs(int which);
502
503 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700504 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
505 * given CPU cluster.
506 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700507 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700508 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700509 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
510 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700511 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700512 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800515 /*
516 * FIXME: it's not correct to use this magic value because it
517 * could clash with a sensor handle (which are defined by
518 * the sensor HAL, and therefore out of our control
519 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 // Magic sensor number for the GPS.
521 public static final int GPS = -10000;
522
523 public abstract int getHandle();
524
525 public abstract Timer getSensorTime();
526 }
527
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700528 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800529 public int mWakeNesting;
530 public long mWakeSumMs;
531 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700532 }
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
535 * The statistics associated with a particular process.
536 */
537 public static abstract class Proc {
538
Dianne Hackborn287952c2010-09-22 22:34:31 -0700539 public static class ExcessivePower {
540 public static final int TYPE_WAKE = 1;
541 public static final int TYPE_CPU = 2;
542
543 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700544 public long overTime;
545 public long usedTime;
546 }
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800549 * Returns true if this process is still active in the battery stats.
550 */
551 public abstract boolean isActive();
552
553 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700554 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700556 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 */
558 public abstract long getUserTime(int which);
559
560 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700561 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700563 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 */
565 public abstract long getSystemTime(int which);
566
567 /**
568 * Returns the number of times the process has been started.
569 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700570 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 */
572 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700573
574 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800575 * Returns the number of times the process has crashed.
576 *
577 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
578 */
579 public abstract int getNumCrashes(int which);
580
581 /**
582 * Returns the number of times the process has ANRed.
583 *
584 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
585 */
586 public abstract int getNumAnrs(int which);
587
588 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700589 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700590 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700591 * @return foreground cpu time in microseconds
592 */
593 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700594
Dianne Hackborn287952c2010-09-22 22:34:31 -0700595 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700596
Dianne Hackborn287952c2010-09-22 22:34:31 -0700597 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
599
600 /**
601 * The statistics associated with a particular package.
602 */
603 public static abstract class Pkg {
604
605 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700606 * Returns information about all wakeup alarms that have been triggered for this
607 * package. The mapping keys are tag names for the alarms, the counter contains
608 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700610 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 /**
613 * Returns a mapping containing service statistics.
614 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700615 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616
617 /**
618 * The statistics associated with a particular service.
619 */
620 public abstract class Serv {
621
622 /**
623 * Returns the amount of time spent started.
624 *
625 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700626 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @return
628 */
629 public abstract long getStartTime(long batteryUptime, int which);
630
631 /**
632 * Returns the total number of times startService() has been called.
633 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700634 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 */
636 public abstract int getStarts(int which);
637
638 /**
639 * Returns the total number times the service has been launched.
640 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700641 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 */
643 public abstract int getLaunches(int which);
644 }
645 }
646 }
647
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800648 public static final class LevelStepTracker {
649 public long mLastStepTime = -1;
650 public int mNumStepDurations;
651 public final long[] mStepDurations;
652
653 public LevelStepTracker(int maxLevelSteps) {
654 mStepDurations = new long[maxLevelSteps];
655 }
656
657 public LevelStepTracker(int numSteps, long[] steps) {
658 mNumStepDurations = numSteps;
659 mStepDurations = new long[numSteps];
660 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
661 }
662
663 public long getDurationAt(int index) {
664 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
665 }
666
667 public int getLevelAt(int index) {
668 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
669 >> STEP_LEVEL_LEVEL_SHIFT);
670 }
671
672 public int getInitModeAt(int index) {
673 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
674 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
675 }
676
677 public int getModModeAt(int index) {
678 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
679 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
680 }
681
682 private void appendHex(long val, int topOffset, StringBuilder out) {
683 boolean hasData = false;
684 while (topOffset >= 0) {
685 int digit = (int)( (val>>topOffset) & 0xf );
686 topOffset -= 4;
687 if (!hasData && digit == 0) {
688 continue;
689 }
690 hasData = true;
691 if (digit >= 0 && digit <= 9) {
692 out.append((char)('0' + digit));
693 } else {
694 out.append((char)('a' + digit - 10));
695 }
696 }
697 }
698
699 public void encodeEntryAt(int index, StringBuilder out) {
700 long item = mStepDurations[index];
701 long duration = item & STEP_LEVEL_TIME_MASK;
702 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
703 >> STEP_LEVEL_LEVEL_SHIFT);
704 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
705 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
706 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
707 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
708 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
709 case Display.STATE_OFF: out.append('f'); break;
710 case Display.STATE_ON: out.append('o'); break;
711 case Display.STATE_DOZE: out.append('d'); break;
712 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
713 }
714 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
715 out.append('p');
716 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700717 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
718 out.append('i');
719 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800720 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
721 case Display.STATE_OFF: out.append('F'); break;
722 case Display.STATE_ON: out.append('O'); break;
723 case Display.STATE_DOZE: out.append('D'); break;
724 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
725 }
726 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
727 out.append('P');
728 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700729 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
730 out.append('I');
731 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800732 out.append('-');
733 appendHex(level, 4, out);
734 out.append('-');
735 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
736 }
737
738 public void decodeEntryAt(int index, String value) {
739 final int N = value.length();
740 int i = 0;
741 char c;
742 long out = 0;
743 while (i < N && (c=value.charAt(i)) != '-') {
744 i++;
745 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800746 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800747 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800748 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800749 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800750 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800751 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800752 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
753 << STEP_LEVEL_INITIAL_MODE_SHIFT);
754 break;
755 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
756 << STEP_LEVEL_INITIAL_MODE_SHIFT);
757 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700758 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
759 << STEP_LEVEL_INITIAL_MODE_SHIFT);
760 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800761 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
762 break;
763 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
764 break;
765 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
766 break;
767 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
768 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
769 break;
770 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
771 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800772 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700773 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
774 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
775 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800776 }
777 }
778 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800779 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800780 while (i < N && (c=value.charAt(i)) != '-') {
781 i++;
782 level <<= 4;
783 if (c >= '0' && c <= '9') {
784 level += c - '0';
785 } else if (c >= 'a' && c <= 'f') {
786 level += c - 'a' + 10;
787 } else if (c >= 'A' && c <= 'F') {
788 level += c - 'A' + 10;
789 }
790 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800791 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800792 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
793 long duration = 0;
794 while (i < N && (c=value.charAt(i)) != '-') {
795 i++;
796 duration <<= 4;
797 if (c >= '0' && c <= '9') {
798 duration += c - '0';
799 } else if (c >= 'a' && c <= 'f') {
800 duration += c - 'a' + 10;
801 } else if (c >= 'A' && c <= 'F') {
802 duration += c - 'A' + 10;
803 }
804 }
805 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
806 }
807
808 public void init() {
809 mLastStepTime = -1;
810 mNumStepDurations = 0;
811 }
812
813 public void clearTime() {
814 mLastStepTime = -1;
815 }
816
817 public long computeTimePerLevel() {
818 final long[] steps = mStepDurations;
819 final int numSteps = mNumStepDurations;
820
821 // For now we'll do a simple average across all steps.
822 if (numSteps <= 0) {
823 return -1;
824 }
825 long total = 0;
826 for (int i=0; i<numSteps; i++) {
827 total += steps[i] & STEP_LEVEL_TIME_MASK;
828 }
829 return total / numSteps;
830 /*
831 long[] buckets = new long[numSteps];
832 int numBuckets = 0;
833 int numToAverage = 4;
834 int i = 0;
835 while (i < numSteps) {
836 long totalTime = 0;
837 int num = 0;
838 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
839 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
840 num++;
841 }
842 buckets[numBuckets] = totalTime / num;
843 numBuckets++;
844 numToAverage *= 2;
845 i += num;
846 }
847 if (numBuckets < 1) {
848 return -1;
849 }
850 long averageTime = buckets[numBuckets-1];
851 for (i=numBuckets-2; i>=0; i--) {
852 averageTime = (averageTime + buckets[i]) / 2;
853 }
854 return averageTime;
855 */
856 }
857
858 public long computeTimeEstimate(long modesOfInterest, long modeValues,
859 int[] outNumOfInterest) {
860 final long[] steps = mStepDurations;
861 final int count = mNumStepDurations;
862 if (count <= 0) {
863 return -1;
864 }
865 long total = 0;
866 int numOfInterest = 0;
867 for (int i=0; i<count; i++) {
868 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
869 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
870 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
871 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
872 // If the modes of interest didn't change during this step period...
873 if ((modMode&modesOfInterest) == 0) {
874 // And the mode values during this period match those we are measuring...
875 if ((initMode&modesOfInterest) == modeValues) {
876 // Then this can be used to estimate the total time!
877 numOfInterest++;
878 total += steps[i] & STEP_LEVEL_TIME_MASK;
879 }
880 }
881 }
882 if (numOfInterest <= 0) {
883 return -1;
884 }
885
886 if (outNumOfInterest != null) {
887 outNumOfInterest[0] = numOfInterest;
888 }
889
890 // The estimated time is the average time we spend in each level, multipled
891 // by 100 -- the total number of battery levels
892 return (total / numOfInterest) * 100;
893 }
894
895 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
896 int stepCount = mNumStepDurations;
897 final long lastStepTime = mLastStepTime;
898 if (lastStepTime >= 0 && numStepLevels > 0) {
899 final long[] steps = mStepDurations;
900 long duration = elapsedRealtime - lastStepTime;
901 for (int i=0; i<numStepLevels; i++) {
902 System.arraycopy(steps, 0, steps, 1, steps.length-1);
903 long thisDuration = duration / (numStepLevels-i);
904 duration -= thisDuration;
905 if (thisDuration > STEP_LEVEL_TIME_MASK) {
906 thisDuration = STEP_LEVEL_TIME_MASK;
907 }
908 steps[0] = thisDuration | modeBits;
909 }
910 stepCount += numStepLevels;
911 if (stepCount > steps.length) {
912 stepCount = steps.length;
913 }
914 }
915 mNumStepDurations = stepCount;
916 mLastStepTime = elapsedRealtime;
917 }
918
919 public void readFromParcel(Parcel in) {
920 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -0700921 if (N > mStepDurations.length) {
922 throw new ParcelFormatException("more step durations than available: " + N);
923 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800924 mNumStepDurations = N;
925 for (int i=0; i<N; i++) {
926 mStepDurations[i] = in.readLong();
927 }
928 }
929
930 public void writeToParcel(Parcel out) {
931 final int N = mNumStepDurations;
932 out.writeInt(N);
933 for (int i=0; i<N; i++) {
934 out.writeLong(mStepDurations[i]);
935 }
936 }
937 }
938
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700939 public static final class PackageChange {
940 public String mPackageName;
941 public boolean mUpdate;
942 public int mVersionCode;
943 }
944
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800945 public static final class DailyItem {
946 public long mStartTime;
947 public long mEndTime;
948 public LevelStepTracker mDischargeSteps;
949 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700950 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800951 }
952
953 public abstract DailyItem getDailyItemLocked(int daysAgo);
954
955 public abstract long getCurrentDailyStartTime();
956
957 public abstract long getNextMinDailyDeadline();
958
959 public abstract long getNextMaxDailyDeadline();
960
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800961 public final static class HistoryTag {
962 public String string;
963 public int uid;
964
965 public int poolIdx;
966
967 public void setTo(HistoryTag o) {
968 string = o.string;
969 uid = o.uid;
970 poolIdx = o.poolIdx;
971 }
972
973 public void setTo(String _string, int _uid) {
974 string = _string;
975 uid = _uid;
976 poolIdx = -1;
977 }
978
979 public void writeToParcel(Parcel dest, int flags) {
980 dest.writeString(string);
981 dest.writeInt(uid);
982 }
983
984 public void readFromParcel(Parcel src) {
985 string = src.readString();
986 uid = src.readInt();
987 poolIdx = -1;
988 }
989
990 @Override
991 public boolean equals(Object o) {
992 if (this == o) return true;
993 if (o == null || getClass() != o.getClass()) return false;
994
995 HistoryTag that = (HistoryTag) o;
996
997 if (uid != that.uid) return false;
998 if (!string.equals(that.string)) return false;
999
1000 return true;
1001 }
1002
1003 @Override
1004 public int hashCode() {
1005 int result = string.hashCode();
1006 result = 31 * result + uid;
1007 return result;
1008 }
1009 }
1010
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001011 /**
1012 * Optional detailed information that can go into a history step. This is typically
1013 * generated each time the battery level changes.
1014 */
1015 public final static class HistoryStepDetails {
1016 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1017 public int userTime;
1018 public int systemTime;
1019
1020 // Top three apps using CPU in the last step, with times in 1/100 second.
1021 public int appCpuUid1;
1022 public int appCpuUTime1;
1023 public int appCpuSTime1;
1024 public int appCpuUid2;
1025 public int appCpuUTime2;
1026 public int appCpuSTime2;
1027 public int appCpuUid3;
1028 public int appCpuUTime3;
1029 public int appCpuSTime3;
1030
1031 // Information from /proc/stat
1032 public int statUserTime;
1033 public int statSystemTime;
1034 public int statIOWaitTime;
1035 public int statIrqTime;
1036 public int statSoftIrqTime;
1037 public int statIdlTime;
1038
1039 public HistoryStepDetails() {
1040 clear();
1041 }
1042
1043 public void clear() {
1044 userTime = systemTime = 0;
1045 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1046 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1047 = appCpuUTime3 = appCpuSTime3 = 0;
1048 }
1049
1050 public void writeToParcel(Parcel out) {
1051 out.writeInt(userTime);
1052 out.writeInt(systemTime);
1053 out.writeInt(appCpuUid1);
1054 out.writeInt(appCpuUTime1);
1055 out.writeInt(appCpuSTime1);
1056 out.writeInt(appCpuUid2);
1057 out.writeInt(appCpuUTime2);
1058 out.writeInt(appCpuSTime2);
1059 out.writeInt(appCpuUid3);
1060 out.writeInt(appCpuUTime3);
1061 out.writeInt(appCpuSTime3);
1062 out.writeInt(statUserTime);
1063 out.writeInt(statSystemTime);
1064 out.writeInt(statIOWaitTime);
1065 out.writeInt(statIrqTime);
1066 out.writeInt(statSoftIrqTime);
1067 out.writeInt(statIdlTime);
1068 }
1069
1070 public void readFromParcel(Parcel in) {
1071 userTime = in.readInt();
1072 systemTime = in.readInt();
1073 appCpuUid1 = in.readInt();
1074 appCpuUTime1 = in.readInt();
1075 appCpuSTime1 = in.readInt();
1076 appCpuUid2 = in.readInt();
1077 appCpuUTime2 = in.readInt();
1078 appCpuSTime2 = in.readInt();
1079 appCpuUid3 = in.readInt();
1080 appCpuUTime3 = in.readInt();
1081 appCpuSTime3 = in.readInt();
1082 statUserTime = in.readInt();
1083 statSystemTime = in.readInt();
1084 statIOWaitTime = in.readInt();
1085 statIrqTime = in.readInt();
1086 statSoftIrqTime = in.readInt();
1087 statIdlTime = in.readInt();
1088 }
1089 }
1090
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001091 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001092 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001093
1094 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001095 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001096
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001097 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001098 public static final byte CMD_NULL = -1;
1099 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001100 public static final byte CMD_CURRENT_TIME = 5;
1101 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001102 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001103 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001104
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001105 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001106
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001107 /**
1108 * Return whether the command code is a delta data update.
1109 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001110 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001111 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001112 }
1113
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001114 public byte batteryLevel;
1115 public byte batteryStatus;
1116 public byte batteryHealth;
1117 public byte batteryPlugType;
1118
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001119 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001120 public char batteryVoltage;
1121
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001122 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001123 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001124 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001125 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001126 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1127 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001128 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001129 public static final int STATE_PHONE_STATE_SHIFT = 6;
1130 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001131 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001132 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001133 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001134
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001135 // These states always appear directly in the first int token
1136 // of a delta change; they should be ones that change relatively
1137 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001138 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1139 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001140 public static final int STATE_GPS_ON_FLAG = 1<<29;
1141 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001142 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001143 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001144 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001145 // These are on the lower bits used for the command; if they change
1146 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001147 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001148 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001149 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001150 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1151 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1152 // empty slot
1153 // empty slot
1154 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001155
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001156 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001157 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1158
1159 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001160
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001161 public int states;
1162
Dianne Hackborn3251b902014-06-20 14:40:53 -07001163 // Constants from WIFI_SUPPL_STATE_*
1164 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1165 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1166 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1167 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1168 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1169 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1170
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001171 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001172 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1173 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1174 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001175 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001176 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1177 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1178 public static final int STATE2_CHARGING_FLAG = 1<<24;
1179 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1180 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1181 public static final int STATE2_CAMERA_FLAG = 1<<21;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001182
1183 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001184 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001185 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1186
1187 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001188
Dianne Hackborn40c87252014-03-19 16:55:40 -07001189 public int states2;
1190
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001191 // The wake lock that was acquired at this point.
1192 public HistoryTag wakelockTag;
1193
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001194 // Kernel wakeup reason at this point.
1195 public HistoryTag wakeReasonTag;
1196
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001197 // Non-null when there is more detailed information at this step.
1198 public HistoryStepDetails stepDetails;
1199
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001200 public static final int EVENT_FLAG_START = 0x8000;
1201 public static final int EVENT_FLAG_FINISH = 0x4000;
1202
1203 // No event in this item.
1204 public static final int EVENT_NONE = 0x0000;
1205 // Event is about a process that is running.
1206 public static final int EVENT_PROC = 0x0001;
1207 // Event is about an application package that is in the foreground.
1208 public static final int EVENT_FOREGROUND = 0x0002;
1209 // Event is about an application package that is at the top of the screen.
1210 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001211 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001212 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001213 // Events for all additional wake locks aquired/release within a wake block.
1214 // These are not generated by default.
1215 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001216 // Event is about an application executing a scheduled job.
1217 public static final int EVENT_JOB = 0x0006;
1218 // Events for users running.
1219 public static final int EVENT_USER_RUNNING = 0x0007;
1220 // Events for foreground user.
1221 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001222 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001223 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001224 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001225 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001226 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001227 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001228 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001229 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001230 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001231 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001232 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001233 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001234 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001235 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001236 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001237 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001238 // Event for a package being on the temporary whitelist.
1239 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001240 // Event for the screen waking up.
1241 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Amith Yamasani67768492015-06-09 12:23:58 -07001242
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001243 // Number of event types.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001244 public static final int EVENT_COUNT = 0x0013;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001245 // Mask to extract out only the type part of the event.
1246 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001247
1248 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1249 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1250 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1251 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1252 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1253 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001254 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1255 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001256 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1257 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001258 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1259 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1260 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1261 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1262 public static final int EVENT_USER_FOREGROUND_START =
1263 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1264 public static final int EVENT_USER_FOREGROUND_FINISH =
1265 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001266 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1267 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001268 public static final int EVENT_TEMP_WHITELIST_START =
1269 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1270 public static final int EVENT_TEMP_WHITELIST_FINISH =
1271 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001272
1273 // For CMD_EVENT.
1274 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001275 public HistoryTag eventTag;
1276
Dianne Hackborn9a755432014-05-15 17:05:22 -07001277 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001278 public long currentTime;
1279
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001280 // Meta-data when reading.
1281 public int numReadInts;
1282
1283 // Pre-allocated objects.
1284 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001285 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001286 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001287
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001288 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001289 }
1290
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001291 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001292 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001293 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001294 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001295 }
1296
1297 public int describeContents() {
1298 return 0;
1299 }
1300
1301 public void writeToParcel(Parcel dest, int flags) {
1302 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001303 int bat = (((int)cmd)&0xff)
1304 | ((((int)batteryLevel)<<8)&0xff00)
1305 | ((((int)batteryStatus)<<16)&0xf0000)
1306 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001307 | ((((int)batteryPlugType)<<24)&0xf000000)
1308 | (wakelockTag != null ? 0x10000000 : 0)
1309 | (wakeReasonTag != null ? 0x20000000 : 0)
1310 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001311 dest.writeInt(bat);
1312 bat = (((int)batteryTemperature)&0xffff)
1313 | ((((int)batteryVoltage)<<16)&0xffff0000);
1314 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001315 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001316 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001317 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001318 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001319 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001320 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001321 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001322 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001323 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001324 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001325 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001326 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001327 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001328 dest.writeLong(currentTime);
1329 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001330 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001331
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001332 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001333 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001334 int bat = src.readInt();
1335 cmd = (byte)(bat&0xff);
1336 batteryLevel = (byte)((bat>>8)&0xff);
1337 batteryStatus = (byte)((bat>>16)&0xf);
1338 batteryHealth = (byte)((bat>>20)&0xf);
1339 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001340 int bat2 = src.readInt();
1341 batteryTemperature = (short)(bat2&0xffff);
1342 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001343 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001344 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001345 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001346 wakelockTag = localWakelockTag;
1347 wakelockTag.readFromParcel(src);
1348 } else {
1349 wakelockTag = null;
1350 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001351 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001352 wakeReasonTag = localWakeReasonTag;
1353 wakeReasonTag.readFromParcel(src);
1354 } else {
1355 wakeReasonTag = null;
1356 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001357 if ((bat&0x40000000) != 0) {
1358 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001359 eventTag = localEventTag;
1360 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001361 } else {
1362 eventCode = EVENT_NONE;
1363 eventTag = null;
1364 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001365 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001366 currentTime = src.readLong();
1367 } else {
1368 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001369 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001370 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001371 }
1372
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001373 public void clear() {
1374 time = 0;
1375 cmd = CMD_NULL;
1376 batteryLevel = 0;
1377 batteryStatus = 0;
1378 batteryHealth = 0;
1379 batteryPlugType = 0;
1380 batteryTemperature = 0;
1381 batteryVoltage = 0;
1382 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001383 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001384 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001385 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001386 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001387 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001388 }
1389
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001390 public void setTo(HistoryItem o) {
1391 time = o.time;
1392 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001393 setToCommon(o);
1394 }
1395
1396 public void setTo(long time, byte cmd, HistoryItem o) {
1397 this.time = time;
1398 this.cmd = cmd;
1399 setToCommon(o);
1400 }
1401
1402 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001403 batteryLevel = o.batteryLevel;
1404 batteryStatus = o.batteryStatus;
1405 batteryHealth = o.batteryHealth;
1406 batteryPlugType = o.batteryPlugType;
1407 batteryTemperature = o.batteryTemperature;
1408 batteryVoltage = o.batteryVoltage;
1409 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001410 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001411 if (o.wakelockTag != null) {
1412 wakelockTag = localWakelockTag;
1413 wakelockTag.setTo(o.wakelockTag);
1414 } else {
1415 wakelockTag = null;
1416 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001417 if (o.wakeReasonTag != null) {
1418 wakeReasonTag = localWakeReasonTag;
1419 wakeReasonTag.setTo(o.wakeReasonTag);
1420 } else {
1421 wakeReasonTag = null;
1422 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001423 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001424 if (o.eventTag != null) {
1425 eventTag = localEventTag;
1426 eventTag.setTo(o.eventTag);
1427 } else {
1428 eventTag = null;
1429 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001430 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001431 }
1432
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001433 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001434 return batteryLevel == o.batteryLevel
1435 && batteryStatus == o.batteryStatus
1436 && batteryHealth == o.batteryHealth
1437 && batteryPlugType == o.batteryPlugType
1438 && batteryTemperature == o.batteryTemperature
1439 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001440 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001441 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001442 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001443 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001444
1445 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001446 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001447 return false;
1448 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001449 if (wakelockTag != o.wakelockTag) {
1450 if (wakelockTag == null || o.wakelockTag == null) {
1451 return false;
1452 }
1453 if (!wakelockTag.equals(o.wakelockTag)) {
1454 return false;
1455 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001456 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001457 if (wakeReasonTag != o.wakeReasonTag) {
1458 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1459 return false;
1460 }
1461 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1462 return false;
1463 }
1464 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001465 if (eventTag != o.eventTag) {
1466 if (eventTag == null || o.eventTag == null) {
1467 return false;
1468 }
1469 if (!eventTag.equals(o.eventTag)) {
1470 return false;
1471 }
1472 }
1473 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001474 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001475 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001476
1477 public final static class HistoryEventTracker {
1478 private final HashMap<String, SparseIntArray>[] mActiveEvents
1479 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1480
1481 public boolean updateState(int code, String name, int uid, int poolIdx) {
1482 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1483 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1484 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1485 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001486 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001487 mActiveEvents[idx] = active;
1488 }
1489 SparseIntArray uids = active.get(name);
1490 if (uids == null) {
1491 uids = new SparseIntArray();
1492 active.put(name, uids);
1493 }
1494 if (uids.indexOfKey(uid) >= 0) {
1495 // Already set, nothing to do!
1496 return false;
1497 }
1498 uids.put(uid, poolIdx);
1499 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1500 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1501 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1502 if (active == null) {
1503 // not currently active, nothing to do.
1504 return false;
1505 }
1506 SparseIntArray uids = active.get(name);
1507 if (uids == null) {
1508 // not currently active, nothing to do.
1509 return false;
1510 }
1511 idx = uids.indexOfKey(uid);
1512 if (idx < 0) {
1513 // not currently active, nothing to do.
1514 return false;
1515 }
1516 uids.removeAt(idx);
1517 if (uids.size() <= 0) {
1518 active.remove(name);
1519 }
1520 }
1521 return true;
1522 }
1523
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001524 public void removeEvents(int code) {
1525 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1526 mActiveEvents[idx] = null;
1527 }
1528
Dianne Hackborn37de0982014-05-09 09:32:18 -07001529 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1530 return mActiveEvents[code];
1531 }
1532 }
1533
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001534 public static final class BitDescription {
1535 public final int mask;
1536 public final int shift;
1537 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001538 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001539 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001540 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001541
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001542 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001543 this.mask = mask;
1544 this.shift = -1;
1545 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001546 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001547 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001548 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001549 }
1550
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001551 public BitDescription(int mask, int shift, String name, String shortName,
1552 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001553 this.mask = mask;
1554 this.shift = shift;
1555 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001556 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001557 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001558 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001559 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001560 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001561
Dianne Hackbornfc064132014-06-02 12:42:12 -07001562 /**
1563 * Don't allow any more batching in to the current history event. This
1564 * is called when printing partial histories, so to ensure that the next
1565 * history event will go in to a new batch after what was printed in the
1566 * last partial history.
1567 */
1568 public abstract void commitCurrentHistoryBatchLocked();
1569
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001570 public abstract int getHistoryTotalSize();
1571
1572 public abstract int getHistoryUsedSize();
1573
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001574 public abstract boolean startIteratingHistoryLocked();
1575
Dianne Hackborn099bc622014-01-22 13:39:16 -08001576 public abstract int getHistoryStringPoolSize();
1577
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001578 public abstract int getHistoryStringPoolBytes();
1579
1580 public abstract String getHistoryTagPoolString(int index);
1581
1582 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001583
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001584 public abstract boolean getNextHistoryLocked(HistoryItem out);
1585
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001586 public abstract void finishIteratingHistoryLocked();
1587
1588 public abstract boolean startIteratingOldHistoryLocked();
1589
1590 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1591
1592 public abstract void finishIteratingOldHistoryLocked();
1593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001595 * Return the base time offset for the battery history.
1596 */
1597 public abstract long getHistoryBaseTime();
1598
1599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 * Returns the number of times the device has been started.
1601 */
1602 public abstract int getStartCount();
1603
1604 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001605 * 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 -08001606 * running on battery.
1607 *
1608 * {@hide}
1609 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001610 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001612 /**
1613 * Returns the number of times the screen was turned on.
1614 *
1615 * {@hide}
1616 */
1617 public abstract int getScreenOnCount(int which);
1618
Jeff Browne95c3cd2014-05-02 16:59:26 -07001619 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1620
Dianne Hackborn617f8772009-03-31 15:04:46 -07001621 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1622 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1623 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1624 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1625 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1626
1627 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1628 "dark", "dim", "medium", "light", "bright"
1629 };
1630
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001631 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1632 "0", "1", "2", "3", "4"
1633 };
1634
Dianne Hackborn617f8772009-03-31 15:04:46 -07001635 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001636
Dianne Hackborn617f8772009-03-31 15:04:46 -07001637 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001638 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001639 * the given brightness
1640 *
1641 * {@hide}
1642 */
1643 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001644 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001647 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001648 * running on battery.
1649 *
1650 * {@hide}
1651 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001652 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001653
1654 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001655 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001656 *
1657 * {@hide}
1658 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001659 public abstract int getPowerSaveModeEnabledCount(int which);
1660
1661 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001662 * Constant for device idle mode: not active.
1663 */
1664 public static final int DEVICE_IDLE_MODE_OFF = 0;
1665
1666 /**
1667 * Constant for device idle mode: active in lightweight mode.
1668 */
1669 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1670
1671 /**
1672 * Constant for device idle mode: active in full mode.
1673 */
1674 public static final int DEVICE_IDLE_MODE_FULL = 2;
1675
1676 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001677 * Returns the time in microseconds that device has been in idle mode while
1678 * running on battery.
1679 *
1680 * {@hide}
1681 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001682 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001683
1684 /**
1685 * Returns the number of times that the devie has gone in to idle mode.
1686 *
1687 * {@hide}
1688 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001689 public abstract int getDeviceIdleModeCount(int mode, int which);
1690
1691 /**
1692 * Return the longest duration we spent in a particular device idle mode (fully in the
1693 * mode, not in idle maintenance etc).
1694 */
1695 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001696
1697 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001698 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001699 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001700 * counts all of the time that we consider the device to be idle, whether or not
1701 * it is currently in the actual device idle mode.
1702 *
1703 * {@hide}
1704 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001705 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001706
1707 /**
1708 * Returns the number of times that the devie has started idling.
1709 *
1710 * {@hide}
1711 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001712 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001713
1714 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001715 * Returns the number of times that connectivity state changed.
1716 *
1717 * {@hide}
1718 */
1719 public abstract int getNumConnectivityChange(int which);
1720
1721 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001722 * 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 -08001723 * running on battery.
1724 *
1725 * {@hide}
1726 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001727 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001728
1729 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001730 * Returns the number of times a phone call was activated.
1731 *
1732 * {@hide}
1733 */
1734 public abstract int getPhoneOnCount(int which);
1735
1736 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001737 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001738 * the given signal strength.
1739 *
1740 * {@hide}
1741 */
1742 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001743 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001744
Dianne Hackborn617f8772009-03-31 15:04:46 -07001745 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001746 * Returns the time in microseconds that the phone has been trying to
1747 * acquire a signal.
1748 *
1749 * {@hide}
1750 */
1751 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001752 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001753
1754 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001755 * Returns the number of times the phone has entered the given signal strength.
1756 *
1757 * {@hide}
1758 */
1759 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1760
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001761 /**
1762 * Returns the time in microseconds that the mobile network has been active
1763 * (in a high power state).
1764 *
1765 * {@hide}
1766 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001767 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001768
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001769 /**
1770 * Returns the number of times that the mobile network has transitioned to the
1771 * active state.
1772 *
1773 * {@hide}
1774 */
1775 public abstract int getMobileRadioActiveCount(int which);
1776
1777 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001778 * Returns the time in microseconds that is the difference between the mobile radio
1779 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1780 * from the radio.
1781 *
1782 * {@hide}
1783 */
1784 public abstract long getMobileRadioActiveAdjustedTime(int which);
1785
1786 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001787 * Returns the time in microseconds that the mobile network has been active
1788 * (in a high power state) but not being able to blame on an app.
1789 *
1790 * {@hide}
1791 */
1792 public abstract long getMobileRadioActiveUnknownTime(int which);
1793
1794 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001795 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001796 *
1797 * {@hide}
1798 */
1799 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001800
Dianne Hackborn627bba72009-03-24 22:32:56 -07001801 public static final int DATA_CONNECTION_NONE = 0;
1802 public static final int DATA_CONNECTION_GPRS = 1;
1803 public static final int DATA_CONNECTION_EDGE = 2;
1804 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001805 public static final int DATA_CONNECTION_CDMA = 4;
1806 public static final int DATA_CONNECTION_EVDO_0 = 5;
1807 public static final int DATA_CONNECTION_EVDO_A = 6;
1808 public static final int DATA_CONNECTION_1xRTT = 7;
1809 public static final int DATA_CONNECTION_HSDPA = 8;
1810 public static final int DATA_CONNECTION_HSUPA = 9;
1811 public static final int DATA_CONNECTION_HSPA = 10;
1812 public static final int DATA_CONNECTION_IDEN = 11;
1813 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001814 public static final int DATA_CONNECTION_LTE = 13;
1815 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001816 public static final int DATA_CONNECTION_HSPAP = 15;
1817 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001818
Dianne Hackborn627bba72009-03-24 22:32:56 -07001819 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001820 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001821 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001822 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001823 };
1824
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001825 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001826
1827 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001828 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001829 * the given data connection.
1830 *
1831 * {@hide}
1832 */
1833 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001834 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001837 * Returns the number of times the phone has entered the given data
1838 * connection type.
1839 *
1840 * {@hide}
1841 */
1842 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001843
Dianne Hackborn3251b902014-06-20 14:40:53 -07001844 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1845 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1846 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1847 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1848 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1849 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1850 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1851 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1852 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1853 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1854 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1855 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1856 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1857
1858 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1859
1860 static final String[] WIFI_SUPPL_STATE_NAMES = {
1861 "invalid", "disconn", "disabled", "inactive", "scanning",
1862 "authenticating", "associating", "associated", "4-way-handshake",
1863 "group-handshake", "completed", "dormant", "uninit"
1864 };
1865
1866 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1867 "inv", "dsc", "dis", "inact", "scan",
1868 "auth", "ascing", "asced", "4-way",
1869 "group", "compl", "dorm", "uninit"
1870 };
1871
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001872 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1873 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001874 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001875 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1876 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001877 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001878 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1879 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1880 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001881 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001882 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001883 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001884 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001885 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1886 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001887 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1888 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1889 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1890 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1891 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1892 new String[] {"in", "out", "emergency", "off"},
1893 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001894 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1895 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1896 SignalStrength.SIGNAL_STRENGTH_NAMES,
1897 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001898 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1899 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1900 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001901 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001902
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001903 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1904 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001905 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001906 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001907 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001908 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001909 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001910 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
1911 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
1912 new String[] { "off", "light", "full", "???" },
1913 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001914 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1915 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1916 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001917 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1918 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1919 new String[] { "0", "1", "2", "3", "4" },
1920 new String[] { "0", "1", "2", "3", "4" }),
1921 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1922 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1923 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001924 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001925 };
1926
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001927 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001928 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001929 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
1930 "screenwake",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001931 };
1932
1933 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001934 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001935 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
1936 "Esw",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001937 };
1938
Dianne Hackborn617f8772009-03-31 15:04:46 -07001939 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001940 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001941 * running on battery.
1942 *
1943 * {@hide}
1944 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001945 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001946
1947 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001948 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001949 * been in the running state while the device was running on battery.
1950 *
1951 * {@hide}
1952 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001953 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001954
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001955 public static final int WIFI_STATE_OFF = 0;
1956 public static final int WIFI_STATE_OFF_SCANNING = 1;
1957 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1958 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1959 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1960 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1961 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1962 public static final int WIFI_STATE_SOFT_AP = 7;
1963
1964 static final String[] WIFI_STATE_NAMES = {
1965 "off", "scanning", "no_net", "disconn",
1966 "sta", "p2p", "sta_p2p", "soft_ap"
1967 };
1968
1969 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1970
1971 /**
1972 * Returns the time in microseconds that WiFi has been running in the given state.
1973 *
1974 * {@hide}
1975 */
1976 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001977 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001978
1979 /**
1980 * Returns the number of times that WiFi has entered the given state.
1981 *
1982 * {@hide}
1983 */
1984 public abstract int getWifiStateCount(int wifiState, int which);
1985
The Android Open Source Project10592532009-03-18 17:39:46 -07001986 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001987 * Returns the time in microseconds that the wifi supplicant has been
1988 * in a given state.
1989 *
1990 * {@hide}
1991 */
1992 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1993
1994 /**
1995 * Returns the number of times that the wifi supplicant has transitioned
1996 * to a given state.
1997 *
1998 * {@hide}
1999 */
2000 public abstract int getWifiSupplStateCount(int state, int which);
2001
2002 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2003
2004 /**
2005 * Returns the time in microseconds that WIFI has been running with
2006 * the given signal strength.
2007 *
2008 * {@hide}
2009 */
2010 public abstract long getWifiSignalStrengthTime(int strengthBin,
2011 long elapsedRealtimeUs, int which);
2012
2013 /**
2014 * Returns the number of times WIFI has entered the given signal strength.
2015 *
2016 * {@hide}
2017 */
2018 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2019
2020 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002021 * Returns the time in microseconds that the flashlight has been on while the device was
2022 * running on battery.
2023 *
2024 * {@hide}
2025 */
2026 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2027
2028 /**
2029 * Returns the number of times that the flashlight has been turned on while the device was
2030 * running on battery.
2031 *
2032 * {@hide}
2033 */
2034 public abstract long getFlashlightOnCount(int which);
2035
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002036 /**
2037 * Returns the time in microseconds that the camera has been on while the device was
2038 * running on battery.
2039 *
2040 * {@hide}
2041 */
2042 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2043
2044
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002045 public static final int NETWORK_MOBILE_RX_DATA = 0;
2046 public static final int NETWORK_MOBILE_TX_DATA = 1;
2047 public static final int NETWORK_WIFI_RX_DATA = 2;
2048 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002049 public static final int NETWORK_BT_RX_DATA = 4;
2050 public static final int NETWORK_BT_TX_DATA = 5;
2051 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002052
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002053 public abstract long getNetworkActivityBytes(int type, int which);
2054 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002055
Adam Lesinskie08af192015-03-25 16:42:59 -07002056 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002057 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002058 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002059 * actual power data.
2060 */
2061 public abstract boolean hasWifiActivityReporting();
2062
2063 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002064 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2065 * in various radio controller states, such as transmit, receive, and idle.
2066 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002067 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002068 public abstract ControllerActivityCounter getWifiControllerActivity();
2069
2070 /**
2071 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2072 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2073 * actual power data.
2074 */
2075 public abstract boolean hasBluetoothActivityReporting();
2076
2077 /**
2078 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2079 * in various radio controller states, such as transmit, receive, and idle.
2080 * @return non-null {@link ControllerActivityCounter}
2081 */
2082 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2083
2084 /**
2085 * Returns true if the BatteryStats object has detailed modem power reports.
2086 * When true, calling {@link #getModemControllerActivity()} will yield the
2087 * actual power data.
2088 */
2089 public abstract boolean hasModemActivityReporting();
2090
2091 /**
2092 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2093 * in various radio controller states, such as transmit, receive, and idle.
2094 * @return non-null {@link ControllerActivityCounter}
2095 */
2096 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002097
The Android Open Source Project10592532009-03-18 17:39:46 -07002098 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002099 * Return the wall clock time when battery stats data collection started.
2100 */
2101 public abstract long getStartClockTime();
2102
2103 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002104 * Return platform version tag that we were running in when the battery stats started.
2105 */
2106 public abstract String getStartPlatformVersion();
2107
2108 /**
2109 * Return platform version tag that we were running in when the battery stats ended.
2110 */
2111 public abstract String getEndPlatformVersion();
2112
2113 /**
2114 * Return the internal version code of the parcelled format.
2115 */
2116 public abstract int getParcelVersion();
2117
2118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 * Return whether we are currently running on battery.
2120 */
2121 public abstract boolean getIsOnBattery();
2122
2123 /**
2124 * Returns a SparseArray containing the statistics for each uid.
2125 */
2126 public abstract SparseArray<? extends Uid> getUidStats();
2127
2128 /**
2129 * Returns the current battery uptime in microseconds.
2130 *
2131 * @param curTime the amount of elapsed realtime in microseconds.
2132 */
2133 public abstract long getBatteryUptime(long curTime);
2134
2135 /**
2136 * Returns the current battery realtime in microseconds.
2137 *
2138 * @param curTime the amount of elapsed realtime in microseconds.
2139 */
2140 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002141
2142 /**
Evan Millar633a1742009-04-02 16:36:33 -07002143 * Returns the battery percentage level at the last time the device was unplugged from power, or
2144 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002145 */
Evan Millar633a1742009-04-02 16:36:33 -07002146 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002147
2148 /**
Evan Millar633a1742009-04-02 16:36:33 -07002149 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2150 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002151 */
Evan Millar633a1742009-04-02 16:36:33 -07002152 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
2154 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002155 * Get the amount the battery has discharged since the stats were
2156 * last reset after charging, as a lower-end approximation.
2157 */
2158 public abstract int getLowDischargeAmountSinceCharge();
2159
2160 /**
2161 * Get the amount the battery has discharged since the stats were
2162 * last reset after charging, as an upper-end approximation.
2163 */
2164 public abstract int getHighDischargeAmountSinceCharge();
2165
2166 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002167 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2168 */
2169 public abstract int getDischargeAmount(int which);
2170
2171 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002172 * Get the amount the battery has discharged while the screen was on,
2173 * since the last time power was unplugged.
2174 */
2175 public abstract int getDischargeAmountScreenOn();
2176
2177 /**
2178 * Get the amount the battery has discharged while the screen was on,
2179 * since the last time the device was charged.
2180 */
2181 public abstract int getDischargeAmountScreenOnSinceCharge();
2182
2183 /**
2184 * Get the amount the battery has discharged while the screen was off,
2185 * since the last time power was unplugged.
2186 */
2187 public abstract int getDischargeAmountScreenOff();
2188
2189 /**
2190 * Get the amount the battery has discharged while the screen was off,
2191 * since the last time the device was charged.
2192 */
2193 public abstract int getDischargeAmountScreenOffSinceCharge();
2194
2195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 * Returns the total, last, or current battery uptime in microseconds.
2197 *
2198 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002199 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 */
2201 public abstract long computeBatteryUptime(long curTime, int which);
2202
2203 /**
2204 * Returns the total, last, or current battery realtime in microseconds.
2205 *
2206 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002207 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 */
2209 public abstract long computeBatteryRealtime(long curTime, int which);
2210
2211 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002212 * Returns the total, last, or current battery screen off uptime in microseconds.
2213 *
2214 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002215 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002216 */
2217 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2218
2219 /**
2220 * Returns the total, last, or current battery screen off realtime in microseconds.
2221 *
2222 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002223 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002224 */
2225 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2226
2227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 * Returns the total, last, or current uptime in microseconds.
2229 *
2230 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002231 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 */
2233 public abstract long computeUptime(long curTime, int which);
2234
2235 /**
2236 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002239 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 */
2241 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002242
2243 /**
2244 * Compute an approximation for how much run time (in microseconds) is remaining on
2245 * the battery. Returns -1 if no time can be computed: either there is not
2246 * enough current data to make a decision, or the battery is currently
2247 * charging.
2248 *
2249 * @param curTime The current elepsed realtime in microseconds.
2250 */
2251 public abstract long computeBatteryTimeRemaining(long curTime);
2252
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002253 // The part of a step duration that is the actual time.
2254 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2255
2256 // Bits in a step duration that are the new battery level we are at.
2257 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002258 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002259
2260 // Bits in a step duration that are the initial mode we were in at that step.
2261 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002262 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002263
2264 // Bits in a step duration that indicate which modes changed during that step.
2265 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002266 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002267
2268 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2269 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2270
2271 // Step duration mode: power save is on.
2272 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2273
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002274 // Step duration mode: device is currently in idle mode.
2275 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2276
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002277 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2278 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002279 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2280 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002281 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2282 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2283 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2284 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2285 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002286 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2287 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002288 };
2289 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2290 (Display.STATE_OFF-1),
2291 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002292 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002293 (Display.STATE_ON-1),
2294 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2295 (Display.STATE_DOZE-1),
2296 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2297 (Display.STATE_DOZE_SUSPEND-1),
2298 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002299 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002300 };
2301 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2302 "screen off",
2303 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002304 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002305 "screen on",
2306 "screen on power save",
2307 "screen doze",
2308 "screen doze power save",
2309 "screen doze-suspend",
2310 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002311 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002312 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002313
2314 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002315 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002316 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002317 public abstract LevelStepTracker getDischargeLevelStepTracker();
2318
2319 /**
2320 * Return the array of daily discharge step durations.
2321 */
2322 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002323
2324 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002325 * Compute an approximation for how much time (in microseconds) remains until the battery
2326 * is fully charged. Returns -1 if no time can be computed: either there is not
2327 * enough current data to make a decision, or the battery is currently
2328 * discharging.
2329 *
2330 * @param curTime The current elepsed realtime in microseconds.
2331 */
2332 public abstract long computeChargeTimeRemaining(long curTime);
2333
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002334 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002335 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002336 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002337 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002338
2339 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002340 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002341 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002342 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002343
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002344 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2345
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002346 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002347
Evan Millarc64edde2009-04-18 12:26:32 -07002348 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002350 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2351
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002352 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 long days = seconds / (60 * 60 * 24);
2354 if (days != 0) {
2355 out.append(days);
2356 out.append("d ");
2357 }
2358 long used = days * 60 * 60 * 24;
2359
2360 long hours = (seconds - used) / (60 * 60);
2361 if (hours != 0 || used != 0) {
2362 out.append(hours);
2363 out.append("h ");
2364 }
2365 used += hours * 60 * 60;
2366
2367 long mins = (seconds-used) / 60;
2368 if (mins != 0 || used != 0) {
2369 out.append(mins);
2370 out.append("m ");
2371 }
2372 used += mins * 60;
2373
2374 if (seconds != 0 || used != 0) {
2375 out.append(seconds-used);
2376 out.append("s ");
2377 }
2378 }
2379
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002380 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002382 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 sb.append(time - (sec * 1000));
2384 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002387 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002388 long sec = time / 1000;
2389 formatTimeRaw(sb, sec);
2390 sb.append(time - (sec * 1000));
2391 sb.append("ms");
2392 }
2393
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002394 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002396 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 }
2398 float perc = ((float)num) / ((float)den) * 100;
2399 mFormatBuilder.setLength(0);
2400 mFormatter.format("%.1f%%", perc);
2401 return mFormatBuilder.toString();
2402 }
2403
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002404 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002405 mFormatBuilder.setLength(0);
2406
2407 if (bytes < BYTES_PER_KB) {
2408 return bytes + "B";
2409 } else if (bytes < BYTES_PER_MB) {
2410 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2411 return mFormatBuilder.toString();
2412 } else if (bytes < BYTES_PER_GB){
2413 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2414 return mFormatBuilder.toString();
2415 } else {
2416 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2417 return mFormatBuilder.toString();
2418 }
2419 }
2420
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002421 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002422 if (timer != null) {
2423 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002424 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002425 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2426 return totalTimeMillis;
2427 }
2428 return 0;
2429 }
2430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 /**
2432 *
2433 * @param sb a StringBuilder object.
2434 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002435 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002437 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 * @param linePrefix a String to be prepended to each line of output.
2439 * @return the line prefix
2440 */
2441 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002442 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002445 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002446
Evan Millarc64edde2009-04-18 12:26:32 -07002447 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 if (totalTimeMillis != 0) {
2449 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002450 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002451 if (name != null) {
2452 sb.append(name);
2453 sb.append(' ');
2454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 sb.append('(');
2456 sb.append(count);
2457 sb.append(" times)");
2458 return ", ";
2459 }
2460 }
2461 return linePrefix;
2462 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002463
2464 /**
2465 *
2466 * @param pw a PrintWriter object to print to.
2467 * @param sb a StringBuilder object.
2468 * @param timer a Timer object contining the wakelock times.
2469 * @param rawRealtime the current on-battery time in microseconds.
2470 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2471 * @param prefix a String to be prepended to each line of output.
2472 * @param type the name of the timer.
2473 */
2474 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2475 long rawRealtime, int which, String prefix, String type) {
2476 if (timer != null) {
2477 // Convert from microseconds to milliseconds with rounding
2478 final long totalTime = (timer.getTotalTimeLocked(
2479 rawRealtime, which) + 500) / 1000;
2480 final int count = timer.getCountLocked(which);
2481 if (totalTime != 0) {
2482 sb.setLength(0);
2483 sb.append(prefix);
2484 sb.append(" ");
2485 sb.append(type);
2486 sb.append(": ");
2487 formatTimeMs(sb, totalTime);
2488 sb.append("realtime (");
2489 sb.append(count);
2490 sb.append(" times)");
2491 pw.println(sb.toString());
2492 return true;
2493 }
2494 }
2495 return false;
2496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497
2498 /**
2499 * Checkin version of wakelock printer. Prints simple comma-separated list.
2500 *
2501 * @param sb a StringBuilder object.
2502 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002503 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002505 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 * @param linePrefix a String to be prepended to each line of output.
2507 * @return the line prefix
2508 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002509 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2510 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 long totalTimeMicros = 0;
2512 int count = 0;
2513 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002514 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002515 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 }
2517 sb.append(linePrefix);
2518 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2519 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002520 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 sb.append(count);
2522 return ",";
2523 }
2524
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002525 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2526 String type) {
2527 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2528 pw.print(',');
2529 pw.print(uid);
2530 pw.print(',');
2531 pw.print(category);
2532 pw.print(',');
2533 pw.print(type);
2534 }
2535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 /**
2537 * Dump a comma-separated line of values for terse checkin mode.
2538 *
2539 * @param pw the PageWriter to dump log to
2540 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2541 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2542 * @param args type-dependent data arguments
2543 */
2544 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2545 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002546 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002547 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002548 pw.print(',');
2549 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002551 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002553
2554 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002555 * Dump a given timer stat for terse checkin mode.
2556 *
2557 * @param pw the PageWriter to dump log to
2558 * @param uid the UID to log
2559 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2560 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2561 * @param timer a {@link Timer} to dump stats for
2562 * @param rawRealtime the current elapsed realtime of the system in microseconds
2563 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2564 */
2565 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2566 Timer timer, long rawRealtime, int which) {
2567 if (timer != null) {
2568 // Convert from microseconds to milliseconds with rounding
2569 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2570 / 1000;
2571 final int count = timer.getCountLocked(which);
2572 if (totalTime != 0) {
2573 dumpLine(pw, uid, category, type, totalTime, count);
2574 }
2575 }
2576 }
2577
2578 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002579 * Checks if the ControllerActivityCounter has any data worth dumping.
2580 */
2581 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2582 if (counter == null) {
2583 return false;
2584 }
2585
2586 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2587 || counter.getRxTimeCounter().getCountLocked(which) != 0
2588 || counter.getPowerCounter().getCountLocked(which) != 0) {
2589 return true;
2590 }
2591
2592 for (LongCounter c : counter.getTxTimeCounters()) {
2593 if (c.getCountLocked(which) != 0) {
2594 return true;
2595 }
2596 }
2597 return false;
2598 }
2599
2600 /**
2601 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2602 * The order of the arguments in the final check in line is:
2603 *
2604 * idle, rx, power, tx...
2605 *
2606 * where tx... is one or more transmit level times.
2607 */
2608 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2609 String type,
2610 ControllerActivityCounter counter,
2611 int which) {
2612 if (!controllerActivityHasData(counter, which)) {
2613 return;
2614 }
2615
2616 dumpLineHeader(pw, uid, category, type);
2617 pw.print(",");
2618 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2619 pw.print(",");
2620 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2621 pw.print(",");
2622 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2623 for (LongCounter c : counter.getTxTimeCounters()) {
2624 pw.print(",");
2625 pw.print(c.getCountLocked(which));
2626 }
2627 pw.println();
2628 }
2629
2630 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2631 String prefix, String controllerName,
2632 ControllerActivityCounter counter,
2633 int which) {
2634 if (controllerActivityHasData(counter, which)) {
2635 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2636 }
2637 }
2638
2639 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2640 String controllerName,
2641 ControllerActivityCounter counter, int which) {
2642 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2643 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2644 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2645 long totalTxTimeMs = 0;
2646 for (LongCounter txState : counter.getTxTimeCounters()) {
2647 totalTxTimeMs += txState.getCountLocked(which);
2648 }
2649
2650 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2651
2652 sb.setLength(0);
2653 sb.append(prefix);
2654 sb.append(" ");
2655 sb.append(controllerName);
2656 sb.append(" Idle time: ");
2657 formatTimeMs(sb, idleTimeMs);
2658 sb.append("(");
2659 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2660 sb.append(")");
2661 pw.println(sb.toString());
2662
2663 sb.setLength(0);
2664 sb.append(prefix);
2665 sb.append(" ");
2666 sb.append(controllerName);
2667 sb.append(" Rx time: ");
2668 formatTimeMs(sb, rxTimeMs);
2669 sb.append("(");
2670 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2671 sb.append(")");
2672 pw.println(sb.toString());
2673
2674 sb.setLength(0);
2675 sb.append(prefix);
2676 sb.append(" ");
2677 sb.append(controllerName);
2678 sb.append(" Tx time: ");
2679 formatTimeMs(sb, totalTxTimeMs);
2680 sb.append("(");
2681 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2682 sb.append(")");
2683 pw.println(sb.toString());
2684
2685 final int numTxLvls = counter.getTxTimeCounters().length;
2686 if (numTxLvls > 1) {
2687 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2688 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2689 sb.setLength(0);
2690 sb.append(prefix);
2691 sb.append(" [");
2692 sb.append(lvl);
2693 sb.append("] ");
2694 formatTimeMs(sb, txLvlTimeMs);
2695 sb.append("(");
2696 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2697 sb.append(")");
2698 pw.println(sb.toString());
2699 }
2700 }
2701
2702 sb.setLength(0);
2703 sb.append(prefix);
2704 sb.append(" ");
2705 sb.append(controllerName);
2706 sb.append(" Power drain: ").append(
2707 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2708 sb.append("mAh");
2709 pw.println(sb.toString());
2710 }
2711
2712 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002713 * Temporary for settings.
2714 */
2715 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2716 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2717 }
2718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 /**
2720 * Checkin server version of dump to produce more compact, computer-readable log.
2721 *
2722 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002724 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2725 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2727 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2728 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2730 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002731 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2732 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2733 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 final long totalRealtime = computeRealtime(rawRealtime, which);
2735 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002736 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002737 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002738 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002739 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2740 rawRealtime, which);
2741 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
2742 rawRealtime, which);
2743 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2744 rawRealtime, which);
2745 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
2746 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002747 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002748 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002749
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002750 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002752 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002753 final int NU = uidStats.size();
2754
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002755 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 // Dump "battery" stat
2758 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002759 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002760 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002761 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002762 getStartClockTime(),
2763 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002765 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002766 long fullWakeLockTimeTotal = 0;
2767 long partialWakeLockTimeTotal = 0;
2768
2769 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002770 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002771
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002772 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2773 = u.getWakelockStats();
2774 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2775 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002776
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002777 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2778 if (fullWakeTimer != null) {
2779 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2780 which);
2781 }
2782
2783 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2784 if (partialWakeTimer != null) {
2785 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2786 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002787 }
2788 }
2789 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002790
2791 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002792 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2793 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2794 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2795 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2796 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2797 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2798 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2799 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002800 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2801 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
2802 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
2803
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002804 // Dump Modem controller stats
2805 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
2806 getModemControllerActivity(), which);
2807
Adam Lesinskie283d332015-04-16 12:29:25 -07002808 // Dump Wifi controller stats
2809 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2810 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002811 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
2812 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0, 0);
2813
2814 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
2815 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002816
2817 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002818 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
2819 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 // Dump misc stats
2822 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002823 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002824 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002825 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002826 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002827 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
2828 getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which), deviceIdlingTime / 1000,
2829 getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07002830 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002831 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
2832 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
2833 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
2834 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
2835 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002836
2837 // Dump screen brightness stats
2838 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2839 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002840 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002841 }
2842 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002843
Dianne Hackborn627bba72009-03-24 22:32:56 -07002844 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002845 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2846 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002847 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002848 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002849 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002850 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002851 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002852 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002853 args[i] = getPhoneSignalStrengthCount(i, which);
2854 }
2855 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002856
Dianne Hackborn627bba72009-03-24 22:32:56 -07002857 // Dump network type stats
2858 args = new Object[NUM_DATA_CONNECTION_TYPES];
2859 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002860 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002861 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002862 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2863 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2864 args[i] = getPhoneDataConnectionCount(i, which);
2865 }
2866 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002867
2868 // Dump wifi state stats
2869 args = new Object[NUM_WIFI_STATES];
2870 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002871 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002872 }
2873 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2874 for (int i=0; i<NUM_WIFI_STATES; i++) {
2875 args[i] = getWifiStateCount(i, which);
2876 }
2877 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2878
Dianne Hackborn3251b902014-06-20 14:40:53 -07002879 // Dump wifi suppl state stats
2880 args = new Object[NUM_WIFI_SUPPL_STATES];
2881 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2882 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2883 }
2884 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2885 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2886 args[i] = getWifiSupplStateCount(i, which);
2887 }
2888 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2889
2890 // Dump wifi signal strength stats
2891 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2892 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2893 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2894 }
2895 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2896 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2897 args[i] = getWifiSignalStrengthCount(i, which);
2898 }
2899 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2900
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002901 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002902 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002903 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002906 if (which == STATS_SINCE_UNPLUGGED) {
2907 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2908 getDischargeStartLevel()-getDischargeCurrentLevel(),
2909 getDischargeStartLevel()-getDischargeCurrentLevel(),
2910 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2911 } else {
2912 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2913 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002914 getDischargeAmountScreenOnSinceCharge(),
2915 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002916 }
2917
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002918 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002919 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002920 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002921 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002922 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002923 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002924 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002925 sb.toString());
2926 }
Evan Millarc64edde2009-04-18 12:26:32 -07002927 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002928 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002929 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002930 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2931 // Not doing the regular wake lock formatting to remain compatible
2932 // with the old checkin format.
2933 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2934 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002935 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002936 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002937 }
2938 }
Evan Millarc64edde2009-04-18 12:26:32 -07002939 }
2940
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002941 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002942 helper.create(this);
2943 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002944 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002945 if (sippers != null && sippers.size() > 0) {
2946 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2947 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002948 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002949 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2950 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2951 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002952 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002953 int uid = 0;
2954 String label;
2955 switch (bs.drainType) {
2956 case IDLE:
2957 label="idle";
2958 break;
2959 case CELL:
2960 label="cell";
2961 break;
2962 case PHONE:
2963 label="phone";
2964 break;
2965 case WIFI:
2966 label="wifi";
2967 break;
2968 case BLUETOOTH:
2969 label="blue";
2970 break;
2971 case SCREEN:
2972 label="scrn";
2973 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002974 case FLASHLIGHT:
2975 label="flashlight";
2976 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002977 case APP:
2978 uid = bs.uidObj.getUid();
2979 label = "uid";
2980 break;
2981 case USER:
2982 uid = UserHandle.getUid(bs.userId, 0);
2983 label = "user";
2984 break;
2985 case UNACCOUNTED:
2986 label = "unacc";
2987 break;
2988 case OVERCOUNTED:
2989 label = "over";
2990 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002991 case CAMERA:
2992 label = "camera";
2993 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002994 default:
2995 label = "???";
2996 }
2997 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07002998 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002999 }
3000 }
3001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 for (int iu = 0; iu < NU; iu++) {
3003 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003004 if (reqUid >= 0 && uid != reqUid) {
3005 continue;
3006 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003007 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003010 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3011 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3012 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3013 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3014 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3015 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3016 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3017 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
3018 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3019 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003020 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3021 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003022 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003023 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3024 wifiBytesRx, wifiBytesTx,
3025 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003026 wifiPacketsRx, wifiPacketsTx,
3027 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003028 }
3029
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003030 // Dump modem controller data, per UID.
3031 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3032 u.getModemControllerActivity(), which);
3033
3034 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003035 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3036 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3037 final int wifiScanCount = u.getWifiScanCount(which);
3038 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003039 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003040 || uidWifiRunningTime != 0) {
3041 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3042 uidWifiRunningTime, wifiScanCount,
3043 /* legacy fields follow, keep at 0 */ 0, 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003046 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3047 u.getWifiControllerActivity(), which);
3048
Dianne Hackborn617f8772009-03-31 15:04:46 -07003049 if (u.hasUserActivity()) {
3050 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3051 boolean hasData = false;
3052 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3053 int val = u.getUserActivityCount(i, which);
3054 args[i] = val;
3055 if (val != 0) hasData = true;
3056 }
3057 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003058 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003059 }
3060 }
3061
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003062 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3063 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3064 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3065 String linePrefix = "";
3066 sb.setLength(0);
3067 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3068 rawRealtime, "f", which, linePrefix);
3069 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3070 rawRealtime, "p", which, linePrefix);
3071 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3072 rawRealtime, "w", which, linePrefix);
3073
3074 // Only log if we had at lease one wakelock...
3075 if (sb.length() > 0) {
3076 String name = wakelocks.keyAt(iw);
3077 if (name.indexOf(',') >= 0) {
3078 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003080 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
3082 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003083
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003084 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3085 for (int isy=syncs.size()-1; isy>=0; isy--) {
3086 final Timer timer = syncs.valueAt(isy);
3087 // Convert from microseconds to milliseconds with rounding
3088 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3089 final int count = timer.getCountLocked(which);
3090 if (totalTime != 0) {
3091 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003092 }
3093 }
3094
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003095 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3096 for (int ij=jobs.size()-1; ij>=0; ij--) {
3097 final Timer timer = jobs.valueAt(ij);
3098 // Convert from microseconds to milliseconds with rounding
3099 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3100 final int count = timer.getCountLocked(which);
3101 if (totalTime != 0) {
3102 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003103 }
3104 }
3105
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003106 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3107 rawRealtime, which);
3108 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3109 rawRealtime, which);
3110 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3111 rawRealtime, which);
3112 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3113 rawRealtime, which);
3114
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003115 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3116 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003117 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003118 final Uid.Sensor se = sensors.valueAt(ise);
3119 final int sensorNumber = sensors.keyAt(ise);
3120 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003121 if (timer != null) {
3122 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003123 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3124 / 1000;
3125 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003126 if (totalTime != 0) {
3127 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
3128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 }
3130 }
3131
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003132 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3133 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003134
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003135 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3136 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003137
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003138 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003139 long totalStateTime = 0;
3140 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003141 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3142 totalStateTime += time;
3143 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003144 }
3145 if (totalStateTime > 0) {
3146 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3147 }
3148
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003149 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3150 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3151 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3152 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3153 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3154 powerCpuMaUs / 1000);
3155 }
3156
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003157 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3158 = u.getProcessStats();
3159 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3160 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003161
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003162 final long userMillis = ps.getUserTime(which);
3163 final long systemMillis = ps.getSystemTime(which);
3164 final long foregroundMillis = ps.getForegroundTime(which);
3165 final int starts = ps.getStarts(which);
3166 final int numCrashes = ps.getNumCrashes(which);
3167 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003168
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003169 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3170 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3171 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3172 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 }
3174 }
3175
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003176 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3177 = u.getPackageStats();
3178 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3179 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3180 int wakeups = 0;
3181 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3182 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3183 wakeups += alarms.valueAt(iwa).getCountLocked(which);
3184 }
3185 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3186 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3187 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3188 final long startTime = ss.getStartTime(batteryUptime, which);
3189 final int starts = ss.getStarts(which);
3190 final int launches = ss.getLaunches(which);
3191 if (startTime != 0 || starts != 0 || launches != 0) {
3192 dumpLine(pw, uid, category, APK_DATA,
3193 wakeups, // wakeup alarms
3194 packageStats.keyAt(ipkg), // Apk
3195 serviceStats.keyAt(isvc), // service
3196 startTime / 1000, // time spent started, in ms
3197 starts,
3198 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 }
3201 }
3202 }
3203 }
3204
Dianne Hackborn81038902012-11-26 17:04:09 -08003205 static final class TimerEntry {
3206 final String mName;
3207 final int mId;
3208 final BatteryStats.Timer mTimer;
3209 final long mTime;
3210 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3211 mName = name;
3212 mId = id;
3213 mTimer = timer;
3214 mTime = time;
3215 }
3216 }
3217
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003218 private void printmAh(PrintWriter printer, double power) {
3219 printer.print(BatteryStatsHelper.makemAh(power));
3220 }
3221
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003222 private void printmAh(StringBuilder sb, double power) {
3223 sb.append(BatteryStatsHelper.makemAh(power));
3224 }
3225
Dianne Hackbornd953c532014-08-16 18:17:38 -07003226 /**
3227 * Temporary for settings.
3228 */
3229 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3230 int reqUid) {
3231 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3232 }
3233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003235 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003236 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3238 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3239 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240
3241 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3242 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3243 final long totalRealtime = computeRealtime(rawRealtime, which);
3244 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003245 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3246 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3247 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003248 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3249 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003250
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003251 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003252
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003253 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003254 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003256 sb.setLength(0);
3257 sb.append(prefix);
3258 sb.append(" Time on battery: ");
3259 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3260 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3261 sb.append(") realtime, ");
3262 formatTimeMs(sb, whichBatteryUptime / 1000);
3263 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3264 sb.append(") uptime");
3265 pw.println(sb.toString());
3266 sb.setLength(0);
3267 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003268 sb.append(" Time on battery screen off: ");
3269 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3270 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3271 sb.append(") realtime, ");
3272 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3273 sb.append("(");
3274 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3275 sb.append(") uptime");
3276 pw.println(sb.toString());
3277 sb.setLength(0);
3278 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003279 sb.append(" Total run time: ");
3280 formatTimeMs(sb, totalRealtime / 1000);
3281 sb.append("realtime, ");
3282 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003283 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003284 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003285 if (batteryTimeRemaining >= 0) {
3286 sb.setLength(0);
3287 sb.append(prefix);
3288 sb.append(" Battery time remaining: ");
3289 formatTimeMs(sb, batteryTimeRemaining / 1000);
3290 pw.println(sb.toString());
3291 }
3292 if (chargeTimeRemaining >= 0) {
3293 sb.setLength(0);
3294 sb.append(prefix);
3295 sb.append(" Charge time remaining: ");
3296 formatTimeMs(sb, chargeTimeRemaining / 1000);
3297 pw.println(sb.toString());
3298 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003299 pw.print(" Start clock time: ");
3300 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3301
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003302 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003303 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003304 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003305 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3306 rawRealtime, which);
3307 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
3308 rawRealtime, which);
3309 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3310 rawRealtime, which);
3311 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
3312 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003313 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3314 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3315 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003316 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003317 sb.append(prefix);
3318 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3319 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003320 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003321 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3322 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003323 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003324 pw.println(sb.toString());
3325 sb.setLength(0);
3326 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003327 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003328 boolean didOne = false;
3329 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003330 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003331 if (time == 0) {
3332 continue;
3333 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003334 sb.append("\n ");
3335 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003336 didOne = true;
3337 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3338 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003339 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003340 sb.append("(");
3341 sb.append(formatRatioLocked(time, screenOnTime));
3342 sb.append(")");
3343 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003344 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003345 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003346 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003347 sb.setLength(0);
3348 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003349 sb.append(" Power save mode enabled: ");
3350 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003351 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003352 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003353 sb.append(")");
3354 pw.println(sb.toString());
3355 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003356 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003357 sb.setLength(0);
3358 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003359 sb.append(" Device light idling: ");
3360 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003361 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003362 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3363 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003364 sb.append("x");
3365 pw.println(sb.toString());
3366 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003367 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003368 sb.setLength(0);
3369 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003370 sb.append(" Idle mode light time: ");
3371 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003372 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003373 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3374 sb.append(") ");
3375 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003376 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003377 sb.append(" -- longest ");
3378 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3379 pw.println(sb.toString());
3380 }
3381 if (deviceIdlingTime != 0) {
3382 sb.setLength(0);
3383 sb.append(prefix);
3384 sb.append(" Device full idling: ");
3385 formatTimeMs(sb, deviceIdlingTime / 1000);
3386 sb.append("(");
3387 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3388 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which));
3389 sb.append("x");
3390 pw.println(sb.toString());
3391 }
3392 if (deviceIdleModeFullTime != 0) {
3393 sb.setLength(0);
3394 sb.append(prefix);
3395 sb.append(" Idle mode full time: ");
3396 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3397 sb.append("(");
3398 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3399 sb.append(") ");
3400 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which));
3401 sb.append("x");
3402 sb.append(" -- longest ");
3403 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003404 pw.println(sb.toString());
3405 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003406 if (phoneOnTime != 0) {
3407 sb.setLength(0);
3408 sb.append(prefix);
3409 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3410 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003411 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003412 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003413 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003414 if (connChanges != 0) {
3415 pw.print(prefix);
3416 pw.print(" Connectivity changes: "); pw.println(connChanges);
3417 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003418
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003419 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003420 long fullWakeLockTimeTotalMicros = 0;
3421 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003422
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003423 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003424
Evan Millar22ac0432009-03-31 11:33:18 -07003425 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003426 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003427
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003428 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3429 = u.getWakelockStats();
3430 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3431 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003432
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003433 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3434 if (fullWakeTimer != null) {
3435 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3436 rawRealtime, which);
3437 }
3438
3439 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3440 if (partialWakeTimer != null) {
3441 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3442 rawRealtime, which);
3443 if (totalTimeMicros > 0) {
3444 if (reqUid < 0) {
3445 // Only show the ordered list of all wake
3446 // locks if the caller is not asking for data
3447 // about a specific uid.
3448 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3449 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003450 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003451 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003452 }
3453 }
3454 }
3455 }
3456
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003457 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3458 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3459 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3460 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3461 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3462 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3463 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3464 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003465 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3466 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003467
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003468 if (fullWakeLockTimeTotalMicros != 0) {
3469 sb.setLength(0);
3470 sb.append(prefix);
3471 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3472 (fullWakeLockTimeTotalMicros + 500) / 1000);
3473 pw.println(sb.toString());
3474 }
3475
3476 if (partialWakeLockTimeTotalMicros != 0) {
3477 sb.setLength(0);
3478 sb.append(prefix);
3479 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3480 (partialWakeLockTimeTotalMicros + 500) / 1000);
3481 pw.println(sb.toString());
3482 }
3483
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003484 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003485 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3486 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3487 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3488 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003489 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003490 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003491 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003492 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003493 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003494 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003495 if (time == 0) {
3496 continue;
3497 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003498 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003499 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003500 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003501 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003502 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003503 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003504 sb.append("(");
3505 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003506 sb.append(") ");
3507 sb.append(getPhoneSignalStrengthCount(i, which));
3508 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003509 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003510 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003511 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003512
3513 sb.setLength(0);
3514 sb.append(prefix);
3515 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003516 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003517 pw.println(sb.toString());
3518
Dianne Hackborn627bba72009-03-24 22:32:56 -07003519 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003520 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003521 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003522 didOne = false;
3523 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003524 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003525 if (time == 0) {
3526 continue;
3527 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003528 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003529 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003530 didOne = true;
3531 sb.append(DATA_CONNECTION_NAMES[i]);
3532 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003533 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003534 sb.append("(");
3535 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003536 sb.append(") ");
3537 sb.append(getPhoneDataConnectionCount(i, which));
3538 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003539 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003540 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003541 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003542
3543 sb.setLength(0);
3544 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003545 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003546 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003547 formatTimeMs(sb, mobileActiveTime / 1000);
3548 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3549 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3550 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003551 pw.println(sb.toString());
3552
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003553 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3554 if (mobileActiveUnknownTime != 0) {
3555 sb.setLength(0);
3556 sb.append(prefix);
3557 sb.append(" Mobile radio active unknown time: ");
3558 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3559 sb.append("(");
3560 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3561 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3562 sb.append("x");
3563 pw.println(sb.toString());
3564 }
3565
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003566 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3567 if (mobileActiveAdjustedTime != 0) {
3568 sb.setLength(0);
3569 sb.append(prefix);
3570 sb.append(" Mobile radio active adjusted time: ");
3571 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3572 sb.append("(");
3573 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3574 sb.append(")");
3575 pw.println(sb.toString());
3576 }
3577
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003578 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3579
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003580 pw.print(prefix);
3581 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3582 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3583 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3584 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003585 sb.setLength(0);
3586 sb.append(prefix);
3587 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3588 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3589 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3590 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003591 sb.append(")");
3592 pw.println(sb.toString());
3593
3594 sb.setLength(0);
3595 sb.append(prefix);
3596 sb.append(" Wifi states:");
3597 didOne = false;
3598 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003599 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003600 if (time == 0) {
3601 continue;
3602 }
3603 sb.append("\n ");
3604 didOne = true;
3605 sb.append(WIFI_STATE_NAMES[i]);
3606 sb.append(" ");
3607 formatTimeMs(sb, time/1000);
3608 sb.append("(");
3609 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3610 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003611 sb.append(getWifiStateCount(i, which));
3612 sb.append("x");
3613 }
3614 if (!didOne) sb.append(" (no activity)");
3615 pw.println(sb.toString());
3616
3617 sb.setLength(0);
3618 sb.append(prefix);
3619 sb.append(" Wifi supplicant states:");
3620 didOne = false;
3621 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3622 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3623 if (time == 0) {
3624 continue;
3625 }
3626 sb.append("\n ");
3627 didOne = true;
3628 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3629 sb.append(" ");
3630 formatTimeMs(sb, time/1000);
3631 sb.append("(");
3632 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3633 sb.append(") ");
3634 sb.append(getWifiSupplStateCount(i, which));
3635 sb.append("x");
3636 }
3637 if (!didOne) sb.append(" (no activity)");
3638 pw.println(sb.toString());
3639
3640 sb.setLength(0);
3641 sb.append(prefix);
3642 sb.append(" Wifi signal levels:");
3643 didOne = false;
3644 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3645 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3646 if (time == 0) {
3647 continue;
3648 }
3649 sb.append("\n ");
3650 sb.append(prefix);
3651 didOne = true;
3652 sb.append("level(");
3653 sb.append(i);
3654 sb.append(") ");
3655 formatTimeMs(sb, time/1000);
3656 sb.append("(");
3657 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3658 sb.append(") ");
3659 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003660 sb.append("x");
3661 }
3662 if (!didOne) sb.append(" (no activity)");
3663 pw.println(sb.toString());
3664
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003665 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003666
Adam Lesinski50e47602015-12-04 17:04:54 -08003667 pw.print(prefix);
3668 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3669 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3670
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003671 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3672 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003673
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003674 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003675
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003676 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003677 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003678 pw.print(prefix); pw.println(" Device is currently unplugged");
3679 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3680 pw.println(getDischargeStartLevel());
3681 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3682 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003683 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003684 pw.print(prefix); pw.println(" Device is currently plugged into power");
3685 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3686 pw.println(getDischargeStartLevel());
3687 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3688 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003689 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003690 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3691 pw.println(getDischargeAmountScreenOn());
3692 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3693 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003694 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003695 } else {
3696 pw.print(prefix); pw.println(" Device battery use since last full charge");
3697 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3698 pw.println(getLowDischargeAmountSinceCharge());
3699 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3700 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003701 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3702 pw.println(getDischargeAmountScreenOnSinceCharge());
3703 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3704 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003705 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003706 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003707
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003708 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003709 helper.create(this);
3710 helper.refreshStats(which, UserHandle.USER_ALL);
3711 List<BatterySipper> sippers = helper.getUsageList();
3712 if (sippers != null && sippers.size() > 0) {
3713 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3714 pw.print(prefix); pw.print(" Capacity: ");
3715 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003716 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003717 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3718 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3719 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3720 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003721 pw.println();
3722 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003723 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003724 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003725 switch (bs.drainType) {
3726 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003727 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003728 break;
3729 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003730 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003731 break;
3732 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003733 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003734 break;
3735 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003736 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003737 break;
3738 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003739 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003740 break;
3741 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003742 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003743 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003744 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003745 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003746 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003747 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003748 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003749 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003750 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003751 break;
3752 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003753 pw.print(" User "); pw.print(bs.userId);
3754 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003755 break;
3756 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003757 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003758 break;
3759 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003760 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003761 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003762 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003763 pw.print(" Camera: ");
3764 break;
3765 default:
3766 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003767 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003768 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003769 printmAh(pw, bs.totalPowerMah);
3770
Adam Lesinski57123002015-06-12 16:12:07 -07003771 if (bs.usagePowerMah != bs.totalPowerMah) {
3772 // If the usage (generic power) isn't the whole amount, we list out
3773 // what components are involved in the calculation.
3774
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003775 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003776 if (bs.usagePowerMah != 0) {
3777 pw.print(" usage=");
3778 printmAh(pw, bs.usagePowerMah);
3779 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003780 if (bs.cpuPowerMah != 0) {
3781 pw.print(" cpu=");
3782 printmAh(pw, bs.cpuPowerMah);
3783 }
3784 if (bs.wakeLockPowerMah != 0) {
3785 pw.print(" wake=");
3786 printmAh(pw, bs.wakeLockPowerMah);
3787 }
3788 if (bs.mobileRadioPowerMah != 0) {
3789 pw.print(" radio=");
3790 printmAh(pw, bs.mobileRadioPowerMah);
3791 }
3792 if (bs.wifiPowerMah != 0) {
3793 pw.print(" wifi=");
3794 printmAh(pw, bs.wifiPowerMah);
3795 }
3796 if (bs.gpsPowerMah != 0) {
3797 pw.print(" gps=");
3798 printmAh(pw, bs.gpsPowerMah);
3799 }
3800 if (bs.sensorPowerMah != 0) {
3801 pw.print(" sensor=");
3802 printmAh(pw, bs.sensorPowerMah);
3803 }
3804 if (bs.cameraPowerMah != 0) {
3805 pw.print(" camera=");
3806 printmAh(pw, bs.cameraPowerMah);
3807 }
3808 if (bs.flashlightPowerMah != 0) {
3809 pw.print(" flash=");
3810 printmAh(pw, bs.flashlightPowerMah);
3811 }
3812 pw.print(" )");
3813 }
3814 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003815 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003816 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003817 }
3818
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003819 sippers = helper.getMobilemsppList();
3820 if (sippers != null && sippers.size() > 0) {
3821 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003822 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003823 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003824 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003825 sb.setLength(0);
3826 sb.append(prefix); sb.append(" Uid ");
3827 UserHandle.formatUid(sb, bs.uidObj.getUid());
3828 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3829 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3830 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003831 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003832 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003833 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003834 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003835 sb.setLength(0);
3836 sb.append(prefix);
3837 sb.append(" TOTAL TIME: ");
3838 formatTimeMs(sb, totalTime);
3839 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3840 sb.append(")");
3841 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003842 pw.println();
3843 }
3844
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003845 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3846 @Override
3847 public int compare(TimerEntry lhs, TimerEntry rhs) {
3848 long lhsTime = lhs.mTime;
3849 long rhsTime = rhs.mTime;
3850 if (lhsTime < rhsTime) {
3851 return 1;
3852 }
3853 if (lhsTime > rhsTime) {
3854 return -1;
3855 }
3856 return 0;
3857 }
3858 };
3859
3860 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003861 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3862 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003863 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003864 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3865 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3866 : kernelWakelocks.entrySet()) {
3867 final BatteryStats.Timer timer = ent.getValue();
3868 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003869 if (totalTimeMillis > 0) {
3870 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3871 }
3872 }
3873 if (ktimers.size() > 0) {
3874 Collections.sort(ktimers, timerComparator);
3875 pw.print(prefix); pw.println(" All kernel wake locks:");
3876 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003877 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003878 String linePrefix = ": ";
3879 sb.setLength(0);
3880 sb.append(prefix);
3881 sb.append(" Kernel Wake lock ");
3882 sb.append(timer.mName);
3883 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3884 which, linePrefix);
3885 if (!linePrefix.equals(": ")) {
3886 sb.append(" realtime");
3887 // Only print out wake locks that were held
3888 pw.println(sb.toString());
3889 }
3890 }
3891 pw.println();
3892 }
3893 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003894
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003895 if (timers.size() > 0) {
3896 Collections.sort(timers, timerComparator);
3897 pw.print(prefix); pw.println(" All partial wake locks:");
3898 for (int i=0; i<timers.size(); i++) {
3899 TimerEntry timer = timers.get(i);
3900 sb.setLength(0);
3901 sb.append(" Wake lock ");
3902 UserHandle.formatUid(sb, timer.mId);
3903 sb.append(" ");
3904 sb.append(timer.mName);
3905 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3906 sb.append(" realtime");
3907 pw.println(sb.toString());
3908 }
3909 timers.clear();
3910 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003911 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003912
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003913 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003914 if (wakeupReasons.size() > 0) {
3915 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003916 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003917 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003918 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003919 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3920 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003921 }
3922 Collections.sort(reasons, timerComparator);
3923 for (int i=0; i<reasons.size(); i++) {
3924 TimerEntry timer = reasons.get(i);
3925 String linePrefix = ": ";
3926 sb.setLength(0);
3927 sb.append(prefix);
3928 sb.append(" Wakeup reason ");
3929 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003930 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3931 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003932 pw.println(sb.toString());
3933 }
3934 pw.println();
3935 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003936 }
Evan Millar22ac0432009-03-31 11:33:18 -07003937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 for (int iu=0; iu<NU; iu++) {
3939 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003940 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003941 continue;
3942 }
3943
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003944 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003945
3946 pw.print(prefix);
3947 pw.print(" ");
3948 UserHandle.formatUid(pw, uid);
3949 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003951
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003952 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3953 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3954 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3955 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003956 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3957 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
3958
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003959 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3960 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003961 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3962 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003963
3964 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3965 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
3966
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003967 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3968 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3969 final int wifiScanCount = u.getWifiScanCount(which);
3970 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003971
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003972 if (mobileRxBytes > 0 || mobileTxBytes > 0
3973 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003974 pw.print(prefix); pw.print(" Mobile network: ");
3975 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003976 pw.print(formatBytesLocked(mobileTxBytes));
3977 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3978 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003980 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3981 sb.setLength(0);
3982 sb.append(prefix); sb.append(" Mobile radio active: ");
3983 formatTimeMs(sb, uidMobileActiveTime / 1000);
3984 sb.append("(");
3985 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3986 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3987 long packets = mobileRxPackets + mobileTxPackets;
3988 if (packets == 0) {
3989 packets = 1;
3990 }
3991 sb.append(" @ ");
3992 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3993 sb.append(" mspp");
3994 pw.println(sb.toString());
3995 }
3996
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003997 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
3998 u.getModemControllerActivity(), which);
3999
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004000 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004001 pw.print(prefix); pw.print(" Wi-Fi network: ");
4002 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004003 pw.print(formatBytesLocked(wifiTxBytes));
4004 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4005 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004006 }
4007
Dianne Hackborn62793e42015-03-09 11:15:41 -07004008 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004009 || uidWifiRunningTime != 0) {
4010 sb.setLength(0);
4011 sb.append(prefix); sb.append(" Wifi Running: ");
4012 formatTimeMs(sb, uidWifiRunningTime / 1000);
4013 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4014 whichBatteryRealtime)); sb.append(")\n");
4015 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4016 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4017 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4018 whichBatteryRealtime)); sb.append(")\n");
4019 sb.append(prefix); sb.append(" Wifi Scan: ");
4020 formatTimeMs(sb, wifiScanTime / 1000);
4021 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004022 whichBatteryRealtime)); sb.append(") ");
4023 sb.append(wifiScanCount);
4024 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004025 pw.println(sb.toString());
4026 }
4027
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004028 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4029 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004030
Adam Lesinski50e47602015-12-04 17:04:54 -08004031 if (btRxBytes > 0 || btTxBytes > 0) {
4032 pw.print(prefix); pw.print(" Bluetooth network: ");
4033 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4034 pw.print(formatBytesLocked(btTxBytes));
4035 pw.println(" sent");
4036 }
4037
Dianne Hackborn617f8772009-03-31 15:04:46 -07004038 if (u.hasUserActivity()) {
4039 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004040 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004041 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004042 if (val != 0) {
4043 if (!hasData) {
4044 sb.setLength(0);
4045 sb.append(" User activity: ");
4046 hasData = true;
4047 } else {
4048 sb.append(", ");
4049 }
4050 sb.append(val);
4051 sb.append(" ");
4052 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4053 }
4054 }
4055 if (hasData) {
4056 pw.println(sb.toString());
4057 }
4058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004060 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4061 = u.getWakelockStats();
4062 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004063 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004064 int countWakelock = 0;
4065 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4066 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4067 String linePrefix = ": ";
4068 sb.setLength(0);
4069 sb.append(prefix);
4070 sb.append(" Wake lock ");
4071 sb.append(wakelocks.keyAt(iw));
4072 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4073 "full", which, linePrefix);
4074 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4075 "partial", which, linePrefix);
4076 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4077 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004078 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4079 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004080 sb.append(" realtime");
4081 pw.println(sb.toString());
4082 uidActivity = true;
4083 countWakelock++;
4084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004085 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4086 rawRealtime, which);
4087 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4088 rawRealtime, which);
4089 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4090 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004091 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004092 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004093 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004094 if (countWakelock > 1) {
4095 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4096 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004097 sb.setLength(0);
4098 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004099 sb.append(" TOTAL wake: ");
4100 boolean needComma = false;
4101 if (totalFullWakelock != 0) {
4102 needComma = true;
4103 formatTimeMs(sb, totalFullWakelock);
4104 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004105 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004106 if (totalPartialWakelock != 0) {
4107 if (needComma) {
4108 sb.append(", ");
4109 }
4110 needComma = true;
4111 formatTimeMs(sb, totalPartialWakelock);
4112 sb.append("partial");
4113 }
4114 if (totalWindowWakelock != 0) {
4115 if (needComma) {
4116 sb.append(", ");
4117 }
4118 needComma = true;
4119 formatTimeMs(sb, totalWindowWakelock);
4120 sb.append("window");
4121 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004122 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004123 if (needComma) {
4124 sb.append(",");
4125 }
4126 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004127 formatTimeMs(sb, totalDrawWakelock);
4128 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004129 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004130 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004131 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004132 }
4133 }
4134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4136 for (int isy=syncs.size()-1; isy>=0; isy--) {
4137 final Timer timer = syncs.valueAt(isy);
4138 // Convert from microseconds to milliseconds with rounding
4139 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4140 final int count = timer.getCountLocked(which);
4141 sb.setLength(0);
4142 sb.append(prefix);
4143 sb.append(" Sync ");
4144 sb.append(syncs.keyAt(isy));
4145 sb.append(": ");
4146 if (totalTime != 0) {
4147 formatTimeMs(sb, totalTime);
4148 sb.append("realtime (");
4149 sb.append(count);
4150 sb.append(" times)");
4151 } else {
4152 sb.append("(not used)");
4153 }
4154 pw.println(sb.toString());
4155 uidActivity = true;
4156 }
4157
4158 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4159 for (int ij=jobs.size()-1; ij>=0; ij--) {
4160 final Timer timer = jobs.valueAt(ij);
4161 // Convert from microseconds to milliseconds with rounding
4162 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4163 final int count = timer.getCountLocked(which);
4164 sb.setLength(0);
4165 sb.append(prefix);
4166 sb.append(" Job ");
4167 sb.append(jobs.keyAt(ij));
4168 sb.append(": ");
4169 if (totalTime != 0) {
4170 formatTimeMs(sb, totalTime);
4171 sb.append("realtime (");
4172 sb.append(count);
4173 sb.append(" times)");
4174 } else {
4175 sb.append("(not used)");
4176 }
4177 pw.println(sb.toString());
4178 uidActivity = true;
4179 }
4180
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004181 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4182 prefix, "Flashlight");
4183 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4184 prefix, "Camera");
4185 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4186 prefix, "Video");
4187 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4188 prefix, "Audio");
4189
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004190 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4191 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004192 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004193 final Uid.Sensor se = sensors.valueAt(ise);
4194 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004195 sb.setLength(0);
4196 sb.append(prefix);
4197 sb.append(" Sensor ");
4198 int handle = se.getHandle();
4199 if (handle == Uid.Sensor.GPS) {
4200 sb.append("GPS");
4201 } else {
4202 sb.append(handle);
4203 }
4204 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004206 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004207 if (timer != null) {
4208 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004209 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004210 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004211 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004212 //timer.logState();
4213 if (totalTime != 0) {
4214 formatTimeMs(sb, totalTime);
4215 sb.append("realtime (");
4216 sb.append(count);
4217 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 } else {
4219 sb.append("(not used)");
4220 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004221 } else {
4222 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004224
4225 pw.println(sb.toString());
4226 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004229 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4230 "Vibrator");
4231 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4232 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004233
Dianne Hackborn61659e52014-07-09 16:13:01 -07004234 long totalStateTime = 0;
4235 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4236 long time = u.getProcessStateTime(ips, rawRealtime, which);
4237 if (time > 0) {
4238 totalStateTime += time;
4239 sb.setLength(0);
4240 sb.append(prefix);
4241 sb.append(" ");
4242 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4243 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004244 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004245 pw.println(sb.toString());
4246 uidActivity = true;
4247 }
4248 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004249 if (totalStateTime > 0) {
4250 sb.setLength(0);
4251 sb.append(prefix);
4252 sb.append(" Total running: ");
4253 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4254 pw.println(sb.toString());
4255 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004256
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004257 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4258 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004259 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4260 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004261 sb.setLength(0);
4262 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004263 sb.append(" Total cpu time: u=");
4264 formatTimeMs(sb, userCpuTimeUs / 1000);
4265 sb.append("s=");
4266 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004267 sb.append("p=");
4268 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4269 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004270 pw.println(sb.toString());
4271 }
4272
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004273 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4274 = u.getProcessStats();
4275 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4276 final Uid.Proc ps = processStats.valueAt(ipr);
4277 long userTime;
4278 long systemTime;
4279 long foregroundTime;
4280 int starts;
4281 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004283 userTime = ps.getUserTime(which);
4284 systemTime = ps.getSystemTime(which);
4285 foregroundTime = ps.getForegroundTime(which);
4286 starts = ps.getStarts(which);
4287 final int numCrashes = ps.getNumCrashes(which);
4288 final int numAnrs = ps.getNumAnrs(which);
4289 numExcessive = which == STATS_SINCE_CHARGED
4290 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004292 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4293 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4294 sb.setLength(0);
4295 sb.append(prefix); sb.append(" Proc ");
4296 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4297 sb.append(prefix); sb.append(" CPU: ");
4298 formatTimeMs(sb, userTime); sb.append("usr + ");
4299 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4300 formatTimeMs(sb, foregroundTime); sb.append("fg");
4301 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4302 sb.append("\n"); sb.append(prefix); sb.append(" ");
4303 boolean hasOne = false;
4304 if (starts != 0) {
4305 hasOne = true;
4306 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004307 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004308 if (numCrashes != 0) {
4309 if (hasOne) {
4310 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004311 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004312 hasOne = true;
4313 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004314 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004315 if (numAnrs != 0) {
4316 if (hasOne) {
4317 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004319 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 }
4321 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004322 pw.println(sb.toString());
4323 for (int e=0; e<numExcessive; e++) {
4324 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4325 if (ew != null) {
4326 pw.print(prefix); pw.print(" * Killed for ");
4327 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4328 pw.print("wake lock");
4329 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4330 pw.print("cpu");
4331 } else {
4332 pw.print("unknown");
4333 }
4334 pw.print(" use: ");
4335 TimeUtils.formatDuration(ew.usedTime, pw);
4336 pw.print(" over ");
4337 TimeUtils.formatDuration(ew.overTime, pw);
4338 if (ew.overTime != 0) {
4339 pw.print(" (");
4340 pw.print((ew.usedTime*100)/ew.overTime);
4341 pw.println("%)");
4342 }
4343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 }
4345 uidActivity = true;
4346 }
4347 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004348
4349 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4350 = u.getPackageStats();
4351 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4352 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4353 pw.println(":");
4354 boolean apkActivity = false;
4355 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4356 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4357 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4358 pw.print(prefix); pw.print(" Wakeup alarm ");
4359 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4360 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4361 pw.println(" times");
4362 apkActivity = true;
4363 }
4364 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4365 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4366 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4367 final long startTime = ss.getStartTime(batteryUptime, which);
4368 final int starts = ss.getStarts(which);
4369 final int launches = ss.getLaunches(which);
4370 if (startTime != 0 || starts != 0 || launches != 0) {
4371 sb.setLength(0);
4372 sb.append(prefix); sb.append(" Service ");
4373 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4374 sb.append(prefix); sb.append(" Created for: ");
4375 formatTimeMs(sb, startTime / 1000);
4376 sb.append("uptime\n");
4377 sb.append(prefix); sb.append(" Starts: ");
4378 sb.append(starts);
4379 sb.append(", launches: "); sb.append(launches);
4380 pw.println(sb.toString());
4381 apkActivity = true;
4382 }
4383 }
4384 if (!apkActivity) {
4385 pw.print(prefix); pw.println(" (nothing executed)");
4386 }
4387 uidActivity = true;
4388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004390 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 }
4392 }
4393 }
4394
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004395 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004396 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004397 int diff = oldval ^ newval;
4398 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004399 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004400 for (int i=0; i<descriptions.length; i++) {
4401 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004402 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004403 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004404 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004405 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004406 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004407 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4408 didWake = true;
4409 pw.print("=");
4410 if (longNames) {
4411 UserHandle.formatUid(pw, wakelockTag.uid);
4412 pw.print(":\"");
4413 pw.print(wakelockTag.string);
4414 pw.print("\"");
4415 } else {
4416 pw.print(wakelockTag.poolIdx);
4417 }
4418 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004419 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004420 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004421 pw.print("=");
4422 int val = (newval&bd.mask)>>bd.shift;
4423 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004424 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004425 } else {
4426 pw.print(val);
4427 }
4428 }
4429 }
4430 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004431 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004432 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004433 if (longNames) {
4434 UserHandle.formatUid(pw, wakelockTag.uid);
4435 pw.print(":\"");
4436 pw.print(wakelockTag.string);
4437 pw.print("\"");
4438 } else {
4439 pw.print(wakelockTag.poolIdx);
4440 }
4441 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004442 }
4443
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004444 public void prepareForDumpLocked() {
4445 }
4446
4447 public static class HistoryPrinter {
4448 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004449 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004450 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004451 int oldStatus = -1;
4452 int oldHealth = -1;
4453 int oldPlug = -1;
4454 int oldTemp = -1;
4455 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004456 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004457
Dianne Hackborn3251b902014-06-20 14:40:53 -07004458 void reset() {
4459 oldState = oldState2 = 0;
4460 oldLevel = -1;
4461 oldStatus = -1;
4462 oldHealth = -1;
4463 oldPlug = -1;
4464 oldTemp = -1;
4465 oldVolt = -1;
4466 }
4467
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004468 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004469 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004470 if (!checkin) {
4471 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004472 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004473 pw.print(" (");
4474 pw.print(rec.numReadInts);
4475 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004476 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004477 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4478 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004479 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004480 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004481 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004482 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004483 }
4484 lastTime = rec.time;
4485 }
4486 if (rec.cmd == HistoryItem.CMD_START) {
4487 if (checkin) {
4488 pw.print(":");
4489 }
4490 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004491 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004492 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4493 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004494 if (checkin) {
4495 pw.print(":");
4496 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004497 if (rec.cmd == HistoryItem.CMD_RESET) {
4498 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004499 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004500 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004501 pw.print("TIME:");
4502 if (checkin) {
4503 pw.println(rec.currentTime);
4504 } else {
4505 pw.print(" ");
4506 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4507 rec.currentTime).toString());
4508 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004509 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4510 if (checkin) {
4511 pw.print(":");
4512 }
4513 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004514 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4515 if (checkin) {
4516 pw.print(":");
4517 }
4518 pw.println("*OVERFLOW*");
4519 } else {
4520 if (!checkin) {
4521 if (rec.batteryLevel < 10) pw.print("00");
4522 else if (rec.batteryLevel < 100) pw.print("0");
4523 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004524 if (verbose) {
4525 pw.print(" ");
4526 if (rec.states < 0) ;
4527 else if (rec.states < 0x10) pw.print("0000000");
4528 else if (rec.states < 0x100) pw.print("000000");
4529 else if (rec.states < 0x1000) pw.print("00000");
4530 else if (rec.states < 0x10000) pw.print("0000");
4531 else if (rec.states < 0x100000) pw.print("000");
4532 else if (rec.states < 0x1000000) pw.print("00");
4533 else if (rec.states < 0x10000000) pw.print("0");
4534 pw.print(Integer.toHexString(rec.states));
4535 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004536 } else {
4537 if (oldLevel != rec.batteryLevel) {
4538 oldLevel = rec.batteryLevel;
4539 pw.print(",Bl="); pw.print(rec.batteryLevel);
4540 }
4541 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004542 if (oldStatus != rec.batteryStatus) {
4543 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004544 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004545 switch (oldStatus) {
4546 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004547 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004548 break;
4549 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004550 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004551 break;
4552 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004553 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004554 break;
4555 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004556 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004557 break;
4558 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004559 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004560 break;
4561 default:
4562 pw.print(oldStatus);
4563 break;
4564 }
4565 }
4566 if (oldHealth != rec.batteryHealth) {
4567 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004568 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004569 switch (oldHealth) {
4570 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004571 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004572 break;
4573 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004574 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004575 break;
4576 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004577 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004578 break;
4579 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004580 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004581 break;
4582 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004583 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004584 break;
4585 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004586 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004587 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004588 case BatteryManager.BATTERY_HEALTH_COLD:
4589 pw.print(checkin ? "c" : "cold");
4590 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004591 default:
4592 pw.print(oldHealth);
4593 break;
4594 }
4595 }
4596 if (oldPlug != rec.batteryPlugType) {
4597 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004598 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004599 switch (oldPlug) {
4600 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004601 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004602 break;
4603 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004604 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004605 break;
4606 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004607 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004608 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004609 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004610 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004611 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004612 default:
4613 pw.print(oldPlug);
4614 break;
4615 }
4616 }
4617 if (oldTemp != rec.batteryTemperature) {
4618 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004619 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004620 pw.print(oldTemp);
4621 }
4622 if (oldVolt != rec.batteryVoltage) {
4623 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004624 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004625 pw.print(oldVolt);
4626 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004627 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004628 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004629 printBitDescriptions(pw, oldState2, rec.states2, null,
4630 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004631 if (rec.wakeReasonTag != null) {
4632 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004633 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004634 pw.print(rec.wakeReasonTag.poolIdx);
4635 } else {
4636 pw.print(" wake_reason=");
4637 pw.print(rec.wakeReasonTag.uid);
4638 pw.print(":\"");
4639 pw.print(rec.wakeReasonTag.string);
4640 pw.print("\"");
4641 }
4642 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004643 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004644 pw.print(checkin ? "," : " ");
4645 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4646 pw.print("+");
4647 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4648 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004649 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004650 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4651 : HISTORY_EVENT_NAMES;
4652 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4653 | HistoryItem.EVENT_FLAG_FINISH);
4654 if (idx >= 0 && idx < eventNames.length) {
4655 pw.print(eventNames[idx]);
4656 } else {
4657 pw.print(checkin ? "Ev" : "event");
4658 pw.print(idx);
4659 }
4660 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004661 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004662 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004663 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004664 UserHandle.formatUid(pw, rec.eventTag.uid);
4665 pw.print(":\"");
4666 pw.print(rec.eventTag.string);
4667 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004668 }
4669 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004670 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004671 if (rec.stepDetails != null) {
4672 if (!checkin) {
4673 pw.print(" Details: cpu=");
4674 pw.print(rec.stepDetails.userTime);
4675 pw.print("u+");
4676 pw.print(rec.stepDetails.systemTime);
4677 pw.print("s");
4678 if (rec.stepDetails.appCpuUid1 >= 0) {
4679 pw.print(" (");
4680 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4681 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4682 if (rec.stepDetails.appCpuUid2 >= 0) {
4683 pw.print(", ");
4684 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4685 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4686 }
4687 if (rec.stepDetails.appCpuUid3 >= 0) {
4688 pw.print(", ");
4689 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4690 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4691 }
4692 pw.print(')');
4693 }
4694 pw.println();
4695 pw.print(" /proc/stat=");
4696 pw.print(rec.stepDetails.statUserTime);
4697 pw.print(" usr, ");
4698 pw.print(rec.stepDetails.statSystemTime);
4699 pw.print(" sys, ");
4700 pw.print(rec.stepDetails.statIOWaitTime);
4701 pw.print(" io, ");
4702 pw.print(rec.stepDetails.statIrqTime);
4703 pw.print(" irq, ");
4704 pw.print(rec.stepDetails.statSoftIrqTime);
4705 pw.print(" sirq, ");
4706 pw.print(rec.stepDetails.statIdlTime);
4707 pw.print(" idle");
4708 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4709 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4710 + rec.stepDetails.statSoftIrqTime;
4711 int total = totalRun + rec.stepDetails.statIdlTime;
4712 if (total > 0) {
4713 pw.print(" (");
4714 float perc = ((float)totalRun) / ((float)total) * 100;
4715 pw.print(String.format("%.1f%%", perc));
4716 pw.print(" of ");
4717 StringBuilder sb = new StringBuilder(64);
4718 formatTimeMsNoSpace(sb, total*10);
4719 pw.print(sb);
4720 pw.print(")");
4721 }
4722 pw.println();
4723 } else {
4724 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4725 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4726 pw.print(rec.stepDetails.userTime);
4727 pw.print(":");
4728 pw.print(rec.stepDetails.systemTime);
4729 if (rec.stepDetails.appCpuUid1 >= 0) {
4730 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4731 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4732 if (rec.stepDetails.appCpuUid2 >= 0) {
4733 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4734 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4735 }
4736 if (rec.stepDetails.appCpuUid3 >= 0) {
4737 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4738 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4739 }
4740 }
4741 pw.println();
4742 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4743 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4744 pw.print(rec.stepDetails.statUserTime);
4745 pw.print(',');
4746 pw.print(rec.stepDetails.statSystemTime);
4747 pw.print(',');
4748 pw.print(rec.stepDetails.statIOWaitTime);
4749 pw.print(',');
4750 pw.print(rec.stepDetails.statIrqTime);
4751 pw.print(',');
4752 pw.print(rec.stepDetails.statSoftIrqTime);
4753 pw.print(',');
4754 pw.print(rec.stepDetails.statIdlTime);
4755 pw.println();
4756 }
4757 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004758 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004759 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004760 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004761 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004762
4763 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4764 UserHandle.formatUid(pw, uid);
4765 pw.print("=");
4766 pw.print(utime);
4767 pw.print("u+");
4768 pw.print(stime);
4769 pw.print("s");
4770 }
4771
4772 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4773 pw.print('/');
4774 pw.print(uid);
4775 pw.print(":");
4776 pw.print(utime);
4777 pw.print(":");
4778 pw.print(stime);
4779 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004780 }
4781
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004782 private void printSizeValue(PrintWriter pw, long size) {
4783 float result = size;
4784 String suffix = "";
4785 if (result >= 10*1024) {
4786 suffix = "KB";
4787 result = result / 1024;
4788 }
4789 if (result >= 10*1024) {
4790 suffix = "MB";
4791 result = result / 1024;
4792 }
4793 if (result >= 10*1024) {
4794 suffix = "GB";
4795 result = result / 1024;
4796 }
4797 if (result >= 10*1024) {
4798 suffix = "TB";
4799 result = result / 1024;
4800 }
4801 if (result >= 10*1024) {
4802 suffix = "PB";
4803 result = result / 1024;
4804 }
4805 pw.print((int)result);
4806 pw.print(suffix);
4807 }
4808
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004809 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4810 String label3, long estimatedTime) {
4811 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004812 return false;
4813 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004814 pw.print(label1);
4815 pw.print(label2);
4816 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004817 StringBuilder sb = new StringBuilder(64);
4818 formatTimeMs(sb, estimatedTime);
4819 pw.print(sb);
4820 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004821 return true;
4822 }
4823
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004824 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4825 LevelStepTracker steps, boolean checkin) {
4826 if (steps == null) {
4827 return false;
4828 }
4829 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004830 if (count <= 0) {
4831 return false;
4832 }
4833 if (!checkin) {
4834 pw.println(header);
4835 }
Kweku Adams030980a2015-04-01 16:07:48 -07004836 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004837 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004838 long duration = steps.getDurationAt(i);
4839 int level = steps.getLevelAt(i);
4840 long initMode = steps.getInitModeAt(i);
4841 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004842 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004843 lineArgs[0] = Long.toString(duration);
4844 lineArgs[1] = Integer.toString(level);
4845 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4846 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4847 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4848 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4849 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4850 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004851 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004852 }
4853 } else {
4854 lineArgs[2] = "";
4855 }
4856 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4857 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4858 } else {
4859 lineArgs[3] = "";
4860 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004861 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004862 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004863 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004864 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004865 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004866 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4867 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004868 pw.print(prefix);
4869 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004870 TimeUtils.formatDuration(duration, pw);
4871 pw.print(" to "); pw.print(level);
4872 boolean haveModes = false;
4873 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4874 pw.print(" (");
4875 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4876 case Display.STATE_OFF: pw.print("screen-off"); break;
4877 case Display.STATE_ON: pw.print("screen-on"); break;
4878 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4879 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004880 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004881 }
4882 haveModes = true;
4883 }
4884 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4885 pw.print(haveModes ? ", " : " (");
4886 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4887 ? "power-save-on" : "power-save-off");
4888 haveModes = true;
4889 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004890 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4891 pw.print(haveModes ? ", " : " (");
4892 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4893 ? "device-idle-on" : "device-idle-off");
4894 haveModes = true;
4895 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004896 if (haveModes) {
4897 pw.print(")");
4898 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004899 pw.println();
4900 }
4901 }
4902 return true;
4903 }
4904
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004905 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004906 public static final int DUMP_DAILY_ONLY = 1<<2;
4907 public static final int DUMP_HISTORY_ONLY = 1<<3;
4908 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4909 public static final int DUMP_VERBOSE = 1<<5;
4910 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004911
Dianne Hackborn37de0982014-05-09 09:32:18 -07004912 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4913 final HistoryPrinter hprinter = new HistoryPrinter();
4914 final HistoryItem rec = new HistoryItem();
4915 long lastTime = -1;
4916 long baseTime = -1;
4917 boolean printed = false;
4918 HistoryEventTracker tracker = null;
4919 while (getNextHistoryLocked(rec)) {
4920 lastTime = rec.time;
4921 if (baseTime < 0) {
4922 baseTime = lastTime;
4923 }
4924 if (rec.time >= histStart) {
4925 if (histStart >= 0 && !printed) {
4926 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004927 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004928 || rec.cmd == HistoryItem.CMD_START
4929 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004930 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004931 hprinter.printNextItem(pw, rec, baseTime, checkin,
4932 (flags&DUMP_VERBOSE) != 0);
4933 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004934 } else if (rec.currentTime != 0) {
4935 printed = true;
4936 byte cmd = rec.cmd;
4937 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004938 hprinter.printNextItem(pw, rec, baseTime, checkin,
4939 (flags&DUMP_VERBOSE) != 0);
4940 rec.cmd = cmd;
4941 }
4942 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004943 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4944 hprinter.printNextItem(pw, rec, baseTime, checkin,
4945 (flags&DUMP_VERBOSE) != 0);
4946 rec.cmd = HistoryItem.CMD_UPDATE;
4947 }
4948 int oldEventCode = rec.eventCode;
4949 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004950 rec.eventTag = new HistoryTag();
4951 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4952 HashMap<String, SparseIntArray> active
4953 = tracker.getStateForEvent(i);
4954 if (active == null) {
4955 continue;
4956 }
4957 for (HashMap.Entry<String, SparseIntArray> ent
4958 : active.entrySet()) {
4959 SparseIntArray uids = ent.getValue();
4960 for (int j=0; j<uids.size(); j++) {
4961 rec.eventCode = i;
4962 rec.eventTag.string = ent.getKey();
4963 rec.eventTag.uid = uids.keyAt(j);
4964 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07004965 hprinter.printNextItem(pw, rec, baseTime, checkin,
4966 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004967 rec.wakeReasonTag = null;
4968 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004969 }
4970 }
4971 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004972 rec.eventCode = oldEventCode;
4973 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004974 tracker = null;
4975 }
4976 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004977 hprinter.printNextItem(pw, rec, baseTime, checkin,
4978 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004979 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
4980 // This is an attempt to aggregate the previous state and generate
4981 // fake events to reflect that state at the point where we start
4982 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07004983 if (tracker == null) {
4984 tracker = new HistoryEventTracker();
4985 }
4986 tracker.updateState(rec.eventCode, rec.eventTag.string,
4987 rec.eventTag.uid, rec.eventTag.poolIdx);
4988 }
4989 }
4990 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004991 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004992 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
4993 }
4994 }
4995
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004996 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
4997 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
4998 if (steps == null) {
4999 return;
5000 }
5001 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5002 if (timeRemaining >= 0) {
5003 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5004 tmpSb.setLength(0);
5005 formatTimeMs(tmpSb, timeRemaining);
5006 pw.print(tmpSb);
5007 pw.print(" (from "); pw.print(tmpOutInt[0]);
5008 pw.println(" steps)");
5009 }
5010 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5011 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5012 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5013 if (estimatedTime > 0) {
5014 pw.print(prefix); pw.print(label); pw.print(" ");
5015 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5016 pw.print(" time: ");
5017 tmpSb.setLength(0);
5018 formatTimeMs(tmpSb, estimatedTime);
5019 pw.print(tmpSb);
5020 pw.print(" (from "); pw.print(tmpOutInt[0]);
5021 pw.println(" steps)");
5022 }
5023 }
5024 }
5025
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005026 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5027 ArrayList<PackageChange> changes) {
5028 if (changes == null) {
5029 return;
5030 }
5031 pw.print(prefix); pw.println("Package changes:");
5032 for (int i=0; i<changes.size(); i++) {
5033 PackageChange pc = changes.get(i);
5034 if (pc.mUpdate) {
5035 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5036 pw.print(" vers="); pw.println(pc.mVersionCode);
5037 } else {
5038 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5039 }
5040 }
5041 }
5042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 /**
5044 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5045 *
5046 * @param pw a Printer to receive the dump output.
5047 */
5048 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005049 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005050 prepareForDumpLocked();
5051
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005052 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005053 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005054
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005055 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005056 final long historyTotalSize = getHistoryTotalSize();
5057 final long historyUsedSize = getHistoryUsedSize();
5058 if (startIteratingHistoryLocked()) {
5059 try {
5060 pw.print("Battery History (");
5061 pw.print((100*historyUsedSize)/historyTotalSize);
5062 pw.print("% used, ");
5063 printSizeValue(pw, historyUsedSize);
5064 pw.print(" used of ");
5065 printSizeValue(pw, historyTotalSize);
5066 pw.print(", ");
5067 pw.print(getHistoryStringPoolSize());
5068 pw.print(" strings using ");
5069 printSizeValue(pw, getHistoryStringPoolBytes());
5070 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005071 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005072 pw.println();
5073 } finally {
5074 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005075 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005076 }
5077
5078 if (startIteratingOldHistoryLocked()) {
5079 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005080 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005081 pw.println("Old battery History:");
5082 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005083 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005084 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005085 if (baseTime < 0) {
5086 baseTime = rec.time;
5087 }
5088 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005089 }
5090 pw.println();
5091 } finally {
5092 finishIteratingOldHistoryLocked();
5093 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005094 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005095 }
5096
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005097 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005098 return;
5099 }
5100
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005101 if (!filtering) {
5102 SparseArray<? extends Uid> uidStats = getUidStats();
5103 final int NU = uidStats.size();
5104 boolean didPid = false;
5105 long nowRealtime = SystemClock.elapsedRealtime();
5106 for (int i=0; i<NU; i++) {
5107 Uid uid = uidStats.valueAt(i);
5108 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5109 if (pids != null) {
5110 for (int j=0; j<pids.size(); j++) {
5111 Uid.Pid pid = pids.valueAt(j);
5112 if (!didPid) {
5113 pw.println("Per-PID Stats:");
5114 didPid = true;
5115 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005116 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5117 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005118 pw.print(" PID "); pw.print(pids.keyAt(j));
5119 pw.print(" wake time: ");
5120 TimeUtils.formatDuration(time, pw);
5121 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005122 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005123 }
5124 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005125 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005126 pw.println();
5127 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005128 }
5129
5130 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005131 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5132 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005133 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5134 if (timeRemaining >= 0) {
5135 pw.print(" Estimated discharge time remaining: ");
5136 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5137 pw.println();
5138 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005139 final LevelStepTracker steps = getDischargeLevelStepTracker();
5140 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5141 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5142 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5143 STEP_LEVEL_MODE_VALUES[i], null));
5144 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005145 pw.println();
5146 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005147 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5148 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005149 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5150 if (timeRemaining >= 0) {
5151 pw.print(" Estimated charge time remaining: ");
5152 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5153 pw.println();
5154 }
5155 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005156 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005157 }
5158 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5159 pw.println("Daily stats:");
5160 pw.print(" Current start time: ");
5161 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5162 getCurrentDailyStartTime()).toString());
5163 pw.print(" Next min deadline: ");
5164 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5165 getNextMinDailyDeadline()).toString());
5166 pw.print(" Next max deadline: ");
5167 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5168 getNextMaxDailyDeadline()).toString());
5169 StringBuilder sb = new StringBuilder(64);
5170 int[] outInt = new int[1];
5171 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5172 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005173 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5174 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005175 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005176 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5177 dsteps, false)) {
5178 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5179 sb, outInt);
5180 }
5181 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5182 csteps, false)) {
5183 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5184 sb, outInt);
5185 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005186 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005187 } else {
5188 pw.println(" Current daily steps:");
5189 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5190 sb, outInt);
5191 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5192 sb, outInt);
5193 }
5194 }
5195 DailyItem dit;
5196 int curIndex = 0;
5197 while ((dit=getDailyItemLocked(curIndex)) != null) {
5198 curIndex++;
5199 if ((flags&DUMP_DAILY_ONLY) != 0) {
5200 pw.println();
5201 }
5202 pw.print(" Daily from ");
5203 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5204 pw.print(" to ");
5205 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5206 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005207 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005208 if (dumpDurationSteps(pw, " ",
5209 " Discharge step durations:", dit.mDischargeSteps, false)) {
5210 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5211 sb, outInt);
5212 }
5213 if (dumpDurationSteps(pw, " ",
5214 " Charge step durations:", dit.mChargeSteps, false)) {
5215 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5216 sb, outInt);
5217 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005218 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005219 } else {
5220 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5221 sb, outInt);
5222 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5223 sb, outInt);
5224 }
5225 }
5226 pw.println();
5227 }
5228 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005229 pw.println("Statistics since last charge:");
5230 pw.println(" System starts: " + getStartCount()
5231 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005232 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5233 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005234 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237
5238 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005239 public void dumpCheckinLocked(Context context, PrintWriter pw,
5240 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005241 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005242
5243 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005244 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5245 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005246
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005247 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5248
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005249 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005250 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005251
5252 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005253 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005254 try {
5255 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5256 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5257 pw.print(HISTORY_STRING_POOL); pw.print(',');
5258 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005259 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005260 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005261 pw.print(",\"");
5262 String str = getHistoryTagPoolString(i);
5263 str = str.replace("\\", "\\\\");
5264 str = str.replace("\"", "\\\"");
5265 pw.print(str);
5266 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005267 pw.println();
5268 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005269 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005270 } finally {
5271 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005272 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005273 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005274 }
5275
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005276 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005277 return;
5278 }
5279
Dianne Hackborne4a59512010-12-07 11:08:07 -08005280 if (apps != null) {
5281 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5282 for (int i=0; i<apps.size(); i++) {
5283 ApplicationInfo ai = apps.get(i);
5284 ArrayList<String> pkgs = uids.get(ai.uid);
5285 if (pkgs == null) {
5286 pkgs = new ArrayList<String>();
5287 uids.put(ai.uid, pkgs);
5288 }
5289 pkgs.add(ai.packageName);
5290 }
5291 SparseArray<? extends Uid> uidStats = getUidStats();
5292 final int NU = uidStats.size();
5293 String[] lineArgs = new String[2];
5294 for (int i=0; i<NU; i++) {
5295 int uid = uidStats.keyAt(i);
5296 ArrayList<String> pkgs = uids.get(uid);
5297 if (pkgs != null) {
5298 for (int j=0; j<pkgs.size(); j++) {
5299 lineArgs[0] = Integer.toString(uid);
5300 lineArgs[1] = pkgs.get(j);
5301 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5302 (Object[])lineArgs);
5303 }
5304 }
5305 }
5306 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005307 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005308 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005309 String[] lineArgs = new String[1];
5310 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5311 if (timeRemaining >= 0) {
5312 lineArgs[0] = Long.toString(timeRemaining);
5313 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5314 (Object[])lineArgs);
5315 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005316 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005317 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5318 if (timeRemaining >= 0) {
5319 lineArgs[0] = Long.toString(timeRemaining);
5320 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5321 (Object[])lineArgs);
5322 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005323 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5324 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327}