blob: 0da77ea041f36d0fe59f76b58cf407c42f5606f7 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborna7c837f2014-01-15 16:20:44 -080027import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080028import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080029import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
38 * A class providing access to battery usage statistics, including information on
39 * wakelocks, processes, packages, and services. All times are represented in microseconds
40 * except where indicated otherwise.
41 * @hide
42 */
43public abstract class BatteryStats implements Parcelable {
44
45 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046
47 /** @hide */
48 public static final String SERVICE_NAME = "batterystats";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * A constant indicating a partial wake lock timer.
52 */
53 public static final int WAKE_TYPE_PARTIAL = 0;
54
55 /**
56 * A constant indicating a full wake lock timer.
57 */
58 public static final int WAKE_TYPE_FULL = 1;
59
60 /**
61 * A constant indicating a window wake lock timer.
62 */
63 public static final int WAKE_TYPE_WINDOW = 2;
64
65 /**
66 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070069
70 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070072 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070074
75 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070076 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070078 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070082 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 /**
86 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 */
88 public static final int WIFI_MULTICAST_ENABLED = 7;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int AUDIO_TURNED_ON = 7;
94
95 /**
96 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070097 */
98 public static final int VIDEO_TURNED_ON = 8;
99
100 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 * A constant indicating a vibrator on timer
102 */
103 public static final int VIBRATOR_ON = 9;
104
105 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700106 * A constant indicating a foreground activity timer
107 */
108 public static final int FOREGROUND_ACTIVITY = 10;
109
110 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700111 * A constant indicating a wifi batched scan is active
112 */
113 public static final int WIFI_BATCHED_SCAN = 11;
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * Include all of the data in the stats, including previously saved data.
117 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700118 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 /**
121 * Include only the last run in the stats.
122 */
123 public static final int STATS_LAST = 1;
124
125 /**
126 * Include only the current run in the stats.
127 */
128 public static final int STATS_CURRENT = 2;
129
130 /**
131 * Include only the run since the last time the device was unplugged in the stats.
132 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700133 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700134
135 // NOTE: Update this list if you add/change any stats above.
136 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700137 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700138 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 /**
141 * Bump the version on this if the checkin format changes.
142 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700143 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700144
145 private static final long BYTES_PER_KB = 1024;
146 private static final long BYTES_PER_MB = 1048576; // 1024^2
147 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Dianne Hackborne4a59512010-12-07 11:08:07 -0800150 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700152 private static final String PROCESS_DATA = "pr";
153 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800154 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700155 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700156 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700157 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String NETWORK_DATA = "nt";
159 private static final String USER_ACTIVITY_DATA = "ua";
160 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800161 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700162 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700163 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700164 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800165 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800166 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700167 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700168 private static final String SCREEN_BRIGHTNESS_DATA = "br";
169 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700170 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700171 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
172 private static final String DATA_CONNECTION_TIME_DATA = "dct";
173 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800174 private static final String WIFI_STATE_TIME_DATA = "wst";
175 private static final String WIFI_STATE_COUNT_DATA = "wsc";
176 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
177 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800178 private static final String POWER_USE_SUMMARY_DATA = "pws";
179 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700181 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private final Formatter mFormatter = new Formatter(mFormatBuilder);
183
184 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700185 * State for keeping track of counting information.
186 */
187 public static abstract class Counter {
188
189 /**
190 * Returns the count associated with this Counter for the
191 * selected type of statistics.
192 *
193 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
194 */
Evan Millarc64edde2009-04-18 12:26:32 -0700195 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700196
197 /**
198 * Temporary for debugging.
199 */
200 public abstract void logState(Printer pw, String prefix);
201 }
202
203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * State for keeping track of timing information.
205 */
206 public static abstract class Timer {
207
208 /**
209 * Returns the count associated with this Timer for the
210 * selected type of statistics.
211 *
212 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
213 */
Evan Millarc64edde2009-04-18 12:26:32 -0700214 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
216 /**
217 * Returns the total time in microseconds associated with this Timer for the
218 * selected type of statistics.
219 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800220 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
222 * @return a time in microseconds
223 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800224 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * Temporary for debugging.
228 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700229 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231
232 /**
233 * The statistics associated with a particular uid.
234 */
235 public static abstract class Uid {
236
237 /**
238 * Returns a mapping containing wakelock statistics.
239 *
240 * @return a Map from Strings to Uid.Wakelock objects.
241 */
242 public abstract Map<String, ? extends Wakelock> getWakelockStats();
243
244 /**
245 * The statistics associated with a particular wake lock.
246 */
247 public static abstract class Wakelock {
248 public abstract Timer getWakeTime(int type);
249 }
250
251 /**
252 * Returns a mapping containing sensor statistics.
253 *
254 * @return a Map from Integer sensor ids to Uid.Sensor objects.
255 */
256 public abstract Map<Integer, ? extends Sensor> getSensorStats();
257
258 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700259 * Returns a mapping containing active process data.
260 */
261 public abstract SparseArray<? extends Pid> getPidStats();
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * Returns a mapping containing process statistics.
265 *
266 * @return a Map from Strings to Uid.Proc objects.
267 */
268 public abstract Map<String, ? extends Proc> getProcessStats();
269
270 /**
271 * Returns a mapping containing package statistics.
272 *
273 * @return a Map from Strings to Uid.Pkg objects.
274 */
275 public abstract Map<String, ? extends Pkg> getPackageStats();
276
277 /**
278 * {@hide}
279 */
280 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700281
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800282 public abstract void noteWifiRunningLocked(long elapsedRealtime);
283 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
284 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
285 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
286 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
287 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
288 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
289 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
290 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
291 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
292 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
293 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
294 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
295 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
296 public abstract void noteActivityResumedLocked(long elapsedRealtime);
297 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800298 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
299 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
300 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
301 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
302 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
303 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
304 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700305 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800306 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Robert Greenwalta029ea12013-09-25 16:38:12 -0700308 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
309
Dianne Hackborn617f8772009-03-31 15:04:46 -0700310 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700311 * Note that these must match the constants in android.os.PowerManager.
312 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
313 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700314 */
315 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700316 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700317 };
318
Jeff Browndf693de2012-07-27 12:03:38 -0700319 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700320
Dianne Hackborn617f8772009-03-31 15:04:46 -0700321 public abstract void noteUserActivityLocked(int type);
322 public abstract boolean hasUserActivity();
323 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700324
325 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800326 public abstract long getNetworkActivityBytes(int type, int which);
327 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800328 public abstract long getMobileRadioActiveTime(int which);
329 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800332 /*
333 * FIXME: it's not correct to use this magic value because it
334 * could clash with a sensor handle (which are defined by
335 * the sensor HAL, and therefore out of our control
336 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 // Magic sensor number for the GPS.
338 public static final int GPS = -10000;
339
340 public abstract int getHandle();
341
342 public abstract Timer getSensorTime();
343 }
344
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700345 public class Pid {
346 public long mWakeSum;
347 public long mWakeStart;
348 }
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * The statistics associated with a particular process.
352 */
353 public static abstract class Proc {
354
Dianne Hackborn287952c2010-09-22 22:34:31 -0700355 public static class ExcessivePower {
356 public static final int TYPE_WAKE = 1;
357 public static final int TYPE_CPU = 2;
358
359 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700360 public long overTime;
361 public long usedTime;
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800365 * Returns true if this process is still active in the battery stats.
366 */
367 public abstract boolean isActive();
368
369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * Returns the total time (in 1/100 sec) spent executing in user code.
371 *
372 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
373 */
374 public abstract long getUserTime(int which);
375
376 /**
377 * Returns the total time (in 1/100 sec) spent executing in system code.
378 *
379 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
380 */
381 public abstract long getSystemTime(int which);
382
383 /**
384 * Returns the number of times the process has been started.
385 *
386 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
387 */
388 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700389
390 /**
391 * Returns the cpu time spent in microseconds while the process was in the foreground.
392 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
393 * @return foreground cpu time in microseconds
394 */
395 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700396
397 /**
398 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
399 * @param speedStep the index of the CPU speed. This is not the actual speed of the
400 * CPU.
401 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
402 * @see BatteryStats#getCpuSpeedSteps()
403 */
404 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700405
Dianne Hackborn287952c2010-09-22 22:34:31 -0700406 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700407
Dianne Hackborn287952c2010-09-22 22:34:31 -0700408 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
411 /**
412 * The statistics associated with a particular package.
413 */
414 public static abstract class Pkg {
415
416 /**
417 * Returns the number of times this package has done something that could wake up the
418 * device from sleep.
419 *
420 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
421 */
422 public abstract int getWakeups(int which);
423
424 /**
425 * Returns a mapping containing service statistics.
426 */
427 public abstract Map<String, ? extends Serv> getServiceStats();
428
429 /**
430 * The statistics associated with a particular service.
431 */
432 public abstract class Serv {
433
434 /**
435 * Returns the amount of time spent started.
436 *
437 * @param batteryUptime elapsed uptime on battery in microseconds.
438 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
439 * @return
440 */
441 public abstract long getStartTime(long batteryUptime, int which);
442
443 /**
444 * Returns the total number of times startService() has been called.
445 *
446 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
447 */
448 public abstract int getStarts(int which);
449
450 /**
451 * Returns the total number times the service has been launched.
452 *
453 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
454 */
455 public abstract int getLaunches(int which);
456 }
457 }
458 }
459
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800460 public final static class HistoryTag {
461 public String string;
462 public int uid;
463
464 public int poolIdx;
465
466 public void setTo(HistoryTag o) {
467 string = o.string;
468 uid = o.uid;
469 poolIdx = o.poolIdx;
470 }
471
472 public void setTo(String _string, int _uid) {
473 string = _string;
474 uid = _uid;
475 poolIdx = -1;
476 }
477
478 public void writeToParcel(Parcel dest, int flags) {
479 dest.writeString(string);
480 dest.writeInt(uid);
481 }
482
483 public void readFromParcel(Parcel src) {
484 string = src.readString();
485 uid = src.readInt();
486 poolIdx = -1;
487 }
488
489 @Override
490 public boolean equals(Object o) {
491 if (this == o) return true;
492 if (o == null || getClass() != o.getClass()) return false;
493
494 HistoryTag that = (HistoryTag) o;
495
496 if (uid != that.uid) return false;
497 if (!string.equals(that.string)) return false;
498
499 return true;
500 }
501
502 @Override
503 public int hashCode() {
504 int result = string.hashCode();
505 result = 31 * result + uid;
506 return result;
507 }
508 }
509
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700510 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700511 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700512
513 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800514
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800515 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800516 public static final byte CMD_NULL = -1;
517 public static final byte CMD_START = 4;
518 public static final byte CMD_OVERFLOW = 5;
519
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700520 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700521
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800522 /**
523 * Return whether the command code is a delta data update.
524 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800525 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800526 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800527 }
528
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700529 public byte batteryLevel;
530 public byte batteryStatus;
531 public byte batteryHealth;
532 public byte batteryPlugType;
533
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900534 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700535 public char batteryVoltage;
536
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700537 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700538 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800539 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700540 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800541 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
542 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700543 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800544 public static final int STATE_PHONE_STATE_SHIFT = 6;
545 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700546 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800547 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800548 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800549
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700550 // These states always appear directly in the first int token
551 // of a delta change; they should be ones that change relatively
552 // frequently.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800553 public static final int STATE_WAKE_LOCK_FLAG = 1<<31;
554 public static final int STATE_SENSOR_ON_FLAG = 1<<30;
555 public static final int STATE_GPS_ON_FLAG = 1<<29;
556 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800557 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800558 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800559 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800560 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700561 // These are on the lower bits used for the command; if they change
562 // we need to write another int of data.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800563 public static final int STATE_PHONE_SCANNING_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700564 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
565 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
566 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
567 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
568 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
569 public static final int STATE_WIFI_ON_FLAG = 1<<17;
570 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700571
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700572 public static final int MOST_INTERESTING_STATES =
573 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
574 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
575
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700576 public int states;
577
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800578 // The wake lock that was acquired at this point.
579 public HistoryTag wakelockTag;
580
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800581 // Kernel wakeup reason at this point.
582 public HistoryTag wakeReasonTag;
583
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800584 public static final int EVENT_FLAG_START = 0x8000;
585 public static final int EVENT_FLAG_FINISH = 0x4000;
586
587 // No event in this item.
588 public static final int EVENT_NONE = 0x0000;
589 // Event is about a process that is running.
590 public static final int EVENT_PROC = 0x0001;
591 // Event is about an application package that is in the foreground.
592 public static final int EVENT_FOREGROUND = 0x0002;
593 // Event is about an application package that is at the top of the screen.
594 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800595 // Event is about an application package that is at the top of the screen.
596 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800597 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800598 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800599
600 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
601 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
602 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
603 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
604 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
605 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800606 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
607 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800608
609 // For CMD_EVENT.
610 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800611 public HistoryTag eventTag;
612
613 // Meta-data when reading.
614 public int numReadInts;
615
616 // Pre-allocated objects.
617 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800618 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800619 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800620
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700621 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700622 }
623
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700624 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700625 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800626 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700627 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700628 }
629
630 public int describeContents() {
631 return 0;
632 }
633
634 public void writeToParcel(Parcel dest, int flags) {
635 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700636 int bat = (((int)cmd)&0xff)
637 | ((((int)batteryLevel)<<8)&0xff00)
638 | ((((int)batteryStatus)<<16)&0xf0000)
639 | ((((int)batteryHealth)<<20)&0xf00000)
640 | ((((int)batteryPlugType)<<24)&0xf000000);
641 dest.writeInt(bat);
642 bat = (((int)batteryTemperature)&0xffff)
643 | ((((int)batteryVoltage)<<16)&0xffff0000);
644 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700645 dest.writeInt(states);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800646 if (wakelockTag != null) {
647 dest.writeInt(1);
648 wakelockTag.writeToParcel(dest, flags);
649 } else {
650 dest.writeInt(0);
651 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800652 if (wakeReasonTag != null) {
653 dest.writeInt(1);
654 wakeReasonTag.writeToParcel(dest, flags);
655 } else {
656 dest.writeInt(0);
657 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800658 dest.writeInt(eventCode);
659 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800660 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800661 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800662 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700663 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700664
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800665 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800666 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700667 int bat = src.readInt();
668 cmd = (byte)(bat&0xff);
669 batteryLevel = (byte)((bat>>8)&0xff);
670 batteryStatus = (byte)((bat>>16)&0xf);
671 batteryHealth = (byte)((bat>>20)&0xf);
672 batteryPlugType = (byte)((bat>>24)&0xf);
673 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900674 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700675 batteryVoltage = (char)((bat>>16)&0xffff);
676 states = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800677 if (src.readInt() != 0) {
678 wakelockTag = localWakelockTag;
679 wakelockTag.readFromParcel(src);
680 } else {
681 wakelockTag = null;
682 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800683 if (src.readInt() != 0) {
684 wakeReasonTag = localWakeReasonTag;
685 wakeReasonTag.readFromParcel(src);
686 } else {
687 wakeReasonTag = null;
688 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800689 eventCode = src.readInt();
690 if (eventCode != EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800691 eventTag = localEventTag;
692 eventTag.readFromParcel(src);
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700693 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800694 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700695 }
696
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700697 public void clear() {
698 time = 0;
699 cmd = CMD_NULL;
700 batteryLevel = 0;
701 batteryStatus = 0;
702 batteryHealth = 0;
703 batteryPlugType = 0;
704 batteryTemperature = 0;
705 batteryVoltage = 0;
706 states = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800707 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800708 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800709 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800710 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700711 }
712
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700713 public void setTo(HistoryItem o) {
714 time = o.time;
715 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800716 setToCommon(o);
717 }
718
719 public void setTo(long time, byte cmd, HistoryItem o) {
720 this.time = time;
721 this.cmd = cmd;
722 setToCommon(o);
723 }
724
725 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700726 batteryLevel = o.batteryLevel;
727 batteryStatus = o.batteryStatus;
728 batteryHealth = o.batteryHealth;
729 batteryPlugType = o.batteryPlugType;
730 batteryTemperature = o.batteryTemperature;
731 batteryVoltage = o.batteryVoltage;
732 states = o.states;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800733 if (o.wakelockTag != null) {
734 wakelockTag = localWakelockTag;
735 wakelockTag.setTo(o.wakelockTag);
736 } else {
737 wakelockTag = null;
738 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800739 if (o.wakeReasonTag != null) {
740 wakeReasonTag = localWakeReasonTag;
741 wakeReasonTag.setTo(o.wakeReasonTag);
742 } else {
743 wakeReasonTag = null;
744 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800745 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800746 if (o.eventTag != null) {
747 eventTag = localEventTag;
748 eventTag.setTo(o.eventTag);
749 } else {
750 eventTag = null;
751 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700752 }
753
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800754 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700755 return batteryLevel == o.batteryLevel
756 && batteryStatus == o.batteryStatus
757 && batteryHealth == o.batteryHealth
758 && batteryPlugType == o.batteryPlugType
759 && batteryTemperature == o.batteryTemperature
760 && batteryVoltage == o.batteryVoltage
761 && states == o.states;
762 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800763
764 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800765 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800766 return false;
767 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800768 if (wakelockTag != o.wakelockTag) {
769 if (wakelockTag == null || o.wakelockTag == null) {
770 return false;
771 }
772 if (!wakelockTag.equals(o.wakelockTag)) {
773 return false;
774 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800775 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800776 if (wakeReasonTag != o.wakeReasonTag) {
777 if (wakeReasonTag == null || o.wakeReasonTag == null) {
778 return false;
779 }
780 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
781 return false;
782 }
783 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800784 if (eventTag != o.eventTag) {
785 if (eventTag == null || o.eventTag == null) {
786 return false;
787 }
788 if (!eventTag.equals(o.eventTag)) {
789 return false;
790 }
791 }
792 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800793 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700794 }
795
796 public static final class BitDescription {
797 public final int mask;
798 public final int shift;
799 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800800 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700801 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800802 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700803
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800804 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700805 this.mask = mask;
806 this.shift = -1;
807 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800808 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700809 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800810 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700811 }
812
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800813 public BitDescription(int mask, int shift, String name, String shortName,
814 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700815 this.mask = mask;
816 this.shift = shift;
817 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800818 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700819 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800820 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700821 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700822 }
823
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800824 public abstract int getHistoryTotalSize();
825
826 public abstract int getHistoryUsedSize();
827
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700828 public abstract boolean startIteratingHistoryLocked();
829
Dianne Hackborn099bc622014-01-22 13:39:16 -0800830 public abstract int getHistoryStringPoolSize();
831
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800832 public abstract int getHistoryStringPoolBytes();
833
834 public abstract String getHistoryTagPoolString(int index);
835
836 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800837
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700838 public abstract boolean getNextHistoryLocked(HistoryItem out);
839
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700840 public abstract void finishIteratingHistoryLocked();
841
842 public abstract boolean startIteratingOldHistoryLocked();
843
844 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
845
846 public abstract void finishIteratingOldHistoryLocked();
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700849 * Return the base time offset for the battery history.
850 */
851 public abstract long getHistoryBaseTime();
852
853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 * Returns the number of times the device has been started.
855 */
856 public abstract int getStartCount();
857
858 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700859 * 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 -0800860 * running on battery.
861 *
862 * {@hide}
863 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800864 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800866 /**
867 * Returns the number of times the screen was turned on.
868 *
869 * {@hide}
870 */
871 public abstract int getScreenOnCount(int which);
872
Dianne Hackborn617f8772009-03-31 15:04:46 -0700873 public static final int SCREEN_BRIGHTNESS_DARK = 0;
874 public static final int SCREEN_BRIGHTNESS_DIM = 1;
875 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
876 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
877 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
878
879 static final String[] SCREEN_BRIGHTNESS_NAMES = {
880 "dark", "dim", "medium", "light", "bright"
881 };
882
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800883 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
884 "0", "1", "2", "3", "4"
885 };
886
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
888
889 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700890 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700891 * the given brightness
892 *
893 * {@hide}
894 */
895 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800896 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700897
898 public abstract int getInputEventCount(int which);
899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700901 * 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 -0800902 * running on battery.
903 *
904 * {@hide}
905 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800906 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700907
908 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800909 * Returns the number of times a phone call was activated.
910 *
911 * {@hide}
912 */
913 public abstract int getPhoneOnCount(int which);
914
915 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700916 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700917 * the given signal strength.
918 *
919 * {@hide}
920 */
921 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800922 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700923
Dianne Hackborn617f8772009-03-31 15:04:46 -0700924 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700925 * Returns the time in microseconds that the phone has been trying to
926 * acquire a signal.
927 *
928 * {@hide}
929 */
930 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800931 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -0700932
933 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700934 * Returns the number of times the phone has entered the given signal strength.
935 *
936 * {@hide}
937 */
938 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
939
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800940 /**
941 * Returns the time in microseconds that the mobile network has been active
942 * (in a high power state).
943 *
944 * {@hide}
945 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800946 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800947
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800948 /**
949 * Returns the number of times that the mobile network has transitioned to the
950 * active state.
951 *
952 * {@hide}
953 */
954 public abstract int getMobileRadioActiveCount(int which);
955
956 /**
957 * Returns the time in microseconds that the mobile network has been active
958 * (in a high power state) but not being able to blame on an app.
959 *
960 * {@hide}
961 */
962 public abstract long getMobileRadioActiveUnknownTime(int which);
963
964 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800965 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800966 *
967 * {@hide}
968 */
969 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800970
Dianne Hackborn627bba72009-03-24 22:32:56 -0700971 public static final int DATA_CONNECTION_NONE = 0;
972 public static final int DATA_CONNECTION_GPRS = 1;
973 public static final int DATA_CONNECTION_EDGE = 2;
974 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700975 public static final int DATA_CONNECTION_CDMA = 4;
976 public static final int DATA_CONNECTION_EVDO_0 = 5;
977 public static final int DATA_CONNECTION_EVDO_A = 6;
978 public static final int DATA_CONNECTION_1xRTT = 7;
979 public static final int DATA_CONNECTION_HSDPA = 8;
980 public static final int DATA_CONNECTION_HSUPA = 9;
981 public static final int DATA_CONNECTION_HSPA = 10;
982 public static final int DATA_CONNECTION_IDEN = 11;
983 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700984 public static final int DATA_CONNECTION_LTE = 13;
985 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800986 public static final int DATA_CONNECTION_HSPAP = 15;
987 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700988
Dianne Hackborn627bba72009-03-24 22:32:56 -0700989 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700990 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700991 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800992 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700993 };
994
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700995 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700996
997 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700998 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700999 * the given data connection.
1000 *
1001 * {@hide}
1002 */
1003 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001004 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001007 * Returns the number of times the phone has entered the given data
1008 * connection type.
1009 *
1010 * {@hide}
1011 */
1012 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001013
1014 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1015 = new BitDescription[] {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001016 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1017 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001018 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001019 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1020 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1021 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001022 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001023 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1024 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001025 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
1026 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001027 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1028 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1029 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1030 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1031 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1032 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1033 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1034 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1035 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1036 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1037 new String[] {"in", "out", "emergency", "off"},
1038 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001039 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001040 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1041 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1042 "0", "1", "2", "3", "4"
1043 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001044 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1045 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1046 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001047 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001048
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001049 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001050 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001051 };
1052
1053 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001054 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001055 };
1056
Dianne Hackborn617f8772009-03-31 15:04:46 -07001057 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001058 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001059 * running on battery.
1060 *
1061 * {@hide}
1062 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001063 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001064
1065 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001066 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001067 * been in the running state while the device was running on battery.
1068 *
1069 * {@hide}
1070 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001071 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001072
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001073 public static final int WIFI_STATE_OFF = 0;
1074 public static final int WIFI_STATE_OFF_SCANNING = 1;
1075 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1076 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1077 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1078 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1079 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1080 public static final int WIFI_STATE_SOFT_AP = 7;
1081
1082 static final String[] WIFI_STATE_NAMES = {
1083 "off", "scanning", "no_net", "disconn",
1084 "sta", "p2p", "sta_p2p", "soft_ap"
1085 };
1086
1087 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1088
1089 /**
1090 * Returns the time in microseconds that WiFi has been running in the given state.
1091 *
1092 * {@hide}
1093 */
1094 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001095 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001096
1097 /**
1098 * Returns the number of times that WiFi has entered the given state.
1099 *
1100 * {@hide}
1101 */
1102 public abstract int getWifiStateCount(int wifiState, int which);
1103
The Android Open Source Project10592532009-03-18 17:39:46 -07001104 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001105 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001106 * running on battery.
1107 *
1108 * {@hide}
1109 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001110 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001111
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001112 public abstract int getBluetoothPingCount();
1113
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001114 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1115 public static final int BLUETOOTH_STATE_LOW = 1;
1116 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1117 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001118
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001119 static final String[] BLUETOOTH_STATE_NAMES = {
1120 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001121 };
1122
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001123 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001124
1125 /**
1126 * Returns the time in microseconds that Bluetooth has been running in the
1127 * given active state.
1128 *
1129 * {@hide}
1130 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001131 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001133
1134 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001135 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001136 *
1137 * {@hide}
1138 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001139 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001140
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001141 public static final int NETWORK_MOBILE_RX_DATA = 0;
1142 public static final int NETWORK_MOBILE_TX_DATA = 1;
1143 public static final int NETWORK_WIFI_RX_DATA = 2;
1144 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001145
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001146 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001147
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001148 public abstract long getNetworkActivityBytes(int type, int which);
1149 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001150
The Android Open Source Project10592532009-03-18 17:39:46 -07001151 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001152 * Return the wall clock time when battery stats data collection started.
1153 */
1154 public abstract long getStartClockTime();
1155
1156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 * Return whether we are currently running on battery.
1158 */
1159 public abstract boolean getIsOnBattery();
1160
1161 /**
1162 * Returns a SparseArray containing the statistics for each uid.
1163 */
1164 public abstract SparseArray<? extends Uid> getUidStats();
1165
1166 /**
1167 * Returns the current battery uptime in microseconds.
1168 *
1169 * @param curTime the amount of elapsed realtime in microseconds.
1170 */
1171 public abstract long getBatteryUptime(long curTime);
1172
1173 /**
1174 * Returns the current battery realtime in microseconds.
1175 *
1176 * @param curTime the amount of elapsed realtime in microseconds.
1177 */
1178 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001179
1180 /**
Evan Millar633a1742009-04-02 16:36:33 -07001181 * Returns the battery percentage level at the last time the device was unplugged from power, or
1182 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001183 */
Evan Millar633a1742009-04-02 16:36:33 -07001184 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001185
1186 /**
Evan Millar633a1742009-04-02 16:36:33 -07001187 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1188 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001189 */
Evan Millar633a1742009-04-02 16:36:33 -07001190 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191
1192 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001193 * Get the amount the battery has discharged since the stats were
1194 * last reset after charging, as a lower-end approximation.
1195 */
1196 public abstract int getLowDischargeAmountSinceCharge();
1197
1198 /**
1199 * Get the amount the battery has discharged since the stats were
1200 * last reset after charging, as an upper-end approximation.
1201 */
1202 public abstract int getHighDischargeAmountSinceCharge();
1203
1204 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001205 * Get the amount the battery has discharged while the screen was on,
1206 * since the last time power was unplugged.
1207 */
1208 public abstract int getDischargeAmountScreenOn();
1209
1210 /**
1211 * Get the amount the battery has discharged while the screen was on,
1212 * since the last time the device was charged.
1213 */
1214 public abstract int getDischargeAmountScreenOnSinceCharge();
1215
1216 /**
1217 * Get the amount the battery has discharged while the screen was off,
1218 * since the last time power was unplugged.
1219 */
1220 public abstract int getDischargeAmountScreenOff();
1221
1222 /**
1223 * Get the amount the battery has discharged while the screen was off,
1224 * since the last time the device was charged.
1225 */
1226 public abstract int getDischargeAmountScreenOffSinceCharge();
1227
1228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 * Returns the total, last, or current battery uptime in microseconds.
1230 *
1231 * @param curTime the elapsed realtime in microseconds.
1232 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1233 */
1234 public abstract long computeBatteryUptime(long curTime, int which);
1235
1236 /**
1237 * Returns the total, last, or current battery realtime in microseconds.
1238 *
1239 * @param curTime the current elapsed realtime in microseconds.
1240 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1241 */
1242 public abstract long computeBatteryRealtime(long curTime, int which);
1243
1244 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001245 * Returns the total, last, or current battery screen off uptime in microseconds.
1246 *
1247 * @param curTime the elapsed realtime in microseconds.
1248 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1249 */
1250 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1251
1252 /**
1253 * Returns the total, last, or current battery screen off realtime in microseconds.
1254 *
1255 * @param curTime the current elapsed realtime in microseconds.
1256 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1257 */
1258 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1259
1260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 * Returns the total, last, or current uptime in microseconds.
1262 *
1263 * @param curTime the current elapsed realtime in microseconds.
1264 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1265 */
1266 public abstract long computeUptime(long curTime, int which);
1267
1268 /**
1269 * Returns the total, last, or current realtime in microseconds.
1270 * *
1271 * @param curTime the current elapsed realtime in microseconds.
1272 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1273 */
1274 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001275
1276 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
Amith Yamasanie43530a2009-08-21 13:11:37 -07001278 /** Returns the number of different speeds that the CPU can run at */
1279 public abstract int getCpuSpeedSteps();
1280
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001281 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1282
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001283 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 long days = seconds / (60 * 60 * 24);
1285 if (days != 0) {
1286 out.append(days);
1287 out.append("d ");
1288 }
1289 long used = days * 60 * 60 * 24;
1290
1291 long hours = (seconds - used) / (60 * 60);
1292 if (hours != 0 || used != 0) {
1293 out.append(hours);
1294 out.append("h ");
1295 }
1296 used += hours * 60 * 60;
1297
1298 long mins = (seconds-used) / 60;
1299 if (mins != 0 || used != 0) {
1300 out.append(mins);
1301 out.append("m ");
1302 }
1303 used += mins * 60;
1304
1305 if (seconds != 0 || used != 0) {
1306 out.append(seconds-used);
1307 out.append("s ");
1308 }
1309 }
1310
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001311 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001313 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 sb.append((time - (sec * 100)) * 10);
1315 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001318 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001320 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 sb.append(time - (sec * 1000));
1322 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001325 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001326 long sec = time / 1000;
1327 formatTimeRaw(sb, sec);
1328 sb.append(time - (sec * 1000));
1329 sb.append("ms");
1330 }
1331
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001332 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001334 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 float perc = ((float)num) / ((float)den) * 100;
1337 mFormatBuilder.setLength(0);
1338 mFormatter.format("%.1f%%", perc);
1339 return mFormatBuilder.toString();
1340 }
1341
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001342 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001343 mFormatBuilder.setLength(0);
1344
1345 if (bytes < BYTES_PER_KB) {
1346 return bytes + "B";
1347 } else if (bytes < BYTES_PER_MB) {
1348 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1349 return mFormatBuilder.toString();
1350 } else if (bytes < BYTES_PER_GB){
1351 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1352 return mFormatBuilder.toString();
1353 } else {
1354 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1355 return mFormatBuilder.toString();
1356 }
1357 }
1358
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001359 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001360 if (timer != null) {
1361 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001362 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001363 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1364 return totalTimeMillis;
1365 }
1366 return 0;
1367 }
1368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 *
1371 * @param sb a StringBuilder object.
1372 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001373 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 * @param name the name of the wakelock.
1375 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1376 * @param linePrefix a String to be prepended to each line of output.
1377 * @return the line prefix
1378 */
1379 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001380 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001383 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384
Evan Millarc64edde2009-04-18 12:26:32 -07001385 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 if (totalTimeMillis != 0) {
1387 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001388 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001389 if (name != null) {
1390 sb.append(name);
1391 sb.append(' ');
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 sb.append('(');
1394 sb.append(count);
1395 sb.append(" times)");
1396 return ", ";
1397 }
1398 }
1399 return linePrefix;
1400 }
1401
1402 /**
1403 * Checkin version of wakelock printer. Prints simple comma-separated list.
1404 *
1405 * @param sb a StringBuilder object.
1406 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001407 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 * @param name the name of the wakelock.
1409 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1410 * @param linePrefix a String to be prepended to each line of output.
1411 * @return the line prefix
1412 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001413 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1414 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 long totalTimeMicros = 0;
1416 int count = 0;
1417 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001418 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001419 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421 sb.append(linePrefix);
1422 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1423 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001424 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 sb.append(count);
1426 return ",";
1427 }
1428
1429 /**
1430 * Dump a comma-separated line of values for terse checkin mode.
1431 *
1432 * @param pw the PageWriter to dump log to
1433 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1434 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1435 * @param args type-dependent data arguments
1436 */
1437 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1438 Object... args ) {
1439 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1440 pw.print(uid); pw.print(',');
1441 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001442 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443
1444 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001445 pw.print(',');
1446 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001448 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450
1451 /**
1452 * Checkin server version of dump to produce more compact, computer-readable log.
1453 *
1454 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001456 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1458 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1459 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1461 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001462 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1463 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1464 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 final long totalRealtime = computeRealtime(rawRealtime, which);
1466 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001467 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1468 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1469 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1470 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1471 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472
1473 StringBuilder sb = new StringBuilder(128);
1474
Evan Millar22ac0432009-03-31 11:33:18 -07001475 SparseArray<? extends Uid> uidStats = getUidStats();
1476 final int NU = uidStats.size();
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 // Dump "battery" stat
1481 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001482 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001483 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001484 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001485 getStartClockTime(),
1486 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001488 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001489 long fullWakeLockTimeTotal = 0;
1490 long partialWakeLockTimeTotal = 0;
1491
1492 for (int iu = 0; iu < NU; iu++) {
1493 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001494
Evan Millar22ac0432009-03-31 11:33:18 -07001495 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1496 if (wakelocks.size() > 0) {
1497 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1498 : wakelocks.entrySet()) {
1499 Uid.Wakelock wl = ent.getValue();
1500
1501 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1502 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001503 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1504 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001505 }
1506
1507 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1508 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001509 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001510 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001511 }
1512 }
1513 }
1514 }
1515
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1517 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1518 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1519 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1520 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1521 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1522 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1523 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1524
1525 // Dump network stats
1526 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1527 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1528 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 // Dump misc stats
1531 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001532 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001533 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001534 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001535 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001536 getInputEventCount(which), getMobileRadioActiveTime(rawRealtime, which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001537
1538 // Dump screen brightness stats
1539 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1540 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001541 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001542 }
1543 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001544
Dianne Hackborn627bba72009-03-24 22:32:56 -07001545 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001546 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1547 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001548 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001549 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001550 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001551 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001552 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001553 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001554 args[i] = getPhoneSignalStrengthCount(i, which);
1555 }
1556 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001557
Dianne Hackborn627bba72009-03-24 22:32:56 -07001558 // Dump network type stats
1559 args = new Object[NUM_DATA_CONNECTION_TYPES];
1560 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001561 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001562 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001563 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1564 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1565 args[i] = getPhoneDataConnectionCount(i, which);
1566 }
1567 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001568
1569 // Dump wifi state stats
1570 args = new Object[NUM_WIFI_STATES];
1571 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001572 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001573 }
1574 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1575 for (int i=0; i<NUM_WIFI_STATES; i++) {
1576 args[i] = getWifiStateCount(i, which);
1577 }
1578 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1579
1580 // Dump bluetooth state stats
1581 args = new Object[NUM_BLUETOOTH_STATES];
1582 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001583 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001584 }
1585 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1586 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1587 args[i] = getBluetoothStateCount(i, which);
1588 }
1589 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1590
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001591 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001592 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001593 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001596 if (which == STATS_SINCE_UNPLUGGED) {
1597 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1598 getDischargeStartLevel()-getDischargeCurrentLevel(),
1599 getDischargeStartLevel()-getDischargeCurrentLevel(),
1600 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1601 } else {
1602 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1603 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1604 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1605 }
1606
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001607 if (reqUid < 0) {
1608 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1609 if (kernelWakelocks.size() > 0) {
1610 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1611 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001613
1614 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1615 sb.toString());
1616 }
Evan Millarc64edde2009-04-18 12:26:32 -07001617 }
1618 }
1619
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001620 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1621 helper.create(this);
1622 helper.refreshStats(which, UserHandle.USER_ALL);
1623 List<BatterySipper> sippers = helper.getUsageList();
1624 if (sippers != null && sippers.size() > 0) {
1625 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1626 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001627 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001628 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1629 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1630 for (int i=0; i<sippers.size(); i++) {
1631 BatterySipper bs = sippers.get(i);
1632 int uid = 0;
1633 String label;
1634 switch (bs.drainType) {
1635 case IDLE:
1636 label="idle";
1637 break;
1638 case CELL:
1639 label="cell";
1640 break;
1641 case PHONE:
1642 label="phone";
1643 break;
1644 case WIFI:
1645 label="wifi";
1646 break;
1647 case BLUETOOTH:
1648 label="blue";
1649 break;
1650 case SCREEN:
1651 label="scrn";
1652 break;
1653 case APP:
1654 uid = bs.uidObj.getUid();
1655 label = "uid";
1656 break;
1657 case USER:
1658 uid = UserHandle.getUid(bs.userId, 0);
1659 label = "user";
1660 break;
1661 case UNACCOUNTED:
1662 label = "unacc";
1663 break;
1664 case OVERCOUNTED:
1665 label = "over";
1666 break;
1667 default:
1668 label = "???";
1669 }
1670 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1671 BatteryStatsHelper.makemAh(bs.value));
1672 }
1673 }
1674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 for (int iu = 0; iu < NU; iu++) {
1676 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001677 if (reqUid >= 0 && uid != reqUid) {
1678 continue;
1679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 Uid u = uidStats.valueAt(iu);
1681 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001682 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1683 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1684 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1685 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1686 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1687 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001688 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1689 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001690 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1691 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001692 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1693 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1694 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001695
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001696 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1697 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001698 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001699 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1700 wifiBytesRx, wifiBytesTx,
1701 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001702 wifiPacketsRx, wifiPacketsTx,
1703 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001704 }
1705
Nick Pelly6ccaa542012-06-15 15:22:47 -07001706 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001707 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001708 dumpLine(pw, uid, category, WIFI_DATA,
1709 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711
Dianne Hackborn617f8772009-03-31 15:04:46 -07001712 if (u.hasUserActivity()) {
1713 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1714 boolean hasData = false;
1715 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1716 int val = u.getUserActivityCount(i, which);
1717 args[i] = val;
1718 if (val != 0) hasData = true;
1719 }
1720 if (hasData) {
1721 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1722 }
1723 }
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1726 if (wakelocks.size() > 0) {
1727 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1728 : wakelocks.entrySet()) {
1729 Uid.Wakelock wl = ent.getValue();
1730 String linePrefix = "";
1731 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001732 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001733 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001734 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001735 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001736 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001737 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738
1739 // Only log if we had at lease one wakelock...
1740 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001741 String name = ent.getKey();
1742 if (name.indexOf(',') >= 0) {
1743 name = name.replace(',', '_');
1744 }
1745 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747 }
1748 }
1749
1750 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1751 if (sensors.size() > 0) {
1752 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1753 : sensors.entrySet()) {
1754 Uid.Sensor se = ent.getValue();
1755 int sensorNumber = ent.getKey();
1756 Timer timer = se.getSensorTime();
1757 if (timer != null) {
1758 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001759 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001760 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 if (totalTime != 0) {
1762 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1763 }
1764 }
1765 }
1766 }
1767
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001768 Timer vibTimer = u.getVibratorOnTimer();
1769 if (vibTimer != null) {
1770 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001771 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001772 int count = vibTimer.getCountLocked(which);
1773 if (totalTime != 0) {
1774 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1775 }
1776 }
1777
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001778 Timer fgTimer = u.getForegroundActivityTimer();
1779 if (fgTimer != null) {
1780 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001781 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001782 int count = fgTimer.getCountLocked(which);
1783 if (totalTime != 0) {
1784 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1785 }
1786 }
1787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1789 if (processStats.size() > 0) {
1790 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1791 : processStats.entrySet()) {
1792 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001793
1794 final long userMillis = ps.getUserTime(which) * 10;
1795 final long systemMillis = ps.getSystemTime(which) * 10;
1796 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1797 final long starts = ps.getStarts(which);
1798
1799 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1800 || starts != 0) {
1801 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1802 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 }
1805 }
1806
1807 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1808 if (packageStats.size() > 0) {
1809 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1810 : packageStats.entrySet()) {
1811
1812 Uid.Pkg ps = ent.getValue();
1813 int wakeups = ps.getWakeups(which);
1814 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1815 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1816 : serviceStats.entrySet()) {
1817 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1818 long startTime = ss.getStartTime(batteryUptime, which);
1819 int starts = ss.getStarts(which);
1820 int launches = ss.getLaunches(which);
1821 if (startTime != 0 || starts != 0 || launches != 0) {
1822 dumpLine(pw, uid, category, APK_DATA,
1823 wakeups, // wakeup alarms
1824 ent.getKey(), // Apk
1825 sent.getKey(), // service
1826 startTime / 1000, // time spent started, in ms
1827 starts,
1828 launches);
1829 }
1830 }
1831 }
1832 }
1833 }
1834 }
1835
Dianne Hackborn81038902012-11-26 17:04:09 -08001836 static final class TimerEntry {
1837 final String mName;
1838 final int mId;
1839 final BatteryStats.Timer mTimer;
1840 final long mTime;
1841 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1842 mName = name;
1843 mId = id;
1844 mTimer = timer;
1845 mTime = time;
1846 }
1847 }
1848
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001849 private void printmAh(PrintWriter printer, double power) {
1850 printer.print(BatteryStatsHelper.makemAh(power));
1851 }
1852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001854 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1855 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1857 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1858 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859
1860 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1861 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1862 final long totalRealtime = computeRealtime(rawRealtime, which);
1863 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001864 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1865 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1866 which);
1867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001869
1870 SparseArray<? extends Uid> uidStats = getUidStats();
1871 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001873 sb.setLength(0);
1874 sb.append(prefix);
1875 sb.append(" Time on battery: ");
1876 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1877 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1878 sb.append(") realtime, ");
1879 formatTimeMs(sb, whichBatteryUptime / 1000);
1880 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1881 sb.append(") uptime");
1882 pw.println(sb.toString());
1883 sb.setLength(0);
1884 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001885 sb.append(" Time on battery screen off: ");
1886 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
1887 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
1888 sb.append(") realtime, ");
1889 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
1890 sb.append("(");
1891 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
1892 sb.append(") uptime");
1893 pw.println(sb.toString());
1894 sb.setLength(0);
1895 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001896 sb.append(" Total run time: ");
1897 formatTimeMs(sb, totalRealtime / 1000);
1898 sb.append("realtime, ");
1899 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001900 sb.append("uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001901 pw.println(sb.toString());
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001902 pw.print(" Start clock time: ");
1903 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1904
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001905 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1906 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1907 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1908 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1909 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001910 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001911 sb.append(prefix);
1912 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1913 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001914 sb.append(") "); sb.append(getScreenOnCount(which));
1915 sb.append("x, Input events: "); sb.append(getInputEventCount(which));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001916 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001917 if (phoneOnTime != 0) {
1918 sb.setLength(0);
1919 sb.append(prefix);
1920 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1921 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1922 sb.append(") "); sb.append(getPhoneOnCount(which));
1923 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001924 sb.setLength(0);
1925 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001926 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07001927 boolean didOne = false;
1928 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001929 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001930 if (time == 0) {
1931 continue;
1932 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001933 sb.append("\n ");
1934 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001935 didOne = true;
1936 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1937 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001938 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001939 sb.append("(");
1940 sb.append(formatRatioLocked(time, screenOnTime));
1941 sb.append(")");
1942 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001943 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07001944 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001945
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001946 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001947 long fullWakeLockTimeTotalMicros = 0;
1948 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001949
Dianne Hackborn81038902012-11-26 17:04:09 -08001950 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1951
Evan Millar22ac0432009-03-31 11:33:18 -07001952 for (int iu = 0; iu < NU; iu++) {
1953 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001954
Evan Millar22ac0432009-03-31 11:33:18 -07001955 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1956 if (wakelocks.size() > 0) {
1957 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1958 : wakelocks.entrySet()) {
1959 Uid.Wakelock wl = ent.getValue();
1960
1961 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1962 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001963 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001964 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001965 }
1966
1967 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1968 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001969 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001970 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001971 if (totalTimeMicros > 0) {
1972 if (reqUid < 0) {
1973 // Only show the ordered list of all wake
1974 // locks if the caller is not asking for data
1975 // about a specific uid.
1976 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1977 partialWakeTimer, totalTimeMicros));
1978 }
1979 partialWakeLockTimeTotalMicros += totalTimeMicros;
1980 }
Evan Millar22ac0432009-03-31 11:33:18 -07001981 }
1982 }
1983 }
1984 }
1985
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001986 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1987 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1988 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1989 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1990 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1991 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1992 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1993 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1994
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001995 if (fullWakeLockTimeTotalMicros != 0) {
1996 sb.setLength(0);
1997 sb.append(prefix);
1998 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
1999 (fullWakeLockTimeTotalMicros + 500) / 1000);
2000 pw.println(sb.toString());
2001 }
2002
2003 if (partialWakeLockTimeTotalMicros != 0) {
2004 sb.setLength(0);
2005 sb.append(prefix);
2006 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2007 (partialWakeLockTimeTotalMicros + 500) / 1000);
2008 pw.println(sb.toString());
2009 }
2010
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002011 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002012 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2013 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2014 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2015 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002016 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002017 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002018 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002019 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002020 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002021 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002022 if (time == 0) {
2023 continue;
2024 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002025 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002026 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002027 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002028 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002029 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002030 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002031 sb.append("(");
2032 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002033 sb.append(") ");
2034 sb.append(getPhoneSignalStrengthCount(i, which));
2035 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002036 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002037 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002038 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002039
2040 sb.setLength(0);
2041 sb.append(prefix);
2042 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002043 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002044 pw.println(sb.toString());
2045
Dianne Hackborn627bba72009-03-24 22:32:56 -07002046 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002047 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002048 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002049 didOne = false;
2050 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002051 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002052 if (time == 0) {
2053 continue;
2054 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002055 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002056 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002057 didOne = true;
2058 sb.append(DATA_CONNECTION_NAMES[i]);
2059 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002060 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002061 sb.append("(");
2062 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002063 sb.append(") ");
2064 sb.append(getPhoneDataConnectionCount(i, which));
2065 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002066 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002067 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002068 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002069
2070 sb.setLength(0);
2071 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002072 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002073 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002074 formatTimeMs(sb, mobileActiveTime / 1000);
2075 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2076 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2077 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002078 pw.println(sb.toString());
2079
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002080 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2081 if (mobileActiveUnknownTime != 0) {
2082 sb.setLength(0);
2083 sb.append(prefix);
2084 sb.append(" Mobile radio active unknown time: ");
2085 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2086 sb.append("(");
2087 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2088 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2089 sb.append("x");
2090 pw.println(sb.toString());
2091 }
2092
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002093 pw.print(prefix);
2094 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2095 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2096 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2097 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002098 sb.setLength(0);
2099 sb.append(prefix);
2100 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2101 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2102 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2103 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002104 sb.append(")");
2105 pw.println(sb.toString());
2106
2107 sb.setLength(0);
2108 sb.append(prefix);
2109 sb.append(" Wifi states:");
2110 didOne = false;
2111 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002112 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002113 if (time == 0) {
2114 continue;
2115 }
2116 sb.append("\n ");
2117 didOne = true;
2118 sb.append(WIFI_STATE_NAMES[i]);
2119 sb.append(" ");
2120 formatTimeMs(sb, time/1000);
2121 sb.append("(");
2122 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2123 sb.append(") ");
2124 sb.append(getPhoneDataConnectionCount(i, which));
2125 sb.append("x");
2126 }
2127 if (!didOne) sb.append(" (no activity)");
2128 pw.println(sb.toString());
2129
2130 sb.setLength(0);
2131 sb.append(prefix);
2132 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002133 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2134 sb.append(")");
2135 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002136
2137 sb.setLength(0);
2138 sb.append(prefix);
2139 sb.append(" Bluetooth states:");
2140 didOne = false;
2141 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002142 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002143 if (time == 0) {
2144 continue;
2145 }
2146 sb.append("\n ");
2147 didOne = true;
2148 sb.append(BLUETOOTH_STATE_NAMES[i]);
2149 sb.append(" ");
2150 formatTimeMs(sb, time/1000);
2151 sb.append("(");
2152 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2153 sb.append(") ");
2154 sb.append(getPhoneDataConnectionCount(i, which));
2155 sb.append("x");
2156 }
2157 if (!didOne) sb.append(" (no activity)");
2158 pw.println(sb.toString());
2159
2160 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002161
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002162 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002163 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002164 pw.print(prefix); pw.println(" Device is currently unplugged");
2165 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2166 pw.println(getDischargeStartLevel());
2167 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2168 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002169 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002170 pw.print(prefix); pw.println(" Device is currently plugged into power");
2171 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2172 pw.println(getDischargeStartLevel());
2173 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2174 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002175 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002176 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2177 pw.println(getDischargeAmountScreenOn());
2178 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2179 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002180 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002181 } else {
2182 pw.print(prefix); pw.println(" Device battery use since last full charge");
2183 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2184 pw.println(getLowDischargeAmountSinceCharge());
2185 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2186 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002187 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2188 pw.println(getDischargeAmountScreenOnSinceCharge());
2189 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2190 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002191 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002192 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002193
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002194 BatteryStatsHelper helper = new BatteryStatsHelper(context);
2195 helper.create(this);
2196 helper.refreshStats(which, UserHandle.USER_ALL);
2197 List<BatterySipper> sippers = helper.getUsageList();
2198 if (sippers != null && sippers.size() > 0) {
2199 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2200 pw.print(prefix); pw.print(" Capacity: ");
2201 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002202 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002203 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2204 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2205 pw.println();
2206 for (int i=0; i<sippers.size(); i++) {
2207 BatterySipper bs = sippers.get(i);
2208 switch (bs.drainType) {
2209 case IDLE:
2210 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2211 pw.println();
2212 break;
2213 case CELL:
2214 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2215 pw.println();
2216 break;
2217 case PHONE:
2218 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2219 pw.println();
2220 break;
2221 case WIFI:
2222 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2223 pw.println();
2224 break;
2225 case BLUETOOTH:
2226 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2227 pw.println();
2228 break;
2229 case SCREEN:
2230 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2231 pw.println();
2232 break;
2233 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002234 pw.print(prefix); pw.print(" Uid ");
2235 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002236 pw.print(": "); printmAh(pw, bs.value); pw.println();
2237 break;
2238 case USER:
2239 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2240 pw.print(": "); printmAh(pw, bs.value); pw.println();
2241 break;
2242 case UNACCOUNTED:
2243 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2244 pw.println();
2245 break;
2246 case OVERCOUNTED:
2247 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2248 pw.println();
2249 break;
2250 }
2251 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002252 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002253 }
2254
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002255 sippers = helper.getMobilemsppList();
2256 if (sippers != null && sippers.size() > 0) {
2257 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002258 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002259 for (int i=0; i<sippers.size(); i++) {
2260 BatterySipper bs = sippers.get(i);
2261 sb.setLength(0);
2262 sb.append(prefix); sb.append(" Uid ");
2263 UserHandle.formatUid(sb, bs.uidObj.getUid());
2264 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2265 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2266 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002267 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002268 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002269 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002270 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002271 sb.setLength(0);
2272 sb.append(prefix);
2273 sb.append(" TOTAL TIME: ");
2274 formatTimeMs(sb, totalTime);
2275 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2276 sb.append(")");
2277 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002278 pw.println();
2279 }
2280
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002281 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2282 @Override
2283 public int compare(TimerEntry lhs, TimerEntry rhs) {
2284 long lhsTime = lhs.mTime;
2285 long rhsTime = rhs.mTime;
2286 if (lhsTime < rhsTime) {
2287 return 1;
2288 }
2289 if (lhsTime > rhsTime) {
2290 return -1;
2291 }
2292 return 0;
2293 }
2294 };
2295
2296 if (reqUid < 0) {
2297 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2298 if (kernelWakelocks.size() > 0) {
2299 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2300 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2301 BatteryStats.Timer timer = ent.getValue();
2302 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2303 if (totalTimeMillis > 0) {
2304 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2305 }
2306 }
2307 if (ktimers.size() > 0) {
2308 Collections.sort(ktimers, timerComparator);
2309 pw.print(prefix); pw.println(" All kernel wake locks:");
2310 for (int i=0; i<ktimers.size(); i++) {
2311 TimerEntry timer = ktimers.get(i);
2312 String linePrefix = ": ";
2313 sb.setLength(0);
2314 sb.append(prefix);
2315 sb.append(" Kernel Wake lock ");
2316 sb.append(timer.mName);
2317 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2318 which, linePrefix);
2319 if (!linePrefix.equals(": ")) {
2320 sb.append(" realtime");
2321 // Only print out wake locks that were held
2322 pw.println(sb.toString());
2323 }
2324 }
2325 pw.println();
2326 }
2327 }
2328 }
2329
Dianne Hackborn81038902012-11-26 17:04:09 -08002330 if (timers.size() > 0) {
2331 Collections.sort(timers, timerComparator);
2332 pw.print(prefix); pw.println(" All partial wake locks:");
2333 for (int i=0; i<timers.size(); i++) {
2334 TimerEntry timer = timers.get(i);
2335 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002336 sb.append(" Wake lock ");
2337 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08002338 sb.append(" ");
2339 sb.append(timer.mName);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002340 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
Dianne Hackborn81038902012-11-26 17:04:09 -08002341 sb.append(" realtime");
2342 pw.println(sb.toString());
2343 }
2344 timers.clear();
2345 pw.println();
2346 }
Evan Millar22ac0432009-03-31 11:33:18 -07002347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 for (int iu=0; iu<NU; iu++) {
2349 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002350 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002351 continue;
2352 }
2353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002355
2356 pw.print(prefix);
2357 pw.print(" ");
2358 UserHandle.formatUid(pw, uid);
2359 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002361
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002362 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2363 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2364 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2365 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2366 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2367 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002368 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2369 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002370 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2371 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002372 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2373 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2374 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002375
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002376 if (mobileRxBytes > 0 || mobileTxBytes > 0
2377 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002378 pw.print(prefix); pw.print(" Mobile network: ");
2379 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002380 pw.print(formatBytesLocked(mobileTxBytes));
2381 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2382 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002384 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2385 sb.setLength(0);
2386 sb.append(prefix); sb.append(" Mobile radio active: ");
2387 formatTimeMs(sb, uidMobileActiveTime / 1000);
2388 sb.append("(");
2389 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2390 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2391 long packets = mobileRxPackets + mobileTxPackets;
2392 if (packets == 0) {
2393 packets = 1;
2394 }
2395 sb.append(" @ ");
2396 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2397 sb.append(" mspp");
2398 pw.println(sb.toString());
2399 }
2400
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002401 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002402 pw.print(prefix); pw.print(" Wi-Fi network: ");
2403 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002404 pw.print(formatBytesLocked(wifiTxBytes));
2405 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2406 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002407 }
2408
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002409 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2410 || uidWifiRunningTime != 0) {
2411 sb.setLength(0);
2412 sb.append(prefix); sb.append(" Wifi Running: ");
2413 formatTimeMs(sb, uidWifiRunningTime / 1000);
2414 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2415 whichBatteryRealtime)); sb.append(")\n");
2416 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2417 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2418 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2419 whichBatteryRealtime)); sb.append(")\n");
2420 sb.append(prefix); sb.append(" Wifi Scan: ");
2421 formatTimeMs(sb, wifiScanTime / 1000);
2422 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2423 whichBatteryRealtime)); sb.append(")");
2424 pw.println(sb.toString());
2425 }
2426
Dianne Hackborn617f8772009-03-31 15:04:46 -07002427 if (u.hasUserActivity()) {
2428 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002429 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002430 int val = u.getUserActivityCount(i, which);
2431 if (val != 0) {
2432 if (!hasData) {
2433 sb.setLength(0);
2434 sb.append(" User activity: ");
2435 hasData = true;
2436 } else {
2437 sb.append(", ");
2438 }
2439 sb.append(val);
2440 sb.append(" ");
2441 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2442 }
2443 }
2444 if (hasData) {
2445 pw.println(sb.toString());
2446 }
2447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448
2449 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2450 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002451 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2452 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2454 : wakelocks.entrySet()) {
2455 Uid.Wakelock wl = ent.getValue();
2456 String linePrefix = ": ";
2457 sb.setLength(0);
2458 sb.append(prefix);
2459 sb.append(" Wake lock ");
2460 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002461 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002463 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002465 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 "window", which, linePrefix);
2467 if (!linePrefix.equals(": ")) {
2468 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002469 // Only print out wake locks that were held
2470 pw.println(sb.toString());
2471 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002472 count++;
2473 }
2474 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002475 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002476 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002477 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002478 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002479 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002480 }
2481 if (count > 1) {
2482 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2483 sb.setLength(0);
2484 sb.append(prefix);
2485 sb.append(" TOTAL wake: ");
2486 boolean needComma = false;
2487 if (totalFull != 0) {
2488 needComma = true;
2489 formatTimeMs(sb, totalFull);
2490 sb.append("full");
2491 }
2492 if (totalPartial != 0) {
2493 if (needComma) {
2494 sb.append(", ");
2495 }
2496 needComma = true;
2497 formatTimeMs(sb, totalPartial);
2498 sb.append("partial");
2499 }
2500 if (totalWindow != 0) {
2501 if (needComma) {
2502 sb.append(", ");
2503 }
2504 needComma = true;
2505 formatTimeMs(sb, totalWindow);
2506 sb.append("window");
2507 }
2508 sb.append(" realtime");
2509 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 }
2512 }
2513
2514 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2515 if (sensors.size() > 0) {
2516 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2517 : sensors.entrySet()) {
2518 Uid.Sensor se = ent.getValue();
2519 int sensorNumber = ent.getKey();
2520 sb.setLength(0);
2521 sb.append(prefix);
2522 sb.append(" Sensor ");
2523 int handle = se.getHandle();
2524 if (handle == Uid.Sensor.GPS) {
2525 sb.append("GPS");
2526 } else {
2527 sb.append(handle);
2528 }
2529 sb.append(": ");
2530
2531 Timer timer = se.getSensorTime();
2532 if (timer != null) {
2533 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002534 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002535 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002536 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 //timer.logState();
2538 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002539 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 sb.append("realtime (");
2541 sb.append(count);
2542 sb.append(" times)");
2543 } else {
2544 sb.append("(not used)");
2545 }
2546 } else {
2547 sb.append("(not used)");
2548 }
2549
2550 pw.println(sb.toString());
2551 uidActivity = true;
2552 }
2553 }
2554
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002555 Timer vibTimer = u.getVibratorOnTimer();
2556 if (vibTimer != null) {
2557 // Convert from microseconds to milliseconds with rounding
2558 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002559 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002560 int count = vibTimer.getCountLocked(which);
2561 //timer.logState();
2562 if (totalTime != 0) {
2563 sb.setLength(0);
2564 sb.append(prefix);
2565 sb.append(" Vibrator: ");
2566 formatTimeMs(sb, totalTime);
2567 sb.append("realtime (");
2568 sb.append(count);
2569 sb.append(" times)");
2570 pw.println(sb.toString());
2571 uidActivity = true;
2572 }
2573 }
2574
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002575 Timer fgTimer = u.getForegroundActivityTimer();
2576 if (fgTimer != null) {
2577 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002578 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002579 int count = fgTimer.getCountLocked(which);
2580 if (totalTime != 0) {
2581 sb.setLength(0);
2582 sb.append(prefix);
2583 sb.append(" Foreground activities: ");
2584 formatTimeMs(sb, totalTime);
2585 sb.append("realtime (");
2586 sb.append(count);
2587 sb.append(" times)");
2588 pw.println(sb.toString());
2589 uidActivity = true;
2590 }
2591 }
2592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2594 if (processStats.size() > 0) {
2595 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2596 : processStats.entrySet()) {
2597 Uid.Proc ps = ent.getValue();
2598 long userTime;
2599 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002600 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002602 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603
2604 userTime = ps.getUserTime(which);
2605 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002606 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002608 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002609 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002611 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002612 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002613 sb.setLength(0);
2614 sb.append(prefix); sb.append(" Proc ");
2615 sb.append(ent.getKey()); sb.append(":\n");
2616 sb.append(prefix); sb.append(" CPU: ");
2617 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002618 formatTime(sb, systemTime); sb.append("krn ; ");
2619 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002620 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002621 sb.append("\n"); sb.append(prefix); sb.append(" ");
2622 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002623 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002624 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002625 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002626 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002627 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002628 pw.print(prefix); pw.print(" * Killed for ");
2629 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2630 pw.print("wake lock");
2631 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2632 pw.print("cpu");
2633 } else {
2634 pw.print("unknown");
2635 }
2636 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002637 TimeUtils.formatDuration(ew.usedTime, pw);
2638 pw.print(" over ");
2639 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002640 if (ew.overTime != 0) {
2641 pw.print(" (");
2642 pw.print((ew.usedTime*100)/ew.overTime);
2643 pw.println("%)");
2644 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002645 }
2646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 uidActivity = true;
2648 }
2649 }
2650 }
2651
2652 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2653 if (packageStats.size() > 0) {
2654 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2655 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002656 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 boolean apkActivity = false;
2658 Uid.Pkg ps = ent.getValue();
2659 int wakeups = ps.getWakeups(which);
2660 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002661 pw.print(prefix); pw.print(" ");
2662 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 apkActivity = true;
2664 }
2665 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2666 if (serviceStats.size() > 0) {
2667 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2668 : serviceStats.entrySet()) {
2669 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2670 long startTime = ss.getStartTime(batteryUptime, which);
2671 int starts = ss.getStarts(which);
2672 int launches = ss.getLaunches(which);
2673 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002674 sb.setLength(0);
2675 sb.append(prefix); sb.append(" Service ");
2676 sb.append(sent.getKey()); sb.append(":\n");
2677 sb.append(prefix); sb.append(" Created for: ");
2678 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002679 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002680 sb.append(prefix); sb.append(" Starts: ");
2681 sb.append(starts);
2682 sb.append(", launches: "); sb.append(launches);
2683 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 apkActivity = true;
2685 }
2686 }
2687 }
2688 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002689 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691 uidActivity = true;
2692 }
2693 }
2694 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002695 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 }
2697 }
2698 }
2699
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002700 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002701 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002702 int diff = oldval ^ newval;
2703 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002704 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002705 for (int i=0; i<descriptions.length; i++) {
2706 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002707 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002708 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002709 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002710 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002711 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002712 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2713 didWake = true;
2714 pw.print("=");
2715 if (longNames) {
2716 UserHandle.formatUid(pw, wakelockTag.uid);
2717 pw.print(":\"");
2718 pw.print(wakelockTag.string);
2719 pw.print("\"");
2720 } else {
2721 pw.print(wakelockTag.poolIdx);
2722 }
2723 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002724 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002725 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002726 pw.print("=");
2727 int val = (newval&bd.mask)>>bd.shift;
2728 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002729 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002730 } else {
2731 pw.print(val);
2732 }
2733 }
2734 }
2735 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002736 if (!didWake && wakelockTag != null) {
2737 pw.print(longNames ? "wake_lock=" : "w=");
2738 if (longNames) {
2739 UserHandle.formatUid(pw, wakelockTag.uid);
2740 pw.print(":\"");
2741 pw.print(wakelockTag.string);
2742 pw.print("\"");
2743 } else {
2744 pw.print(wakelockTag.poolIdx);
2745 }
2746 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002747 }
2748
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002749 public void prepareForDumpLocked() {
2750 }
2751
2752 public static class HistoryPrinter {
2753 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002754 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002755 int oldStatus = -1;
2756 int oldHealth = -1;
2757 int oldPlug = -1;
2758 int oldTemp = -1;
2759 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002760 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002761
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002762 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2763 if (!checkin) {
2764 pw.print(" ");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002765 if (now >= 0) {
2766 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2767 } else {
2768 TimeUtils.formatDuration(rec.time, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2769 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002770 pw.print(" (");
2771 pw.print(rec.numReadInts);
2772 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002773 } else {
2774 if (lastTime < 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002775 if (now >= 0) {
2776 pw.print("@");
2777 pw.print(rec.time-now);
2778 } else {
2779 pw.print(rec.time);
2780 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002781 } else {
2782 pw.print(rec.time-lastTime);
2783 }
2784 lastTime = rec.time;
2785 }
2786 if (rec.cmd == HistoryItem.CMD_START) {
2787 if (checkin) {
2788 pw.print(":");
2789 }
2790 pw.println("START");
2791 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2792 if (checkin) {
2793 pw.print(":");
2794 }
2795 pw.println("*OVERFLOW*");
2796 } else {
2797 if (!checkin) {
2798 if (rec.batteryLevel < 10) pw.print("00");
2799 else if (rec.batteryLevel < 100) pw.print("0");
2800 pw.print(rec.batteryLevel);
2801 pw.print(" ");
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002802 if (rec.states < 0) ;
2803 else if (rec.states < 0x10) pw.print("0000000");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002804 else if (rec.states < 0x100) pw.print("000000");
2805 else if (rec.states < 0x1000) pw.print("00000");
2806 else if (rec.states < 0x10000) pw.print("0000");
2807 else if (rec.states < 0x100000) pw.print("000");
2808 else if (rec.states < 0x1000000) pw.print("00");
2809 else if (rec.states < 0x10000000) pw.print("0");
2810 pw.print(Integer.toHexString(rec.states));
2811 } else {
2812 if (oldLevel != rec.batteryLevel) {
2813 oldLevel = rec.batteryLevel;
2814 pw.print(",Bl="); pw.print(rec.batteryLevel);
2815 }
2816 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002817 if (oldStatus != rec.batteryStatus) {
2818 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002819 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002820 switch (oldStatus) {
2821 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002822 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002823 break;
2824 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002825 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002826 break;
2827 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002828 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002829 break;
2830 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002831 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002832 break;
2833 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002834 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002835 break;
2836 default:
2837 pw.print(oldStatus);
2838 break;
2839 }
2840 }
2841 if (oldHealth != rec.batteryHealth) {
2842 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002843 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002844 switch (oldHealth) {
2845 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002846 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002847 break;
2848 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002849 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002850 break;
2851 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002852 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002853 break;
2854 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002855 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002856 break;
2857 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002858 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002859 break;
2860 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002861 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002862 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002863 case BatteryManager.BATTERY_HEALTH_COLD:
2864 pw.print(checkin ? "c" : "cold");
2865 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002866 default:
2867 pw.print(oldHealth);
2868 break;
2869 }
2870 }
2871 if (oldPlug != rec.batteryPlugType) {
2872 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002873 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002874 switch (oldPlug) {
2875 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002876 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002877 break;
2878 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002879 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002880 break;
2881 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002882 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002883 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002884 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002885 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002886 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002887 default:
2888 pw.print(oldPlug);
2889 break;
2890 }
2891 }
2892 if (oldTemp != rec.batteryTemperature) {
2893 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002894 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002895 pw.print(oldTemp);
2896 }
2897 if (oldVolt != rec.batteryVoltage) {
2898 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002899 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002900 pw.print(oldVolt);
2901 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002902 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002903 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002904 if (rec.wakeReasonTag != null) {
2905 if (checkin) {
2906 pw.print(",Wr=");
2907 pw.print(rec.wakeReasonTag.poolIdx);
2908 } else {
2909 pw.print(" wake_reason=");
2910 pw.print(rec.wakeReasonTag.uid);
2911 pw.print(":\"");
2912 pw.print(rec.wakeReasonTag.string);
2913 pw.print("\"");
2914 }
2915 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002916 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002917 pw.print(checkin ? "," : " ");
2918 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
2919 pw.print("+");
2920 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
2921 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002922 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002923 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
2924 : HISTORY_EVENT_NAMES;
2925 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
2926 | HistoryItem.EVENT_FLAG_FINISH);
2927 if (idx >= 0 && idx < eventNames.length) {
2928 pw.print(eventNames[idx]);
2929 } else {
2930 pw.print(checkin ? "Ev" : "event");
2931 pw.print(idx);
2932 }
2933 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002934 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002935 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002936 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002937 UserHandle.formatUid(pw, rec.eventTag.uid);
2938 pw.print(":\"");
2939 pw.print(rec.eventTag.string);
2940 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002941 }
2942 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002943 pw.println();
2944 }
2945 oldState = rec.states;
2946 }
2947 }
2948
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002949 private void printSizeValue(PrintWriter pw, long size) {
2950 float result = size;
2951 String suffix = "";
2952 if (result >= 10*1024) {
2953 suffix = "KB";
2954 result = result / 1024;
2955 }
2956 if (result >= 10*1024) {
2957 suffix = "MB";
2958 result = result / 1024;
2959 }
2960 if (result >= 10*1024) {
2961 suffix = "GB";
2962 result = result / 1024;
2963 }
2964 if (result >= 10*1024) {
2965 suffix = "TB";
2966 result = result / 1024;
2967 }
2968 if (result >= 10*1024) {
2969 suffix = "PB";
2970 result = result / 1024;
2971 }
2972 pw.print((int)result);
2973 pw.print(suffix);
2974 }
2975
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002976 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
2977 public static final int DUMP_CHARGED_ONLY = 1<<1;
2978 public static final int DUMP_HISTORY_ONLY = 1<<2;
2979 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
2980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 /**
2982 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2983 *
2984 * @param pw a Printer to receive the dump output.
2985 */
2986 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002987 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002988 prepareForDumpLocked();
2989
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002990 final boolean filtering =
2991 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002992
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002993 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
2994 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2995
2996 final HistoryItem rec = new HistoryItem();
2997 final long historyTotalSize = getHistoryTotalSize();
2998 final long historyUsedSize = getHistoryUsedSize();
2999 if (startIteratingHistoryLocked()) {
3000 try {
3001 pw.print("Battery History (");
3002 pw.print((100*historyUsedSize)/historyTotalSize);
3003 pw.print("% used, ");
3004 printSizeValue(pw, historyUsedSize);
3005 pw.print(" used of ");
3006 printSizeValue(pw, historyTotalSize);
3007 pw.print(", ");
3008 pw.print(getHistoryStringPoolSize());
3009 pw.print(" strings using ");
3010 printSizeValue(pw, getHistoryStringPoolBytes());
3011 pw.println("):");
3012 HistoryPrinter hprinter = new HistoryPrinter();
3013 long lastTime = -1;
3014 while (getNextHistoryLocked(rec)) {
3015 lastTime = rec.time;
3016 if (rec.time >= histStart) {
3017 hprinter.printNextItem(pw, rec, histStart >= 0 ? -1 : now, false);
3018 }
3019 }
3020 if (histStart >= 0) {
3021 pw.print(" NEXT: "); pw.println(lastTime+1);
3022 }
3023 pw.println();
3024 } finally {
3025 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003026 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003027 }
3028
3029 if (startIteratingOldHistoryLocked()) {
3030 try {
3031 pw.println("Old battery History:");
3032 HistoryPrinter hprinter = new HistoryPrinter();
3033 while (getNextOldHistoryLocked(rec)) {
3034 hprinter.printNextItem(pw, rec, now, false);
3035 }
3036 pw.println();
3037 } finally {
3038 finishIteratingOldHistoryLocked();
3039 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003040 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003041 }
3042
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003043 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003044 return;
3045 }
3046
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003047 if (!filtering) {
3048 SparseArray<? extends Uid> uidStats = getUidStats();
3049 final int NU = uidStats.size();
3050 boolean didPid = false;
3051 long nowRealtime = SystemClock.elapsedRealtime();
3052 for (int i=0; i<NU; i++) {
3053 Uid uid = uidStats.valueAt(i);
3054 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3055 if (pids != null) {
3056 for (int j=0; j<pids.size(); j++) {
3057 Uid.Pid pid = pids.valueAt(j);
3058 if (!didPid) {
3059 pw.println("Per-PID Stats:");
3060 didPid = true;
3061 }
3062 long time = pid.mWakeSum + (pid.mWakeStart != 0
3063 ? (nowRealtime - pid.mWakeStart) : 0);
3064 pw.print(" PID "); pw.print(pids.keyAt(j));
3065 pw.print(" wake time: ");
3066 TimeUtils.formatDuration(time, pw);
3067 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003068 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003069 }
3070 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003071 if (didPid) {
3072 pw.println("");
3073 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003074 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003075
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003076 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003077 pw.println("Statistics since last charge:");
3078 pw.println(" System starts: " + getStartCount()
3079 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003080 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003081 pw.println("");
3082 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003083 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3084 pw.println("Statistics since last unplugged:");
3085 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003090 public void dumpCheckinLocked(Context context, PrintWriter pw,
3091 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003092 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003094 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3095
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003096 final boolean filtering =
3097 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3098
3099 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003100 final HistoryItem rec = new HistoryItem();
3101 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003102 try {
3103 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3104 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3105 pw.print(HISTORY_STRING_POOL); pw.print(',');
3106 pw.print(i);
3107 pw.print(',');
3108 pw.print(getHistoryTagPoolString(i));
3109 pw.print(',');
3110 pw.print(getHistoryTagPoolUid(i));
3111 pw.println();
3112 }
3113 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003114 long lastTime = -1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003115 while (getNextHistoryLocked(rec)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003116 lastTime = rec.time;
3117 if (rec.time >= histStart) {
3118 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3119 pw.print(HISTORY_DATA); pw.print(',');
3120 hprinter.printNextItem(pw, rec, histStart >= 0 ? -1 : now, true);
3121 }
3122 }
3123 if (histStart >= 0) {
3124 pw.print("NEXT: "); pw.println(lastTime+1);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003125 }
3126 } finally {
3127 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003128 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003129 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003130 }
3131
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003132 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003133 return;
3134 }
3135
Dianne Hackborne4a59512010-12-07 11:08:07 -08003136 if (apps != null) {
3137 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3138 for (int i=0; i<apps.size(); i++) {
3139 ApplicationInfo ai = apps.get(i);
3140 ArrayList<String> pkgs = uids.get(ai.uid);
3141 if (pkgs == null) {
3142 pkgs = new ArrayList<String>();
3143 uids.put(ai.uid, pkgs);
3144 }
3145 pkgs.add(ai.packageName);
3146 }
3147 SparseArray<? extends Uid> uidStats = getUidStats();
3148 final int NU = uidStats.size();
3149 String[] lineArgs = new String[2];
3150 for (int i=0; i<NU; i++) {
3151 int uid = uidStats.keyAt(i);
3152 ArrayList<String> pkgs = uids.get(uid);
3153 if (pkgs != null) {
3154 for (int j=0; j<pkgs.size(); j++) {
3155 lineArgs[0] = Integer.toString(uid);
3156 lineArgs[1] = pkgs.get(j);
3157 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3158 (Object[])lineArgs);
3159 }
3160 }
3161 }
3162 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003163 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003164 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003165 }
3166 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003167 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
3169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170}