blob: 12646bde77f2aacbd66fb4225e1093d4a93e7ca7 [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;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070044
45 /** @hide */
46 public static final String SERVICE_NAME = "batterystats";
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 /**
49 * A constant indicating a partial wake lock timer.
50 */
51 public static final int WAKE_TYPE_PARTIAL = 0;
52
53 /**
54 * A constant indicating a full wake lock timer.
55 */
56 public static final int WAKE_TYPE_FULL = 1;
57
58 /**
59 * A constant indicating a window wake lock timer.
60 */
61 public static final int WAKE_TYPE_WINDOW = 2;
62
63 /**
64 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
66 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070067
68 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070069 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070070 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070072
73 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070074 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070075 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070076 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070077
78 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070079 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070080 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Robert Greenwalt5347bd42009-05-13 15:10:16 -070083 /**
84 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 */
86 public static final int WIFI_MULTICAST_ENABLED = 7;
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070089 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070090 */
91 public static final int AUDIO_TURNED_ON = 7;
92
93 /**
94 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070095 */
96 public static final int VIDEO_TURNED_ON = 8;
97
98 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -080099 * A constant indicating a vibrator on timer
100 */
101 public static final int VIBRATOR_ON = 9;
102
103 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700104 * A constant indicating a foreground activity timer
105 */
106 public static final int FOREGROUND_ACTIVITY = 10;
107
108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * Include all of the data in the stats, including previously saved data.
110 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700111 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 /**
114 * Include only the last run in the stats.
115 */
116 public static final int STATS_LAST = 1;
117
118 /**
119 * Include only the current run in the stats.
120 */
121 public static final int STATS_CURRENT = 2;
122
123 /**
124 * Include only the run since the last time the device was unplugged in the stats.
125 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700126 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700127
128 // NOTE: Update this list if you add/change any stats above.
129 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700130 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700131 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 /**
134 * Bump the version on this if the checkin format changes.
135 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700136 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700137
138 private static final long BYTES_PER_KB = 1024;
139 private static final long BYTES_PER_MB = 1048576; // 1024^2
140 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Dianne Hackborne4a59512010-12-07 11:08:07 -0800143 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700145 private static final String PROCESS_DATA = "pr";
146 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800147 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700148 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700149 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700150 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700151 private static final String NETWORK_DATA = "nt";
152 private static final String USER_ACTIVITY_DATA = "ua";
153 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800154 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700155 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700156 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700157 private static final String MISC_DATA = "m";
158 private static final String SCREEN_BRIGHTNESS_DATA = "br";
159 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700160 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700161 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
162 private static final String DATA_CONNECTION_TIME_DATA = "dct";
163 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700165 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private final Formatter mFormatter = new Formatter(mFormatBuilder);
167
168 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700169 * State for keeping track of counting information.
170 */
171 public static abstract class Counter {
172
173 /**
174 * Returns the count associated with this Counter for the
175 * selected type of statistics.
176 *
177 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
178 */
Evan Millarc64edde2009-04-18 12:26:32 -0700179 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700180
181 /**
182 * Temporary for debugging.
183 */
184 public abstract void logState(Printer pw, String prefix);
185 }
186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * State for keeping track of timing information.
189 */
190 public static abstract class Timer {
191
192 /**
193 * Returns the count associated with this Timer for the
194 * selected type of statistics.
195 *
196 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
197 */
Evan Millarc64edde2009-04-18 12:26:32 -0700198 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 /**
201 * Returns the total time in microseconds associated with this Timer for the
202 * selected type of statistics.
203 *
204 * @param batteryRealtime system realtime on battery in microseconds
205 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
206 * @return a time in microseconds
207 */
Evan Millarc64edde2009-04-18 12:26:32 -0700208 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
211 * Temporary for debugging.
212 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700213 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215
216 /**
217 * The statistics associated with a particular uid.
218 */
219 public static abstract class Uid {
220
221 /**
222 * Returns a mapping containing wakelock statistics.
223 *
224 * @return a Map from Strings to Uid.Wakelock objects.
225 */
226 public abstract Map<String, ? extends Wakelock> getWakelockStats();
227
228 /**
229 * The statistics associated with a particular wake lock.
230 */
231 public static abstract class Wakelock {
232 public abstract Timer getWakeTime(int type);
233 }
234
235 /**
236 * Returns a mapping containing sensor statistics.
237 *
238 * @return a Map from Integer sensor ids to Uid.Sensor objects.
239 */
240 public abstract Map<Integer, ? extends Sensor> getSensorStats();
241
242 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700243 * Returns a mapping containing active process data.
244 */
245 public abstract SparseArray<? extends Pid> getPidStats();
246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * Returns a mapping containing process statistics.
249 *
250 * @return a Map from Strings to Uid.Proc objects.
251 */
252 public abstract Map<String, ? extends Proc> getProcessStats();
253
254 /**
255 * Returns a mapping containing package statistics.
256 *
257 * @return a Map from Strings to Uid.Pkg objects.
258 */
259 public abstract Map<String, ? extends Pkg> getPackageStats();
260
261 /**
262 * {@hide}
263 */
264 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700265
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700266 public abstract void noteWifiRunningLocked();
267 public abstract void noteWifiStoppedLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700268 public abstract void noteFullWifiLockAcquiredLocked();
269 public abstract void noteFullWifiLockReleasedLocked();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700270 public abstract void noteWifiScanStartedLocked();
271 public abstract void noteWifiScanStoppedLocked();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700272 public abstract void noteWifiMulticastEnabledLocked();
273 public abstract void noteWifiMulticastDisabledLocked();
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700274 public abstract void noteAudioTurnedOnLocked();
275 public abstract void noteAudioTurnedOffLocked();
276 public abstract void noteVideoTurnedOnLocked();
277 public abstract void noteVideoTurnedOffLocked();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700278 public abstract void noteActivityResumedLocked();
279 public abstract void noteActivityPausedLocked();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700280 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700281 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700282 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700283 public abstract long getWifiMulticastTime(long batteryRealtime,
284 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700285 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
286 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700287 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800288 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
Dianne Hackborn617f8772009-03-31 15:04:46 -0700290 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700291 * Note that these must match the constants in android.os.PowerManager.
292 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
293 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700294 */
295 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700296 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700297 };
298
Jeff Browndf693de2012-07-27 12:03:38 -0700299 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700300
Dianne Hackborn617f8772009-03-31 15:04:46 -0700301 public abstract void noteUserActivityLocked(int type);
302 public abstract boolean hasUserActivity();
303 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700304
305 public abstract boolean hasNetworkActivity();
306 public abstract long getNetworkActivityCount(int type, int which);
307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800309 /*
310 * FIXME: it's not correct to use this magic value because it
311 * could clash with a sensor handle (which are defined by
312 * the sensor HAL, and therefore out of our control
313 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 // Magic sensor number for the GPS.
315 public static final int GPS = -10000;
316
317 public abstract int getHandle();
318
319 public abstract Timer getSensorTime();
320 }
321
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700322 public class Pid {
323 public long mWakeSum;
324 public long mWakeStart;
325 }
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * The statistics associated with a particular process.
329 */
330 public static abstract class Proc {
331
Dianne Hackborn287952c2010-09-22 22:34:31 -0700332 public static class ExcessivePower {
333 public static final int TYPE_WAKE = 1;
334 public static final int TYPE_CPU = 2;
335
336 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700337 public long overTime;
338 public long usedTime;
339 }
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
342 * Returns the total time (in 1/100 sec) spent executing in user code.
343 *
344 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
345 */
346 public abstract long getUserTime(int which);
347
348 /**
349 * Returns the total time (in 1/100 sec) spent executing in system code.
350 *
351 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
352 */
353 public abstract long getSystemTime(int which);
354
355 /**
356 * Returns the number of times the process has been started.
357 *
358 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
359 */
360 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700361
362 /**
363 * Returns the cpu time spent in microseconds while the process was in the foreground.
364 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
365 * @return foreground cpu time in microseconds
366 */
367 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700368
369 /**
370 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
371 * @param speedStep the index of the CPU speed. This is not the actual speed of the
372 * CPU.
373 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
374 * @see BatteryStats#getCpuSpeedSteps()
375 */
376 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700377
Dianne Hackborn287952c2010-09-22 22:34:31 -0700378 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700379
Dianne Hackborn287952c2010-09-22 22:34:31 -0700380 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
383 /**
384 * The statistics associated with a particular package.
385 */
386 public static abstract class Pkg {
387
388 /**
389 * Returns the number of times this package has done something that could wake up the
390 * device from sleep.
391 *
392 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
393 */
394 public abstract int getWakeups(int which);
395
396 /**
397 * Returns a mapping containing service statistics.
398 */
399 public abstract Map<String, ? extends Serv> getServiceStats();
400
401 /**
402 * The statistics associated with a particular service.
403 */
404 public abstract class Serv {
405
406 /**
407 * Returns the amount of time spent started.
408 *
409 * @param batteryUptime elapsed uptime on battery in microseconds.
410 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
411 * @return
412 */
413 public abstract long getStartTime(long batteryUptime, int which);
414
415 /**
416 * Returns the total number of times startService() has been called.
417 *
418 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
419 */
420 public abstract int getStarts(int which);
421
422 /**
423 * Returns the total number times the service has been launched.
424 *
425 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
426 */
427 public abstract int getLaunches(int which);
428 }
429 }
430 }
431
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700432 public final static class HistoryItem implements Parcelable {
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700433 static final String TAG = "HistoryItem";
434 static final boolean DEBUG = false;
435
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700436 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700437
438 public long time;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700439
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700440 public static final byte CMD_NULL = 0;
441 public static final byte CMD_UPDATE = 1;
442 public static final byte CMD_START = 2;
443 public static final byte CMD_OVERFLOW = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700444
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700445 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700446
447 public byte batteryLevel;
448 public byte batteryStatus;
449 public byte batteryHealth;
450 public byte batteryPlugType;
451
452 public char batteryTemperature;
453 public char batteryVoltage;
454
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700455 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700456 public static final int STATE_BRIGHTNESS_MASK = 0x0000000f;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700457 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700458 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700459 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x000000f0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700460 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 4;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700461 // Constants from ServiceState.STATE_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700462 public static final int STATE_PHONE_STATE_MASK = 0x00000f00;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700463 public static final int STATE_PHONE_STATE_SHIFT = 8;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700464 // Constants from DATA_CONNECTION_*
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700465 public static final int STATE_DATA_CONNECTION_MASK = 0x0000f000;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700466 public static final int STATE_DATA_CONNECTION_SHIFT = 12;
467
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700468 // These states always appear directly in the first int token
469 // of a delta change; they should be ones that change relatively
470 // frequently.
471 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
472 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700473 public static final int STATE_GPS_ON_FLAG = 1<<28;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700474 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
475 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
476 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
Nick Pelly6ccaa542012-06-15 15:22:47 -0700477 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700478 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
479 // These are on the lower bits used for the command; if they change
480 // we need to write another int of data.
481 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
482 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
483 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
484 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
485 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
486 public static final int STATE_WIFI_ON_FLAG = 1<<17;
487 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700488
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700489 public static final int MOST_INTERESTING_STATES =
490 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
491 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
492
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700493 public int states;
494
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700495 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700496 }
497
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700498 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700499 this.time = time;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700500 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700501 }
502
503 public int describeContents() {
504 return 0;
505 }
506
507 public void writeToParcel(Parcel dest, int flags) {
508 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700509 int bat = (((int)cmd)&0xff)
510 | ((((int)batteryLevel)<<8)&0xff00)
511 | ((((int)batteryStatus)<<16)&0xf0000)
512 | ((((int)batteryHealth)<<20)&0xf00000)
513 | ((((int)batteryPlugType)<<24)&0xf000000);
514 dest.writeInt(bat);
515 bat = (((int)batteryTemperature)&0xffff)
516 | ((((int)batteryVoltage)<<16)&0xffff0000);
517 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700518 dest.writeInt(states);
519 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700520
521 private void readFromParcel(Parcel src) {
522 int bat = src.readInt();
523 cmd = (byte)(bat&0xff);
524 batteryLevel = (byte)((bat>>8)&0xff);
525 batteryStatus = (byte)((bat>>16)&0xf);
526 batteryHealth = (byte)((bat>>20)&0xf);
527 batteryPlugType = (byte)((bat>>24)&0xf);
528 bat = src.readInt();
529 batteryTemperature = (char)(bat&0xffff);
530 batteryVoltage = (char)((bat>>16)&0xffff);
531 states = src.readInt();
532 }
533
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700534 // Part of initial delta int that specifies the time delta.
535 static final int DELTA_TIME_MASK = 0x3ffff;
536 static final int DELTA_TIME_ABS = 0x3fffd; // Following is an entire abs update.
537 static final int DELTA_TIME_INT = 0x3fffe; // The delta is a following int
538 static final int DELTA_TIME_LONG = 0x3ffff; // The delta is a following long
539 // Part of initial delta int holding the command code.
540 static final int DELTA_CMD_MASK = 0x3;
541 static final int DELTA_CMD_SHIFT = 18;
542 // Flag in delta int: a new battery level int follows.
543 static final int DELTA_BATTERY_LEVEL_FLAG = 1<<20;
544 // Flag in delta int: a new full state and battery status int follows.
545 static final int DELTA_STATE_FLAG = 1<<21;
546 static final int DELTA_STATE_MASK = 0xffc00000;
547
548 public void writeDelta(Parcel dest, HistoryItem last) {
549 if (last == null || last.cmd != CMD_UPDATE) {
550 dest.writeInt(DELTA_TIME_ABS);
551 writeToParcel(dest, 0);
552 return;
553 }
554
555 final long deltaTime = time - last.time;
556 final int lastBatteryLevelInt = last.buildBatteryLevelInt();
557 final int lastStateInt = last.buildStateInt();
558
559 int deltaTimeToken;
560 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
561 deltaTimeToken = DELTA_TIME_LONG;
562 } else if (deltaTime >= DELTA_TIME_ABS) {
563 deltaTimeToken = DELTA_TIME_INT;
564 } else {
565 deltaTimeToken = (int)deltaTime;
566 }
567 int firstToken = deltaTimeToken
568 | (cmd<<DELTA_CMD_SHIFT)
569 | (states&DELTA_STATE_MASK);
570 final int batteryLevelInt = buildBatteryLevelInt();
571 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
572 if (batteryLevelIntChanged) {
573 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
574 }
575 final int stateInt = buildStateInt();
576 final boolean stateIntChanged = stateInt != lastStateInt;
577 if (stateIntChanged) {
578 firstToken |= DELTA_STATE_FLAG;
579 }
580 dest.writeInt(firstToken);
581 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
582 + " deltaTime=" + deltaTime);
583
584 if (deltaTimeToken >= DELTA_TIME_INT) {
585 if (deltaTimeToken == DELTA_TIME_INT) {
586 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
587 dest.writeInt((int)deltaTime);
588 } else {
589 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
590 dest.writeLong(deltaTime);
591 }
592 }
593 if (batteryLevelIntChanged) {
594 dest.writeInt(batteryLevelInt);
595 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
596 + Integer.toHexString(batteryLevelInt)
597 + " batteryLevel=" + batteryLevel
598 + " batteryTemp=" + (int)batteryTemperature
599 + " batteryVolt=" + (int)batteryVoltage);
600 }
601 if (stateIntChanged) {
602 dest.writeInt(stateInt);
603 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
604 + Integer.toHexString(stateInt)
605 + " batteryStatus=" + batteryStatus
606 + " batteryHealth=" + batteryHealth
607 + " batteryPlugType=" + batteryPlugType
608 + " states=0x" + Integer.toHexString(states));
609 }
610 }
611
612 private int buildBatteryLevelInt() {
613 return ((((int)batteryLevel)<<24)&0xff000000)
614 | ((((int)batteryTemperature)<<14)&0x00ffc000)
615 | (((int)batteryVoltage)&0x00003fff);
616 }
617
618 private int buildStateInt() {
619 return ((((int)batteryStatus)<<28)&0xf0000000)
620 | ((((int)batteryHealth)<<24)&0x0f000000)
621 | ((((int)batteryPlugType)<<22)&0x00c00000)
622 | (states&(~DELTA_STATE_MASK));
623 }
624
625 public void readDelta(Parcel src) {
626 int firstToken = src.readInt();
627 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
628 cmd = (byte)((firstToken>>DELTA_CMD_SHIFT)&DELTA_CMD_MASK);
629 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
630 + " deltaTimeToken=" + deltaTimeToken);
631
632 if (deltaTimeToken < DELTA_TIME_ABS) {
633 time += deltaTimeToken;
634 } else if (deltaTimeToken == DELTA_TIME_ABS) {
635 time = src.readLong();
636 readFromParcel(src);
637 return;
638 } else if (deltaTimeToken == DELTA_TIME_INT) {
639 int delta = src.readInt();
640 time += delta;
641 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
642 } else {
643 long delta = src.readLong();
644 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
645 time += delta;
646 }
647
648 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
649 int batteryLevelInt = src.readInt();
650 batteryLevel = (byte)((batteryLevelInt>>24)&0xff);
651 batteryTemperature = (char)((batteryLevelInt>>14)&0x3ff);
652 batteryVoltage = (char)(batteryLevelInt&0x3fff);
653 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
654 + Integer.toHexString(batteryLevelInt)
655 + " batteryLevel=" + batteryLevel
656 + " batteryTemp=" + (int)batteryTemperature
657 + " batteryVolt=" + (int)batteryVoltage);
658 }
659
660 if ((firstToken&DELTA_STATE_FLAG) != 0) {
661 int stateInt = src.readInt();
662 states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
663 batteryStatus = (byte)((stateInt>>28)&0xf);
664 batteryHealth = (byte)((stateInt>>24)&0xf);
665 batteryPlugType = (byte)((stateInt>>22)&0x3);
666 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
667 + Integer.toHexString(stateInt)
668 + " batteryStatus=" + batteryStatus
669 + " batteryHealth=" + batteryHealth
670 + " batteryPlugType=" + batteryPlugType
671 + " states=0x" + Integer.toHexString(states));
672 } else {
673 states = (firstToken&DELTA_STATE_MASK) | (states&(~DELTA_STATE_MASK));
674 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700675 }
676
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700677 public void clear() {
678 time = 0;
679 cmd = CMD_NULL;
680 batteryLevel = 0;
681 batteryStatus = 0;
682 batteryHealth = 0;
683 batteryPlugType = 0;
684 batteryTemperature = 0;
685 batteryVoltage = 0;
686 states = 0;
687 }
688
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700689 public void setTo(HistoryItem o) {
690 time = o.time;
691 cmd = o.cmd;
692 batteryLevel = o.batteryLevel;
693 batteryStatus = o.batteryStatus;
694 batteryHealth = o.batteryHealth;
695 batteryPlugType = o.batteryPlugType;
696 batteryTemperature = o.batteryTemperature;
697 batteryVoltage = o.batteryVoltage;
698 states = o.states;
699 }
700
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700701 public void setTo(long time, byte cmd, HistoryItem o) {
702 this.time = time;
703 this.cmd = cmd;
704 batteryLevel = o.batteryLevel;
705 batteryStatus = o.batteryStatus;
706 batteryHealth = o.batteryHealth;
707 batteryPlugType = o.batteryPlugType;
708 batteryTemperature = o.batteryTemperature;
709 batteryVoltage = o.batteryVoltage;
710 states = o.states;
711 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700712
713 public boolean same(HistoryItem o) {
714 return batteryLevel == o.batteryLevel
715 && batteryStatus == o.batteryStatus
716 && batteryHealth == o.batteryHealth
717 && batteryPlugType == o.batteryPlugType
718 && batteryTemperature == o.batteryTemperature
719 && batteryVoltage == o.batteryVoltage
720 && states == o.states;
721 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700722 }
723
724 public static final class BitDescription {
725 public final int mask;
726 public final int shift;
727 public final String name;
728 public final String[] values;
729
730 public BitDescription(int mask, String name) {
731 this.mask = mask;
732 this.shift = -1;
733 this.name = name;
734 this.values = null;
735 }
736
737 public BitDescription(int mask, int shift, String name, String[] values) {
738 this.mask = mask;
739 this.shift = shift;
740 this.name = name;
741 this.values = values;
742 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700743 }
744
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700745 public abstract boolean startIteratingHistoryLocked();
746
747 public abstract boolean getNextHistoryLocked(HistoryItem out);
748
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700749 public abstract void finishIteratingHistoryLocked();
750
751 public abstract boolean startIteratingOldHistoryLocked();
752
753 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
754
755 public abstract void finishIteratingOldHistoryLocked();
756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700758 * Return the base time offset for the battery history.
759 */
760 public abstract long getHistoryBaseTime();
761
762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 * Returns the number of times the device has been started.
764 */
765 public abstract int getStartCount();
766
767 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700768 * 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 -0800769 * running on battery.
770 *
771 * {@hide}
772 */
773 public abstract long getScreenOnTime(long batteryRealtime, int which);
774
Dianne Hackborn617f8772009-03-31 15:04:46 -0700775 public static final int SCREEN_BRIGHTNESS_DARK = 0;
776 public static final int SCREEN_BRIGHTNESS_DIM = 1;
777 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
778 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
779 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
780
781 static final String[] SCREEN_BRIGHTNESS_NAMES = {
782 "dark", "dim", "medium", "light", "bright"
783 };
784
785 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
786
787 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700788 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700789 * the given brightness
790 *
791 * {@hide}
792 */
793 public abstract long getScreenBrightnessTime(int brightnessBin,
794 long batteryRealtime, int which);
795
796 public abstract int getInputEventCount(int which);
797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700799 * 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 -0800800 * running on battery.
801 *
802 * {@hide}
803 */
804 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700805
806 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700807 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700808 * the given signal strength.
809 *
810 * {@hide}
811 */
812 public abstract long getPhoneSignalStrengthTime(int strengthBin,
813 long batteryRealtime, int which);
814
Dianne Hackborn617f8772009-03-31 15:04:46 -0700815 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700816 * Returns the time in microseconds that the phone has been trying to
817 * acquire a signal.
818 *
819 * {@hide}
820 */
821 public abstract long getPhoneSignalScanningTime(
822 long batteryRealtime, int which);
823
824 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700825 * Returns the number of times the phone has entered the given signal strength.
826 *
827 * {@hide}
828 */
829 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
830
Dianne Hackborn627bba72009-03-24 22:32:56 -0700831 public static final int DATA_CONNECTION_NONE = 0;
832 public static final int DATA_CONNECTION_GPRS = 1;
833 public static final int DATA_CONNECTION_EDGE = 2;
834 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700835 public static final int DATA_CONNECTION_CDMA = 4;
836 public static final int DATA_CONNECTION_EVDO_0 = 5;
837 public static final int DATA_CONNECTION_EVDO_A = 6;
838 public static final int DATA_CONNECTION_1xRTT = 7;
839 public static final int DATA_CONNECTION_HSDPA = 8;
840 public static final int DATA_CONNECTION_HSUPA = 9;
841 public static final int DATA_CONNECTION_HSPA = 10;
842 public static final int DATA_CONNECTION_IDEN = 11;
843 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700844 public static final int DATA_CONNECTION_LTE = 13;
845 public static final int DATA_CONNECTION_EHRPD = 14;
846 public static final int DATA_CONNECTION_OTHER = 15;
847
Dianne Hackborn627bba72009-03-24 22:32:56 -0700848 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700849 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700850 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
851 "ehrpd", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700852 };
853
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700854 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700855
856 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700857 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700858 * the given data connection.
859 *
860 * {@hide}
861 */
862 public abstract long getPhoneDataConnectionTime(int dataType,
863 long batteryRealtime, int which);
864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700866 * Returns the number of times the phone has entered the given data
867 * connection type.
868 *
869 * {@hide}
870 */
871 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700872
873 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
874 = new BitDescription[] {
875 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged"),
876 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen"),
877 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps"),
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700878 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700879 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning"),
880 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi"),
881 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running"),
882 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock"),
Nick Pelly6ccaa542012-06-15 15:22:47 -0700883 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700884 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast"),
885 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth"),
886 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio"),
887 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video"),
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700888 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock"),
889 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700890 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
891 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness",
892 SCREEN_BRIGHTNESS_NAMES),
893 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
894 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength",
Wink Saville52840902011-02-18 12:40:47 -0800895 SignalStrength.SIGNAL_STRENGTH_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700896 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
897 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state",
898 new String[] {"in", "out", "emergency", "off"}),
899 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
900 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn",
901 DATA_CONNECTION_NAMES),
902 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700903
904 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700905 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700906 * running on battery.
907 *
908 * {@hide}
909 */
910 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700911
912 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700913 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700914 * been in the running state while the device was running on battery.
915 *
916 * {@hide}
917 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700918 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700919
The Android Open Source Project10592532009-03-18 17:39:46 -0700920 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700921 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700922 * running on battery.
923 *
924 * {@hide}
925 */
926 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
927
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700928 public static final int NETWORK_MOBILE_RX_BYTES = 0;
929 public static final int NETWORK_MOBILE_TX_BYTES = 1;
930 public static final int NETWORK_WIFI_RX_BYTES = 2;
931 public static final int NETWORK_WIFI_TX_BYTES = 3;
932
933 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_BYTES + 1;
934
935 public abstract long getNetworkActivityCount(int type, int which);
936
The Android Open Source Project10592532009-03-18 17:39:46 -0700937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 * Return whether we are currently running on battery.
939 */
940 public abstract boolean getIsOnBattery();
941
942 /**
943 * Returns a SparseArray containing the statistics for each uid.
944 */
945 public abstract SparseArray<? extends Uid> getUidStats();
946
947 /**
948 * Returns the current battery uptime in microseconds.
949 *
950 * @param curTime the amount of elapsed realtime in microseconds.
951 */
952 public abstract long getBatteryUptime(long curTime);
953
954 /**
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700955 * @deprecated use getRadioDataUptime
956 */
957 public long getRadioDataUptimeMs() {
958 return getRadioDataUptime() / 1000;
959 }
960
961 /**
962 * Returns the time that the radio was on for data transfers.
963 * @return the uptime in microseconds while unplugged
964 */
965 public abstract long getRadioDataUptime();
966
967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 * Returns the current battery realtime in microseconds.
969 *
970 * @param curTime the amount of elapsed realtime in microseconds.
971 */
972 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -0700973
974 /**
Evan Millar633a1742009-04-02 16:36:33 -0700975 * Returns the battery percentage level at the last time the device was unplugged from power, or
976 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -0700977 */
Evan Millar633a1742009-04-02 16:36:33 -0700978 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -0700979
980 /**
Evan Millar633a1742009-04-02 16:36:33 -0700981 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
982 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -0700983 */
Evan Millar633a1742009-04-02 16:36:33 -0700984 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
986 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700987 * Get the amount the battery has discharged since the stats were
988 * last reset after charging, as a lower-end approximation.
989 */
990 public abstract int getLowDischargeAmountSinceCharge();
991
992 /**
993 * Get the amount the battery has discharged since the stats were
994 * last reset after charging, as an upper-end approximation.
995 */
996 public abstract int getHighDischargeAmountSinceCharge();
997
998 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800999 * Get the amount the battery has discharged while the screen was on,
1000 * since the last time power was unplugged.
1001 */
1002 public abstract int getDischargeAmountScreenOn();
1003
1004 /**
1005 * Get the amount the battery has discharged while the screen was on,
1006 * since the last time the device was charged.
1007 */
1008 public abstract int getDischargeAmountScreenOnSinceCharge();
1009
1010 /**
1011 * Get the amount the battery has discharged while the screen was off,
1012 * since the last time power was unplugged.
1013 */
1014 public abstract int getDischargeAmountScreenOff();
1015
1016 /**
1017 * Get the amount the battery has discharged while the screen was off,
1018 * since the last time the device was charged.
1019 */
1020 public abstract int getDischargeAmountScreenOffSinceCharge();
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * Returns the total, last, or current battery uptime in microseconds.
1024 *
1025 * @param curTime the elapsed realtime in microseconds.
1026 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1027 */
1028 public abstract long computeBatteryUptime(long curTime, int which);
1029
1030 /**
1031 * Returns the total, last, or current battery realtime in microseconds.
1032 *
1033 * @param curTime the current elapsed realtime in microseconds.
1034 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1035 */
1036 public abstract long computeBatteryRealtime(long curTime, int which);
1037
1038 /**
1039 * Returns the total, last, or current uptime in microseconds.
1040 *
1041 * @param curTime the current elapsed realtime in microseconds.
1042 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1043 */
1044 public abstract long computeUptime(long curTime, int which);
1045
1046 /**
1047 * Returns the total, last, or current realtime in microseconds.
1048 * *
1049 * @param curTime the current elapsed realtime in microseconds.
1050 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1051 */
1052 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001053
1054 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055
Amith Yamasanie43530a2009-08-21 13:11:37 -07001056 /** Returns the number of different speeds that the CPU can run at */
1057 public abstract int getCpuSpeedSteps();
1058
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001059 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 long days = seconds / (60 * 60 * 24);
1061 if (days != 0) {
1062 out.append(days);
1063 out.append("d ");
1064 }
1065 long used = days * 60 * 60 * 24;
1066
1067 long hours = (seconds - used) / (60 * 60);
1068 if (hours != 0 || used != 0) {
1069 out.append(hours);
1070 out.append("h ");
1071 }
1072 used += hours * 60 * 60;
1073
1074 long mins = (seconds-used) / 60;
1075 if (mins != 0 || used != 0) {
1076 out.append(mins);
1077 out.append("m ");
1078 }
1079 used += mins * 60;
1080
1081 if (seconds != 0 || used != 0) {
1082 out.append(seconds-used);
1083 out.append("s ");
1084 }
1085 }
1086
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001087 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001089 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 sb.append((time - (sec * 100)) * 10);
1091 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001094 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001096 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 sb.append(time - (sec * 1000));
1098 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100
1101 private final String formatRatioLocked(long num, long den) {
1102 if (den == 0L) {
1103 return "---%";
1104 }
1105 float perc = ((float)num) / ((float)den) * 100;
1106 mFormatBuilder.setLength(0);
1107 mFormatter.format("%.1f%%", perc);
1108 return mFormatBuilder.toString();
1109 }
1110
Evan Millar22ac0432009-03-31 11:33:18 -07001111 private final String formatBytesLocked(long bytes) {
1112 mFormatBuilder.setLength(0);
1113
1114 if (bytes < BYTES_PER_KB) {
1115 return bytes + "B";
1116 } else if (bytes < BYTES_PER_MB) {
1117 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1118 return mFormatBuilder.toString();
1119 } else if (bytes < BYTES_PER_GB){
1120 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1121 return mFormatBuilder.toString();
1122 } else {
1123 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1124 return mFormatBuilder.toString();
1125 }
1126 }
1127
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001128 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1129 if (timer != null) {
1130 // Convert from microseconds to milliseconds with rounding
1131 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1132 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1133 return totalTimeMillis;
1134 }
1135 return 0;
1136 }
1137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 *
1140 * @param sb a StringBuilder object.
1141 * @param timer a Timer object contining the wakelock times.
1142 * @param batteryRealtime the current on-battery time in microseconds.
1143 * @param name the name of the wakelock.
1144 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1145 * @param linePrefix a String to be prepended to each line of output.
1146 * @return the line prefix
1147 */
1148 private static final String printWakeLock(StringBuilder sb, Timer timer,
1149 long batteryRealtime, String name, int which, String linePrefix) {
1150
1151 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001152 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153
Evan Millarc64edde2009-04-18 12:26:32 -07001154 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (totalTimeMillis != 0) {
1156 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001157 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001158 if (name != null) {
1159 sb.append(name);
1160 sb.append(' ');
1161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 sb.append('(');
1163 sb.append(count);
1164 sb.append(" times)");
1165 return ", ";
1166 }
1167 }
1168 return linePrefix;
1169 }
1170
1171 /**
1172 * Checkin version of wakelock printer. Prints simple comma-separated list.
1173 *
1174 * @param sb a StringBuilder object.
1175 * @param timer a Timer object contining the wakelock times.
1176 * @param now the current time in microseconds.
1177 * @param name the name of the wakelock.
1178 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1179 * @param linePrefix a String to be prepended to each line of output.
1180 * @return the line prefix
1181 */
1182 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001183 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 long totalTimeMicros = 0;
1185 int count = 0;
1186 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001187 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1188 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 sb.append(linePrefix);
1191 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1192 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001193 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 sb.append(count);
1195 return ",";
1196 }
1197
1198 /**
1199 * Dump a comma-separated line of values for terse checkin mode.
1200 *
1201 * @param pw the PageWriter to dump log to
1202 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1203 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1204 * @param args type-dependent data arguments
1205 */
1206 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1207 Object... args ) {
1208 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1209 pw.print(uid); pw.print(',');
1210 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001211 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212
1213 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001214 pw.print(',');
1215 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001217 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219
1220 /**
1221 * Checkin server version of dump to produce more compact, computer-readable log.
1222 *
1223 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001225 public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1227 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1228 final long batteryUptime = getBatteryUptime(rawUptime);
1229 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1230 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1231 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1232 final long totalRealtime = computeRealtime(rawRealtime, which);
1233 final long totalUptime = computeUptime(rawUptime, which);
1234 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1235 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001236 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001237 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001238 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239
1240 StringBuilder sb = new StringBuilder(128);
1241
Evan Millar22ac0432009-03-31 11:33:18 -07001242 SparseArray<? extends Uid> uidStats = getUidStats();
1243 final int NU = uidStats.size();
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 // Dump "battery" stat
1248 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001249 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001250 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1251 totalRealtime / 1000, totalUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252
Evan Millar22ac0432009-03-31 11:33:18 -07001253 // Calculate total network and wakelock times across all uids.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001254 long mobileRxTotal = 0;
1255 long mobileTxTotal = 0;
1256 long wifiRxTotal = 0;
1257 long wifiTxTotal = 0;
Evan Millar22ac0432009-03-31 11:33:18 -07001258 long fullWakeLockTimeTotal = 0;
1259 long partialWakeLockTimeTotal = 0;
1260
1261 for (int iu = 0; iu < NU; iu++) {
1262 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001263 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1264 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1265 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1266 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1267
Evan Millar22ac0432009-03-31 11:33:18 -07001268 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1269 if (wakelocks.size() > 0) {
1270 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1271 : wakelocks.entrySet()) {
1272 Uid.Wakelock wl = ent.getValue();
1273
1274 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1275 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001276 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001277 }
1278
1279 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1280 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001281 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001282 batteryRealtime, which);
1283 }
1284 }
1285 }
1286 }
1287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 // Dump misc stats
1289 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001290 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001291 wifiRunningTime / 1000, bluetoothOnTime / 1000,
1292 mobileRxTotal, mobileTxTotal, wifiRxTotal, wifiTxTotal,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001293 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1294 getInputEventCount(which));
1295
1296 // Dump screen brightness stats
1297 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1298 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1299 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1300 }
1301 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001302
Dianne Hackborn627bba72009-03-24 22:32:56 -07001303 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001304 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1305 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001306 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1307 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001308 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001309 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1310 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001311 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001312 args[i] = getPhoneSignalStrengthCount(i, which);
1313 }
1314 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001315
1316 // Dump network type stats
1317 args = new Object[NUM_DATA_CONNECTION_TYPES];
1318 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1319 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1320 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001321 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1322 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1323 args[i] = getPhoneDataConnectionCount(i, which);
1324 }
1325 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001326
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001327 if (which == STATS_SINCE_UNPLUGGED) {
Evan Millare84de8d2009-04-02 22:16:12 -07001328 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001329 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001332 if (which == STATS_SINCE_UNPLUGGED) {
1333 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1334 getDischargeStartLevel()-getDischargeCurrentLevel(),
1335 getDischargeStartLevel()-getDischargeCurrentLevel(),
1336 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1337 } else {
1338 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1339 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1340 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1341 }
1342
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001343 if (reqUid < 0) {
1344 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1345 if (kernelWakelocks.size() > 0) {
1346 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1347 sb.setLength(0);
1348 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1349
1350 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1351 sb.toString());
1352 }
Evan Millarc64edde2009-04-18 12:26:32 -07001353 }
1354 }
1355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 for (int iu = 0; iu < NU; iu++) {
1357 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001358 if (reqUid >= 0 && uid != reqUid) {
1359 continue;
1360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 Uid u = uidStats.valueAt(iu);
1362 // Dump Network stats per uid, if any
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001363 long mobileRx = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1364 long mobileTx = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1365 long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1366 long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001367 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001368 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001369 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001370
1371 if (mobileRx > 0 || mobileTx > 0 || wifiRx > 0 || wifiTx > 0) {
1372 dumpLine(pw, uid, category, NETWORK_DATA, mobileRx, mobileTx, wifiRx, wifiTx);
1373 }
1374
Nick Pelly6ccaa542012-06-15 15:22:47 -07001375 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001376 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001377 dumpLine(pw, uid, category, WIFI_DATA,
1378 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
Dianne Hackborn617f8772009-03-31 15:04:46 -07001381 if (u.hasUserActivity()) {
1382 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1383 boolean hasData = false;
1384 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1385 int val = u.getUserActivityCount(i, which);
1386 args[i] = val;
1387 if (val != 0) hasData = true;
1388 }
1389 if (hasData) {
1390 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1391 }
1392 }
1393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1395 if (wakelocks.size() > 0) {
1396 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1397 : wakelocks.entrySet()) {
1398 Uid.Wakelock wl = ent.getValue();
1399 String linePrefix = "";
1400 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001401 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1402 batteryRealtime, "f", which, linePrefix);
1403 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1404 batteryRealtime, "p", which, linePrefix);
1405 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1406 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407
1408 // Only log if we had at lease one wakelock...
1409 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001410 String name = ent.getKey();
1411 if (name.indexOf(',') >= 0) {
1412 name = name.replace(',', '_');
1413 }
1414 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
1417 }
1418
1419 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1420 if (sensors.size() > 0) {
1421 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1422 : sensors.entrySet()) {
1423 Uid.Sensor se = ent.getValue();
1424 int sensorNumber = ent.getKey();
1425 Timer timer = se.getSensorTime();
1426 if (timer != null) {
1427 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001428 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1429 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 if (totalTime != 0) {
1431 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1432 }
1433 }
1434 }
1435 }
1436
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001437 Timer vibTimer = u.getVibratorOnTimer();
1438 if (vibTimer != null) {
1439 // Convert from microseconds to milliseconds with rounding
1440 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1441 int count = vibTimer.getCountLocked(which);
1442 if (totalTime != 0) {
1443 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1444 }
1445 }
1446
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001447 Timer fgTimer = u.getForegroundActivityTimer();
1448 if (fgTimer != null) {
1449 // Convert from microseconds to milliseconds with rounding
1450 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1451 int count = fgTimer.getCountLocked(which);
1452 if (totalTime != 0) {
1453 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1454 }
1455 }
1456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1458 if (processStats.size() > 0) {
1459 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1460 : processStats.entrySet()) {
1461 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001462
1463 final long userMillis = ps.getUserTime(which) * 10;
1464 final long systemMillis = ps.getSystemTime(which) * 10;
1465 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1466 final long starts = ps.getStarts(which);
1467
1468 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1469 || starts != 0) {
1470 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1471 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
1473 }
1474 }
1475
1476 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1477 if (packageStats.size() > 0) {
1478 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1479 : packageStats.entrySet()) {
1480
1481 Uid.Pkg ps = ent.getValue();
1482 int wakeups = ps.getWakeups(which);
1483 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1484 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1485 : serviceStats.entrySet()) {
1486 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1487 long startTime = ss.getStartTime(batteryUptime, which);
1488 int starts = ss.getStarts(which);
1489 int launches = ss.getLaunches(which);
1490 if (startTime != 0 || starts != 0 || launches != 0) {
1491 dumpLine(pw, uid, category, APK_DATA,
1492 wakeups, // wakeup alarms
1493 ent.getKey(), // Apk
1494 sent.getKey(), // service
1495 startTime / 1000, // time spent started, in ms
1496 starts,
1497 launches);
1498 }
1499 }
1500 }
1501 }
1502 }
1503 }
1504
Dianne Hackborn81038902012-11-26 17:04:09 -08001505 static final class TimerEntry {
1506 final String mName;
1507 final int mId;
1508 final BatteryStats.Timer mTimer;
1509 final long mTime;
1510 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1511 mName = name;
1512 mId = id;
1513 mTimer = timer;
1514 mTime = time;
1515 }
1516 }
1517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 @SuppressWarnings("unused")
Dianne Hackborn81038902012-11-26 17:04:09 -08001519 public final void dumpLocked(PrintWriter pw, String prefix, final int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1521 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1522 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001523 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524
1525 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1526 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1527 final long totalRealtime = computeRealtime(rawRealtime, which);
1528 final long totalUptime = computeUptime(rawUptime, which);
1529
1530 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001531
1532 SparseArray<? extends Uid> uidStats = getUidStats();
1533 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001535 sb.setLength(0);
1536 sb.append(prefix);
1537 sb.append(" Time on battery: ");
1538 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1539 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1540 sb.append(") realtime, ");
1541 formatTimeMs(sb, whichBatteryUptime / 1000);
1542 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1543 sb.append(") uptime");
1544 pw.println(sb.toString());
1545 sb.setLength(0);
1546 sb.append(prefix);
1547 sb.append(" Total run time: ");
1548 formatTimeMs(sb, totalRealtime / 1000);
1549 sb.append("realtime, ");
1550 formatTimeMs(sb, totalUptime / 1000);
1551 sb.append("uptime, ");
1552 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
The Android Open Source Project10592532009-03-18 17:39:46 -07001554 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1555 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001556 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001557 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1558 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001559 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001560 sb.append(prefix);
1561 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1562 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1563 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1564 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1565 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1566 sb.append(")");
1567 pw.println(sb.toString());
1568 sb.setLength(0);
1569 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001570 sb.append(" Screen brightnesses: ");
1571 boolean didOne = false;
1572 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1573 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1574 if (time == 0) {
1575 continue;
1576 }
1577 if (didOne) sb.append(", ");
1578 didOne = true;
1579 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1580 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001581 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001582 sb.append("(");
1583 sb.append(formatRatioLocked(time, screenOnTime));
1584 sb.append(")");
1585 }
1586 if (!didOne) sb.append("No activity");
1587 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001588
Evan Millar22ac0432009-03-31 11:33:18 -07001589 // Calculate total network and wakelock times across all uids.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001590 long mobileRxTotal = 0;
1591 long mobileTxTotal = 0;
1592 long wifiRxTotal = 0;
1593 long wifiTxTotal = 0;
Evan Millar22ac0432009-03-31 11:33:18 -07001594 long fullWakeLockTimeTotalMicros = 0;
1595 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001596
1597 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1598 @Override
1599 public int compare(TimerEntry lhs, TimerEntry rhs) {
1600 long lhsTime = lhs.mTime;
1601 long rhsTime = rhs.mTime;
1602 if (lhsTime < rhsTime) {
1603 return 1;
1604 }
1605 if (lhsTime > rhsTime) {
1606 return -1;
1607 }
1608 return 0;
1609 }
1610 };
1611
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001612 if (reqUid < 0) {
1613 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1614 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001615 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001616 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001617 BatteryStats.Timer timer = ent.getValue();
1618 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1619 if (totalTimeMillis > 0) {
1620 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1621 }
1622 }
1623 Collections.sort(timers, timerComparator);
1624 for (int i=0; i<timers.size(); i++) {
1625 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001626 String linePrefix = ": ";
1627 sb.setLength(0);
1628 sb.append(prefix);
1629 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001630 sb.append(timer.mName);
1631 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1632 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001633 if (!linePrefix.equals(": ")) {
1634 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001635 // Only print out wake locks that were held
1636 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001637 }
Evan Millarc64edde2009-04-18 12:26:32 -07001638 }
Evan Millarc64edde2009-04-18 12:26:32 -07001639 }
1640 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001641
1642 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1643
Evan Millar22ac0432009-03-31 11:33:18 -07001644 for (int iu = 0; iu < NU; iu++) {
1645 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001646 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1647 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1648 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1649 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1650
Evan Millar22ac0432009-03-31 11:33:18 -07001651 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1652 if (wakelocks.size() > 0) {
1653 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1654 : wakelocks.entrySet()) {
1655 Uid.Wakelock wl = ent.getValue();
1656
1657 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1658 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001659 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001660 batteryRealtime, which);
1661 }
1662
1663 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1664 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001665 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001666 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001667 if (totalTimeMicros > 0) {
1668 if (reqUid < 0) {
1669 // Only show the ordered list of all wake
1670 // locks if the caller is not asking for data
1671 // about a specific uid.
1672 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1673 partialWakeTimer, totalTimeMicros));
1674 }
1675 partialWakeLockTimeTotalMicros += totalTimeMicros;
1676 }
Evan Millar22ac0432009-03-31 11:33:18 -07001677 }
1678 }
1679 }
1680 }
1681
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001682 pw.print(prefix);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001683 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotal));
1684 pw.print(", Total sent: "); pw.println(formatBytesLocked(mobileTxTotal));
1685 pw.print(prefix);
1686 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotal));
1687 pw.print(", Total sent: "); pw.println(formatBytesLocked(wifiTxTotal));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001688 sb.setLength(0);
1689 sb.append(prefix);
1690 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1691 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001692 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001693 (partialWakeLockTimeTotalMicros + 500) / 1000);
1694 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001695
Dianne Hackborn627bba72009-03-24 22:32:56 -07001696 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001697 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001698 sb.append(" Signal levels: ");
1699 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001700 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001701 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1702 if (time == 0) {
1703 continue;
1704 }
1705 if (didOne) sb.append(", ");
1706 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001707 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001708 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001709 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001710 sb.append("(");
1711 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001712 sb.append(") ");
1713 sb.append(getPhoneSignalStrengthCount(i, which));
1714 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001715 }
1716 if (!didOne) sb.append("No activity");
1717 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001718
1719 sb.setLength(0);
1720 sb.append(prefix);
1721 sb.append(" Signal scanning time: ");
1722 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1723 pw.println(sb.toString());
1724
Dianne Hackborn627bba72009-03-24 22:32:56 -07001725 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001726 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001727 sb.append(" Radio types: ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001728 didOne = false;
1729 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1730 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1731 if (time == 0) {
1732 continue;
1733 }
1734 if (didOne) sb.append(", ");
1735 didOne = true;
1736 sb.append(DATA_CONNECTION_NAMES[i]);
1737 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001738 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001739 sb.append("(");
1740 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001741 sb.append(") ");
1742 sb.append(getPhoneDataConnectionCount(i, which));
1743 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001744 }
1745 if (!didOne) sb.append("No activity");
1746 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001747
1748 sb.setLength(0);
1749 sb.append(prefix);
1750 sb.append(" Radio data uptime when unplugged: ");
1751 sb.append(getRadioDataUptime() / 1000);
1752 sb.append(" ms");
1753 pw.println(sb.toString());
1754
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001755 sb.setLength(0);
1756 sb.append(prefix);
1757 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1758 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1759 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1760 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1761 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1762 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1763 sb.append(")");
1764 pw.println(sb.toString());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001765
The Android Open Source Project10592532009-03-18 17:39:46 -07001766 pw.println(" ");
1767
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001768 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001769 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001770 pw.print(prefix); pw.println(" Device is currently unplugged");
1771 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1772 pw.println(getDischargeStartLevel());
1773 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1774 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07001775 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001776 pw.print(prefix); pw.println(" Device is currently plugged into power");
1777 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1778 pw.println(getDischargeStartLevel());
1779 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1780 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001781 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001782 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1783 pw.println(getDischargeAmountScreenOn());
1784 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1785 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001786 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001787 } else {
1788 pw.print(prefix); pw.println(" Device battery use since last full charge");
1789 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1790 pw.println(getLowDischargeAmountSinceCharge());
1791 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1792 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001793 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1794 pw.println(getDischargeAmountScreenOnSinceCharge());
1795 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1796 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08001797 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07001798 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001799
1800 if (timers.size() > 0) {
1801 Collections.sort(timers, timerComparator);
1802 pw.print(prefix); pw.println(" All partial wake locks:");
1803 for (int i=0; i<timers.size(); i++) {
1804 TimerEntry timer = timers.get(i);
1805 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001806 sb.append(" Wake lock ");
1807 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08001808 sb.append(" ");
1809 sb.append(timer.mName);
1810 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1811 sb.append(" realtime");
1812 pw.println(sb.toString());
1813 }
1814 timers.clear();
1815 pw.println();
1816 }
Evan Millar22ac0432009-03-31 11:33:18 -07001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 for (int iu=0; iu<NU; iu++) {
1819 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08001820 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001821 continue;
1822 }
1823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001825
1826 pw.print(prefix);
1827 pw.print(" ");
1828 UserHandle.formatUid(pw, uid);
1829 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 boolean uidActivity = false;
1831
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001832 long mobileRxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1833 long mobileTxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1834 long wifiRxBytes = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1835 long wifiTxBytes = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001836 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001837 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001838 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001839
1840 if (mobileRxBytes > 0 || mobileTxBytes > 0) {
1841 pw.print(prefix); pw.print(" Mobile network: ");
1842 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
1843 pw.print(formatBytesLocked(mobileTxBytes)); pw.println(" sent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001845 if (wifiRxBytes > 0 || wifiTxBytes > 0) {
1846 pw.print(prefix); pw.print(" Wi-Fi network: ");
1847 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
1848 pw.print(formatBytesLocked(wifiTxBytes)); pw.println(" sent");
1849 }
1850
Dianne Hackborn617f8772009-03-31 15:04:46 -07001851 if (u.hasUserActivity()) {
1852 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07001853 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001854 int val = u.getUserActivityCount(i, which);
1855 if (val != 0) {
1856 if (!hasData) {
1857 sb.setLength(0);
1858 sb.append(" User activity: ");
1859 hasData = true;
1860 } else {
1861 sb.append(", ");
1862 }
1863 sb.append(val);
1864 sb.append(" ");
1865 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1866 }
1867 }
1868 if (hasData) {
1869 pw.println(sb.toString());
1870 }
1871 }
1872
Nick Pelly6ccaa542012-06-15 15:22:47 -07001873 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001874 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001875 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001876 sb.append(prefix); sb.append(" Wifi Running: ");
1877 formatTimeMs(sb, uidWifiRunningTime / 1000);
1878 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001879 whichBatteryRealtime)); sb.append(")\n");
1880 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001881 formatTimeMs(sb, fullWifiLockOnTime / 1000);
1882 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001883 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001884 sb.append(prefix); sb.append(" Wifi Scan: ");
1885 formatTimeMs(sb, wifiScanTime / 1000);
1886 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001887 whichBatteryRealtime)); sb.append(")");
1888 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890
1891 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1892 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001893 long totalFull = 0, totalPartial = 0, totalWindow = 0;
1894 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1896 : wakelocks.entrySet()) {
1897 Uid.Wakelock wl = ent.getValue();
1898 String linePrefix = ": ";
1899 sb.setLength(0);
1900 sb.append(prefix);
1901 sb.append(" Wake lock ");
1902 sb.append(ent.getKey());
1903 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
1904 "full", which, linePrefix);
1905 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
1906 "partial", which, linePrefix);
1907 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
1908 "window", which, linePrefix);
1909 if (!linePrefix.equals(": ")) {
1910 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001911 // Only print out wake locks that were held
1912 pw.println(sb.toString());
1913 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001914 count++;
1915 }
1916 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
1917 batteryRealtime, which);
1918 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
1919 batteryRealtime, which);
1920 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
1921 batteryRealtime, which);
1922 }
1923 if (count > 1) {
1924 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
1925 sb.setLength(0);
1926 sb.append(prefix);
1927 sb.append(" TOTAL wake: ");
1928 boolean needComma = false;
1929 if (totalFull != 0) {
1930 needComma = true;
1931 formatTimeMs(sb, totalFull);
1932 sb.append("full");
1933 }
1934 if (totalPartial != 0) {
1935 if (needComma) {
1936 sb.append(", ");
1937 }
1938 needComma = true;
1939 formatTimeMs(sb, totalPartial);
1940 sb.append("partial");
1941 }
1942 if (totalWindow != 0) {
1943 if (needComma) {
1944 sb.append(", ");
1945 }
1946 needComma = true;
1947 formatTimeMs(sb, totalWindow);
1948 sb.append("window");
1949 }
1950 sb.append(" realtime");
1951 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 }
1955
1956 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1957 if (sensors.size() > 0) {
1958 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1959 : sensors.entrySet()) {
1960 Uid.Sensor se = ent.getValue();
1961 int sensorNumber = ent.getKey();
1962 sb.setLength(0);
1963 sb.append(prefix);
1964 sb.append(" Sensor ");
1965 int handle = se.getHandle();
1966 if (handle == Uid.Sensor.GPS) {
1967 sb.append("GPS");
1968 } else {
1969 sb.append(handle);
1970 }
1971 sb.append(": ");
1972
1973 Timer timer = se.getSensorTime();
1974 if (timer != null) {
1975 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001976 long totalTime = (timer.getTotalTimeLocked(
1977 batteryRealtime, which) + 500) / 1000;
1978 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 //timer.logState();
1980 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001981 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 sb.append("realtime (");
1983 sb.append(count);
1984 sb.append(" times)");
1985 } else {
1986 sb.append("(not used)");
1987 }
1988 } else {
1989 sb.append("(not used)");
1990 }
1991
1992 pw.println(sb.toString());
1993 uidActivity = true;
1994 }
1995 }
1996
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001997 Timer vibTimer = u.getVibratorOnTimer();
1998 if (vibTimer != null) {
1999 // Convert from microseconds to milliseconds with rounding
2000 long totalTime = (vibTimer.getTotalTimeLocked(
2001 batteryRealtime, which) + 500) / 1000;
2002 int count = vibTimer.getCountLocked(which);
2003 //timer.logState();
2004 if (totalTime != 0) {
2005 sb.setLength(0);
2006 sb.append(prefix);
2007 sb.append(" Vibrator: ");
2008 formatTimeMs(sb, totalTime);
2009 sb.append("realtime (");
2010 sb.append(count);
2011 sb.append(" times)");
2012 pw.println(sb.toString());
2013 uidActivity = true;
2014 }
2015 }
2016
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002017 Timer fgTimer = u.getForegroundActivityTimer();
2018 if (fgTimer != null) {
2019 // Convert from microseconds to milliseconds with rounding
2020 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2021 int count = fgTimer.getCountLocked(which);
2022 if (totalTime != 0) {
2023 sb.setLength(0);
2024 sb.append(prefix);
2025 sb.append(" Foreground activities: ");
2026 formatTimeMs(sb, totalTime);
2027 sb.append("realtime (");
2028 sb.append(count);
2029 sb.append(" times)");
2030 pw.println(sb.toString());
2031 uidActivity = true;
2032 }
2033 }
2034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2036 if (processStats.size() > 0) {
2037 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2038 : processStats.entrySet()) {
2039 Uid.Proc ps = ent.getValue();
2040 long userTime;
2041 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002042 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002044 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045
2046 userTime = ps.getUserTime(which);
2047 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002048 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002050 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002051 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002053 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002054 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002055 sb.setLength(0);
2056 sb.append(prefix); sb.append(" Proc ");
2057 sb.append(ent.getKey()); sb.append(":\n");
2058 sb.append(prefix); sb.append(" CPU: ");
2059 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002060 formatTime(sb, systemTime); sb.append("krn ; ");
2061 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002062 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002063 sb.append("\n"); sb.append(prefix); sb.append(" ");
2064 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002065 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002066 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002067 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002068 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002069 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002070 pw.print(prefix); pw.print(" * Killed for ");
2071 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2072 pw.print("wake lock");
2073 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2074 pw.print("cpu");
2075 } else {
2076 pw.print("unknown");
2077 }
2078 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002079 TimeUtils.formatDuration(ew.usedTime, pw);
2080 pw.print(" over ");
2081 TimeUtils.formatDuration(ew.overTime, pw);
2082 pw.print(" (");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002083 pw.print((ew.usedTime*100)/ew.overTime);
2084 pw.println("%)");
2085 }
2086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 uidActivity = true;
2088 }
2089 }
2090 }
2091
2092 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2093 if (packageStats.size() > 0) {
2094 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2095 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002096 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 boolean apkActivity = false;
2098 Uid.Pkg ps = ent.getValue();
2099 int wakeups = ps.getWakeups(which);
2100 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002101 pw.print(prefix); pw.print(" ");
2102 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 apkActivity = true;
2104 }
2105 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2106 if (serviceStats.size() > 0) {
2107 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2108 : serviceStats.entrySet()) {
2109 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2110 long startTime = ss.getStartTime(batteryUptime, which);
2111 int starts = ss.getStarts(which);
2112 int launches = ss.getLaunches(which);
2113 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002114 sb.setLength(0);
2115 sb.append(prefix); sb.append(" Service ");
2116 sb.append(sent.getKey()); sb.append(":\n");
2117 sb.append(prefix); sb.append(" Created for: ");
2118 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002119 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002120 sb.append(prefix); sb.append(" Starts: ");
2121 sb.append(starts);
2122 sb.append(", launches: "); sb.append(launches);
2123 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 apkActivity = true;
2125 }
2126 }
2127 }
2128 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002129 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131 uidActivity = true;
2132 }
2133 }
2134 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002135 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
2137 }
2138 }
2139
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002140 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, BitDescription[] descriptions) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002141 int diff = oldval ^ newval;
2142 if (diff == 0) return;
2143 for (int i=0; i<descriptions.length; i++) {
2144 BitDescription bd = descriptions[i];
2145 if ((diff&bd.mask) != 0) {
2146 if (bd.shift < 0) {
2147 pw.print((newval&bd.mask) != 0 ? " +" : " -");
2148 pw.print(bd.name);
2149 } else {
2150 pw.print(" ");
2151 pw.print(bd.name);
2152 pw.print("=");
2153 int val = (newval&bd.mask)>>bd.shift;
2154 if (bd.values != null && val >= 0 && val < bd.values.length) {
2155 pw.print(bd.values[val]);
2156 } else {
2157 pw.print(val);
2158 }
2159 }
2160 }
2161 }
2162 }
2163
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002164 public void prepareForDumpLocked() {
2165 }
2166
2167 public static class HistoryPrinter {
2168 int oldState = 0;
2169 int oldStatus = -1;
2170 int oldHealth = -1;
2171 int oldPlug = -1;
2172 int oldTemp = -1;
2173 int oldVolt = -1;
2174
2175 public void printNextItem(PrintWriter pw, HistoryItem rec, long now) {
2176 pw.print(" ");
2177 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2178 pw.print(" ");
2179 if (rec.cmd == HistoryItem.CMD_START) {
2180 pw.println(" START");
2181 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2182 pw.println(" *OVERFLOW*");
2183 } else {
2184 if (rec.batteryLevel < 10) pw.print("00");
2185 else if (rec.batteryLevel < 100) pw.print("0");
2186 pw.print(rec.batteryLevel);
2187 pw.print(" ");
2188 if (rec.states < 0x10) pw.print("0000000");
2189 else if (rec.states < 0x100) pw.print("000000");
2190 else if (rec.states < 0x1000) pw.print("00000");
2191 else if (rec.states < 0x10000) pw.print("0000");
2192 else if (rec.states < 0x100000) pw.print("000");
2193 else if (rec.states < 0x1000000) pw.print("00");
2194 else if (rec.states < 0x10000000) pw.print("0");
2195 pw.print(Integer.toHexString(rec.states));
2196 if (oldStatus != rec.batteryStatus) {
2197 oldStatus = rec.batteryStatus;
2198 pw.print(" status=");
2199 switch (oldStatus) {
2200 case BatteryManager.BATTERY_STATUS_UNKNOWN:
2201 pw.print("unknown");
2202 break;
2203 case BatteryManager.BATTERY_STATUS_CHARGING:
2204 pw.print("charging");
2205 break;
2206 case BatteryManager.BATTERY_STATUS_DISCHARGING:
2207 pw.print("discharging");
2208 break;
2209 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
2210 pw.print("not-charging");
2211 break;
2212 case BatteryManager.BATTERY_STATUS_FULL:
2213 pw.print("full");
2214 break;
2215 default:
2216 pw.print(oldStatus);
2217 break;
2218 }
2219 }
2220 if (oldHealth != rec.batteryHealth) {
2221 oldHealth = rec.batteryHealth;
2222 pw.print(" health=");
2223 switch (oldHealth) {
2224 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
2225 pw.print("unknown");
2226 break;
2227 case BatteryManager.BATTERY_HEALTH_GOOD:
2228 pw.print("good");
2229 break;
2230 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
2231 pw.print("overheat");
2232 break;
2233 case BatteryManager.BATTERY_HEALTH_DEAD:
2234 pw.print("dead");
2235 break;
2236 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
2237 pw.print("over-voltage");
2238 break;
2239 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
2240 pw.print("failure");
2241 break;
2242 default:
2243 pw.print(oldHealth);
2244 break;
2245 }
2246 }
2247 if (oldPlug != rec.batteryPlugType) {
2248 oldPlug = rec.batteryPlugType;
2249 pw.print(" plug=");
2250 switch (oldPlug) {
2251 case 0:
2252 pw.print("none");
2253 break;
2254 case BatteryManager.BATTERY_PLUGGED_AC:
2255 pw.print("ac");
2256 break;
2257 case BatteryManager.BATTERY_PLUGGED_USB:
2258 pw.print("usb");
2259 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002260 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
2261 pw.print("wireless");
2262 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002263 default:
2264 pw.print(oldPlug);
2265 break;
2266 }
2267 }
2268 if (oldTemp != rec.batteryTemperature) {
2269 oldTemp = rec.batteryTemperature;
2270 pw.print(" temp=");
2271 pw.print(oldTemp);
2272 }
2273 if (oldVolt != rec.batteryVoltage) {
2274 oldVolt = rec.batteryVoltage;
2275 pw.print(" volt=");
2276 pw.print(oldVolt);
2277 }
2278 printBitDescriptions(pw, oldState, rec.states,
2279 HISTORY_STATE_DESCRIPTIONS);
2280 pw.println();
2281 }
2282 oldState = rec.states;
2283 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002284
2285 public void printNextItemCheckin(PrintWriter pw, HistoryItem rec, long now) {
2286 pw.print(rec.time-now);
2287 pw.print(",");
2288 if (rec.cmd == HistoryItem.CMD_START) {
2289 pw.print("start");
2290 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2291 pw.print("overflow");
2292 } else {
2293 pw.print(rec.batteryLevel);
2294 pw.print(",");
2295 pw.print(rec.states);
2296 pw.print(",");
2297 pw.print(rec.batteryStatus);
2298 pw.print(",");
2299 pw.print(rec.batteryHealth);
2300 pw.print(",");
2301 pw.print(rec.batteryPlugType);
2302 pw.print(",");
2303 pw.print((int)rec.batteryTemperature);
2304 pw.print(",");
2305 pw.print((int)rec.batteryVoltage);
2306 }
2307 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002308 }
2309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 /**
2311 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2312 *
2313 * @param pw a Printer to receive the dump output.
2314 */
2315 @SuppressWarnings("unused")
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002316 public void dumpLocked(PrintWriter pw, boolean isUnpluggedOnly, int reqUid) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002317 prepareForDumpLocked();
2318
2319 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2320
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002321 final HistoryItem rec = new HistoryItem();
2322 if (startIteratingHistoryLocked()) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002323 pw.println("Battery History:");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002324 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002325 while (getNextHistoryLocked(rec)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002326 hprinter.printNextItem(pw, rec, now);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002327 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002328 finishIteratingHistoryLocked();
2329 pw.println("");
2330 }
2331
2332 if (startIteratingOldHistoryLocked()) {
2333 pw.println("Old battery History:");
2334 HistoryPrinter hprinter = new HistoryPrinter();
2335 while (getNextOldHistoryLocked(rec)) {
2336 hprinter.printNextItem(pw, rec, now);
2337 }
2338 finishIteratingOldHistoryLocked();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002339 pw.println("");
2340 }
2341
2342 SparseArray<? extends Uid> uidStats = getUidStats();
2343 final int NU = uidStats.size();
2344 boolean didPid = false;
2345 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002346 for (int i=0; i<NU; i++) {
2347 Uid uid = uidStats.valueAt(i);
2348 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2349 if (pids != null) {
2350 for (int j=0; j<pids.size(); j++) {
2351 Uid.Pid pid = pids.valueAt(j);
2352 if (!didPid) {
2353 pw.println("Per-PID Stats:");
2354 didPid = true;
2355 }
2356 long time = pid.mWakeSum + (pid.mWakeStart != 0
2357 ? (nowRealtime - pid.mWakeStart) : 0);
2358 pw.print(" PID "); pw.print(pids.keyAt(j));
2359 pw.print(" wake time: ");
2360 TimeUtils.formatDuration(time, pw);
2361 pw.println("");
2362 }
2363 }
2364 }
2365 if (didPid) {
2366 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002367 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002368
2369 if (!isUnpluggedOnly) {
2370 pw.println("Statistics since last charge:");
2371 pw.println(" System starts: " + getStartCount()
2372 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002373 dumpLocked(pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002374 pw.println("");
2375 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002376 pw.println("Statistics since last unplugged:");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002377 dumpLocked(pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379
2380 @SuppressWarnings("unused")
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002381 public void dumpCheckinLocked(
2382 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002383 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002385 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2386
2387 final HistoryItem rec = new HistoryItem();
2388 if (startIteratingHistoryLocked()) {
2389 HistoryPrinter hprinter = new HistoryPrinter();
2390 while (getNextHistoryLocked(rec)) {
2391 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2392 pw.print(0); pw.print(',');
2393 pw.print("h"); pw.print(',');
2394 hprinter.printNextItemCheckin(pw, rec, now);
2395 pw.println();
2396 }
2397 finishIteratingHistoryLocked();
2398 }
2399
Dianne Hackborne4a59512010-12-07 11:08:07 -08002400 if (apps != null) {
2401 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2402 for (int i=0; i<apps.size(); i++) {
2403 ApplicationInfo ai = apps.get(i);
2404 ArrayList<String> pkgs = uids.get(ai.uid);
2405 if (pkgs == null) {
2406 pkgs = new ArrayList<String>();
2407 uids.put(ai.uid, pkgs);
2408 }
2409 pkgs.add(ai.packageName);
2410 }
2411 SparseArray<? extends Uid> uidStats = getUidStats();
2412 final int NU = uidStats.size();
2413 String[] lineArgs = new String[2];
2414 for (int i=0; i<NU; i++) {
2415 int uid = uidStats.keyAt(i);
2416 ArrayList<String> pkgs = uids.get(uid);
2417 if (pkgs != null) {
2418 for (int j=0; j<pkgs.size(); j++) {
2419 lineArgs[0] = Integer.toString(uid);
2420 lineArgs[1] = pkgs.get(j);
2421 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2422 (Object[])lineArgs);
2423 }
2424 }
2425 }
2426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 if (isUnpluggedOnly) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002428 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430 else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002431 dumpCheckinLocked(pw, STATS_SINCE_CHARGED, -1);
2432 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 }
2434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435}