blob: 9e9820f6980c4b093269a3be14574a299a55cb34 [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 Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborna7c837f2014-01-15 16:20:44 -080027import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080028import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080029import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
38 * A class providing access to battery usage statistics, including information on
39 * wakelocks, processes, packages, and services. All times are represented in microseconds
40 * except where indicated otherwise.
41 * @hide
42 */
43public abstract class BatteryStats implements Parcelable {
44
45 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046
47 /** @hide */
48 public static final String SERVICE_NAME = "batterystats";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * A constant indicating a partial wake lock timer.
52 */
53 public static final int WAKE_TYPE_PARTIAL = 0;
54
55 /**
56 * A constant indicating a full wake lock timer.
57 */
58 public static final int WAKE_TYPE_FULL = 1;
59
60 /**
61 * A constant indicating a window wake lock timer.
62 */
63 public static final int WAKE_TYPE_WINDOW = 2;
64
65 /**
66 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070069
70 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070072 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070074
75 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070076 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070078 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070082 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 /**
86 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 */
88 public static final int WIFI_MULTICAST_ENABLED = 7;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int AUDIO_TURNED_ON = 7;
94
95 /**
96 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070097 */
98 public static final int VIDEO_TURNED_ON = 8;
99
100 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 * A constant indicating a vibrator on timer
102 */
103 public static final int VIBRATOR_ON = 9;
104
105 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700106 * A constant indicating a foreground activity timer
107 */
108 public static final int FOREGROUND_ACTIVITY = 10;
109
110 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700111 * A constant indicating a wifi batched scan is active
112 */
113 public static final int WIFI_BATCHED_SCAN = 11;
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * Include all of the data in the stats, including previously saved data.
117 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700118 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * Include only the current run in the stats.
122 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700123 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 /**
126 * Include only the run since the last time the device was unplugged in the stats.
127 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700128 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700129
130 // NOTE: Update this list if you add/change any stats above.
131 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700132 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700133 private static final String[] STAT_NAMES = { "l", "c", "u" };
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
136 * Bump the version on this if the checkin format changes.
137 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700138 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700139
140 private static final long BYTES_PER_KB = 1024;
141 private static final long BYTES_PER_MB = 1048576; // 1024^2
142 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Dianne Hackborne4a59512010-12-07 11:08:07 -0800145 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700147 private static final String PROCESS_DATA = "pr";
148 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800149 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700150 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700151 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700152 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700153 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700154 private static final String NETWORK_DATA = "nt";
155 private static final String USER_ACTIVITY_DATA = "ua";
156 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800157 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700159 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700160 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800161 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800162 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700163 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700164 private static final String SCREEN_BRIGHTNESS_DATA = "br";
165 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700166 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700167 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
168 private static final String DATA_CONNECTION_TIME_DATA = "dct";
169 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800170 private static final String WIFI_STATE_TIME_DATA = "wst";
171 private static final String WIFI_STATE_COUNT_DATA = "wsc";
172 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
173 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800174 private static final String POWER_USE_SUMMARY_DATA = "pws";
175 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700176 private static final String DISCHARGE_STEP_DATA = "dsd";
177 private static final String CHARGE_STEP_DATA = "csd";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700179 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 private final Formatter mFormatter = new Formatter(mFormatBuilder);
181
182 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700183 * State for keeping track of counting information.
184 */
185 public static abstract class Counter {
186
187 /**
188 * Returns the count associated with this Counter for the
189 * selected type of statistics.
190 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700191 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700192 */
Evan Millarc64edde2009-04-18 12:26:32 -0700193 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700194
195 /**
196 * Temporary for debugging.
197 */
198 public abstract void logState(Printer pw, String prefix);
199 }
200
201 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700202 * State for keeping track of long counting information.
203 */
204 public static abstract class LongCounter {
205
206 /**
207 * Returns the count associated with this Counter for the
208 * selected type of statistics.
209 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700210 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700211 */
212 public abstract long getCountLocked(int which);
213
214 /**
215 * Temporary for debugging.
216 */
217 public abstract void logState(Printer pw, String prefix);
218 }
219
220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * State for keeping track of timing information.
222 */
223 public static abstract class Timer {
224
225 /**
226 * Returns the count associated with this Timer for the
227 * selected type of statistics.
228 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700229 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Evan Millarc64edde2009-04-18 12:26:32 -0700231 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233 /**
234 * Returns the total time in microseconds associated with this Timer for the
235 * selected type of statistics.
236 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800237 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700238 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * @return a time in microseconds
240 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800241 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 /**
244 * Temporary for debugging.
245 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700246 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
248
249 /**
250 * The statistics associated with a particular uid.
251 */
252 public static abstract class Uid {
253
254 /**
255 * Returns a mapping containing wakelock statistics.
256 *
257 * @return a Map from Strings to Uid.Wakelock objects.
258 */
259 public abstract Map<String, ? extends Wakelock> getWakelockStats();
260
261 /**
262 * The statistics associated with a particular wake lock.
263 */
264 public static abstract class Wakelock {
265 public abstract Timer getWakeTime(int type);
266 }
267
268 /**
269 * Returns a mapping containing sensor statistics.
270 *
271 * @return a Map from Integer sensor ids to Uid.Sensor objects.
272 */
273 public abstract Map<Integer, ? extends Sensor> getSensorStats();
274
275 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700276 * Returns a mapping containing active process data.
277 */
278 public abstract SparseArray<? extends Pid> getPidStats();
279
280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * Returns a mapping containing process statistics.
282 *
283 * @return a Map from Strings to Uid.Proc objects.
284 */
285 public abstract Map<String, ? extends Proc> getProcessStats();
286
287 /**
288 * Returns a mapping containing package statistics.
289 *
290 * @return a Map from Strings to Uid.Pkg objects.
291 */
292 public abstract Map<String, ? extends Pkg> getPackageStats();
293
294 /**
295 * {@hide}
296 */
297 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700298
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800299 public abstract void noteWifiRunningLocked(long elapsedRealtime);
300 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
301 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
302 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
303 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
304 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
305 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
306 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
307 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
308 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
309 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
310 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
311 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
312 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
313 public abstract void noteActivityResumedLocked(long elapsedRealtime);
314 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800315 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
316 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
317 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
318 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
319 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
320 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
321 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700322 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800323 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
Robert Greenwalta029ea12013-09-25 16:38:12 -0700325 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
326
Dianne Hackborn617f8772009-03-31 15:04:46 -0700327 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700328 * Note that these must match the constants in android.os.PowerManager.
329 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
330 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700331 */
332 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700333 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700334 };
335
Jeff Browndf693de2012-07-27 12:03:38 -0700336 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700337
Dianne Hackborn617f8772009-03-31 15:04:46 -0700338 public abstract void noteUserActivityLocked(int type);
339 public abstract boolean hasUserActivity();
340 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700341
342 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800343 public abstract long getNetworkActivityBytes(int type, int which);
344 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800345 public abstract long getMobileRadioActiveTime(int which);
346 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800349 /*
350 * FIXME: it's not correct to use this magic value because it
351 * could clash with a sensor handle (which are defined by
352 * the sensor HAL, and therefore out of our control
353 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 // Magic sensor number for the GPS.
355 public static final int GPS = -10000;
356
357 public abstract int getHandle();
358
359 public abstract Timer getSensorTime();
360 }
361
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700362 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800363 public int mWakeNesting;
364 public long mWakeSumMs;
365 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700366 }
367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * The statistics associated with a particular process.
370 */
371 public static abstract class Proc {
372
Dianne Hackborn287952c2010-09-22 22:34:31 -0700373 public static class ExcessivePower {
374 public static final int TYPE_WAKE = 1;
375 public static final int TYPE_CPU = 2;
376
377 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700378 public long overTime;
379 public long usedTime;
380 }
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800383 * Returns true if this process is still active in the battery stats.
384 */
385 public abstract boolean isActive();
386
387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * Returns the total time (in 1/100 sec) spent executing in user code.
389 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700390 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
392 public abstract long getUserTime(int which);
393
394 /**
395 * Returns the total time (in 1/100 sec) spent executing in system code.
396 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700397 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 */
399 public abstract long getSystemTime(int which);
400
401 /**
402 * Returns the number of times the process has been started.
403 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700404 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 */
406 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700407
408 /**
409 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700410 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700411 * @return foreground cpu time in microseconds
412 */
413 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700414
415 /**
416 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
417 * @param speedStep the index of the CPU speed. This is not the actual speed of the
418 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700419 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700420 * @see BatteryStats#getCpuSpeedSteps()
421 */
422 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700423
Dianne Hackborn287952c2010-09-22 22:34:31 -0700424 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700425
Dianne Hackborn287952c2010-09-22 22:34:31 -0700426 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428
429 /**
430 * The statistics associated with a particular package.
431 */
432 public static abstract class Pkg {
433
434 /**
435 * Returns the number of times this package has done something that could wake up the
436 * device from sleep.
437 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700438 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 */
440 public abstract int getWakeups(int which);
441
442 /**
443 * Returns a mapping containing service statistics.
444 */
445 public abstract Map<String, ? extends Serv> getServiceStats();
446
447 /**
448 * The statistics associated with a particular service.
449 */
450 public abstract class Serv {
451
452 /**
453 * Returns the amount of time spent started.
454 *
455 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700456 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * @return
458 */
459 public abstract long getStartTime(long batteryUptime, int which);
460
461 /**
462 * Returns the total number of times startService() has been called.
463 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700464 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
466 public abstract int getStarts(int which);
467
468 /**
469 * Returns the total number times the service has been launched.
470 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700471 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
473 public abstract int getLaunches(int which);
474 }
475 }
476 }
477
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800478 public final static class HistoryTag {
479 public String string;
480 public int uid;
481
482 public int poolIdx;
483
484 public void setTo(HistoryTag o) {
485 string = o.string;
486 uid = o.uid;
487 poolIdx = o.poolIdx;
488 }
489
490 public void setTo(String _string, int _uid) {
491 string = _string;
492 uid = _uid;
493 poolIdx = -1;
494 }
495
496 public void writeToParcel(Parcel dest, int flags) {
497 dest.writeString(string);
498 dest.writeInt(uid);
499 }
500
501 public void readFromParcel(Parcel src) {
502 string = src.readString();
503 uid = src.readInt();
504 poolIdx = -1;
505 }
506
507 @Override
508 public boolean equals(Object o) {
509 if (this == o) return true;
510 if (o == null || getClass() != o.getClass()) return false;
511
512 HistoryTag that = (HistoryTag) o;
513
514 if (uid != that.uid) return false;
515 if (!string.equals(that.string)) return false;
516
517 return true;
518 }
519
520 @Override
521 public int hashCode() {
522 int result = string.hashCode();
523 result = 31 * result + uid;
524 return result;
525 }
526 }
527
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700528 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700529 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700530
531 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800532
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800533 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800534 public static final byte CMD_NULL = -1;
535 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800536 public static final byte CMD_CURRENT_TIME = 5;
537 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800538
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700539 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700540
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800541 /**
542 * Return whether the command code is a delta data update.
543 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800544 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800545 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800546 }
547
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700548 public byte batteryLevel;
549 public byte batteryStatus;
550 public byte batteryHealth;
551 public byte batteryPlugType;
552
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900553 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700554 public char batteryVoltage;
555
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700556 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700557 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800558 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700559 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800560 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
561 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700562 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800563 public static final int STATE_PHONE_STATE_SHIFT = 6;
564 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700565 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800566 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800567 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800568
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700569 // These states always appear directly in the first int token
570 // of a delta change; they should be ones that change relatively
571 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700572 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
573 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800574 public static final int STATE_GPS_ON_FLAG = 1<<29;
575 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800576 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800577 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800578 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800579 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700580 // These are on the lower bits used for the command; if they change
581 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700582 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700583 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700584 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700585 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
586 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
587 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
588 public static final int STATE_WIFI_ON_FLAG = 1<<17;
589 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700590
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700591 public static final int MOST_INTERESTING_STATES =
592 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
593 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
594
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700595 public int states;
596
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700597 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700598 public int states2;
599
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800600 // The wake lock that was acquired at this point.
601 public HistoryTag wakelockTag;
602
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800603 // Kernel wakeup reason at this point.
604 public HistoryTag wakeReasonTag;
605
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800606 public static final int EVENT_FLAG_START = 0x8000;
607 public static final int EVENT_FLAG_FINISH = 0x4000;
608
609 // No event in this item.
610 public static final int EVENT_NONE = 0x0000;
611 // Event is about a process that is running.
612 public static final int EVENT_PROC = 0x0001;
613 // Event is about an application package that is in the foreground.
614 public static final int EVENT_FOREGROUND = 0x0002;
615 // Event is about an application package that is at the top of the screen.
616 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800617 // Event is about an application package that is at the top of the screen.
618 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800619 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800620 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800621
622 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
623 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
624 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
625 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
626 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
627 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800628 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
629 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800630
631 // For CMD_EVENT.
632 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800633 public HistoryTag eventTag;
634
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800635 // Only set for CMD_CURRENT_TIME.
636 public long currentTime;
637
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800638 // Meta-data when reading.
639 public int numReadInts;
640
641 // Pre-allocated objects.
642 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800643 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800644 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800645
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700646 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700647 }
648
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700649 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700650 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800651 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700652 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700653 }
654
655 public int describeContents() {
656 return 0;
657 }
658
659 public void writeToParcel(Parcel dest, int flags) {
660 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700661 int bat = (((int)cmd)&0xff)
662 | ((((int)batteryLevel)<<8)&0xff00)
663 | ((((int)batteryStatus)<<16)&0xf0000)
664 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800665 | ((((int)batteryPlugType)<<24)&0xf000000)
666 | (wakelockTag != null ? 0x10000000 : 0)
667 | (wakeReasonTag != null ? 0x20000000 : 0)
668 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700669 dest.writeInt(bat);
670 bat = (((int)batteryTemperature)&0xffff)
671 | ((((int)batteryVoltage)<<16)&0xffff0000);
672 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700673 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700674 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800675 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800676 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800677 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800678 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800679 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800680 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800681 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800682 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800683 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800684 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800685 if (cmd == CMD_CURRENT_TIME) {
686 dest.writeLong(currentTime);
687 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700688 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700689
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800690 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800691 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700692 int bat = src.readInt();
693 cmd = (byte)(bat&0xff);
694 batteryLevel = (byte)((bat>>8)&0xff);
695 batteryStatus = (byte)((bat>>16)&0xf);
696 batteryHealth = (byte)((bat>>20)&0xf);
697 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800698 int bat2 = src.readInt();
699 batteryTemperature = (short)(bat2&0xffff);
700 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700701 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700702 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800703 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800704 wakelockTag = localWakelockTag;
705 wakelockTag.readFromParcel(src);
706 } else {
707 wakelockTag = null;
708 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800709 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800710 wakeReasonTag = localWakeReasonTag;
711 wakeReasonTag.readFromParcel(src);
712 } else {
713 wakeReasonTag = null;
714 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800715 if ((bat&0x40000000) != 0) {
716 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800717 eventTag = localEventTag;
718 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800719 } else {
720 eventCode = EVENT_NONE;
721 eventTag = null;
722 }
723 if (cmd == CMD_CURRENT_TIME) {
724 currentTime = src.readLong();
725 } else {
726 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700727 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800728 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700729 }
730
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700731 public void clear() {
732 time = 0;
733 cmd = CMD_NULL;
734 batteryLevel = 0;
735 batteryStatus = 0;
736 batteryHealth = 0;
737 batteryPlugType = 0;
738 batteryTemperature = 0;
739 batteryVoltage = 0;
740 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700741 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800742 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800743 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800744 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800745 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700746 }
747
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700748 public void setTo(HistoryItem o) {
749 time = o.time;
750 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800751 setToCommon(o);
752 }
753
754 public void setTo(long time, byte cmd, HistoryItem o) {
755 this.time = time;
756 this.cmd = cmd;
757 setToCommon(o);
758 }
759
760 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700761 batteryLevel = o.batteryLevel;
762 batteryStatus = o.batteryStatus;
763 batteryHealth = o.batteryHealth;
764 batteryPlugType = o.batteryPlugType;
765 batteryTemperature = o.batteryTemperature;
766 batteryVoltage = o.batteryVoltage;
767 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700768 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800769 if (o.wakelockTag != null) {
770 wakelockTag = localWakelockTag;
771 wakelockTag.setTo(o.wakelockTag);
772 } else {
773 wakelockTag = null;
774 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800775 if (o.wakeReasonTag != null) {
776 wakeReasonTag = localWakeReasonTag;
777 wakeReasonTag.setTo(o.wakeReasonTag);
778 } else {
779 wakeReasonTag = null;
780 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800781 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800782 if (o.eventTag != null) {
783 eventTag = localEventTag;
784 eventTag.setTo(o.eventTag);
785 } else {
786 eventTag = null;
787 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800788 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700789 }
790
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800791 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700792 return batteryLevel == o.batteryLevel
793 && batteryStatus == o.batteryStatus
794 && batteryHealth == o.batteryHealth
795 && batteryPlugType == o.batteryPlugType
796 && batteryTemperature == o.batteryTemperature
797 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800798 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700799 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800800 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700801 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800802
803 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800804 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800805 return false;
806 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800807 if (wakelockTag != o.wakelockTag) {
808 if (wakelockTag == null || o.wakelockTag == null) {
809 return false;
810 }
811 if (!wakelockTag.equals(o.wakelockTag)) {
812 return false;
813 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800814 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800815 if (wakeReasonTag != o.wakeReasonTag) {
816 if (wakeReasonTag == null || o.wakeReasonTag == null) {
817 return false;
818 }
819 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
820 return false;
821 }
822 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800823 if (eventTag != o.eventTag) {
824 if (eventTag == null || o.eventTag == null) {
825 return false;
826 }
827 if (!eventTag.equals(o.eventTag)) {
828 return false;
829 }
830 }
831 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800832 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700833 }
834
835 public static final class BitDescription {
836 public final int mask;
837 public final int shift;
838 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800839 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700840 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800841 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700842
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800843 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700844 this.mask = mask;
845 this.shift = -1;
846 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800847 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700848 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800849 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700850 }
851
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800852 public BitDescription(int mask, int shift, String name, String shortName,
853 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700854 this.mask = mask;
855 this.shift = shift;
856 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800857 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700858 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800859 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700860 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700861 }
862
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800863 public abstract int getHistoryTotalSize();
864
865 public abstract int getHistoryUsedSize();
866
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700867 public abstract boolean startIteratingHistoryLocked();
868
Dianne Hackborn099bc622014-01-22 13:39:16 -0800869 public abstract int getHistoryStringPoolSize();
870
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800871 public abstract int getHistoryStringPoolBytes();
872
873 public abstract String getHistoryTagPoolString(int index);
874
875 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800876
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700877 public abstract boolean getNextHistoryLocked(HistoryItem out);
878
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700879 public abstract void finishIteratingHistoryLocked();
880
881 public abstract boolean startIteratingOldHistoryLocked();
882
883 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
884
885 public abstract void finishIteratingOldHistoryLocked();
886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700888 * Return the base time offset for the battery history.
889 */
890 public abstract long getHistoryBaseTime();
891
892 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * Returns the number of times the device has been started.
894 */
895 public abstract int getStartCount();
896
897 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700898 * 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 -0800899 * running on battery.
900 *
901 * {@hide}
902 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800903 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800905 /**
906 * Returns the number of times the screen was turned on.
907 *
908 * {@hide}
909 */
910 public abstract int getScreenOnCount(int which);
911
Dianne Hackborn617f8772009-03-31 15:04:46 -0700912 public static final int SCREEN_BRIGHTNESS_DARK = 0;
913 public static final int SCREEN_BRIGHTNESS_DIM = 1;
914 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
915 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
916 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
917
918 static final String[] SCREEN_BRIGHTNESS_NAMES = {
919 "dark", "dim", "medium", "light", "bright"
920 };
921
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800922 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
923 "0", "1", "2", "3", "4"
924 };
925
Dianne Hackborn617f8772009-03-31 15:04:46 -0700926 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
927
928 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700929 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700930 * the given brightness
931 *
932 * {@hide}
933 */
934 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800935 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700936
937 public abstract int getInputEventCount(int which);
938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700940 * 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 -0800941 * running on battery.
942 *
943 * {@hide}
944 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800945 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700946
947 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800948 * Returns the number of times a phone call was activated.
949 *
950 * {@hide}
951 */
952 public abstract int getPhoneOnCount(int which);
953
954 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700955 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700956 * the given signal strength.
957 *
958 * {@hide}
959 */
960 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800961 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700962
Dianne Hackborn617f8772009-03-31 15:04:46 -0700963 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700964 * Returns the time in microseconds that the phone has been trying to
965 * acquire a signal.
966 *
967 * {@hide}
968 */
969 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800970 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -0700971
972 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700973 * Returns the number of times the phone has entered the given signal strength.
974 *
975 * {@hide}
976 */
977 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
978
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800979 /**
980 * Returns the time in microseconds that the mobile network has been active
981 * (in a high power state).
982 *
983 * {@hide}
984 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800985 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800986
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800987 /**
988 * Returns the number of times that the mobile network has transitioned to the
989 * active state.
990 *
991 * {@hide}
992 */
993 public abstract int getMobileRadioActiveCount(int which);
994
995 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700996 * Returns the time in microseconds that is the difference between the mobile radio
997 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
998 * from the radio.
999 *
1000 * {@hide}
1001 */
1002 public abstract long getMobileRadioActiveAdjustedTime(int which);
1003
1004 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001005 * Returns the time in microseconds that the mobile network has been active
1006 * (in a high power state) but not being able to blame on an app.
1007 *
1008 * {@hide}
1009 */
1010 public abstract long getMobileRadioActiveUnknownTime(int which);
1011
1012 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001013 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001014 *
1015 * {@hide}
1016 */
1017 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001018
Dianne Hackborn627bba72009-03-24 22:32:56 -07001019 public static final int DATA_CONNECTION_NONE = 0;
1020 public static final int DATA_CONNECTION_GPRS = 1;
1021 public static final int DATA_CONNECTION_EDGE = 2;
1022 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001023 public static final int DATA_CONNECTION_CDMA = 4;
1024 public static final int DATA_CONNECTION_EVDO_0 = 5;
1025 public static final int DATA_CONNECTION_EVDO_A = 6;
1026 public static final int DATA_CONNECTION_1xRTT = 7;
1027 public static final int DATA_CONNECTION_HSDPA = 8;
1028 public static final int DATA_CONNECTION_HSUPA = 9;
1029 public static final int DATA_CONNECTION_HSPA = 10;
1030 public static final int DATA_CONNECTION_IDEN = 11;
1031 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001032 public static final int DATA_CONNECTION_LTE = 13;
1033 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001034 public static final int DATA_CONNECTION_HSPAP = 15;
1035 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001036
Dianne Hackborn627bba72009-03-24 22:32:56 -07001037 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001038 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001039 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001040 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001041 };
1042
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001043 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001044
1045 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001046 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001047 * the given data connection.
1048 *
1049 * {@hide}
1050 */
1051 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001052 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001055 * Returns the number of times the phone has entered the given data
1056 * connection type.
1057 *
1058 * {@hide}
1059 */
1060 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001061
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001062 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1063 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001064 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001065 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1066 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001067 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001068 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1069 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1070 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001071 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001072 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1073 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001074 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001075 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1076 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1077 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1078 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1079 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1080 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1081 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1082 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1083 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1084 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1085 new String[] {"in", "out", "emergency", "off"},
1086 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001087 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001088 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1089 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1090 "0", "1", "2", "3", "4"
1091 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001092 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1093 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1094 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001095 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001096
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001097 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1098 = new BitDescription[] {
1099 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1100 };
1101
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001102 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001103 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001104 };
1105
1106 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001107 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001108 };
1109
Dianne Hackborn617f8772009-03-31 15:04:46 -07001110 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001111 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001112 * running on battery.
1113 *
1114 * {@hide}
1115 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001116 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001117
1118 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001119 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001120 * been in the running state while the device was running on battery.
1121 *
1122 * {@hide}
1123 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001124 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001125
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001126 public static final int WIFI_STATE_OFF = 0;
1127 public static final int WIFI_STATE_OFF_SCANNING = 1;
1128 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1129 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1130 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1131 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1132 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1133 public static final int WIFI_STATE_SOFT_AP = 7;
1134
1135 static final String[] WIFI_STATE_NAMES = {
1136 "off", "scanning", "no_net", "disconn",
1137 "sta", "p2p", "sta_p2p", "soft_ap"
1138 };
1139
1140 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1141
1142 /**
1143 * Returns the time in microseconds that WiFi has been running in the given state.
1144 *
1145 * {@hide}
1146 */
1147 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001148 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001149
1150 /**
1151 * Returns the number of times that WiFi has entered the given state.
1152 *
1153 * {@hide}
1154 */
1155 public abstract int getWifiStateCount(int wifiState, int which);
1156
The Android Open Source Project10592532009-03-18 17:39:46 -07001157 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001158 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001159 * running on battery.
1160 *
1161 * {@hide}
1162 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001164
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001165 public abstract int getBluetoothPingCount();
1166
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001167 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1168 public static final int BLUETOOTH_STATE_LOW = 1;
1169 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1170 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001171
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001172 static final String[] BLUETOOTH_STATE_NAMES = {
1173 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001174 };
1175
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001176 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001177
1178 /**
1179 * Returns the time in microseconds that Bluetooth has been running in the
1180 * given active state.
1181 *
1182 * {@hide}
1183 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001184 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001185 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001186
1187 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001188 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001189 *
1190 * {@hide}
1191 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001192 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001193
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001194 public static final int NETWORK_MOBILE_RX_DATA = 0;
1195 public static final int NETWORK_MOBILE_TX_DATA = 1;
1196 public static final int NETWORK_WIFI_RX_DATA = 2;
1197 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001198
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001199 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001200
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001201 public abstract long getNetworkActivityBytes(int type, int which);
1202 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001203
The Android Open Source Project10592532009-03-18 17:39:46 -07001204 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001205 * Return the wall clock time when battery stats data collection started.
1206 */
1207 public abstract long getStartClockTime();
1208
1209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 * Return whether we are currently running on battery.
1211 */
1212 public abstract boolean getIsOnBattery();
1213
1214 /**
1215 * Returns a SparseArray containing the statistics for each uid.
1216 */
1217 public abstract SparseArray<? extends Uid> getUidStats();
1218
1219 /**
1220 * Returns the current battery uptime in microseconds.
1221 *
1222 * @param curTime the amount of elapsed realtime in microseconds.
1223 */
1224 public abstract long getBatteryUptime(long curTime);
1225
1226 /**
1227 * Returns the current battery realtime in microseconds.
1228 *
1229 * @param curTime the amount of elapsed realtime in microseconds.
1230 */
1231 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001232
1233 /**
Evan Millar633a1742009-04-02 16:36:33 -07001234 * Returns the battery percentage level at the last time the device was unplugged from power, or
1235 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001236 */
Evan Millar633a1742009-04-02 16:36:33 -07001237 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001238
1239 /**
Evan Millar633a1742009-04-02 16:36:33 -07001240 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1241 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001242 */
Evan Millar633a1742009-04-02 16:36:33 -07001243 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244
1245 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001246 * Get the amount the battery has discharged since the stats were
1247 * last reset after charging, as a lower-end approximation.
1248 */
1249 public abstract int getLowDischargeAmountSinceCharge();
1250
1251 /**
1252 * Get the amount the battery has discharged since the stats were
1253 * last reset after charging, as an upper-end approximation.
1254 */
1255 public abstract int getHighDischargeAmountSinceCharge();
1256
1257 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001258 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1259 */
1260 public abstract int getDischargeAmount(int which);
1261
1262 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001263 * Get the amount the battery has discharged while the screen was on,
1264 * since the last time power was unplugged.
1265 */
1266 public abstract int getDischargeAmountScreenOn();
1267
1268 /**
1269 * Get the amount the battery has discharged while the screen was on,
1270 * since the last time the device was charged.
1271 */
1272 public abstract int getDischargeAmountScreenOnSinceCharge();
1273
1274 /**
1275 * Get the amount the battery has discharged while the screen was off,
1276 * since the last time power was unplugged.
1277 */
1278 public abstract int getDischargeAmountScreenOff();
1279
1280 /**
1281 * Get the amount the battery has discharged while the screen was off,
1282 * since the last time the device was charged.
1283 */
1284 public abstract int getDischargeAmountScreenOffSinceCharge();
1285
1286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 * Returns the total, last, or current battery uptime in microseconds.
1288 *
1289 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001290 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 */
1292 public abstract long computeBatteryUptime(long curTime, int which);
1293
1294 /**
1295 * Returns the total, last, or current battery realtime in microseconds.
1296 *
1297 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001298 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 */
1300 public abstract long computeBatteryRealtime(long curTime, int which);
1301
1302 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001303 * Returns the total, last, or current battery screen off uptime in microseconds.
1304 *
1305 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001306 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001307 */
1308 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1309
1310 /**
1311 * Returns the total, last, or current battery screen off realtime in microseconds.
1312 *
1313 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001314 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001315 */
1316 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1317
1318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 * Returns the total, last, or current uptime in microseconds.
1320 *
1321 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001322 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 */
1324 public abstract long computeUptime(long curTime, int which);
1325
1326 /**
1327 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001330 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 */
1332 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001333
1334 /**
1335 * Compute an approximation for how much run time (in microseconds) is remaining on
1336 * the battery. Returns -1 if no time can be computed: either there is not
1337 * enough current data to make a decision, or the battery is currently
1338 * charging.
1339 *
1340 * @param curTime The current elepsed realtime in microseconds.
1341 */
1342 public abstract long computeBatteryTimeRemaining(long curTime);
1343
1344 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001345 * Return the historical number of discharge steps we currently have.
1346 */
1347 public abstract int getNumDischargeStepDurations();
1348
1349 /**
1350 * Return the array of discharge step durations; the number of valid
1351 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1352 * These values are in milliseconds.
1353 */
1354 public abstract long[] getDischargeStepDurationsArray();
1355
1356 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001357 * Compute an approximation for how much time (in microseconds) remains until the battery
1358 * is fully charged. Returns -1 if no time can be computed: either there is not
1359 * enough current data to make a decision, or the battery is currently
1360 * discharging.
1361 *
1362 * @param curTime The current elepsed realtime in microseconds.
1363 */
1364 public abstract long computeChargeTimeRemaining(long curTime);
1365
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001366 /**
1367 * Return the historical number of charge steps we currently have.
1368 */
1369 public abstract int getNumChargeStepDurations();
1370
1371 /**
1372 * Return the array of charge step durations; the number of valid
1373 * items in it is returned by {@link #getNumChargeStepDurations()}.
1374 * These values are in milliseconds.
1375 */
1376 public abstract long[] getChargeStepDurationsArray();
1377
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001378 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1379
Evan Millarc64edde2009-04-18 12:26:32 -07001380 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
Amith Yamasanie43530a2009-08-21 13:11:37 -07001382 /** Returns the number of different speeds that the CPU can run at */
1383 public abstract int getCpuSpeedSteps();
1384
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001385 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1386
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001387 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 long days = seconds / (60 * 60 * 24);
1389 if (days != 0) {
1390 out.append(days);
1391 out.append("d ");
1392 }
1393 long used = days * 60 * 60 * 24;
1394
1395 long hours = (seconds - used) / (60 * 60);
1396 if (hours != 0 || used != 0) {
1397 out.append(hours);
1398 out.append("h ");
1399 }
1400 used += hours * 60 * 60;
1401
1402 long mins = (seconds-used) / 60;
1403 if (mins != 0 || used != 0) {
1404 out.append(mins);
1405 out.append("m ");
1406 }
1407 used += mins * 60;
1408
1409 if (seconds != 0 || used != 0) {
1410 out.append(seconds-used);
1411 out.append("s ");
1412 }
1413 }
1414
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001415 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001417 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 sb.append((time - (sec * 100)) * 10);
1419 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001422 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001424 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 sb.append(time - (sec * 1000));
1426 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001429 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001430 long sec = time / 1000;
1431 formatTimeRaw(sb, sec);
1432 sb.append(time - (sec * 1000));
1433 sb.append("ms");
1434 }
1435
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001436 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001438 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 float perc = ((float)num) / ((float)den) * 100;
1441 mFormatBuilder.setLength(0);
1442 mFormatter.format("%.1f%%", perc);
1443 return mFormatBuilder.toString();
1444 }
1445
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001446 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001447 mFormatBuilder.setLength(0);
1448
1449 if (bytes < BYTES_PER_KB) {
1450 return bytes + "B";
1451 } else if (bytes < BYTES_PER_MB) {
1452 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1453 return mFormatBuilder.toString();
1454 } else if (bytes < BYTES_PER_GB){
1455 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1456 return mFormatBuilder.toString();
1457 } else {
1458 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1459 return mFormatBuilder.toString();
1460 }
1461 }
1462
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001463 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001464 if (timer != null) {
1465 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001466 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001467 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1468 return totalTimeMillis;
1469 }
1470 return 0;
1471 }
1472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 /**
1474 *
1475 * @param sb a StringBuilder object.
1476 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001477 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001479 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * @param linePrefix a String to be prepended to each line of output.
1481 * @return the line prefix
1482 */
1483 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001484 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
1486 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001487 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488
Evan Millarc64edde2009-04-18 12:26:32 -07001489 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 if (totalTimeMillis != 0) {
1491 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001492 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001493 if (name != null) {
1494 sb.append(name);
1495 sb.append(' ');
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 sb.append('(');
1498 sb.append(count);
1499 sb.append(" times)");
1500 return ", ";
1501 }
1502 }
1503 return linePrefix;
1504 }
1505
1506 /**
1507 * Checkin version of wakelock printer. Prints simple comma-separated list.
1508 *
1509 * @param sb a StringBuilder object.
1510 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001511 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001513 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * @param linePrefix a String to be prepended to each line of output.
1515 * @return the line prefix
1516 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001517 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1518 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 long totalTimeMicros = 0;
1520 int count = 0;
1521 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001522 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001523 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 sb.append(linePrefix);
1526 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1527 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001528 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 sb.append(count);
1530 return ",";
1531 }
1532
1533 /**
1534 * Dump a comma-separated line of values for terse checkin mode.
1535 *
1536 * @param pw the PageWriter to dump log to
1537 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1538 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1539 * @param args type-dependent data arguments
1540 */
1541 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1542 Object... args ) {
1543 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1544 pw.print(uid); pw.print(',');
1545 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001546 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547
1548 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001549 pw.print(',');
1550 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001552 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554
1555 /**
1556 * Checkin server version of dump to produce more compact, computer-readable log.
1557 *
1558 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001560 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1562 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1563 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1565 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001566 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1567 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1568 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 final long totalRealtime = computeRealtime(rawRealtime, which);
1570 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001571 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1572 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1573 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1574 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1575 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576
1577 StringBuilder sb = new StringBuilder(128);
1578
Evan Millar22ac0432009-03-31 11:33:18 -07001579 SparseArray<? extends Uid> uidStats = getUidStats();
1580 final int NU = uidStats.size();
1581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 // Dump "battery" stat
1585 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001586 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001587 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001588 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001589 getStartClockTime(),
1590 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001592 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001593 long fullWakeLockTimeTotal = 0;
1594 long partialWakeLockTimeTotal = 0;
1595
1596 for (int iu = 0; iu < NU; iu++) {
1597 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001598
Evan Millar22ac0432009-03-31 11:33:18 -07001599 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1600 if (wakelocks.size() > 0) {
1601 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1602 : wakelocks.entrySet()) {
1603 Uid.Wakelock wl = ent.getValue();
1604
1605 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1606 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001607 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1608 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001609 }
1610
1611 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1612 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001613 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001614 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001615 }
1616 }
1617 }
1618 }
1619
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001620 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1621 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1622 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1623 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1624 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1625 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1626 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1627 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1628
1629 // Dump network stats
1630 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1631 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1632 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 // Dump misc stats
1635 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001636 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001637 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001638 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001639 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001640 getInputEventCount(which), getMobileRadioActiveTime(rawRealtime, which),
1641 getMobileRadioActiveAdjustedTime(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001642
1643 // Dump screen brightness stats
1644 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1645 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001646 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001647 }
1648 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001649
Dianne Hackborn627bba72009-03-24 22:32:56 -07001650 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001651 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1652 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001653 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001654 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001655 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001656 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001657 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001658 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001659 args[i] = getPhoneSignalStrengthCount(i, which);
1660 }
1661 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001662
Dianne Hackborn627bba72009-03-24 22:32:56 -07001663 // Dump network type stats
1664 args = new Object[NUM_DATA_CONNECTION_TYPES];
1665 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001666 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001667 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001668 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1669 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1670 args[i] = getPhoneDataConnectionCount(i, which);
1671 }
1672 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001673
1674 // Dump wifi state stats
1675 args = new Object[NUM_WIFI_STATES];
1676 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001677 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001678 }
1679 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1680 for (int i=0; i<NUM_WIFI_STATES; i++) {
1681 args[i] = getWifiStateCount(i, which);
1682 }
1683 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1684
1685 // Dump bluetooth state stats
1686 args = new Object[NUM_BLUETOOTH_STATES];
1687 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001688 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001689 }
1690 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1691 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1692 args[i] = getBluetoothStateCount(i, which);
1693 }
1694 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1695
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001696 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001697 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001698 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001701 if (which == STATS_SINCE_UNPLUGGED) {
1702 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1703 getDischargeStartLevel()-getDischargeCurrentLevel(),
1704 getDischargeStartLevel()-getDischargeCurrentLevel(),
1705 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1706 } else {
1707 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1708 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1709 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1710 }
1711
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001712 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001713 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001714 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001715 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001716 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001717 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001718 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001719 sb.toString());
1720 }
Evan Millarc64edde2009-04-18 12:26:32 -07001721 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001722 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1723 if (wakeupReasons.size() > 0) {
1724 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1725 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1726 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1727 }
1728 }
Evan Millarc64edde2009-04-18 12:26:32 -07001729 }
1730
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001731 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001732 helper.create(this);
1733 helper.refreshStats(which, UserHandle.USER_ALL);
1734 List<BatterySipper> sippers = helper.getUsageList();
1735 if (sippers != null && sippers.size() > 0) {
1736 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1737 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001738 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001739 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1740 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1741 for (int i=0; i<sippers.size(); i++) {
1742 BatterySipper bs = sippers.get(i);
1743 int uid = 0;
1744 String label;
1745 switch (bs.drainType) {
1746 case IDLE:
1747 label="idle";
1748 break;
1749 case CELL:
1750 label="cell";
1751 break;
1752 case PHONE:
1753 label="phone";
1754 break;
1755 case WIFI:
1756 label="wifi";
1757 break;
1758 case BLUETOOTH:
1759 label="blue";
1760 break;
1761 case SCREEN:
1762 label="scrn";
1763 break;
1764 case APP:
1765 uid = bs.uidObj.getUid();
1766 label = "uid";
1767 break;
1768 case USER:
1769 uid = UserHandle.getUid(bs.userId, 0);
1770 label = "user";
1771 break;
1772 case UNACCOUNTED:
1773 label = "unacc";
1774 break;
1775 case OVERCOUNTED:
1776 label = "over";
1777 break;
1778 default:
1779 label = "???";
1780 }
1781 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1782 BatteryStatsHelper.makemAh(bs.value));
1783 }
1784 }
1785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 for (int iu = 0; iu < NU; iu++) {
1787 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001788 if (reqUid >= 0 && uid != reqUid) {
1789 continue;
1790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 Uid u = uidStats.valueAt(iu);
1792 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001793 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1794 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1795 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1796 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1797 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1798 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001799 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1800 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001801 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1802 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001803 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1804 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1805 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001806
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001807 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1808 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001809 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1811 wifiBytesRx, wifiBytesTx,
1812 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001813 wifiPacketsRx, wifiPacketsTx,
1814 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001815 }
1816
Nick Pelly6ccaa542012-06-15 15:22:47 -07001817 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001818 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001819 dumpLine(pw, uid, category, WIFI_DATA,
1820 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822
Dianne Hackborn617f8772009-03-31 15:04:46 -07001823 if (u.hasUserActivity()) {
1824 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1825 boolean hasData = false;
1826 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1827 int val = u.getUserActivityCount(i, which);
1828 args[i] = val;
1829 if (val != 0) hasData = true;
1830 }
1831 if (hasData) {
1832 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1833 }
1834 }
1835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1837 if (wakelocks.size() > 0) {
1838 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1839 : wakelocks.entrySet()) {
1840 Uid.Wakelock wl = ent.getValue();
1841 String linePrefix = "";
1842 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001843 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001844 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001845 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001846 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001847 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001848 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849
1850 // Only log if we had at lease one wakelock...
1851 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001852 String name = ent.getKey();
1853 if (name.indexOf(',') >= 0) {
1854 name = name.replace(',', '_');
1855 }
1856 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
1858 }
1859 }
1860
1861 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1862 if (sensors.size() > 0) {
1863 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1864 : sensors.entrySet()) {
1865 Uid.Sensor se = ent.getValue();
1866 int sensorNumber = ent.getKey();
1867 Timer timer = se.getSensorTime();
1868 if (timer != null) {
1869 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001870 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001871 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 if (totalTime != 0) {
1873 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1874 }
1875 }
1876 }
1877 }
1878
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001879 Timer vibTimer = u.getVibratorOnTimer();
1880 if (vibTimer != null) {
1881 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001882 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001883 int count = vibTimer.getCountLocked(which);
1884 if (totalTime != 0) {
1885 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1886 }
1887 }
1888
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001889 Timer fgTimer = u.getForegroundActivityTimer();
1890 if (fgTimer != null) {
1891 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001892 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001893 int count = fgTimer.getCountLocked(which);
1894 if (totalTime != 0) {
1895 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1896 }
1897 }
1898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1900 if (processStats.size() > 0) {
1901 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1902 : processStats.entrySet()) {
1903 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001904
1905 final long userMillis = ps.getUserTime(which) * 10;
1906 final long systemMillis = ps.getSystemTime(which) * 10;
1907 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1908 final long starts = ps.getStarts(which);
1909
1910 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1911 || starts != 0) {
1912 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1913 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
1915 }
1916 }
1917
1918 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1919 if (packageStats.size() > 0) {
1920 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1921 : packageStats.entrySet()) {
1922
1923 Uid.Pkg ps = ent.getValue();
1924 int wakeups = ps.getWakeups(which);
1925 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1926 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1927 : serviceStats.entrySet()) {
1928 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1929 long startTime = ss.getStartTime(batteryUptime, which);
1930 int starts = ss.getStarts(which);
1931 int launches = ss.getLaunches(which);
1932 if (startTime != 0 || starts != 0 || launches != 0) {
1933 dumpLine(pw, uid, category, APK_DATA,
1934 wakeups, // wakeup alarms
1935 ent.getKey(), // Apk
1936 sent.getKey(), // service
1937 startTime / 1000, // time spent started, in ms
1938 starts,
1939 launches);
1940 }
1941 }
1942 }
1943 }
1944 }
1945 }
1946
Dianne Hackborn81038902012-11-26 17:04:09 -08001947 static final class TimerEntry {
1948 final String mName;
1949 final int mId;
1950 final BatteryStats.Timer mTimer;
1951 final long mTime;
1952 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1953 mName = name;
1954 mId = id;
1955 mTimer = timer;
1956 mTime = time;
1957 }
1958 }
1959
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001960 private void printmAh(PrintWriter printer, double power) {
1961 printer.print(BatteryStatsHelper.makemAh(power));
1962 }
1963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001965 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1966 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1968 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1969 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970
1971 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1972 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1973 final long totalRealtime = computeRealtime(rawRealtime, which);
1974 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001975 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1976 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1977 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001978 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
1979 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001982
1983 SparseArray<? extends Uid> uidStats = getUidStats();
1984 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001986 sb.setLength(0);
1987 sb.append(prefix);
1988 sb.append(" Time on battery: ");
1989 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1990 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1991 sb.append(") realtime, ");
1992 formatTimeMs(sb, whichBatteryUptime / 1000);
1993 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1994 sb.append(") uptime");
1995 pw.println(sb.toString());
1996 sb.setLength(0);
1997 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001998 sb.append(" Time on battery screen off: ");
1999 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2000 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2001 sb.append(") realtime, ");
2002 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2003 sb.append("(");
2004 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2005 sb.append(") uptime");
2006 pw.println(sb.toString());
2007 sb.setLength(0);
2008 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002009 sb.append(" Total run time: ");
2010 formatTimeMs(sb, totalRealtime / 1000);
2011 sb.append("realtime, ");
2012 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002013 sb.append("uptime");
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002014 if (batteryTimeRemaining >= 0) {
2015 sb.setLength(0);
2016 sb.append(prefix);
2017 sb.append(" Battery time remaining: ");
2018 formatTimeMs(sb, batteryTimeRemaining / 1000);
2019 pw.println(sb.toString());
2020 }
2021 if (chargeTimeRemaining >= 0) {
2022 sb.setLength(0);
2023 sb.append(prefix);
2024 sb.append(" Charge time remaining: ");
2025 formatTimeMs(sb, chargeTimeRemaining / 1000);
2026 pw.println(sb.toString());
2027 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002028 pw.print(" Start clock time: ");
2029 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2030
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002031 final long screenOnTime = getScreenOnTime(rawRealtime, which);
2032 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2033 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2034 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2035 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002036 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002037 sb.append(prefix);
2038 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2039 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002040 sb.append(") "); sb.append(getScreenOnCount(which));
2041 sb.append("x, Input events: "); sb.append(getInputEventCount(which));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002042 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002043 if (phoneOnTime != 0) {
2044 sb.setLength(0);
2045 sb.append(prefix);
2046 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2047 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2048 sb.append(") "); sb.append(getPhoneOnCount(which));
2049 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002050 sb.setLength(0);
2051 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002052 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002053 boolean didOne = false;
2054 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002055 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002056 if (time == 0) {
2057 continue;
2058 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002059 sb.append("\n ");
2060 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002061 didOne = true;
2062 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2063 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002064 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002065 sb.append("(");
2066 sb.append(formatRatioLocked(time, screenOnTime));
2067 sb.append(")");
2068 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002069 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002070 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002071
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002072 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002073 long fullWakeLockTimeTotalMicros = 0;
2074 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002075
Dianne Hackborn81038902012-11-26 17:04:09 -08002076 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2077
Evan Millar22ac0432009-03-31 11:33:18 -07002078 for (int iu = 0; iu < NU; iu++) {
2079 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002080
Evan Millar22ac0432009-03-31 11:33:18 -07002081 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2082 if (wakelocks.size() > 0) {
2083 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2084 : wakelocks.entrySet()) {
2085 Uid.Wakelock wl = ent.getValue();
2086
2087 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2088 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002089 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002090 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002091 }
2092
2093 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2094 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002095 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002096 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002097 if (totalTimeMicros > 0) {
2098 if (reqUid < 0) {
2099 // Only show the ordered list of all wake
2100 // locks if the caller is not asking for data
2101 // about a specific uid.
2102 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2103 partialWakeTimer, totalTimeMicros));
2104 }
2105 partialWakeLockTimeTotalMicros += totalTimeMicros;
2106 }
Evan Millar22ac0432009-03-31 11:33:18 -07002107 }
2108 }
2109 }
2110 }
2111
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002112 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2113 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2114 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2115 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2116 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2117 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2118 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2119 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2120
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002121 if (fullWakeLockTimeTotalMicros != 0) {
2122 sb.setLength(0);
2123 sb.append(prefix);
2124 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2125 (fullWakeLockTimeTotalMicros + 500) / 1000);
2126 pw.println(sb.toString());
2127 }
2128
2129 if (partialWakeLockTimeTotalMicros != 0) {
2130 sb.setLength(0);
2131 sb.append(prefix);
2132 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2133 (partialWakeLockTimeTotalMicros + 500) / 1000);
2134 pw.println(sb.toString());
2135 }
2136
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002137 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002138 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2139 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2140 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2141 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002142 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002143 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002144 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002145 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002146 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002147 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002148 if (time == 0) {
2149 continue;
2150 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002151 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002152 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002153 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002154 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002155 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002156 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002157 sb.append("(");
2158 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002159 sb.append(") ");
2160 sb.append(getPhoneSignalStrengthCount(i, which));
2161 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002162 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002163 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002164 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002165
2166 sb.setLength(0);
2167 sb.append(prefix);
2168 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002169 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002170 pw.println(sb.toString());
2171
Dianne Hackborn627bba72009-03-24 22:32:56 -07002172 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002173 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002174 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002175 didOne = false;
2176 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002177 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002178 if (time == 0) {
2179 continue;
2180 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002181 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002182 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002183 didOne = true;
2184 sb.append(DATA_CONNECTION_NAMES[i]);
2185 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002186 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002187 sb.append("(");
2188 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002189 sb.append(") ");
2190 sb.append(getPhoneDataConnectionCount(i, which));
2191 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002192 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002193 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002194 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002195
2196 sb.setLength(0);
2197 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002198 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002199 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002200 formatTimeMs(sb, mobileActiveTime / 1000);
2201 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2202 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2203 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002204 pw.println(sb.toString());
2205
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002206 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2207 if (mobileActiveUnknownTime != 0) {
2208 sb.setLength(0);
2209 sb.append(prefix);
2210 sb.append(" Mobile radio active unknown time: ");
2211 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2212 sb.append("(");
2213 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2214 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2215 sb.append("x");
2216 pw.println(sb.toString());
2217 }
2218
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002219 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2220 if (mobileActiveAdjustedTime != 0) {
2221 sb.setLength(0);
2222 sb.append(prefix);
2223 sb.append(" Mobile radio active adjusted time: ");
2224 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2225 sb.append("(");
2226 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2227 sb.append(")");
2228 pw.println(sb.toString());
2229 }
2230
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002231 pw.print(prefix);
2232 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2233 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2234 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2235 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002236 sb.setLength(0);
2237 sb.append(prefix);
2238 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2239 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2240 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2241 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002242 sb.append(")");
2243 pw.println(sb.toString());
2244
2245 sb.setLength(0);
2246 sb.append(prefix);
2247 sb.append(" Wifi states:");
2248 didOne = false;
2249 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002250 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002251 if (time == 0) {
2252 continue;
2253 }
2254 sb.append("\n ");
2255 didOne = true;
2256 sb.append(WIFI_STATE_NAMES[i]);
2257 sb.append(" ");
2258 formatTimeMs(sb, time/1000);
2259 sb.append("(");
2260 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2261 sb.append(") ");
2262 sb.append(getPhoneDataConnectionCount(i, which));
2263 sb.append("x");
2264 }
2265 if (!didOne) sb.append(" (no activity)");
2266 pw.println(sb.toString());
2267
2268 sb.setLength(0);
2269 sb.append(prefix);
2270 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002271 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2272 sb.append(")");
2273 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002274
2275 sb.setLength(0);
2276 sb.append(prefix);
2277 sb.append(" Bluetooth states:");
2278 didOne = false;
2279 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002280 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002281 if (time == 0) {
2282 continue;
2283 }
2284 sb.append("\n ");
2285 didOne = true;
2286 sb.append(BLUETOOTH_STATE_NAMES[i]);
2287 sb.append(" ");
2288 formatTimeMs(sb, time/1000);
2289 sb.append("(");
2290 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2291 sb.append(") ");
2292 sb.append(getPhoneDataConnectionCount(i, which));
2293 sb.append("x");
2294 }
2295 if (!didOne) sb.append(" (no activity)");
2296 pw.println(sb.toString());
2297
2298 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002299
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002300 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002301 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002302 pw.print(prefix); pw.println(" Device is currently unplugged");
2303 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2304 pw.println(getDischargeStartLevel());
2305 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2306 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002307 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002308 pw.print(prefix); pw.println(" Device is currently plugged into power");
2309 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2310 pw.println(getDischargeStartLevel());
2311 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2312 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002313 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002314 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2315 pw.println(getDischargeAmountScreenOn());
2316 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2317 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002318 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002319 } else {
2320 pw.print(prefix); pw.println(" Device battery use since last full charge");
2321 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2322 pw.println(getLowDischargeAmountSinceCharge());
2323 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2324 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002325 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2326 pw.println(getDischargeAmountScreenOnSinceCharge());
2327 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2328 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002329 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002330 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002331
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002332 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002333 helper.create(this);
2334 helper.refreshStats(which, UserHandle.USER_ALL);
2335 List<BatterySipper> sippers = helper.getUsageList();
2336 if (sippers != null && sippers.size() > 0) {
2337 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2338 pw.print(prefix); pw.print(" Capacity: ");
2339 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002340 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002341 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2342 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2343 pw.println();
2344 for (int i=0; i<sippers.size(); i++) {
2345 BatterySipper bs = sippers.get(i);
2346 switch (bs.drainType) {
2347 case IDLE:
2348 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2349 pw.println();
2350 break;
2351 case CELL:
2352 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2353 pw.println();
2354 break;
2355 case PHONE:
2356 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2357 pw.println();
2358 break;
2359 case WIFI:
2360 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2361 pw.println();
2362 break;
2363 case BLUETOOTH:
2364 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2365 pw.println();
2366 break;
2367 case SCREEN:
2368 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2369 pw.println();
2370 break;
2371 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002372 pw.print(prefix); pw.print(" Uid ");
2373 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002374 pw.print(": "); printmAh(pw, bs.value); pw.println();
2375 break;
2376 case USER:
2377 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2378 pw.print(": "); printmAh(pw, bs.value); pw.println();
2379 break;
2380 case UNACCOUNTED:
2381 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2382 pw.println();
2383 break;
2384 case OVERCOUNTED:
2385 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2386 pw.println();
2387 break;
2388 }
2389 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002390 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002391 }
2392
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002393 sippers = helper.getMobilemsppList();
2394 if (sippers != null && sippers.size() > 0) {
2395 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002396 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002397 for (int i=0; i<sippers.size(); i++) {
2398 BatterySipper bs = sippers.get(i);
2399 sb.setLength(0);
2400 sb.append(prefix); sb.append(" Uid ");
2401 UserHandle.formatUid(sb, bs.uidObj.getUid());
2402 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2403 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2404 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002405 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002406 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002407 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002408 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002409 sb.setLength(0);
2410 sb.append(prefix);
2411 sb.append(" TOTAL TIME: ");
2412 formatTimeMs(sb, totalTime);
2413 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2414 sb.append(")");
2415 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002416 pw.println();
2417 }
2418
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002419 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2420 @Override
2421 public int compare(TimerEntry lhs, TimerEntry rhs) {
2422 long lhsTime = lhs.mTime;
2423 long rhsTime = rhs.mTime;
2424 if (lhsTime < rhsTime) {
2425 return 1;
2426 }
2427 if (lhsTime > rhsTime) {
2428 return -1;
2429 }
2430 return 0;
2431 }
2432 };
2433
2434 if (reqUid < 0) {
2435 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2436 if (kernelWakelocks.size() > 0) {
2437 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2438 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2439 BatteryStats.Timer timer = ent.getValue();
2440 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2441 if (totalTimeMillis > 0) {
2442 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2443 }
2444 }
2445 if (ktimers.size() > 0) {
2446 Collections.sort(ktimers, timerComparator);
2447 pw.print(prefix); pw.println(" All kernel wake locks:");
2448 for (int i=0; i<ktimers.size(); i++) {
2449 TimerEntry timer = ktimers.get(i);
2450 String linePrefix = ": ";
2451 sb.setLength(0);
2452 sb.append(prefix);
2453 sb.append(" Kernel Wake lock ");
2454 sb.append(timer.mName);
2455 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2456 which, linePrefix);
2457 if (!linePrefix.equals(": ")) {
2458 sb.append(" realtime");
2459 // Only print out wake locks that were held
2460 pw.println(sb.toString());
2461 }
2462 }
2463 pw.println();
2464 }
2465 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002466
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002467 if (timers.size() > 0) {
2468 Collections.sort(timers, timerComparator);
2469 pw.print(prefix); pw.println(" All partial wake locks:");
2470 for (int i=0; i<timers.size(); i++) {
2471 TimerEntry timer = timers.get(i);
2472 sb.setLength(0);
2473 sb.append(" Wake lock ");
2474 UserHandle.formatUid(sb, timer.mId);
2475 sb.append(" ");
2476 sb.append(timer.mName);
2477 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2478 sb.append(" realtime");
2479 pw.println(sb.toString());
2480 }
2481 timers.clear();
2482 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002483 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002484
2485 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2486 if (wakeupReasons.size() > 0) {
2487 pw.print(prefix); pw.println(" All wakeup reasons:");
2488 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2489 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2490 BatteryStats.LongCounter counter = ent.getValue();
2491 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2492 ent.getValue().getCountLocked(which)));
2493 }
2494 Collections.sort(reasons, timerComparator);
2495 for (int i=0; i<reasons.size(); i++) {
2496 TimerEntry timer = reasons.get(i);
2497 String linePrefix = ": ";
2498 sb.setLength(0);
2499 sb.append(prefix);
2500 sb.append(" Wakeup reason ");
2501 sb.append(timer.mName);
2502 sb.append(": ");
2503 formatTimeMs(sb, timer.mTime);
2504 sb.append("realtime");
2505 pw.println(sb.toString());
2506 }
2507 pw.println();
2508 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002509 }
Evan Millar22ac0432009-03-31 11:33:18 -07002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 for (int iu=0; iu<NU; iu++) {
2512 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002513 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002514 continue;
2515 }
2516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002518
2519 pw.print(prefix);
2520 pw.print(" ");
2521 UserHandle.formatUid(pw, uid);
2522 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002524
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002525 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2526 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2527 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2528 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2529 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2530 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002531 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2532 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002533 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2534 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002535 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2536 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2537 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002538
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002539 if (mobileRxBytes > 0 || mobileTxBytes > 0
2540 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002541 pw.print(prefix); pw.print(" Mobile network: ");
2542 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002543 pw.print(formatBytesLocked(mobileTxBytes));
2544 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2545 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002547 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2548 sb.setLength(0);
2549 sb.append(prefix); sb.append(" Mobile radio active: ");
2550 formatTimeMs(sb, uidMobileActiveTime / 1000);
2551 sb.append("(");
2552 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2553 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2554 long packets = mobileRxPackets + mobileTxPackets;
2555 if (packets == 0) {
2556 packets = 1;
2557 }
2558 sb.append(" @ ");
2559 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2560 sb.append(" mspp");
2561 pw.println(sb.toString());
2562 }
2563
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002564 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002565 pw.print(prefix); pw.print(" Wi-Fi network: ");
2566 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002567 pw.print(formatBytesLocked(wifiTxBytes));
2568 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2569 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002570 }
2571
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002572 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2573 || uidWifiRunningTime != 0) {
2574 sb.setLength(0);
2575 sb.append(prefix); sb.append(" Wifi Running: ");
2576 formatTimeMs(sb, uidWifiRunningTime / 1000);
2577 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2578 whichBatteryRealtime)); sb.append(")\n");
2579 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2580 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2581 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2582 whichBatteryRealtime)); sb.append(")\n");
2583 sb.append(prefix); sb.append(" Wifi Scan: ");
2584 formatTimeMs(sb, wifiScanTime / 1000);
2585 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2586 whichBatteryRealtime)); sb.append(")");
2587 pw.println(sb.toString());
2588 }
2589
Dianne Hackborn617f8772009-03-31 15:04:46 -07002590 if (u.hasUserActivity()) {
2591 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002592 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002593 int val = u.getUserActivityCount(i, which);
2594 if (val != 0) {
2595 if (!hasData) {
2596 sb.setLength(0);
2597 sb.append(" User activity: ");
2598 hasData = true;
2599 } else {
2600 sb.append(", ");
2601 }
2602 sb.append(val);
2603 sb.append(" ");
2604 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2605 }
2606 }
2607 if (hasData) {
2608 pw.println(sb.toString());
2609 }
2610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611
2612 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2613 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002614 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2615 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2617 : wakelocks.entrySet()) {
2618 Uid.Wakelock wl = ent.getValue();
2619 String linePrefix = ": ";
2620 sb.setLength(0);
2621 sb.append(prefix);
2622 sb.append(" Wake lock ");
2623 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002624 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002626 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002628 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 "window", which, linePrefix);
2630 if (!linePrefix.equals(": ")) {
2631 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002632 // Only print out wake locks that were held
2633 pw.println(sb.toString());
2634 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002635 count++;
2636 }
2637 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002638 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002639 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002640 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002641 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002643 }
2644 if (count > 1) {
2645 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2646 sb.setLength(0);
2647 sb.append(prefix);
2648 sb.append(" TOTAL wake: ");
2649 boolean needComma = false;
2650 if (totalFull != 0) {
2651 needComma = true;
2652 formatTimeMs(sb, totalFull);
2653 sb.append("full");
2654 }
2655 if (totalPartial != 0) {
2656 if (needComma) {
2657 sb.append(", ");
2658 }
2659 needComma = true;
2660 formatTimeMs(sb, totalPartial);
2661 sb.append("partial");
2662 }
2663 if (totalWindow != 0) {
2664 if (needComma) {
2665 sb.append(", ");
2666 }
2667 needComma = true;
2668 formatTimeMs(sb, totalWindow);
2669 sb.append("window");
2670 }
2671 sb.append(" realtime");
2672 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 }
2675 }
2676
2677 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2678 if (sensors.size() > 0) {
2679 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2680 : sensors.entrySet()) {
2681 Uid.Sensor se = ent.getValue();
2682 int sensorNumber = ent.getKey();
2683 sb.setLength(0);
2684 sb.append(prefix);
2685 sb.append(" Sensor ");
2686 int handle = se.getHandle();
2687 if (handle == Uid.Sensor.GPS) {
2688 sb.append("GPS");
2689 } else {
2690 sb.append(handle);
2691 }
2692 sb.append(": ");
2693
2694 Timer timer = se.getSensorTime();
2695 if (timer != null) {
2696 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002697 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002698 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002699 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 //timer.logState();
2701 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002702 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 sb.append("realtime (");
2704 sb.append(count);
2705 sb.append(" times)");
2706 } else {
2707 sb.append("(not used)");
2708 }
2709 } else {
2710 sb.append("(not used)");
2711 }
2712
2713 pw.println(sb.toString());
2714 uidActivity = true;
2715 }
2716 }
2717
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002718 Timer vibTimer = u.getVibratorOnTimer();
2719 if (vibTimer != null) {
2720 // Convert from microseconds to milliseconds with rounding
2721 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002722 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002723 int count = vibTimer.getCountLocked(which);
2724 //timer.logState();
2725 if (totalTime != 0) {
2726 sb.setLength(0);
2727 sb.append(prefix);
2728 sb.append(" Vibrator: ");
2729 formatTimeMs(sb, totalTime);
2730 sb.append("realtime (");
2731 sb.append(count);
2732 sb.append(" times)");
2733 pw.println(sb.toString());
2734 uidActivity = true;
2735 }
2736 }
2737
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002738 Timer fgTimer = u.getForegroundActivityTimer();
2739 if (fgTimer != null) {
2740 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002741 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002742 int count = fgTimer.getCountLocked(which);
2743 if (totalTime != 0) {
2744 sb.setLength(0);
2745 sb.append(prefix);
2746 sb.append(" Foreground activities: ");
2747 formatTimeMs(sb, totalTime);
2748 sb.append("realtime (");
2749 sb.append(count);
2750 sb.append(" times)");
2751 pw.println(sb.toString());
2752 uidActivity = true;
2753 }
2754 }
2755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2757 if (processStats.size() > 0) {
2758 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2759 : processStats.entrySet()) {
2760 Uid.Proc ps = ent.getValue();
2761 long userTime;
2762 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002763 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002765 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766
2767 userTime = ps.getUserTime(which);
2768 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002769 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002771 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002772 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002774 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002775 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002776 sb.setLength(0);
2777 sb.append(prefix); sb.append(" Proc ");
2778 sb.append(ent.getKey()); sb.append(":\n");
2779 sb.append(prefix); sb.append(" CPU: ");
2780 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002781 formatTime(sb, systemTime); sb.append("krn ; ");
2782 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002783 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002784 sb.append("\n"); sb.append(prefix); sb.append(" ");
2785 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002786 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002787 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002788 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002789 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002790 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002791 pw.print(prefix); pw.print(" * Killed for ");
2792 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2793 pw.print("wake lock");
2794 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2795 pw.print("cpu");
2796 } else {
2797 pw.print("unknown");
2798 }
2799 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002800 TimeUtils.formatDuration(ew.usedTime, pw);
2801 pw.print(" over ");
2802 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002803 if (ew.overTime != 0) {
2804 pw.print(" (");
2805 pw.print((ew.usedTime*100)/ew.overTime);
2806 pw.println("%)");
2807 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002808 }
2809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 uidActivity = true;
2811 }
2812 }
2813 }
2814
2815 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2816 if (packageStats.size() > 0) {
2817 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2818 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002819 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 boolean apkActivity = false;
2821 Uid.Pkg ps = ent.getValue();
2822 int wakeups = ps.getWakeups(which);
2823 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002824 pw.print(prefix); pw.print(" ");
2825 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 apkActivity = true;
2827 }
2828 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2829 if (serviceStats.size() > 0) {
2830 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2831 : serviceStats.entrySet()) {
2832 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2833 long startTime = ss.getStartTime(batteryUptime, which);
2834 int starts = ss.getStarts(which);
2835 int launches = ss.getLaunches(which);
2836 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002837 sb.setLength(0);
2838 sb.append(prefix); sb.append(" Service ");
2839 sb.append(sent.getKey()); sb.append(":\n");
2840 sb.append(prefix); sb.append(" Created for: ");
2841 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002842 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002843 sb.append(prefix); sb.append(" Starts: ");
2844 sb.append(starts);
2845 sb.append(", launches: "); sb.append(launches);
2846 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 apkActivity = true;
2848 }
2849 }
2850 }
2851 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002852 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 }
2854 uidActivity = true;
2855 }
2856 }
2857 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002858 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860 }
2861 }
2862
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002863 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002864 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002865 int diff = oldval ^ newval;
2866 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002867 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002868 for (int i=0; i<descriptions.length; i++) {
2869 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002870 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002871 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002872 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002873 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002874 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002875 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2876 didWake = true;
2877 pw.print("=");
2878 if (longNames) {
2879 UserHandle.formatUid(pw, wakelockTag.uid);
2880 pw.print(":\"");
2881 pw.print(wakelockTag.string);
2882 pw.print("\"");
2883 } else {
2884 pw.print(wakelockTag.poolIdx);
2885 }
2886 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002887 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002888 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002889 pw.print("=");
2890 int val = (newval&bd.mask)>>bd.shift;
2891 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002892 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002893 } else {
2894 pw.print(val);
2895 }
2896 }
2897 }
2898 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002899 if (!didWake && wakelockTag != null) {
2900 pw.print(longNames ? "wake_lock=" : "w=");
2901 if (longNames) {
2902 UserHandle.formatUid(pw, wakelockTag.uid);
2903 pw.print(":\"");
2904 pw.print(wakelockTag.string);
2905 pw.print("\"");
2906 } else {
2907 pw.print(wakelockTag.poolIdx);
2908 }
2909 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002910 }
2911
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002912 public void prepareForDumpLocked() {
2913 }
2914
2915 public static class HistoryPrinter {
2916 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002917 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002918 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002919 int oldStatus = -1;
2920 int oldHealth = -1;
2921 int oldPlug = -1;
2922 int oldTemp = -1;
2923 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002924 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002925 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002926
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002927 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002928 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002929 if (!checkin) {
2930 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002931 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002932 pw.print(" (");
2933 pw.print(rec.numReadInts);
2934 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002935 } else {
2936 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002937 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002938 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002939 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002940 }
2941 lastTime = rec.time;
2942 }
2943 if (rec.cmd == HistoryItem.CMD_START) {
2944 if (checkin) {
2945 pw.print(":");
2946 }
2947 pw.println("START");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002948 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
2949 if (checkin) {
2950 pw.print(":");
2951 }
2952 pw.print("TIME:");
2953 if (checkin) {
2954 pw.println(rec.currentTime);
2955 } else {
2956 pw.print(" ");
2957 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
2958 rec.currentTime).toString());
2959 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002960 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2961 if (checkin) {
2962 pw.print(":");
2963 }
2964 pw.println("*OVERFLOW*");
2965 } else {
2966 if (!checkin) {
2967 if (rec.batteryLevel < 10) pw.print("00");
2968 else if (rec.batteryLevel < 100) pw.print("0");
2969 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002970 if (verbose) {
2971 pw.print(" ");
2972 if (rec.states < 0) ;
2973 else if (rec.states < 0x10) pw.print("0000000");
2974 else if (rec.states < 0x100) pw.print("000000");
2975 else if (rec.states < 0x1000) pw.print("00000");
2976 else if (rec.states < 0x10000) pw.print("0000");
2977 else if (rec.states < 0x100000) pw.print("000");
2978 else if (rec.states < 0x1000000) pw.print("00");
2979 else if (rec.states < 0x10000000) pw.print("0");
2980 pw.print(Integer.toHexString(rec.states));
2981 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002982 } else {
2983 if (oldLevel != rec.batteryLevel) {
2984 oldLevel = rec.batteryLevel;
2985 pw.print(",Bl="); pw.print(rec.batteryLevel);
2986 }
2987 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002988 if (oldStatus != rec.batteryStatus) {
2989 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002990 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002991 switch (oldStatus) {
2992 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002993 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002994 break;
2995 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002996 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002997 break;
2998 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002999 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003000 break;
3001 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003002 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003003 break;
3004 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003005 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003006 break;
3007 default:
3008 pw.print(oldStatus);
3009 break;
3010 }
3011 }
3012 if (oldHealth != rec.batteryHealth) {
3013 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003014 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003015 switch (oldHealth) {
3016 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003017 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003018 break;
3019 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003020 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003021 break;
3022 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003023 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003024 break;
3025 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003026 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003027 break;
3028 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003029 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003030 break;
3031 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003032 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003033 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003034 case BatteryManager.BATTERY_HEALTH_COLD:
3035 pw.print(checkin ? "c" : "cold");
3036 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003037 default:
3038 pw.print(oldHealth);
3039 break;
3040 }
3041 }
3042 if (oldPlug != rec.batteryPlugType) {
3043 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003044 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003045 switch (oldPlug) {
3046 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003047 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003048 break;
3049 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003050 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003051 break;
3052 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003053 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003054 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003055 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003056 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003057 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003058 default:
3059 pw.print(oldPlug);
3060 break;
3061 }
3062 }
3063 if (oldTemp != rec.batteryTemperature) {
3064 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003065 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003066 pw.print(oldTemp);
3067 }
3068 if (oldVolt != rec.batteryVoltage) {
3069 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003070 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003071 pw.print(oldVolt);
3072 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003073 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003074 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003075 printBitDescriptions(pw, oldState2, rec.states2, null,
3076 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003077 if (rec.wakeReasonTag != null) {
3078 if (checkin) {
3079 pw.print(",Wr=");
3080 pw.print(rec.wakeReasonTag.poolIdx);
3081 } else {
3082 pw.print(" wake_reason=");
3083 pw.print(rec.wakeReasonTag.uid);
3084 pw.print(":\"");
3085 pw.print(rec.wakeReasonTag.string);
3086 pw.print("\"");
3087 }
3088 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003089 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003090 pw.print(checkin ? "," : " ");
3091 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3092 pw.print("+");
3093 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3094 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003095 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003096 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3097 : HISTORY_EVENT_NAMES;
3098 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3099 | HistoryItem.EVENT_FLAG_FINISH);
3100 if (idx >= 0 && idx < eventNames.length) {
3101 pw.print(eventNames[idx]);
3102 } else {
3103 pw.print(checkin ? "Ev" : "event");
3104 pw.print(idx);
3105 }
3106 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003107 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003108 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003109 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003110 UserHandle.formatUid(pw, rec.eventTag.uid);
3111 pw.print(":\"");
3112 pw.print(rec.eventTag.string);
3113 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003114 }
3115 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003116 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003117 oldState = rec.states;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003118 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003119 }
3120 }
3121
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003122 private void printSizeValue(PrintWriter pw, long size) {
3123 float result = size;
3124 String suffix = "";
3125 if (result >= 10*1024) {
3126 suffix = "KB";
3127 result = result / 1024;
3128 }
3129 if (result >= 10*1024) {
3130 suffix = "MB";
3131 result = result / 1024;
3132 }
3133 if (result >= 10*1024) {
3134 suffix = "GB";
3135 result = result / 1024;
3136 }
3137 if (result >= 10*1024) {
3138 suffix = "TB";
3139 result = result / 1024;
3140 }
3141 if (result >= 10*1024) {
3142 suffix = "PB";
3143 result = result / 1024;
3144 }
3145 pw.print((int)result);
3146 pw.print(suffix);
3147 }
3148
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003149 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3150 int count, boolean checkin) {
3151 if (count <= 0) {
3152 return false;
3153 }
3154 if (!checkin) {
3155 pw.println(header);
3156 }
3157 String[] lineArgs = new String[1];
3158 for (int i=0; i<count; i++) {
3159 if (checkin) {
3160 lineArgs[0] = Long.toString(steps[i]);
3161 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3162 } else {
3163 pw.print(" #"); pw.print(i); pw.print(": ");
3164 TimeUtils.formatDuration(steps[i], pw);
3165 pw.println();
3166 }
3167 }
3168 return true;
3169 }
3170
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003171 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3172 public static final int DUMP_CHARGED_ONLY = 1<<1;
3173 public static final int DUMP_HISTORY_ONLY = 1<<2;
3174 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003175 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 /**
3178 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3179 *
3180 * @param pw a Printer to receive the dump output.
3181 */
3182 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003183 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003184 prepareForDumpLocked();
3185
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003186 final boolean filtering =
3187 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003188
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003189 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
3190 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3191
3192 final HistoryItem rec = new HistoryItem();
3193 final long historyTotalSize = getHistoryTotalSize();
3194 final long historyUsedSize = getHistoryUsedSize();
3195 if (startIteratingHistoryLocked()) {
3196 try {
3197 pw.print("Battery History (");
3198 pw.print((100*historyUsedSize)/historyTotalSize);
3199 pw.print("% used, ");
3200 printSizeValue(pw, historyUsedSize);
3201 pw.print(" used of ");
3202 printSizeValue(pw, historyTotalSize);
3203 pw.print(", ");
3204 pw.print(getHistoryStringPoolSize());
3205 pw.print(" strings using ");
3206 printSizeValue(pw, getHistoryStringPoolBytes());
3207 pw.println("):");
3208 HistoryPrinter hprinter = new HistoryPrinter();
3209 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003210 long baseTime = -1;
3211 boolean printed = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003212 while (getNextHistoryLocked(rec)) {
3213 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003214 if (baseTime < 0) {
3215 baseTime = lastTime;
3216 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003217 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003218 if (histStart >= 0 && !printed) {
3219 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3220 printed = true;
3221 } else if (rec.currentTime != 0) {
3222 printed = true;
3223 byte cmd = rec.cmd;
3224 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3225 hprinter.printNextItem(pw, rec, baseTime, false,
3226 (flags&DUMP_VERBOSE) != 0);
3227 rec.cmd = cmd;
3228 }
3229 }
3230 hprinter.printNextItem(pw, rec, baseTime, false,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003231 (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003232 }
3233 }
3234 if (histStart >= 0) {
3235 pw.print(" NEXT: "); pw.println(lastTime+1);
3236 }
3237 pw.println();
3238 } finally {
3239 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003240 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003241 }
3242
3243 if (startIteratingOldHistoryLocked()) {
3244 try {
3245 pw.println("Old battery History:");
3246 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003247 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003248 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003249 if (baseTime < 0) {
3250 baseTime = rec.time;
3251 }
3252 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003253 }
3254 pw.println();
3255 } finally {
3256 finishIteratingOldHistoryLocked();
3257 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003258 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003259 }
3260
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003261 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003262 return;
3263 }
3264
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003265 if (!filtering) {
3266 SparseArray<? extends Uid> uidStats = getUidStats();
3267 final int NU = uidStats.size();
3268 boolean didPid = false;
3269 long nowRealtime = SystemClock.elapsedRealtime();
3270 for (int i=0; i<NU; i++) {
3271 Uid uid = uidStats.valueAt(i);
3272 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3273 if (pids != null) {
3274 for (int j=0; j<pids.size(); j++) {
3275 Uid.Pid pid = pids.valueAt(j);
3276 if (!didPid) {
3277 pw.println("Per-PID Stats:");
3278 didPid = true;
3279 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003280 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3281 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003282 pw.print(" PID "); pw.print(pids.keyAt(j));
3283 pw.print(" wake time: ");
3284 TimeUtils.formatDuration(time, pw);
3285 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003286 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003287 }
3288 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003289 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003290 pw.println();
3291 }
3292 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3293 getNumDischargeStepDurations(), false)) {
3294 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3295 if (timeRemaining >= 0) {
3296 pw.print(" Estimated discharge time remaining: ");
3297 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3298 pw.println();
3299 }
3300 pw.println();
3301 }
3302 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3303 getNumChargeStepDurations(), false)) {
3304 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3305 if (timeRemaining >= 0) {
3306 pw.print(" Estimated charge time remaining: ");
3307 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3308 pw.println();
3309 }
3310 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003311 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003312 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003313
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003314 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003315 pw.println("Statistics since last charge:");
3316 pw.println(" System starts: " + getStartCount()
3317 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003318 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003319 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003320 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003321 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3322 pw.println("Statistics since last unplugged:");
3323 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 }
3326
3327 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003328 public void dumpCheckinLocked(Context context, PrintWriter pw,
3329 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003330 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003332 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3333
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003334 final boolean filtering =
3335 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3336
3337 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003338 final HistoryItem rec = new HistoryItem();
3339 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003340 try {
3341 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3342 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3343 pw.print(HISTORY_STRING_POOL); pw.print(',');
3344 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003345 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003346 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003347 pw.print(",\"");
3348 String str = getHistoryTagPoolString(i);
3349 str = str.replace("\\", "\\\\");
3350 str = str.replace("\"", "\\\"");
3351 pw.print(str);
3352 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003353 pw.println();
3354 }
3355 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003356 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003357 long baseTime = -1;
3358 boolean printed = false;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003359 while (getNextHistoryLocked(rec)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003360 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003361 if (baseTime < 0) {
3362 baseTime = lastTime;
3363 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003364 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003365 if (histStart >= 0 && !printed) {
3366 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3367 printed = true;
3368 } else if (rec.currentTime != 0) {
3369 printed = true;
3370 byte cmd = rec.cmd;
3371 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3372 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3373 pw.print(HISTORY_DATA); pw.print(',');
3374 hprinter.printNextItem(pw, rec, baseTime, true, false);
3375 rec.cmd = cmd;
3376 }
3377 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003378 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3379 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003380 hprinter.printNextItem(pw, rec, baseTime, true, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003381 }
3382 }
3383 if (histStart >= 0) {
3384 pw.print("NEXT: "); pw.println(lastTime+1);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003385 }
3386 } finally {
3387 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003388 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003389 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003390 }
3391
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003392 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003393 return;
3394 }
3395
Dianne Hackborne4a59512010-12-07 11:08:07 -08003396 if (apps != null) {
3397 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3398 for (int i=0; i<apps.size(); i++) {
3399 ApplicationInfo ai = apps.get(i);
3400 ArrayList<String> pkgs = uids.get(ai.uid);
3401 if (pkgs == null) {
3402 pkgs = new ArrayList<String>();
3403 uids.put(ai.uid, pkgs);
3404 }
3405 pkgs.add(ai.packageName);
3406 }
3407 SparseArray<? extends Uid> uidStats = getUidStats();
3408 final int NU = uidStats.size();
3409 String[] lineArgs = new String[2];
3410 for (int i=0; i<NU; i++) {
3411 int uid = uidStats.keyAt(i);
3412 ArrayList<String> pkgs = uids.get(uid);
3413 if (pkgs != null) {
3414 for (int j=0; j<pkgs.size(); j++) {
3415 lineArgs[0] = Integer.toString(uid);
3416 lineArgs[1] = pkgs.get(j);
3417 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3418 (Object[])lineArgs);
3419 }
3420 }
3421 }
3422 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003423 if (!filtering) {
3424 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
3425 getNumDischargeStepDurations(), true);
3426 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
3427 getNumChargeStepDurations(), true);
3428 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003429 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003430 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003431 }
3432 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003433 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436}