blob: 32bbfbd3c609c5b78cea04eba559bf0c597071a5 [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 Hackborne4a59512010-12-07 11:08:07 -080027import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080028import android.telephony.SignalStrength;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.Log;
30import android.util.Printer;
Dianne Hackborn1fadab52011-04-14 17:57:33 -070031import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35/**
36 * A class providing access to battery usage statistics, including information on
37 * wakelocks, processes, packages, and services. All times are represented in microseconds
38 * except where indicated otherwise.
39 * @hide
40 */
41public abstract class BatteryStats implements Parcelable {
42
43 private static final boolean LOCAL_LOGV = false;
44
45 /**
46 * A constant indicating a partial wake lock timer.
47 */
48 public static final int WAKE_TYPE_PARTIAL = 0;
49
50 /**
51 * A constant indicating a full wake lock timer.
52 */
53 public static final int WAKE_TYPE_FULL = 1;
54
55 /**
56 * A constant indicating a window wake lock timer.
57 */
58 public static final int WAKE_TYPE_WINDOW = 2;
59
60 /**
61 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 */
63 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070064
65 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070066 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070067 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070068 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070069
70 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070071 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070072 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070073 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070074
75 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070076 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070078 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
Robert Greenwalt5347bd42009-05-13 15:10:16 -070080 /**
81 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070082 */
83 public static final int WIFI_MULTICAST_ENABLED = 7;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070086 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070087 */
88 public static final int AUDIO_TURNED_ON = 7;
89
90 /**
91 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int VIDEO_TURNED_ON = 8;
94
95 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -080096 * A constant indicating a vibrator on timer
97 */
98 public static final int VIBRATOR_ON = 9;
99
100 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700101 * A constant indicating a foreground activity timer
102 */
103 public static final int FOREGROUND_ACTIVITY = 10;
104
105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * Include all of the data in the stats, including previously saved data.
107 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700108 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 /**
111 * Include only the last run in the stats.
112 */
113 public static final int STATS_LAST = 1;
114
115 /**
116 * Include only the current run in the stats.
117 */
118 public static final int STATS_CURRENT = 2;
119
120 /**
121 * Include only the run since the last time the device was unplugged in the stats.
122 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700123 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700124
125 // NOTE: Update this list if you add/change any stats above.
126 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700127 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700128 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
130 /**
131 * Bump the version on this if the checkin format changes.
132 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700133 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700134
135 private static final long BYTES_PER_KB = 1024;
136 private static final long BYTES_PER_MB = 1048576; // 1024^2
137 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Dianne Hackborne4a59512010-12-07 11:08:07 -0800140 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700142 private static final String PROCESS_DATA = "pr";
143 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800144 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700145 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700146 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700147 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700148 private static final String NETWORK_DATA = "nt";
149 private static final String USER_ACTIVITY_DATA = "ua";
150 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800151 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700152 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700153 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700154 private static final String MISC_DATA = "m";
155 private static final String SCREEN_BRIGHTNESS_DATA = "br";
156 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700157 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
159 private static final String DATA_CONNECTION_TIME_DATA = "dct";
160 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700162 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private final Formatter mFormatter = new Formatter(mFormatBuilder);
164
165 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700166 * State for keeping track of counting information.
167 */
168 public static abstract class Counter {
169
170 /**
171 * Returns the count associated with this Counter for the
172 * selected type of statistics.
173 *
174 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
175 */
Evan Millarc64edde2009-04-18 12:26:32 -0700176 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700177
178 /**
179 * Temporary for debugging.
180 */
181 public abstract void logState(Printer pw, String prefix);
182 }
183
184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * State for keeping track of timing information.
186 */
187 public static abstract class Timer {
188
189 /**
190 * Returns the count associated with this Timer for the
191 * selected type of statistics.
192 *
193 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
194 */
Evan Millarc64edde2009-04-18 12:26:32 -0700195 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 /**
198 * Returns the total time in microseconds associated with this Timer for the
199 * selected type of statistics.
200 *
201 * @param batteryRealtime system realtime on battery in microseconds
202 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
203 * @return a time in microseconds
204 */
Evan Millarc64edde2009-04-18 12:26:32 -0700205 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
208 * Temporary for debugging.
209 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700210 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212
213 /**
214 * The statistics associated with a particular uid.
215 */
216 public static abstract class Uid {
217
218 /**
219 * Returns a mapping containing wakelock statistics.
220 *
221 * @return a Map from Strings to Uid.Wakelock objects.
222 */
223 public abstract Map<String, ? extends Wakelock> getWakelockStats();
224
225 /**
226 * The statistics associated with a particular wake lock.
227 */
228 public static abstract class Wakelock {
229 public abstract Timer getWakeTime(int type);
230 }
231
232 /**
233 * Returns a mapping containing sensor statistics.
234 *
235 * @return a Map from Integer sensor ids to Uid.Sensor objects.
236 */
237 public abstract Map<Integer, ? extends Sensor> getSensorStats();
238
239 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700240 * Returns a mapping containing active process data.
241 */
242 public abstract SparseArray<? extends Pid> getPidStats();
243
244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * Returns a mapping containing process statistics.
246 *
247 * @return a Map from Strings to Uid.Proc objects.
248 */
249 public abstract Map<String, ? extends Proc> getProcessStats();
250
251 /**
252 * Returns a mapping containing package statistics.
253 *
254 * @return a Map from Strings to Uid.Pkg objects.
255 */
256 public abstract Map<String, ? extends Pkg> getPackageStats();
257
258 /**
259 * {@hide}
260 */
261 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700262
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700263 public abstract void noteWifiRunningLocked();
264 public abstract void noteWifiStoppedLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700265 public abstract void noteFullWifiLockAcquiredLocked();
266 public abstract void noteFullWifiLockReleasedLocked();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700267 public abstract void noteWifiScanStartedLocked();
268 public abstract void noteWifiScanStoppedLocked();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700269 public abstract void noteWifiMulticastEnabledLocked();
270 public abstract void noteWifiMulticastDisabledLocked();
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700271 public abstract void noteAudioTurnedOnLocked();
272 public abstract void noteAudioTurnedOffLocked();
273 public abstract void noteVideoTurnedOnLocked();
274 public abstract void noteVideoTurnedOffLocked();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700275 public abstract void noteActivityResumedLocked();
276 public abstract void noteActivityPausedLocked();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700277 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700278 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700279 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700280 public abstract long getWifiMulticastTime(long batteryRealtime,
281 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700282 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
283 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700284 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800285 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
Dianne Hackborn617f8772009-03-31 15:04:46 -0700287 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700288 * Note that these must match the constants in android.os.PowerManager.
289 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
290 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700291 */
292 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700293 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700294 };
295
Jeff Browndf693de2012-07-27 12:03:38 -0700296 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700297
Dianne Hackborn617f8772009-03-31 15:04:46 -0700298 public abstract void noteUserActivityLocked(int type);
299 public abstract boolean hasUserActivity();
300 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700301
302 public abstract boolean hasNetworkActivity();
303 public abstract long getNetworkActivityCount(int type, int which);
304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800306 /*
307 * FIXME: it's not correct to use this magic value because it
308 * could clash with a sensor handle (which are defined by
309 * the sensor HAL, and therefore out of our control
310 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 // Magic sensor number for the GPS.
312 public static final int GPS = -10000;
313
314 public abstract int getHandle();
315
316 public abstract Timer getSensorTime();
317 }
318
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700319 public class Pid {
320 public long mWakeSum;
321 public long mWakeStart;
322 }
323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * The statistics associated with a particular process.
326 */
327 public static abstract class Proc {
328
Dianne Hackborn287952c2010-09-22 22:34:31 -0700329 public static class ExcessivePower {
330 public static final int TYPE_WAKE = 1;
331 public static final int TYPE_CPU = 2;
332
333 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700334 public long overTime;
335 public long usedTime;
336 }
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Returns the total time (in 1/100 sec) spent executing in user code.
340 *
341 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
342 */
343 public abstract long getUserTime(int which);
344
345 /**
346 * Returns the total time (in 1/100 sec) spent executing in system code.
347 *
348 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
349 */
350 public abstract long getSystemTime(int which);
351
352 /**
353 * Returns the number of times the process has been started.
354 *
355 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
356 */
357 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700358
359 /**
360 * Returns the cpu time spent in microseconds while the process was in the foreground.
361 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
362 * @return foreground cpu time in microseconds
363 */
364 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700365
366 /**
367 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
368 * @param speedStep the index of the CPU speed. This is not the actual speed of the
369 * CPU.
370 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
371 * @see BatteryStats#getCpuSpeedSteps()
372 */
373 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700374
Dianne Hackborn287952c2010-09-22 22:34:31 -0700375 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700376
Dianne Hackborn287952c2010-09-22 22:34:31 -0700377 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 /**
381 * The statistics associated with a particular package.
382 */
383 public static abstract class Pkg {
384
385 /**
386 * Returns the number of times this package has done something that could wake up the
387 * device from sleep.
388 *
389 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
390 */
391 public abstract int getWakeups(int which);
392
393 /**
394 * Returns a mapping containing service statistics.
395 */
396 public abstract Map<String, ? extends Serv> getServiceStats();
397
398 /**
399 * The statistics associated with a particular service.
400 */
401 public abstract class Serv {
402
403 /**
404 * Returns the amount of time spent started.
405 *
406 * @param batteryUptime elapsed uptime on battery in microseconds.
407 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
408 * @return
409 */
410 public abstract long getStartTime(long batteryUptime, int which);
411
412 /**
413 * Returns the total number of times startService() has been called.
414 *
415 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
416 */
417 public abstract int getStarts(int which);
418
419 /**
420 * Returns the total number times the service has been launched.
421 *
422 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
423 */
424 public abstract int getLaunches(int which);
425 }
426 }
427 }
428
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700429 public final static class HistoryItem implements Parcelable {
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700430 static final String TAG = "HistoryItem";
431 static final boolean DEBUG = false;
432
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700433 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700434
435 public long time;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700436
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700437 public static final byte CMD_NULL = 0;
438 public static final byte CMD_UPDATE = 1;
439 public static final byte CMD_START = 2;
440 public static final byte CMD_OVERFLOW = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700441
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700442 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700443
444 public byte batteryLevel;
445 public byte batteryStatus;
446 public byte batteryHealth;
447 public byte batteryPlugType;
448
449 public char batteryTemperature;
450 public char batteryVoltage;
451
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700452 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700453 public static final int STATE_BRIGHTNESS_MASK = 0x0000000f;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700454 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700455 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700456 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x000000f0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700457 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 4;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700458 // Constants from ServiceState.STATE_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700459 public static final int STATE_PHONE_STATE_MASK = 0x00000f00;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700460 public static final int STATE_PHONE_STATE_SHIFT = 8;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700461 // Constants from DATA_CONNECTION_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700462 public static final int STATE_DATA_CONNECTION_MASK = 0x0000f000;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700463 public static final int STATE_DATA_CONNECTION_SHIFT = 12;
464
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700465 // These states always appear directly in the first int token
466 // of a delta change; they should be ones that change relatively
467 // frequently.
468 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
469 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700470 public static final int STATE_GPS_ON_FLAG = 1<<28;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700471 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
472 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
473 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
Nick Pelly6ccaa542012-06-15 15:22:47 -0700474 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700475 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
476 // These are on the lower bits used for the command; if they change
477 // we need to write another int of data.
478 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
479 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
480 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
481 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
482 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
483 public static final int STATE_WIFI_ON_FLAG = 1<<17;
484 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700485
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700486 public static final int MOST_INTERESTING_STATES =
487 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
488 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
489
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700490 public int states;
491
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700492 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700493 }
494
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700495 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700496 this.time = time;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700497 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700498 }
499
500 public int describeContents() {
501 return 0;
502 }
503
504 public void writeToParcel(Parcel dest, int flags) {
505 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700506 int bat = (((int)cmd)&0xff)
507 | ((((int)batteryLevel)<<8)&0xff00)
508 | ((((int)batteryStatus)<<16)&0xf0000)
509 | ((((int)batteryHealth)<<20)&0xf00000)
510 | ((((int)batteryPlugType)<<24)&0xf000000);
511 dest.writeInt(bat);
512 bat = (((int)batteryTemperature)&0xffff)
513 | ((((int)batteryVoltage)<<16)&0xffff0000);
514 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700515 dest.writeInt(states);
516 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700517
518 private void readFromParcel(Parcel src) {
519 int bat = src.readInt();
520 cmd = (byte)(bat&0xff);
521 batteryLevel = (byte)((bat>>8)&0xff);
522 batteryStatus = (byte)((bat>>16)&0xf);
523 batteryHealth = (byte)((bat>>20)&0xf);
524 batteryPlugType = (byte)((bat>>24)&0xf);
525 bat = src.readInt();
526 batteryTemperature = (char)(bat&0xffff);
527 batteryVoltage = (char)((bat>>16)&0xffff);
528 states = src.readInt();
529 }
530
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700531 // Part of initial delta int that specifies the time delta.
532 static final int DELTA_TIME_MASK = 0x3ffff;
533 static final int DELTA_TIME_ABS = 0x3fffd; // Following is an entire abs update.
534 static final int DELTA_TIME_INT = 0x3fffe; // The delta is a following int
535 static final int DELTA_TIME_LONG = 0x3ffff; // The delta is a following long
536 // Part of initial delta int holding the command code.
537 static final int DELTA_CMD_MASK = 0x3;
538 static final int DELTA_CMD_SHIFT = 18;
539 // Flag in delta int: a new battery level int follows.
540 static final int DELTA_BATTERY_LEVEL_FLAG = 1<<20;
541 // Flag in delta int: a new full state and battery status int follows.
542 static final int DELTA_STATE_FLAG = 1<<21;
543 static final int DELTA_STATE_MASK = 0xffc00000;
544
545 public void writeDelta(Parcel dest, HistoryItem last) {
546 if (last == null || last.cmd != CMD_UPDATE) {
547 dest.writeInt(DELTA_TIME_ABS);
548 writeToParcel(dest, 0);
549 return;
550 }
551
552 final long deltaTime = time - last.time;
553 final int lastBatteryLevelInt = last.buildBatteryLevelInt();
554 final int lastStateInt = last.buildStateInt();
555
556 int deltaTimeToken;
557 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
558 deltaTimeToken = DELTA_TIME_LONG;
559 } else if (deltaTime >= DELTA_TIME_ABS) {
560 deltaTimeToken = DELTA_TIME_INT;
561 } else {
562 deltaTimeToken = (int)deltaTime;
563 }
564 int firstToken = deltaTimeToken
565 | (cmd<<DELTA_CMD_SHIFT)
566 | (states&DELTA_STATE_MASK);
567 final int batteryLevelInt = buildBatteryLevelInt();
568 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
569 if (batteryLevelIntChanged) {
570 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
571 }
572 final int stateInt = buildStateInt();
573 final boolean stateIntChanged = stateInt != lastStateInt;
574 if (stateIntChanged) {
575 firstToken |= DELTA_STATE_FLAG;
576 }
577 dest.writeInt(firstToken);
578 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
579 + " deltaTime=" + deltaTime);
580
581 if (deltaTimeToken >= DELTA_TIME_INT) {
582 if (deltaTimeToken == DELTA_TIME_INT) {
583 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
584 dest.writeInt((int)deltaTime);
585 } else {
586 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
587 dest.writeLong(deltaTime);
588 }
589 }
590 if (batteryLevelIntChanged) {
591 dest.writeInt(batteryLevelInt);
592 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
593 + Integer.toHexString(batteryLevelInt)
594 + " batteryLevel=" + batteryLevel
595 + " batteryTemp=" + (int)batteryTemperature
596 + " batteryVolt=" + (int)batteryVoltage);
597 }
598 if (stateIntChanged) {
599 dest.writeInt(stateInt);
600 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
601 + Integer.toHexString(stateInt)
602 + " batteryStatus=" + batteryStatus
603 + " batteryHealth=" + batteryHealth
604 + " batteryPlugType=" + batteryPlugType
605 + " states=0x" + Integer.toHexString(states));
606 }
607 }
608
609 private int buildBatteryLevelInt() {
610 return ((((int)batteryLevel)<<24)&0xff000000)
611 | ((((int)batteryTemperature)<<14)&0x00ffc000)
612 | (((int)batteryVoltage)&0x00003fff);
613 }
614
615 private int buildStateInt() {
616 return ((((int)batteryStatus)<<28)&0xf0000000)
617 | ((((int)batteryHealth)<<24)&0x0f000000)
618 | ((((int)batteryPlugType)<<22)&0x00c00000)
619 | (states&(~DELTA_STATE_MASK));
620 }
621
622 public void readDelta(Parcel src) {
623 int firstToken = src.readInt();
624 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
625 cmd = (byte)((firstToken>>DELTA_CMD_SHIFT)&DELTA_CMD_MASK);
626 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
627 + " deltaTimeToken=" + deltaTimeToken);
628
629 if (deltaTimeToken < DELTA_TIME_ABS) {
630 time += deltaTimeToken;
631 } else if (deltaTimeToken == DELTA_TIME_ABS) {
632 time = src.readLong();
633 readFromParcel(src);
634 return;
635 } else if (deltaTimeToken == DELTA_TIME_INT) {
636 int delta = src.readInt();
637 time += delta;
638 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
639 } else {
640 long delta = src.readLong();
641 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
642 time += delta;
643 }
644
645 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
646 int batteryLevelInt = src.readInt();
647 batteryLevel = (byte)((batteryLevelInt>>24)&0xff);
648 batteryTemperature = (char)((batteryLevelInt>>14)&0x3ff);
649 batteryVoltage = (char)(batteryLevelInt&0x3fff);
650 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
651 + Integer.toHexString(batteryLevelInt)
652 + " batteryLevel=" + batteryLevel
653 + " batteryTemp=" + (int)batteryTemperature
654 + " batteryVolt=" + (int)batteryVoltage);
655 }
656
657 if ((firstToken&DELTA_STATE_FLAG) != 0) {
658 int stateInt = src.readInt();
659 states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
660 batteryStatus = (byte)((stateInt>>28)&0xf);
661 batteryHealth = (byte)((stateInt>>24)&0xf);
662 batteryPlugType = (byte)((stateInt>>22)&0x3);
663 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
664 + Integer.toHexString(stateInt)
665 + " batteryStatus=" + batteryStatus
666 + " batteryHealth=" + batteryHealth
667 + " batteryPlugType=" + batteryPlugType
668 + " states=0x" + Integer.toHexString(states));
669 } else {
670 states = (firstToken&DELTA_STATE_MASK) | (states&(~DELTA_STATE_MASK));
671 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700672 }
673
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700674 public void clear() {
675 time = 0;
676 cmd = CMD_NULL;
677 batteryLevel = 0;
678 batteryStatus = 0;
679 batteryHealth = 0;
680 batteryPlugType = 0;
681 batteryTemperature = 0;
682 batteryVoltage = 0;
683 states = 0;
684 }
685
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700686 public void setTo(HistoryItem o) {
687 time = o.time;
688 cmd = o.cmd;
689 batteryLevel = o.batteryLevel;
690 batteryStatus = o.batteryStatus;
691 batteryHealth = o.batteryHealth;
692 batteryPlugType = o.batteryPlugType;
693 batteryTemperature = o.batteryTemperature;
694 batteryVoltage = o.batteryVoltage;
695 states = o.states;
696 }
697
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700698 public void setTo(long time, byte cmd, HistoryItem o) {
699 this.time = time;
700 this.cmd = cmd;
701 batteryLevel = o.batteryLevel;
702 batteryStatus = o.batteryStatus;
703 batteryHealth = o.batteryHealth;
704 batteryPlugType = o.batteryPlugType;
705 batteryTemperature = o.batteryTemperature;
706 batteryVoltage = o.batteryVoltage;
707 states = o.states;
708 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700709
710 public boolean same(HistoryItem o) {
711 return batteryLevel == o.batteryLevel
712 && batteryStatus == o.batteryStatus
713 && batteryHealth == o.batteryHealth
714 && batteryPlugType == o.batteryPlugType
715 && batteryTemperature == o.batteryTemperature
716 && batteryVoltage == o.batteryVoltage
717 && states == o.states;
718 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700719 }
720
721 public static final class BitDescription {
722 public final int mask;
723 public final int shift;
724 public final String name;
725 public final String[] values;
726
727 public BitDescription(int mask, String name) {
728 this.mask = mask;
729 this.shift = -1;
730 this.name = name;
731 this.values = null;
732 }
733
734 public BitDescription(int mask, int shift, String name, String[] values) {
735 this.mask = mask;
736 this.shift = shift;
737 this.name = name;
738 this.values = values;
739 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700740 }
741
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700742 public abstract boolean startIteratingHistoryLocked();
743
744 public abstract boolean getNextHistoryLocked(HistoryItem out);
745
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700746 public abstract void finishIteratingHistoryLocked();
747
748 public abstract boolean startIteratingOldHistoryLocked();
749
750 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
751
752 public abstract void finishIteratingOldHistoryLocked();
753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700755 * Return the base time offset for the battery history.
756 */
757 public abstract long getHistoryBaseTime();
758
759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * Returns the number of times the device has been started.
761 */
762 public abstract int getStartCount();
763
764 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700765 * 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 -0800766 * running on battery.
767 *
768 * {@hide}
769 */
770 public abstract long getScreenOnTime(long batteryRealtime, int which);
771
Dianne Hackborn617f8772009-03-31 15:04:46 -0700772 public static final int SCREEN_BRIGHTNESS_DARK = 0;
773 public static final int SCREEN_BRIGHTNESS_DIM = 1;
774 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
775 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
776 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
777
778 static final String[] SCREEN_BRIGHTNESS_NAMES = {
779 "dark", "dim", "medium", "light", "bright"
780 };
781
782 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
783
784 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700785 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700786 * the given brightness
787 *
788 * {@hide}
789 */
790 public abstract long getScreenBrightnessTime(int brightnessBin,
791 long batteryRealtime, int which);
792
793 public abstract int getInputEventCount(int which);
794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700796 * 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 -0800797 * running on battery.
798 *
799 * {@hide}
800 */
801 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700802
803 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700804 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700805 * the given signal strength.
806 *
807 * {@hide}
808 */
809 public abstract long getPhoneSignalStrengthTime(int strengthBin,
810 long batteryRealtime, int which);
811
Dianne Hackborn617f8772009-03-31 15:04:46 -0700812 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700813 * Returns the time in microseconds that the phone has been trying to
814 * acquire a signal.
815 *
816 * {@hide}
817 */
818 public abstract long getPhoneSignalScanningTime(
819 long batteryRealtime, int which);
820
821 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700822 * Returns the number of times the phone has entered the given signal strength.
823 *
824 * {@hide}
825 */
826 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
827
Dianne Hackborn627bba72009-03-24 22:32:56 -0700828 public static final int DATA_CONNECTION_NONE = 0;
829 public static final int DATA_CONNECTION_GPRS = 1;
830 public static final int DATA_CONNECTION_EDGE = 2;
831 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700832 public static final int DATA_CONNECTION_CDMA = 4;
833 public static final int DATA_CONNECTION_EVDO_0 = 5;
834 public static final int DATA_CONNECTION_EVDO_A = 6;
835 public static final int DATA_CONNECTION_1xRTT = 7;
836 public static final int DATA_CONNECTION_HSDPA = 8;
837 public static final int DATA_CONNECTION_HSUPA = 9;
838 public static final int DATA_CONNECTION_HSPA = 10;
839 public static final int DATA_CONNECTION_IDEN = 11;
840 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700841 public static final int DATA_CONNECTION_LTE = 13;
842 public static final int DATA_CONNECTION_EHRPD = 14;
843 public static final int DATA_CONNECTION_OTHER = 15;
844
Dianne Hackborn627bba72009-03-24 22:32:56 -0700845 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700846 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700847 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
848 "ehrpd", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700849 };
850
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700851 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700852
853 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700854 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700855 * the given data connection.
856 *
857 * {@hide}
858 */
859 public abstract long getPhoneDataConnectionTime(int dataType,
860 long batteryRealtime, int which);
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700863 * Returns the number of times the phone has entered the given data
864 * connection type.
865 *
866 * {@hide}
867 */
868 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700869
870 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
871 = new BitDescription[] {
872 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged"),
873 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen"),
874 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps"),
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700875 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700876 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning"),
877 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi"),
878 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running"),
879 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock"),
Nick Pelly6ccaa542012-06-15 15:22:47 -0700880 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700881 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast"),
882 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth"),
883 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio"),
884 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video"),
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700885 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock"),
886 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700887 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
888 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness",
889 SCREEN_BRIGHTNESS_NAMES),
890 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
891 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength",
Wink Saville52840902011-02-18 12:40:47 -0800892 SignalStrength.SIGNAL_STRENGTH_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700893 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
894 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state",
895 new String[] {"in", "out", "emergency", "off"}),
896 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
897 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn",
898 DATA_CONNECTION_NAMES),
899 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700900
901 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700902 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700903 * running on battery.
904 *
905 * {@hide}
906 */
907 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700908
909 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700910 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700911 * been in the running state while the device was running on battery.
912 *
913 * {@hide}
914 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700915 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700916
The Android Open Source Project10592532009-03-18 17:39:46 -0700917 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700918 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700919 * running on battery.
920 *
921 * {@hide}
922 */
923 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
924
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700925 public static final int NETWORK_MOBILE_RX_BYTES = 0;
926 public static final int NETWORK_MOBILE_TX_BYTES = 1;
927 public static final int NETWORK_WIFI_RX_BYTES = 2;
928 public static final int NETWORK_WIFI_TX_BYTES = 3;
929
930 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_BYTES + 1;
931
932 public abstract long getNetworkActivityCount(int type, int which);
933
The Android Open Source Project10592532009-03-18 17:39:46 -0700934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 * Return whether we are currently running on battery.
936 */
937 public abstract boolean getIsOnBattery();
938
939 /**
940 * Returns a SparseArray containing the statistics for each uid.
941 */
942 public abstract SparseArray<? extends Uid> getUidStats();
943
944 /**
945 * Returns the current battery uptime in microseconds.
946 *
947 * @param curTime the amount of elapsed realtime in microseconds.
948 */
949 public abstract long getBatteryUptime(long curTime);
950
951 /**
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700952 * @deprecated use getRadioDataUptime
953 */
954 public long getRadioDataUptimeMs() {
955 return getRadioDataUptime() / 1000;
956 }
957
958 /**
959 * Returns the time that the radio was on for data transfers.
960 * @return the uptime in microseconds while unplugged
961 */
962 public abstract long getRadioDataUptime();
963
964 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 * Returns the current battery realtime in microseconds.
966 *
967 * @param curTime the amount of elapsed realtime in microseconds.
968 */
969 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -0700970
971 /**
Evan Millar633a1742009-04-02 16:36:33 -0700972 * Returns the battery percentage level at the last time the device was unplugged from power, or
973 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -0700974 */
Evan Millar633a1742009-04-02 16:36:33 -0700975 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -0700976
977 /**
Evan Millar633a1742009-04-02 16:36:33 -0700978 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
979 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -0700980 */
Evan Millar633a1742009-04-02 16:36:33 -0700981 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
983 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700984 * Get the amount the battery has discharged since the stats were
985 * last reset after charging, as a lower-end approximation.
986 */
987 public abstract int getLowDischargeAmountSinceCharge();
988
989 /**
990 * Get the amount the battery has discharged since the stats were
991 * last reset after charging, as an upper-end approximation.
992 */
993 public abstract int getHighDischargeAmountSinceCharge();
994
995 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800996 * Get the amount the battery has discharged while the screen was on,
997 * since the last time power was unplugged.
998 */
999 public abstract int getDischargeAmountScreenOn();
1000
1001 /**
1002 * Get the amount the battery has discharged while the screen was on,
1003 * since the last time the device was charged.
1004 */
1005 public abstract int getDischargeAmountScreenOnSinceCharge();
1006
1007 /**
1008 * Get the amount the battery has discharged while the screen was off,
1009 * since the last time power was unplugged.
1010 */
1011 public abstract int getDischargeAmountScreenOff();
1012
1013 /**
1014 * Get the amount the battery has discharged while the screen was off,
1015 * since the last time the device was charged.
1016 */
1017 public abstract int getDischargeAmountScreenOffSinceCharge();
1018
1019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * Returns the total, last, or current battery uptime in microseconds.
1021 *
1022 * @param curTime the elapsed realtime in microseconds.
1023 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1024 */
1025 public abstract long computeBatteryUptime(long curTime, int which);
1026
1027 /**
1028 * Returns the total, last, or current battery realtime in microseconds.
1029 *
1030 * @param curTime the current elapsed realtime in microseconds.
1031 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1032 */
1033 public abstract long computeBatteryRealtime(long curTime, int which);
1034
1035 /**
1036 * Returns the total, last, or current uptime in microseconds.
1037 *
1038 * @param curTime the current elapsed realtime in microseconds.
1039 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1040 */
1041 public abstract long computeUptime(long curTime, int which);
1042
1043 /**
1044 * Returns the total, last, or current realtime in microseconds.
1045 * *
1046 * @param curTime the current elapsed realtime in microseconds.
1047 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1048 */
1049 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001050
1051 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
Amith Yamasanie43530a2009-08-21 13:11:37 -07001053 /** Returns the number of different speeds that the CPU can run at */
1054 public abstract int getCpuSpeedSteps();
1055
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001056 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 long days = seconds / (60 * 60 * 24);
1058 if (days != 0) {
1059 out.append(days);
1060 out.append("d ");
1061 }
1062 long used = days * 60 * 60 * 24;
1063
1064 long hours = (seconds - used) / (60 * 60);
1065 if (hours != 0 || used != 0) {
1066 out.append(hours);
1067 out.append("h ");
1068 }
1069 used += hours * 60 * 60;
1070
1071 long mins = (seconds-used) / 60;
1072 if (mins != 0 || used != 0) {
1073 out.append(mins);
1074 out.append("m ");
1075 }
1076 used += mins * 60;
1077
1078 if (seconds != 0 || used != 0) {
1079 out.append(seconds-used);
1080 out.append("s ");
1081 }
1082 }
1083
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001084 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001086 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 sb.append((time - (sec * 100)) * 10);
1088 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001091 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001093 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 sb.append(time - (sec * 1000));
1095 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
1098 private final String formatRatioLocked(long num, long den) {
1099 if (den == 0L) {
1100 return "---%";
1101 }
1102 float perc = ((float)num) / ((float)den) * 100;
1103 mFormatBuilder.setLength(0);
1104 mFormatter.format("%.1f%%", perc);
1105 return mFormatBuilder.toString();
1106 }
1107
Evan Millar22ac0432009-03-31 11:33:18 -07001108 private final String formatBytesLocked(long bytes) {
1109 mFormatBuilder.setLength(0);
1110
1111 if (bytes < BYTES_PER_KB) {
1112 return bytes + "B";
1113 } else if (bytes < BYTES_PER_MB) {
1114 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1115 return mFormatBuilder.toString();
1116 } else if (bytes < BYTES_PER_GB){
1117 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1118 return mFormatBuilder.toString();
1119 } else {
1120 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1121 return mFormatBuilder.toString();
1122 }
1123 }
1124
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001125 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1126 if (timer != null) {
1127 // Convert from microseconds to milliseconds with rounding
1128 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1129 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1130 return totalTimeMillis;
1131 }
1132 return 0;
1133 }
1134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 *
1137 * @param sb a StringBuilder object.
1138 * @param timer a Timer object contining the wakelock times.
1139 * @param batteryRealtime the current on-battery time in microseconds.
1140 * @param name the name of the wakelock.
1141 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1142 * @param linePrefix a String to be prepended to each line of output.
1143 * @return the line prefix
1144 */
1145 private static final String printWakeLock(StringBuilder sb, Timer timer,
1146 long batteryRealtime, String name, int which, String linePrefix) {
1147
1148 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001149 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150
Evan Millarc64edde2009-04-18 12:26:32 -07001151 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if (totalTimeMillis != 0) {
1153 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001154 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001155 if (name != null) {
1156 sb.append(name);
1157 sb.append(' ');
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 sb.append('(');
1160 sb.append(count);
1161 sb.append(" times)");
1162 return ", ";
1163 }
1164 }
1165 return linePrefix;
1166 }
1167
1168 /**
1169 * Checkin version of wakelock printer. Prints simple comma-separated list.
1170 *
1171 * @param sb a StringBuilder object.
1172 * @param timer a Timer object contining the wakelock times.
1173 * @param now the current time in microseconds.
1174 * @param name the name of the wakelock.
1175 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1176 * @param linePrefix a String to be prepended to each line of output.
1177 * @return the line prefix
1178 */
1179 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001180 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 long totalTimeMicros = 0;
1182 int count = 0;
1183 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001184 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1185 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 sb.append(linePrefix);
1188 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1189 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001190 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 sb.append(count);
1192 return ",";
1193 }
1194
1195 /**
1196 * Dump a comma-separated line of values for terse checkin mode.
1197 *
1198 * @param pw the PageWriter to dump log to
1199 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1200 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1201 * @param args type-dependent data arguments
1202 */
1203 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1204 Object... args ) {
1205 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1206 pw.print(uid); pw.print(',');
1207 pw.print(category); pw.print(',');
1208 pw.print(type);
1209
1210 for (Object arg : args) {
1211 pw.print(',');
1212 pw.print(arg);
1213 }
1214 pw.print('\n');
1215 }
1216
1217 /**
1218 * Checkin server version of dump to produce more compact, computer-readable log.
1219 *
1220 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001222 public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1224 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1225 final long batteryUptime = getBatteryUptime(rawUptime);
1226 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1227 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1228 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1229 final long totalRealtime = computeRealtime(rawRealtime, which);
1230 final long totalUptime = computeUptime(rawUptime, which);
1231 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1232 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001233 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001234 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001235 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236
1237 StringBuilder sb = new StringBuilder(128);
1238
Evan Millar22ac0432009-03-31 11:33:18 -07001239 SparseArray<? extends Uid> uidStats = getUidStats();
1240 final int NU = uidStats.size();
1241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 // Dump "battery" stat
1245 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001246 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001247 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1248 totalRealtime / 1000, totalUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
Evan Millar22ac0432009-03-31 11:33:18 -07001250 // Calculate total network and wakelock times across all uids.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001251 long mobileRxTotal = 0;
1252 long mobileTxTotal = 0;
1253 long wifiRxTotal = 0;
1254 long wifiTxTotal = 0;
Evan Millar22ac0432009-03-31 11:33:18 -07001255 long fullWakeLockTimeTotal = 0;
1256 long partialWakeLockTimeTotal = 0;
1257
1258 for (int iu = 0; iu < NU; iu++) {
1259 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001260 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1261 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1262 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1263 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1264
Evan Millar22ac0432009-03-31 11:33:18 -07001265 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1266 if (wakelocks.size() > 0) {
1267 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1268 : wakelocks.entrySet()) {
1269 Uid.Wakelock wl = ent.getValue();
1270
1271 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1272 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001273 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001274 }
1275
1276 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1277 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001278 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001279 batteryRealtime, which);
1280 }
1281 }
1282 }
1283 }
1284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 // Dump misc stats
1286 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001287 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001288 wifiRunningTime / 1000, bluetoothOnTime / 1000,
1289 mobileRxTotal, mobileTxTotal, wifiRxTotal, wifiTxTotal,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001290 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1291 getInputEventCount(which));
1292
1293 // Dump screen brightness stats
1294 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1295 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1296 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1297 }
1298 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001299
Dianne Hackborn627bba72009-03-24 22:32:56 -07001300 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001301 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1302 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001303 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1304 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001305 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001306 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1307 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001308 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001309 args[i] = getPhoneSignalStrengthCount(i, which);
1310 }
1311 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001312
1313 // Dump network type stats
1314 args = new Object[NUM_DATA_CONNECTION_TYPES];
1315 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1316 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1317 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001318 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1319 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1320 args[i] = getPhoneDataConnectionCount(i, which);
1321 }
1322 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001323
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001324 if (which == STATS_SINCE_UNPLUGGED) {
Evan Millare84de8d2009-04-02 22:16:12 -07001325 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001326 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001329 if (which == STATS_SINCE_UNPLUGGED) {
1330 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1331 getDischargeStartLevel()-getDischargeCurrentLevel(),
1332 getDischargeStartLevel()-getDischargeCurrentLevel(),
1333 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1334 } else {
1335 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1336 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1337 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1338 }
1339
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001340 if (reqUid < 0) {
1341 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1342 if (kernelWakelocks.size() > 0) {
1343 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1344 sb.setLength(0);
1345 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1346
1347 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1348 sb.toString());
1349 }
Evan Millarc64edde2009-04-18 12:26:32 -07001350 }
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 for (int iu = 0; iu < NU; iu++) {
1354 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001355 if (reqUid >= 0 && uid != reqUid) {
1356 continue;
1357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 Uid u = uidStats.valueAt(iu);
1359 // Dump Network stats per uid, if any
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001360 long mobileRx = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1361 long mobileTx = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1362 long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1363 long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001364 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001365 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001366 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001367
1368 if (mobileRx > 0 || mobileTx > 0 || wifiRx > 0 || wifiTx > 0) {
1369 dumpLine(pw, uid, category, NETWORK_DATA, mobileRx, mobileTx, wifiRx, wifiTx);
1370 }
1371
Nick Pelly6ccaa542012-06-15 15:22:47 -07001372 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001373 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001374 dumpLine(pw, uid, category, WIFI_DATA,
1375 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377
Dianne Hackborn617f8772009-03-31 15:04:46 -07001378 if (u.hasUserActivity()) {
1379 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1380 boolean hasData = false;
1381 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1382 int val = u.getUserActivityCount(i, which);
1383 args[i] = val;
1384 if (val != 0) hasData = true;
1385 }
1386 if (hasData) {
1387 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1388 }
1389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1392 if (wakelocks.size() > 0) {
1393 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1394 : wakelocks.entrySet()) {
1395 Uid.Wakelock wl = ent.getValue();
1396 String linePrefix = "";
1397 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001398 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1399 batteryRealtime, "f", which, linePrefix);
1400 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1401 batteryRealtime, "p", which, linePrefix);
1402 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1403 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404
1405 // Only log if we had at lease one wakelock...
1406 if (sb.length() > 0) {
1407 dumpLine(pw, uid, category, WAKELOCK_DATA, ent.getKey(), sb.toString());
1408 }
1409 }
1410 }
1411
1412 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1413 if (sensors.size() > 0) {
1414 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1415 : sensors.entrySet()) {
1416 Uid.Sensor se = ent.getValue();
1417 int sensorNumber = ent.getKey();
1418 Timer timer = se.getSensorTime();
1419 if (timer != null) {
1420 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001421 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1422 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 if (totalTime != 0) {
1424 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1425 }
1426 }
1427 }
1428 }
1429
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001430 Timer vibTimer = u.getVibratorOnTimer();
1431 if (vibTimer != null) {
1432 // Convert from microseconds to milliseconds with rounding
1433 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1434 int count = vibTimer.getCountLocked(which);
1435 if (totalTime != 0) {
1436 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1437 }
1438 }
1439
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001440 Timer fgTimer = u.getForegroundActivityTimer();
1441 if (fgTimer != null) {
1442 // Convert from microseconds to milliseconds with rounding
1443 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1444 int count = fgTimer.getCountLocked(which);
1445 if (totalTime != 0) {
1446 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1447 }
1448 }
1449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1451 if (processStats.size() > 0) {
1452 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1453 : processStats.entrySet()) {
1454 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001455
1456 final long userMillis = ps.getUserTime(which) * 10;
1457 final long systemMillis = ps.getSystemTime(which) * 10;
1458 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1459 final long starts = ps.getStarts(which);
1460
1461 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1462 || starts != 0) {
1463 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1464 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467 }
1468
1469 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1470 if (packageStats.size() > 0) {
1471 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1472 : packageStats.entrySet()) {
1473
1474 Uid.Pkg ps = ent.getValue();
1475 int wakeups = ps.getWakeups(which);
1476 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1477 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1478 : serviceStats.entrySet()) {
1479 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1480 long startTime = ss.getStartTime(batteryUptime, which);
1481 int starts = ss.getStarts(which);
1482 int launches = ss.getLaunches(which);
1483 if (startTime != 0 || starts != 0 || launches != 0) {
1484 dumpLine(pw, uid, category, APK_DATA,
1485 wakeups, // wakeup alarms
1486 ent.getKey(), // Apk
1487 sent.getKey(), // service
1488 startTime / 1000, // time spent started, in ms
1489 starts,
1490 launches);
1491 }
1492 }
1493 }
1494 }
1495 }
1496 }
1497
Dianne Hackborn81038902012-11-26 17:04:09 -08001498 static final class TimerEntry {
1499 final String mName;
1500 final int mId;
1501 final BatteryStats.Timer mTimer;
1502 final long mTime;
1503 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1504 mName = name;
1505 mId = id;
1506 mTimer = timer;
1507 mTime = time;
1508 }
1509 }
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 @SuppressWarnings("unused")
Dianne Hackborn81038902012-11-26 17:04:09 -08001512 public final void dumpLocked(PrintWriter pw, String prefix, final int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1514 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1515 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001516 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517
1518 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1519 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1520 final long totalRealtime = computeRealtime(rawRealtime, which);
1521 final long totalUptime = computeUptime(rawUptime, which);
1522
1523 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001524
1525 SparseArray<? extends Uid> uidStats = getUidStats();
1526 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001528 sb.setLength(0);
1529 sb.append(prefix);
1530 sb.append(" Time on battery: ");
1531 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1532 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1533 sb.append(") realtime, ");
1534 formatTimeMs(sb, whichBatteryUptime / 1000);
1535 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1536 sb.append(") uptime");
1537 pw.println(sb.toString());
1538 sb.setLength(0);
1539 sb.append(prefix);
1540 sb.append(" Total run time: ");
1541 formatTimeMs(sb, totalRealtime / 1000);
1542 sb.append("realtime, ");
1543 formatTimeMs(sb, totalUptime / 1000);
1544 sb.append("uptime, ");
1545 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546
The Android Open Source Project10592532009-03-18 17:39:46 -07001547 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1548 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001549 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001550 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1551 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001552 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001553 sb.append(prefix);
1554 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1555 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1556 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1557 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1558 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1559 sb.append(")");
1560 pw.println(sb.toString());
1561 sb.setLength(0);
1562 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001563 sb.append(" Screen brightnesses: ");
1564 boolean didOne = false;
1565 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1566 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1567 if (time == 0) {
1568 continue;
1569 }
1570 if (didOne) sb.append(", ");
1571 didOne = true;
1572 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1573 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001574 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001575 sb.append("(");
1576 sb.append(formatRatioLocked(time, screenOnTime));
1577 sb.append(")");
1578 }
1579 if (!didOne) sb.append("No activity");
1580 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001581
Evan Millar22ac0432009-03-31 11:33:18 -07001582 // Calculate total network and wakelock times across all uids.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001583 long mobileRxTotal = 0;
1584 long mobileTxTotal = 0;
1585 long wifiRxTotal = 0;
1586 long wifiTxTotal = 0;
Evan Millar22ac0432009-03-31 11:33:18 -07001587 long fullWakeLockTimeTotalMicros = 0;
1588 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001589
1590 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1591 @Override
1592 public int compare(TimerEntry lhs, TimerEntry rhs) {
1593 long lhsTime = lhs.mTime;
1594 long rhsTime = rhs.mTime;
1595 if (lhsTime < rhsTime) {
1596 return 1;
1597 }
1598 if (lhsTime > rhsTime) {
1599 return -1;
1600 }
1601 return 0;
1602 }
1603 };
1604
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001605 if (reqUid < 0) {
1606 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1607 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001608 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001609 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001610 BatteryStats.Timer timer = ent.getValue();
1611 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1612 if (totalTimeMillis > 0) {
1613 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1614 }
1615 }
1616 Collections.sort(timers, timerComparator);
1617 for (int i=0; i<timers.size(); i++) {
1618 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001619 String linePrefix = ": ";
1620 sb.setLength(0);
1621 sb.append(prefix);
1622 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001623 sb.append(timer.mName);
1624 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1625 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001626 if (!linePrefix.equals(": ")) {
1627 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001628 // Only print out wake locks that were held
1629 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001630 }
Evan Millarc64edde2009-04-18 12:26:32 -07001631 }
Evan Millarc64edde2009-04-18 12:26:32 -07001632 }
1633 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001634
1635 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1636
Evan Millar22ac0432009-03-31 11:33:18 -07001637 for (int iu = 0; iu < NU; iu++) {
1638 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001639 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1640 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1641 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1642 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1643
Evan Millar22ac0432009-03-31 11:33:18 -07001644 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1645 if (wakelocks.size() > 0) {
1646 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1647 : wakelocks.entrySet()) {
1648 Uid.Wakelock wl = ent.getValue();
1649
1650 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1651 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001652 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001653 batteryRealtime, which);
1654 }
1655
1656 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1657 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001658 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001659 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001660 if (totalTimeMicros > 0) {
1661 if (reqUid < 0) {
1662 // Only show the ordered list of all wake
1663 // locks if the caller is not asking for data
1664 // about a specific uid.
1665 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1666 partialWakeTimer, totalTimeMicros));
1667 }
1668 partialWakeLockTimeTotalMicros += totalTimeMicros;
1669 }
Evan Millar22ac0432009-03-31 11:33:18 -07001670 }
1671 }
1672 }
1673 }
1674
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001675 pw.print(prefix);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001676 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotal));
1677 pw.print(", Total sent: "); pw.println(formatBytesLocked(mobileTxTotal));
1678 pw.print(prefix);
1679 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotal));
1680 pw.print(", Total sent: "); pw.println(formatBytesLocked(wifiTxTotal));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001681 sb.setLength(0);
1682 sb.append(prefix);
1683 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1684 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001685 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001686 (partialWakeLockTimeTotalMicros + 500) / 1000);
1687 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001688
Dianne Hackborn627bba72009-03-24 22:32:56 -07001689 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001690 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001691 sb.append(" Signal levels: ");
1692 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001693 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001694 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1695 if (time == 0) {
1696 continue;
1697 }
1698 if (didOne) sb.append(", ");
1699 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001700 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001701 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001702 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001703 sb.append("(");
1704 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001705 sb.append(") ");
1706 sb.append(getPhoneSignalStrengthCount(i, which));
1707 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001708 }
1709 if (!didOne) sb.append("No activity");
1710 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001711
1712 sb.setLength(0);
1713 sb.append(prefix);
1714 sb.append(" Signal scanning time: ");
1715 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1716 pw.println(sb.toString());
1717
Dianne Hackborn627bba72009-03-24 22:32:56 -07001718 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001719 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001720 sb.append(" Radio types: ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001721 didOne = false;
1722 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1723 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1724 if (time == 0) {
1725 continue;
1726 }
1727 if (didOne) sb.append(", ");
1728 didOne = true;
1729 sb.append(DATA_CONNECTION_NAMES[i]);
1730 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001731 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001732 sb.append("(");
1733 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001734 sb.append(") ");
1735 sb.append(getPhoneDataConnectionCount(i, which));
1736 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001737 }
1738 if (!didOne) sb.append("No activity");
1739 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001740
1741 sb.setLength(0);
1742 sb.append(prefix);
1743 sb.append(" Radio data uptime when unplugged: ");
1744 sb.append(getRadioDataUptime() / 1000);
1745 sb.append(" ms");
1746 pw.println(sb.toString());
1747
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001748 sb.setLength(0);
1749 sb.append(prefix);
1750 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1751 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1752 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1753 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1754 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1755 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1756 sb.append(")");
1757 pw.println(sb.toString());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001758
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 pw.println(" ");
1760
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001761 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001762 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001763 pw.print(prefix); pw.println(" Device is currently unplugged");
1764 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1765 pw.println(getDischargeStartLevel());
1766 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1767 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07001768 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001769 pw.print(prefix); pw.println(" Device is currently plugged into power");
1770 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1771 pw.println(getDischargeStartLevel());
1772 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1773 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001774 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001775 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1776 pw.println(getDischargeAmountScreenOn());
1777 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1778 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001779 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001780 } else {
1781 pw.print(prefix); pw.println(" Device battery use since last full charge");
1782 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1783 pw.println(getLowDischargeAmountSinceCharge());
1784 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1785 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001786 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1787 pw.println(getDischargeAmountScreenOnSinceCharge());
1788 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1789 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08001790 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07001791 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001792
1793 if (timers.size() > 0) {
1794 Collections.sort(timers, timerComparator);
1795 pw.print(prefix); pw.println(" All partial wake locks:");
1796 for (int i=0; i<timers.size(); i++) {
1797 TimerEntry timer = timers.get(i);
1798 sb.setLength(0);
1799 sb.append(" Wake lock #");
1800 sb.append(timer.mId);
1801 sb.append(" ");
1802 sb.append(timer.mName);
1803 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1804 sb.append(" realtime");
1805 pw.println(sb.toString());
1806 }
1807 timers.clear();
1808 pw.println();
1809 }
Evan Millar22ac0432009-03-31 11:33:18 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 for (int iu=0; iu<NU; iu++) {
1812 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08001813 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001814 continue;
1815 }
1816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 Uid u = uidStats.valueAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 pw.println(prefix + " #" + uid + ":");
1820 boolean uidActivity = false;
1821
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001822 long mobileRxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1823 long mobileTxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1824 long wifiRxBytes = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1825 long wifiTxBytes = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001826 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001827 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001828 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001829
1830 if (mobileRxBytes > 0 || mobileTxBytes > 0) {
1831 pw.print(prefix); pw.print(" Mobile network: ");
1832 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
1833 pw.print(formatBytesLocked(mobileTxBytes)); pw.println(" sent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001835 if (wifiRxBytes > 0 || wifiTxBytes > 0) {
1836 pw.print(prefix); pw.print(" Wi-Fi network: ");
1837 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
1838 pw.print(formatBytesLocked(wifiTxBytes)); pw.println(" sent");
1839 }
1840
Dianne Hackborn617f8772009-03-31 15:04:46 -07001841 if (u.hasUserActivity()) {
1842 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07001843 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 int val = u.getUserActivityCount(i, which);
1845 if (val != 0) {
1846 if (!hasData) {
1847 sb.setLength(0);
1848 sb.append(" User activity: ");
1849 hasData = true;
1850 } else {
1851 sb.append(", ");
1852 }
1853 sb.append(val);
1854 sb.append(" ");
1855 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1856 }
1857 }
1858 if (hasData) {
1859 pw.println(sb.toString());
1860 }
1861 }
1862
Nick Pelly6ccaa542012-06-15 15:22:47 -07001863 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001864 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001865 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001866 sb.append(prefix); sb.append(" Wifi Running: ");
1867 formatTimeMs(sb, uidWifiRunningTime / 1000);
1868 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001869 whichBatteryRealtime)); sb.append(")\n");
1870 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001871 formatTimeMs(sb, fullWifiLockOnTime / 1000);
1872 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001873 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001874 sb.append(prefix); sb.append(" Wifi Scan: ");
1875 formatTimeMs(sb, wifiScanTime / 1000);
1876 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001877 whichBatteryRealtime)); sb.append(")");
1878 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880
1881 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1882 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001883 long totalFull = 0, totalPartial = 0, totalWindow = 0;
1884 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1886 : wakelocks.entrySet()) {
1887 Uid.Wakelock wl = ent.getValue();
1888 String linePrefix = ": ";
1889 sb.setLength(0);
1890 sb.append(prefix);
1891 sb.append(" Wake lock ");
1892 sb.append(ent.getKey());
1893 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
1894 "full", which, linePrefix);
1895 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
1896 "partial", which, linePrefix);
1897 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
1898 "window", which, linePrefix);
1899 if (!linePrefix.equals(": ")) {
1900 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001901 // Only print out wake locks that were held
1902 pw.println(sb.toString());
1903 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001904 count++;
1905 }
1906 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
1907 batteryRealtime, which);
1908 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
1909 batteryRealtime, which);
1910 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
1911 batteryRealtime, which);
1912 }
1913 if (count > 1) {
1914 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
1915 sb.setLength(0);
1916 sb.append(prefix);
1917 sb.append(" TOTAL wake: ");
1918 boolean needComma = false;
1919 if (totalFull != 0) {
1920 needComma = true;
1921 formatTimeMs(sb, totalFull);
1922 sb.append("full");
1923 }
1924 if (totalPartial != 0) {
1925 if (needComma) {
1926 sb.append(", ");
1927 }
1928 needComma = true;
1929 formatTimeMs(sb, totalPartial);
1930 sb.append("partial");
1931 }
1932 if (totalWindow != 0) {
1933 if (needComma) {
1934 sb.append(", ");
1935 }
1936 needComma = true;
1937 formatTimeMs(sb, totalWindow);
1938 sb.append("window");
1939 }
1940 sb.append(" realtime");
1941 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 }
1945
1946 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1947 if (sensors.size() > 0) {
1948 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1949 : sensors.entrySet()) {
1950 Uid.Sensor se = ent.getValue();
1951 int sensorNumber = ent.getKey();
1952 sb.setLength(0);
1953 sb.append(prefix);
1954 sb.append(" Sensor ");
1955 int handle = se.getHandle();
1956 if (handle == Uid.Sensor.GPS) {
1957 sb.append("GPS");
1958 } else {
1959 sb.append(handle);
1960 }
1961 sb.append(": ");
1962
1963 Timer timer = se.getSensorTime();
1964 if (timer != null) {
1965 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001966 long totalTime = (timer.getTotalTimeLocked(
1967 batteryRealtime, which) + 500) / 1000;
1968 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 //timer.logState();
1970 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001971 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 sb.append("realtime (");
1973 sb.append(count);
1974 sb.append(" times)");
1975 } else {
1976 sb.append("(not used)");
1977 }
1978 } else {
1979 sb.append("(not used)");
1980 }
1981
1982 pw.println(sb.toString());
1983 uidActivity = true;
1984 }
1985 }
1986
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001987 Timer vibTimer = u.getVibratorOnTimer();
1988 if (vibTimer != null) {
1989 // Convert from microseconds to milliseconds with rounding
1990 long totalTime = (vibTimer.getTotalTimeLocked(
1991 batteryRealtime, which) + 500) / 1000;
1992 int count = vibTimer.getCountLocked(which);
1993 //timer.logState();
1994 if (totalTime != 0) {
1995 sb.setLength(0);
1996 sb.append(prefix);
1997 sb.append(" Vibrator: ");
1998 formatTimeMs(sb, totalTime);
1999 sb.append("realtime (");
2000 sb.append(count);
2001 sb.append(" times)");
2002 pw.println(sb.toString());
2003 uidActivity = true;
2004 }
2005 }
2006
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002007 Timer fgTimer = u.getForegroundActivityTimer();
2008 if (fgTimer != null) {
2009 // Convert from microseconds to milliseconds with rounding
2010 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2011 int count = fgTimer.getCountLocked(which);
2012 if (totalTime != 0) {
2013 sb.setLength(0);
2014 sb.append(prefix);
2015 sb.append(" Foreground activities: ");
2016 formatTimeMs(sb, totalTime);
2017 sb.append("realtime (");
2018 sb.append(count);
2019 sb.append(" times)");
2020 pw.println(sb.toString());
2021 uidActivity = true;
2022 }
2023 }
2024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2026 if (processStats.size() > 0) {
2027 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2028 : processStats.entrySet()) {
2029 Uid.Proc ps = ent.getValue();
2030 long userTime;
2031 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002032 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002034 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035
2036 userTime = ps.getUserTime(which);
2037 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002038 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002040 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002041 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002043 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002044 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002045 sb.setLength(0);
2046 sb.append(prefix); sb.append(" Proc ");
2047 sb.append(ent.getKey()); sb.append(":\n");
2048 sb.append(prefix); sb.append(" CPU: ");
2049 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002050 formatTime(sb, systemTime); sb.append("krn ; ");
2051 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002052 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002053 sb.append("\n"); sb.append(prefix); sb.append(" ");
2054 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002055 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002056 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002057 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002058 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002059 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002060 pw.print(prefix); pw.print(" * Killed for ");
2061 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2062 pw.print("wake lock");
2063 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2064 pw.print("cpu");
2065 } else {
2066 pw.print("unknown");
2067 }
2068 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002069 TimeUtils.formatDuration(ew.usedTime, pw);
2070 pw.print(" over ");
2071 TimeUtils.formatDuration(ew.overTime, pw);
2072 pw.print(" (");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002073 pw.print((ew.usedTime*100)/ew.overTime);
2074 pw.println("%)");
2075 }
2076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 uidActivity = true;
2078 }
2079 }
2080 }
2081
2082 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2083 if (packageStats.size() > 0) {
2084 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2085 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002086 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 boolean apkActivity = false;
2088 Uid.Pkg ps = ent.getValue();
2089 int wakeups = ps.getWakeups(which);
2090 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002091 pw.print(prefix); pw.print(" ");
2092 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 apkActivity = true;
2094 }
2095 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2096 if (serviceStats.size() > 0) {
2097 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2098 : serviceStats.entrySet()) {
2099 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2100 long startTime = ss.getStartTime(batteryUptime, which);
2101 int starts = ss.getStarts(which);
2102 int launches = ss.getLaunches(which);
2103 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002104 sb.setLength(0);
2105 sb.append(prefix); sb.append(" Service ");
2106 sb.append(sent.getKey()); sb.append(":\n");
2107 sb.append(prefix); sb.append(" Created for: ");
2108 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002109 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002110 sb.append(prefix); sb.append(" Starts: ");
2111 sb.append(starts);
2112 sb.append(", launches: "); sb.append(launches);
2113 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 apkActivity = true;
2115 }
2116 }
2117 }
2118 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002119 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121 uidActivity = true;
2122 }
2123 }
2124 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002125 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 }
2128 }
2129
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002130 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, BitDescription[] descriptions) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002131 int diff = oldval ^ newval;
2132 if (diff == 0) return;
2133 for (int i=0; i<descriptions.length; i++) {
2134 BitDescription bd = descriptions[i];
2135 if ((diff&bd.mask) != 0) {
2136 if (bd.shift < 0) {
2137 pw.print((newval&bd.mask) != 0 ? " +" : " -");
2138 pw.print(bd.name);
2139 } else {
2140 pw.print(" ");
2141 pw.print(bd.name);
2142 pw.print("=");
2143 int val = (newval&bd.mask)>>bd.shift;
2144 if (bd.values != null && val >= 0 && val < bd.values.length) {
2145 pw.print(bd.values[val]);
2146 } else {
2147 pw.print(val);
2148 }
2149 }
2150 }
2151 }
2152 }
2153
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002154 public void prepareForDumpLocked() {
2155 }
2156
2157 public static class HistoryPrinter {
2158 int oldState = 0;
2159 int oldStatus = -1;
2160 int oldHealth = -1;
2161 int oldPlug = -1;
2162 int oldTemp = -1;
2163 int oldVolt = -1;
2164
2165 public void printNextItem(PrintWriter pw, HistoryItem rec, long now) {
2166 pw.print(" ");
2167 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2168 pw.print(" ");
2169 if (rec.cmd == HistoryItem.CMD_START) {
2170 pw.println(" START");
2171 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2172 pw.println(" *OVERFLOW*");
2173 } else {
2174 if (rec.batteryLevel < 10) pw.print("00");
2175 else if (rec.batteryLevel < 100) pw.print("0");
2176 pw.print(rec.batteryLevel);
2177 pw.print(" ");
2178 if (rec.states < 0x10) pw.print("0000000");
2179 else if (rec.states < 0x100) pw.print("000000");
2180 else if (rec.states < 0x1000) pw.print("00000");
2181 else if (rec.states < 0x10000) pw.print("0000");
2182 else if (rec.states < 0x100000) pw.print("000");
2183 else if (rec.states < 0x1000000) pw.print("00");
2184 else if (rec.states < 0x10000000) pw.print("0");
2185 pw.print(Integer.toHexString(rec.states));
2186 if (oldStatus != rec.batteryStatus) {
2187 oldStatus = rec.batteryStatus;
2188 pw.print(" status=");
2189 switch (oldStatus) {
2190 case BatteryManager.BATTERY_STATUS_UNKNOWN:
2191 pw.print("unknown");
2192 break;
2193 case BatteryManager.BATTERY_STATUS_CHARGING:
2194 pw.print("charging");
2195 break;
2196 case BatteryManager.BATTERY_STATUS_DISCHARGING:
2197 pw.print("discharging");
2198 break;
2199 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
2200 pw.print("not-charging");
2201 break;
2202 case BatteryManager.BATTERY_STATUS_FULL:
2203 pw.print("full");
2204 break;
2205 default:
2206 pw.print(oldStatus);
2207 break;
2208 }
2209 }
2210 if (oldHealth != rec.batteryHealth) {
2211 oldHealth = rec.batteryHealth;
2212 pw.print(" health=");
2213 switch (oldHealth) {
2214 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
2215 pw.print("unknown");
2216 break;
2217 case BatteryManager.BATTERY_HEALTH_GOOD:
2218 pw.print("good");
2219 break;
2220 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
2221 pw.print("overheat");
2222 break;
2223 case BatteryManager.BATTERY_HEALTH_DEAD:
2224 pw.print("dead");
2225 break;
2226 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
2227 pw.print("over-voltage");
2228 break;
2229 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
2230 pw.print("failure");
2231 break;
2232 default:
2233 pw.print(oldHealth);
2234 break;
2235 }
2236 }
2237 if (oldPlug != rec.batteryPlugType) {
2238 oldPlug = rec.batteryPlugType;
2239 pw.print(" plug=");
2240 switch (oldPlug) {
2241 case 0:
2242 pw.print("none");
2243 break;
2244 case BatteryManager.BATTERY_PLUGGED_AC:
2245 pw.print("ac");
2246 break;
2247 case BatteryManager.BATTERY_PLUGGED_USB:
2248 pw.print("usb");
2249 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002250 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
2251 pw.print("wireless");
2252 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002253 default:
2254 pw.print(oldPlug);
2255 break;
2256 }
2257 }
2258 if (oldTemp != rec.batteryTemperature) {
2259 oldTemp = rec.batteryTemperature;
2260 pw.print(" temp=");
2261 pw.print(oldTemp);
2262 }
2263 if (oldVolt != rec.batteryVoltage) {
2264 oldVolt = rec.batteryVoltage;
2265 pw.print(" volt=");
2266 pw.print(oldVolt);
2267 }
2268 printBitDescriptions(pw, oldState, rec.states,
2269 HISTORY_STATE_DESCRIPTIONS);
2270 pw.println();
2271 }
2272 oldState = rec.states;
2273 }
2274 }
2275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 /**
2277 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2278 *
2279 * @param pw a Printer to receive the dump output.
2280 */
2281 @SuppressWarnings("unused")
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002282 public void dumpLocked(PrintWriter pw, boolean isUnpluggedOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002283 prepareForDumpLocked();
2284
2285 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2286
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002287 final HistoryItem rec = new HistoryItem();
2288 if (startIteratingHistoryLocked()) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002289 pw.println("Battery History:");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002290 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002291 while (getNextHistoryLocked(rec)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002292 hprinter.printNextItem(pw, rec, now);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002293 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002294 finishIteratingHistoryLocked();
2295 pw.println("");
2296 }
2297
2298 if (startIteratingOldHistoryLocked()) {
2299 pw.println("Old battery History:");
2300 HistoryPrinter hprinter = new HistoryPrinter();
2301 while (getNextOldHistoryLocked(rec)) {
2302 hprinter.printNextItem(pw, rec, now);
2303 }
2304 finishIteratingOldHistoryLocked();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002305 pw.println("");
2306 }
2307
2308 SparseArray<? extends Uid> uidStats = getUidStats();
2309 final int NU = uidStats.size();
2310 boolean didPid = false;
2311 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002312 for (int i=0; i<NU; i++) {
2313 Uid uid = uidStats.valueAt(i);
2314 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2315 if (pids != null) {
2316 for (int j=0; j<pids.size(); j++) {
2317 Uid.Pid pid = pids.valueAt(j);
2318 if (!didPid) {
2319 pw.println("Per-PID Stats:");
2320 didPid = true;
2321 }
2322 long time = pid.mWakeSum + (pid.mWakeStart != 0
2323 ? (nowRealtime - pid.mWakeStart) : 0);
2324 pw.print(" PID "); pw.print(pids.keyAt(j));
2325 pw.print(" wake time: ");
2326 TimeUtils.formatDuration(time, pw);
2327 pw.println("");
2328 }
2329 }
2330 }
2331 if (didPid) {
2332 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002333 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002334
2335 if (!isUnpluggedOnly) {
2336 pw.println("Statistics since last charge:");
2337 pw.println(" System starts: " + getStartCount()
2338 + ", currently on battery: " + getIsOnBattery());
2339 dumpLocked(pw, "", STATS_SINCE_CHARGED, -1);
2340 pw.println("");
2341 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002342 pw.println("Statistics since last unplugged:");
2343 dumpLocked(pw, "", STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 }
2345
2346 @SuppressWarnings("unused")
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002347 public void dumpCheckinLocked(
2348 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002349 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
Dianne Hackborne4a59512010-12-07 11:08:07 -08002351 if (apps != null) {
2352 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2353 for (int i=0; i<apps.size(); i++) {
2354 ApplicationInfo ai = apps.get(i);
2355 ArrayList<String> pkgs = uids.get(ai.uid);
2356 if (pkgs == null) {
2357 pkgs = new ArrayList<String>();
2358 uids.put(ai.uid, pkgs);
2359 }
2360 pkgs.add(ai.packageName);
2361 }
2362 SparseArray<? extends Uid> uidStats = getUidStats();
2363 final int NU = uidStats.size();
2364 String[] lineArgs = new String[2];
2365 for (int i=0; i<NU; i++) {
2366 int uid = uidStats.keyAt(i);
2367 ArrayList<String> pkgs = uids.get(uid);
2368 if (pkgs != null) {
2369 for (int j=0; j<pkgs.size(); j++) {
2370 lineArgs[0] = Integer.toString(uid);
2371 lineArgs[1] = pkgs.get(j);
2372 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2373 (Object[])lineArgs);
2374 }
2375 }
2376 }
2377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 if (isUnpluggedOnly) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002379 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 }
2381 else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002382 dumpCheckinLocked(pw, STATS_SINCE_CHARGED, -1);
2383 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386}