blob: bfce0c1c6f5b9f8a9a263a5e6b8df42e91b08e8c [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 *
220 * @param batteryRealtime system realtime on battery in microseconds
221 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
222 * @return a time in microseconds
223 */
Evan Millarc64edde2009-04-18 12:26:32 -0700224 public abstract long getTotalTimeLocked(long batteryRealtime, 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 Hackborn58e0eef2010-09-16 01:22:10 -0700298 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700299 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700300 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalta029ea12013-09-25 16:38:12 -0700301 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700302 public abstract long getWifiMulticastTime(long batteryRealtime,
303 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700304 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
305 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700306 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800307 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Robert Greenwalta029ea12013-09-25 16:38:12 -0700309 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
310
Dianne Hackborn617f8772009-03-31 15:04:46 -0700311 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700312 * Note that these must match the constants in android.os.PowerManager.
313 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
314 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700315 */
316 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700317 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700318 };
319
Jeff Browndf693de2012-07-27 12:03:38 -0700320 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700321
Dianne Hackborn617f8772009-03-31 15:04:46 -0700322 public abstract void noteUserActivityLocked(int type);
323 public abstract boolean hasUserActivity();
324 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700325
326 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800327 public abstract long getNetworkActivityBytes(int type, int which);
328 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800329 public abstract long getMobileRadioActiveTime(int which);
330 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800333 /*
334 * FIXME: it's not correct to use this magic value because it
335 * could clash with a sensor handle (which are defined by
336 * the sensor HAL, and therefore out of our control
337 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 // Magic sensor number for the GPS.
339 public static final int GPS = -10000;
340
341 public abstract int getHandle();
342
343 public abstract Timer getSensorTime();
344 }
345
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700346 public class Pid {
347 public long mWakeSum;
348 public long mWakeStart;
349 }
350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 /**
352 * The statistics associated with a particular process.
353 */
354 public static abstract class Proc {
355
Dianne Hackborn287952c2010-09-22 22:34:31 -0700356 public static class ExcessivePower {
357 public static final int TYPE_WAKE = 1;
358 public static final int TYPE_CPU = 2;
359
360 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700361 public long overTime;
362 public long usedTime;
363 }
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800366 * Returns true if this process is still active in the battery stats.
367 */
368 public abstract boolean isActive();
369
370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * Returns the total time (in 1/100 sec) spent executing in user code.
372 *
373 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
374 */
375 public abstract long getUserTime(int which);
376
377 /**
378 * Returns the total time (in 1/100 sec) spent executing in system code.
379 *
380 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
381 */
382 public abstract long getSystemTime(int which);
383
384 /**
385 * Returns the number of times the process has been started.
386 *
387 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
388 */
389 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700390
391 /**
392 * Returns the cpu time spent in microseconds while the process was in the foreground.
393 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
394 * @return foreground cpu time in microseconds
395 */
396 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700397
398 /**
399 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
400 * @param speedStep the index of the CPU speed. This is not the actual speed of the
401 * CPU.
402 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
403 * @see BatteryStats#getCpuSpeedSteps()
404 */
405 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700406
Dianne Hackborn287952c2010-09-22 22:34:31 -0700407 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700408
Dianne Hackborn287952c2010-09-22 22:34:31 -0700409 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
412 /**
413 * The statistics associated with a particular package.
414 */
415 public static abstract class Pkg {
416
417 /**
418 * Returns the number of times this package has done something that could wake up the
419 * device from sleep.
420 *
421 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
422 */
423 public abstract int getWakeups(int which);
424
425 /**
426 * Returns a mapping containing service statistics.
427 */
428 public abstract Map<String, ? extends Serv> getServiceStats();
429
430 /**
431 * The statistics associated with a particular service.
432 */
433 public abstract class Serv {
434
435 /**
436 * Returns the amount of time spent started.
437 *
438 * @param batteryUptime elapsed uptime on battery in microseconds.
439 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
440 * @return
441 */
442 public abstract long getStartTime(long batteryUptime, int which);
443
444 /**
445 * Returns the total number of times startService() has been called.
446 *
447 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
448 */
449 public abstract int getStarts(int which);
450
451 /**
452 * Returns the total number times the service has been launched.
453 *
454 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
455 */
456 public abstract int getLaunches(int which);
457 }
458 }
459 }
460
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800461 public final static class HistoryTag {
462 public String string;
463 public int uid;
464
465 public int poolIdx;
466
467 public void setTo(HistoryTag o) {
468 string = o.string;
469 uid = o.uid;
470 poolIdx = o.poolIdx;
471 }
472
473 public void setTo(String _string, int _uid) {
474 string = _string;
475 uid = _uid;
476 poolIdx = -1;
477 }
478
479 public void writeToParcel(Parcel dest, int flags) {
480 dest.writeString(string);
481 dest.writeInt(uid);
482 }
483
484 public void readFromParcel(Parcel src) {
485 string = src.readString();
486 uid = src.readInt();
487 poolIdx = -1;
488 }
489
490 @Override
491 public boolean equals(Object o) {
492 if (this == o) return true;
493 if (o == null || getClass() != o.getClass()) return false;
494
495 HistoryTag that = (HistoryTag) o;
496
497 if (uid != that.uid) return false;
498 if (!string.equals(that.string)) return false;
499
500 return true;
501 }
502
503 @Override
504 public int hashCode() {
505 int result = string.hashCode();
506 result = 31 * result + uid;
507 return result;
508 }
509 }
510
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700511 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700512 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700513
514 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800515
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800516 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800517 public static final byte CMD_NULL = -1;
518 public static final byte CMD_START = 4;
519 public static final byte CMD_OVERFLOW = 5;
520
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700521 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700522
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800523 /**
524 * Return whether the command code is a delta data update.
525 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800526 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800527 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800528 }
529
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700530 public byte batteryLevel;
531 public byte batteryStatus;
532 public byte batteryHealth;
533 public byte batteryPlugType;
534
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900535 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700536 public char batteryVoltage;
537
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700538 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700539 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800540 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700541 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800542 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
543 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700544 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800545 public static final int STATE_PHONE_STATE_SHIFT = 6;
546 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700547 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800548 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800549 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800550
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700551 // These states always appear directly in the first int token
552 // of a delta change; they should be ones that change relatively
553 // frequently.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800554 public static final int STATE_WAKE_LOCK_FLAG = 1<<31;
555 public static final int STATE_SENSOR_ON_FLAG = 1<<30;
556 public static final int STATE_GPS_ON_FLAG = 1<<29;
557 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800558 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800559 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800560 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800561 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700562 // These are on the lower bits used for the command; if they change
563 // we need to write another int of data.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800564 public static final int STATE_PHONE_SCANNING_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700565 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
566 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
567 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
568 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
569 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
570 public static final int STATE_WIFI_ON_FLAG = 1<<17;
571 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700572
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700573 public static final int MOST_INTERESTING_STATES =
574 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
575 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
576
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700577 public int states;
578
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800579 // The wake lock that was acquired at this point.
580 public HistoryTag wakelockTag;
581
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800582 public static final int EVENT_FLAG_START = 0x8000;
583 public static final int EVENT_FLAG_FINISH = 0x4000;
584
585 // No event in this item.
586 public static final int EVENT_NONE = 0x0000;
587 // Event is about a process that is running.
588 public static final int EVENT_PROC = 0x0001;
589 // Event is about an application package that is in the foreground.
590 public static final int EVENT_FOREGROUND = 0x0002;
591 // Event is about an application package that is at the top of the screen.
592 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800593 // Event is about an application package that is at the top of the screen.
594 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800595 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800596 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800597
598 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
599 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
600 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
601 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
602 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
603 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800604 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
605 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800606
607 // For CMD_EVENT.
608 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800609 public HistoryTag eventTag;
610
611 // Meta-data when reading.
612 public int numReadInts;
613
614 // Pre-allocated objects.
615 public final HistoryTag localWakelockTag = new HistoryTag();
616 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800617
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700618 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700619 }
620
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700621 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700622 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800623 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700624 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700625 }
626
627 public int describeContents() {
628 return 0;
629 }
630
631 public void writeToParcel(Parcel dest, int flags) {
632 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700633 int bat = (((int)cmd)&0xff)
634 | ((((int)batteryLevel)<<8)&0xff00)
635 | ((((int)batteryStatus)<<16)&0xf0000)
636 | ((((int)batteryHealth)<<20)&0xf00000)
637 | ((((int)batteryPlugType)<<24)&0xf000000);
638 dest.writeInt(bat);
639 bat = (((int)batteryTemperature)&0xffff)
640 | ((((int)batteryVoltage)<<16)&0xffff0000);
641 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700642 dest.writeInt(states);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800643 if (wakelockTag != null) {
644 dest.writeInt(1);
645 wakelockTag.writeToParcel(dest, flags);
646 } else {
647 dest.writeInt(0);
648 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800649 dest.writeInt(eventCode);
650 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800651 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800652 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800653 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700654 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700655
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800656 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800657 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700658 int bat = src.readInt();
659 cmd = (byte)(bat&0xff);
660 batteryLevel = (byte)((bat>>8)&0xff);
661 batteryStatus = (byte)((bat>>16)&0xf);
662 batteryHealth = (byte)((bat>>20)&0xf);
663 batteryPlugType = (byte)((bat>>24)&0xf);
664 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900665 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700666 batteryVoltage = (char)((bat>>16)&0xffff);
667 states = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800668 if (src.readInt() != 0) {
669 wakelockTag = localWakelockTag;
670 wakelockTag.readFromParcel(src);
671 } else {
672 wakelockTag = null;
673 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800674 eventCode = src.readInt();
675 if (eventCode != EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800676 eventTag = localEventTag;
677 eventTag.readFromParcel(src);
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700678 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800679 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700680 }
681
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700682 public void clear() {
683 time = 0;
684 cmd = CMD_NULL;
685 batteryLevel = 0;
686 batteryStatus = 0;
687 batteryHealth = 0;
688 batteryPlugType = 0;
689 batteryTemperature = 0;
690 batteryVoltage = 0;
691 states = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800692 wakelockTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800693 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800694 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700695 }
696
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700697 public void setTo(HistoryItem o) {
698 time = o.time;
699 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800700 setToCommon(o);
701 }
702
703 public void setTo(long time, byte cmd, HistoryItem o) {
704 this.time = time;
705 this.cmd = cmd;
706 setToCommon(o);
707 }
708
709 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700710 batteryLevel = o.batteryLevel;
711 batteryStatus = o.batteryStatus;
712 batteryHealth = o.batteryHealth;
713 batteryPlugType = o.batteryPlugType;
714 batteryTemperature = o.batteryTemperature;
715 batteryVoltage = o.batteryVoltage;
716 states = o.states;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800717 if (o.wakelockTag != null) {
718 wakelockTag = localWakelockTag;
719 wakelockTag.setTo(o.wakelockTag);
720 } else {
721 wakelockTag = null;
722 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800723 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800724 if (o.eventTag != null) {
725 eventTag = localEventTag;
726 eventTag.setTo(o.eventTag);
727 } else {
728 eventTag = null;
729 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700730 }
731
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800732 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700733 return batteryLevel == o.batteryLevel
734 && batteryStatus == o.batteryStatus
735 && batteryHealth == o.batteryHealth
736 && batteryPlugType == o.batteryPlugType
737 && batteryTemperature == o.batteryTemperature
738 && batteryVoltage == o.batteryVoltage
739 && states == o.states;
740 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800741
742 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800743 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800744 return false;
745 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800746 if (wakelockTag != o.wakelockTag) {
747 if (wakelockTag == null || o.wakelockTag == null) {
748 return false;
749 }
750 if (!wakelockTag.equals(o.wakelockTag)) {
751 return false;
752 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800753 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800754 if (eventTag != o.eventTag) {
755 if (eventTag == null || o.eventTag == null) {
756 return false;
757 }
758 if (!eventTag.equals(o.eventTag)) {
759 return false;
760 }
761 }
762 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800763 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700764 }
765
766 public static final class BitDescription {
767 public final int mask;
768 public final int shift;
769 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800770 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700771 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800772 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700773
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800774 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700775 this.mask = mask;
776 this.shift = -1;
777 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800778 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700779 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800780 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700781 }
782
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800783 public BitDescription(int mask, int shift, String name, String shortName,
784 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700785 this.mask = mask;
786 this.shift = shift;
787 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800788 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700789 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800790 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700791 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700792 }
793
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800794 public abstract int getHistoryTotalSize();
795
796 public abstract int getHistoryUsedSize();
797
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700798 public abstract boolean startIteratingHistoryLocked();
799
Dianne Hackborn099bc622014-01-22 13:39:16 -0800800 public abstract int getHistoryStringPoolSize();
801
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800802 public abstract int getHistoryStringPoolBytes();
803
804 public abstract String getHistoryTagPoolString(int index);
805
806 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800807
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700808 public abstract boolean getNextHistoryLocked(HistoryItem out);
809
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700810 public abstract void finishIteratingHistoryLocked();
811
812 public abstract boolean startIteratingOldHistoryLocked();
813
814 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
815
816 public abstract void finishIteratingOldHistoryLocked();
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700819 * Return the base time offset for the battery history.
820 */
821 public abstract long getHistoryBaseTime();
822
823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 * Returns the number of times the device has been started.
825 */
826 public abstract int getStartCount();
827
828 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700829 * 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 -0800830 * running on battery.
831 *
832 * {@hide}
833 */
834 public abstract long getScreenOnTime(long batteryRealtime, int which);
835
Dianne Hackborn617f8772009-03-31 15:04:46 -0700836 public static final int SCREEN_BRIGHTNESS_DARK = 0;
837 public static final int SCREEN_BRIGHTNESS_DIM = 1;
838 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
839 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
840 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
841
842 static final String[] SCREEN_BRIGHTNESS_NAMES = {
843 "dark", "dim", "medium", "light", "bright"
844 };
845
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800846 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
847 "0", "1", "2", "3", "4"
848 };
849
Dianne Hackborn617f8772009-03-31 15:04:46 -0700850 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
851
852 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700853 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700854 * the given brightness
855 *
856 * {@hide}
857 */
858 public abstract long getScreenBrightnessTime(int brightnessBin,
859 long batteryRealtime, int which);
860
861 public abstract int getInputEventCount(int which);
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700864 * 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 -0800865 * running on battery.
866 *
867 * {@hide}
868 */
869 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700870
871 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700872 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700873 * the given signal strength.
874 *
875 * {@hide}
876 */
877 public abstract long getPhoneSignalStrengthTime(int strengthBin,
878 long batteryRealtime, int which);
879
Dianne Hackborn617f8772009-03-31 15:04:46 -0700880 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700881 * Returns the time in microseconds that the phone has been trying to
882 * acquire a signal.
883 *
884 * {@hide}
885 */
886 public abstract long getPhoneSignalScanningTime(
887 long batteryRealtime, int which);
888
889 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700890 * Returns the number of times the phone has entered the given signal strength.
891 *
892 * {@hide}
893 */
894 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
895
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800896 /**
897 * Returns the time in microseconds that the mobile network has been active
898 * (in a high power state).
899 *
900 * {@hide}
901 */
902 public abstract long getMobileRadioActiveTime(long batteryRealtime, int which);
903
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800904 /**
905 * Returns the number of times that the mobile network has transitioned to the
906 * active state.
907 *
908 * {@hide}
909 */
910 public abstract int getMobileRadioActiveCount(int which);
911
912 /**
913 * Returns the time in microseconds that the mobile network has been active
914 * (in a high power state) but not being able to blame on an app.
915 *
916 * {@hide}
917 */
918 public abstract long getMobileRadioActiveUnknownTime(int which);
919
920 /**
921 * Return count of number of times radio was app that could not be blamed on apps.
922 *
923 * {@hide}
924 */
925 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800926
Dianne Hackborn627bba72009-03-24 22:32:56 -0700927 public static final int DATA_CONNECTION_NONE = 0;
928 public static final int DATA_CONNECTION_GPRS = 1;
929 public static final int DATA_CONNECTION_EDGE = 2;
930 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700931 public static final int DATA_CONNECTION_CDMA = 4;
932 public static final int DATA_CONNECTION_EVDO_0 = 5;
933 public static final int DATA_CONNECTION_EVDO_A = 6;
934 public static final int DATA_CONNECTION_1xRTT = 7;
935 public static final int DATA_CONNECTION_HSDPA = 8;
936 public static final int DATA_CONNECTION_HSUPA = 9;
937 public static final int DATA_CONNECTION_HSPA = 10;
938 public static final int DATA_CONNECTION_IDEN = 11;
939 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700940 public static final int DATA_CONNECTION_LTE = 13;
941 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800942 public static final int DATA_CONNECTION_HSPAP = 15;
943 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700944
Dianne Hackborn627bba72009-03-24 22:32:56 -0700945 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700946 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700947 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800948 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700949 };
950
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700951 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700952
953 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700954 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700955 * the given data connection.
956 *
957 * {@hide}
958 */
959 public abstract long getPhoneDataConnectionTime(int dataType,
960 long batteryRealtime, int which);
961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700963 * Returns the number of times the phone has entered the given data
964 * connection type.
965 *
966 * {@hide}
967 */
968 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700969
970 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
971 = new BitDescription[] {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800972 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
973 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800974 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800975 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
976 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
977 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800978 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800979 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
980 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800981 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
982 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800983 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
984 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
985 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
986 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
987 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
988 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
989 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
990 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
991 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
992 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
993 new String[] {"in", "out", "emergency", "off"},
994 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700995 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800996 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
997 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
998 "0", "1", "2", "3", "4"
999 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001000 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1001 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1002 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001003 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001004
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001005 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001006 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001007 };
1008
1009 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001010 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001011 };
1012
Dianne Hackborn617f8772009-03-31 15:04:46 -07001013 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001014 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001015 * running on battery.
1016 *
1017 * {@hide}
1018 */
1019 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001020
1021 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001022 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001023 * been in the running state while the device was running on battery.
1024 *
1025 * {@hide}
1026 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001027 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001028
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001029 public static final int WIFI_STATE_OFF = 0;
1030 public static final int WIFI_STATE_OFF_SCANNING = 1;
1031 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1032 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1033 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1034 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1035 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1036 public static final int WIFI_STATE_SOFT_AP = 7;
1037
1038 static final String[] WIFI_STATE_NAMES = {
1039 "off", "scanning", "no_net", "disconn",
1040 "sta", "p2p", "sta_p2p", "soft_ap"
1041 };
1042
1043 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1044
1045 /**
1046 * Returns the time in microseconds that WiFi has been running in the given state.
1047 *
1048 * {@hide}
1049 */
1050 public abstract long getWifiStateTime(int wifiState,
1051 long batteryRealtime, int which);
1052
1053 /**
1054 * Returns the number of times that WiFi has entered the given state.
1055 *
1056 * {@hide}
1057 */
1058 public abstract int getWifiStateCount(int wifiState, int which);
1059
The Android Open Source Project10592532009-03-18 17:39:46 -07001060 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001061 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001062 * running on battery.
1063 *
1064 * {@hide}
1065 */
1066 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
1067
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001068 public abstract int getBluetoothPingCount();
1069
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001070 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1071 public static final int BLUETOOTH_STATE_LOW = 1;
1072 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1073 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001074
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001075 static final String[] BLUETOOTH_STATE_NAMES = {
1076 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001077 };
1078
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001079 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001080
1081 /**
1082 * Returns the time in microseconds that Bluetooth has been running in the
1083 * given active state.
1084 *
1085 * {@hide}
1086 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001087 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001088 long batteryRealtime, int which);
1089
1090 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001091 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001092 *
1093 * {@hide}
1094 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001095 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001096
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001097 public static final int NETWORK_MOBILE_RX_DATA = 0;
1098 public static final int NETWORK_MOBILE_TX_DATA = 1;
1099 public static final int NETWORK_WIFI_RX_DATA = 2;
1100 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001101
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001102 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001103
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001104 public abstract long getNetworkActivityBytes(int type, int which);
1105 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001106
The Android Open Source Project10592532009-03-18 17:39:46 -07001107 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001108 * Return the wall clock time when battery stats data collection started.
1109 */
1110 public abstract long getStartClockTime();
1111
1112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 * Return whether we are currently running on battery.
1114 */
1115 public abstract boolean getIsOnBattery();
1116
1117 /**
1118 * Returns a SparseArray containing the statistics for each uid.
1119 */
1120 public abstract SparseArray<? extends Uid> getUidStats();
1121
1122 /**
1123 * Returns the current battery uptime in microseconds.
1124 *
1125 * @param curTime the amount of elapsed realtime in microseconds.
1126 */
1127 public abstract long getBatteryUptime(long curTime);
1128
1129 /**
1130 * Returns the current battery realtime in microseconds.
1131 *
1132 * @param curTime the amount of elapsed realtime in microseconds.
1133 */
1134 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001135
1136 /**
Evan Millar633a1742009-04-02 16:36:33 -07001137 * Returns the battery percentage level at the last time the device was unplugged from power, or
1138 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001139 */
Evan Millar633a1742009-04-02 16:36:33 -07001140 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001141
1142 /**
Evan Millar633a1742009-04-02 16:36:33 -07001143 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1144 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001145 */
Evan Millar633a1742009-04-02 16:36:33 -07001146 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147
1148 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001149 * Get the amount the battery has discharged since the stats were
1150 * last reset after charging, as a lower-end approximation.
1151 */
1152 public abstract int getLowDischargeAmountSinceCharge();
1153
1154 /**
1155 * Get the amount the battery has discharged since the stats were
1156 * last reset after charging, as an upper-end approximation.
1157 */
1158 public abstract int getHighDischargeAmountSinceCharge();
1159
1160 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001161 * Get the amount the battery has discharged while the screen was on,
1162 * since the last time power was unplugged.
1163 */
1164 public abstract int getDischargeAmountScreenOn();
1165
1166 /**
1167 * Get the amount the battery has discharged while the screen was on,
1168 * since the last time the device was charged.
1169 */
1170 public abstract int getDischargeAmountScreenOnSinceCharge();
1171
1172 /**
1173 * Get the amount the battery has discharged while the screen was off,
1174 * since the last time power was unplugged.
1175 */
1176 public abstract int getDischargeAmountScreenOff();
1177
1178 /**
1179 * Get the amount the battery has discharged while the screen was off,
1180 * since the last time the device was charged.
1181 */
1182 public abstract int getDischargeAmountScreenOffSinceCharge();
1183
1184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 * Returns the total, last, or current battery uptime in microseconds.
1186 *
1187 * @param curTime the elapsed realtime in microseconds.
1188 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1189 */
1190 public abstract long computeBatteryUptime(long curTime, int which);
1191
1192 /**
1193 * Returns the total, last, or current battery realtime in microseconds.
1194 *
1195 * @param curTime the current elapsed realtime in microseconds.
1196 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1197 */
1198 public abstract long computeBatteryRealtime(long curTime, int which);
1199
1200 /**
1201 * Returns the total, last, or current uptime in microseconds.
1202 *
1203 * @param curTime the current elapsed realtime in microseconds.
1204 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1205 */
1206 public abstract long computeUptime(long curTime, int which);
1207
1208 /**
1209 * Returns the total, last, or current realtime in microseconds.
1210 * *
1211 * @param curTime the current elapsed realtime in microseconds.
1212 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1213 */
1214 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001215
1216 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217
Amith Yamasanie43530a2009-08-21 13:11:37 -07001218 /** Returns the number of different speeds that the CPU can run at */
1219 public abstract int getCpuSpeedSteps();
1220
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001221 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1222
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001223 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 long days = seconds / (60 * 60 * 24);
1225 if (days != 0) {
1226 out.append(days);
1227 out.append("d ");
1228 }
1229 long used = days * 60 * 60 * 24;
1230
1231 long hours = (seconds - used) / (60 * 60);
1232 if (hours != 0 || used != 0) {
1233 out.append(hours);
1234 out.append("h ");
1235 }
1236 used += hours * 60 * 60;
1237
1238 long mins = (seconds-used) / 60;
1239 if (mins != 0 || used != 0) {
1240 out.append(mins);
1241 out.append("m ");
1242 }
1243 used += mins * 60;
1244
1245 if (seconds != 0 || used != 0) {
1246 out.append(seconds-used);
1247 out.append("s ");
1248 }
1249 }
1250
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001251 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001253 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 sb.append((time - (sec * 100)) * 10);
1255 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001258 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001260 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 sb.append(time - (sec * 1000));
1262 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001265 private final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
1266 long sec = time / 1000;
1267 formatTimeRaw(sb, sec);
1268 sb.append(time - (sec * 1000));
1269 sb.append("ms");
1270 }
1271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 private final String formatRatioLocked(long num, long den) {
1273 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001274 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 }
1276 float perc = ((float)num) / ((float)den) * 100;
1277 mFormatBuilder.setLength(0);
1278 mFormatter.format("%.1f%%", perc);
1279 return mFormatBuilder.toString();
1280 }
1281
Evan Millar22ac0432009-03-31 11:33:18 -07001282 private final String formatBytesLocked(long bytes) {
1283 mFormatBuilder.setLength(0);
1284
1285 if (bytes < BYTES_PER_KB) {
1286 return bytes + "B";
1287 } else if (bytes < BYTES_PER_MB) {
1288 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1289 return mFormatBuilder.toString();
1290 } else if (bytes < BYTES_PER_GB){
1291 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1292 return mFormatBuilder.toString();
1293 } else {
1294 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1295 return mFormatBuilder.toString();
1296 }
1297 }
1298
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001299 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1300 if (timer != null) {
1301 // Convert from microseconds to milliseconds with rounding
1302 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1303 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1304 return totalTimeMillis;
1305 }
1306 return 0;
1307 }
1308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 *
1311 * @param sb a StringBuilder object.
1312 * @param timer a Timer object contining the wakelock times.
1313 * @param batteryRealtime the current on-battery time in microseconds.
1314 * @param name the name of the wakelock.
1315 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1316 * @param linePrefix a String to be prepended to each line of output.
1317 * @return the line prefix
1318 */
1319 private static final String printWakeLock(StringBuilder sb, Timer timer,
1320 long batteryRealtime, String name, int which, String linePrefix) {
1321
1322 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001323 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324
Evan Millarc64edde2009-04-18 12:26:32 -07001325 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 if (totalTimeMillis != 0) {
1327 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001328 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001329 if (name != null) {
1330 sb.append(name);
1331 sb.append(' ');
1332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 sb.append('(');
1334 sb.append(count);
1335 sb.append(" times)");
1336 return ", ";
1337 }
1338 }
1339 return linePrefix;
1340 }
1341
1342 /**
1343 * Checkin version of wakelock printer. Prints simple comma-separated list.
1344 *
1345 * @param sb a StringBuilder object.
1346 * @param timer a Timer object contining the wakelock times.
1347 * @param now the current time in microseconds.
1348 * @param name the name of the wakelock.
1349 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1350 * @param linePrefix a String to be prepended to each line of output.
1351 * @return the line prefix
1352 */
1353 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001354 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 long totalTimeMicros = 0;
1356 int count = 0;
1357 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001358 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1359 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 sb.append(linePrefix);
1362 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1363 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001364 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 sb.append(count);
1366 return ",";
1367 }
1368
1369 /**
1370 * Dump a comma-separated line of values for terse checkin mode.
1371 *
1372 * @param pw the PageWriter to dump log to
1373 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1374 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1375 * @param args type-dependent data arguments
1376 */
1377 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1378 Object... args ) {
1379 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1380 pw.print(uid); pw.print(',');
1381 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001382 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383
1384 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001385 pw.print(',');
1386 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001388 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390
1391 /**
1392 * Checkin server version of dump to produce more compact, computer-readable log.
1393 *
1394 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001396 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1398 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1399 final long batteryUptime = getBatteryUptime(rawUptime);
1400 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1401 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1402 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1403 final long totalRealtime = computeRealtime(rawRealtime, which);
1404 final long totalUptime = computeUptime(rawUptime, which);
1405 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1406 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001407 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001408 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001409 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410
1411 StringBuilder sb = new StringBuilder(128);
1412
Evan Millar22ac0432009-03-31 11:33:18 -07001413 SparseArray<? extends Uid> uidStats = getUidStats();
1414 final int NU = uidStats.size();
1415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 // Dump "battery" stat
1419 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001420 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001421 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001422 totalRealtime / 1000, totalUptime / 1000,
1423 getStartClockTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001425 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001426 long fullWakeLockTimeTotal = 0;
1427 long partialWakeLockTimeTotal = 0;
1428
1429 for (int iu = 0; iu < NU; iu++) {
1430 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001431
Evan Millar22ac0432009-03-31 11:33:18 -07001432 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1433 if (wakelocks.size() > 0) {
1434 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1435 : wakelocks.entrySet()) {
1436 Uid.Wakelock wl = ent.getValue();
1437
1438 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1439 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001440 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001441 }
1442
1443 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1444 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001445 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001446 batteryRealtime, which);
1447 }
1448 }
1449 }
1450 }
1451
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1453 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1454 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1455 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1456 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1457 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1458 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1459 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1460
1461 // Dump network stats
1462 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1463 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1464 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 // Dump misc stats
1467 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001468 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001469 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001470 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001471 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001472 getInputEventCount(which), getMobileRadioActiveTime(batteryRealtime, which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001473
1474 // Dump screen brightness stats
1475 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1476 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1477 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1478 }
1479 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001480
Dianne Hackborn627bba72009-03-24 22:32:56 -07001481 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001482 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1483 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001484 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1485 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001486 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001487 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1488 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001489 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001490 args[i] = getPhoneSignalStrengthCount(i, which);
1491 }
1492 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001493
Dianne Hackborn627bba72009-03-24 22:32:56 -07001494 // Dump network type stats
1495 args = new Object[NUM_DATA_CONNECTION_TYPES];
1496 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1497 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1498 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001499 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1500 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1501 args[i] = getPhoneDataConnectionCount(i, which);
1502 }
1503 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001504
1505 // Dump wifi state stats
1506 args = new Object[NUM_WIFI_STATES];
1507 for (int i=0; i<NUM_WIFI_STATES; i++) {
1508 args[i] = getWifiStateTime(i, batteryRealtime, which) / 1000;
1509 }
1510 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1511 for (int i=0; i<NUM_WIFI_STATES; i++) {
1512 args[i] = getWifiStateCount(i, which);
1513 }
1514 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1515
1516 // Dump bluetooth state stats
1517 args = new Object[NUM_BLUETOOTH_STATES];
1518 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1519 args[i] = getBluetoothStateTime(i, batteryRealtime, which) / 1000;
1520 }
1521 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1522 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1523 args[i] = getBluetoothStateCount(i, which);
1524 }
1525 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1526
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001527 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001528 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001529 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001532 if (which == STATS_SINCE_UNPLUGGED) {
1533 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1534 getDischargeStartLevel()-getDischargeCurrentLevel(),
1535 getDischargeStartLevel()-getDischargeCurrentLevel(),
1536 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1537 } else {
1538 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1539 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1540 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1541 }
1542
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001543 if (reqUid < 0) {
1544 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1545 if (kernelWakelocks.size() > 0) {
1546 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1547 sb.setLength(0);
1548 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1549
1550 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1551 sb.toString());
1552 }
Evan Millarc64edde2009-04-18 12:26:32 -07001553 }
1554 }
1555
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001556 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1557 helper.create(this);
1558 helper.refreshStats(which, UserHandle.USER_ALL);
1559 List<BatterySipper> sippers = helper.getUsageList();
1560 if (sippers != null && sippers.size() > 0) {
1561 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1562 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001563 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001564 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1565 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1566 for (int i=0; i<sippers.size(); i++) {
1567 BatterySipper bs = sippers.get(i);
1568 int uid = 0;
1569 String label;
1570 switch (bs.drainType) {
1571 case IDLE:
1572 label="idle";
1573 break;
1574 case CELL:
1575 label="cell";
1576 break;
1577 case PHONE:
1578 label="phone";
1579 break;
1580 case WIFI:
1581 label="wifi";
1582 break;
1583 case BLUETOOTH:
1584 label="blue";
1585 break;
1586 case SCREEN:
1587 label="scrn";
1588 break;
1589 case APP:
1590 uid = bs.uidObj.getUid();
1591 label = "uid";
1592 break;
1593 case USER:
1594 uid = UserHandle.getUid(bs.userId, 0);
1595 label = "user";
1596 break;
1597 case UNACCOUNTED:
1598 label = "unacc";
1599 break;
1600 case OVERCOUNTED:
1601 label = "over";
1602 break;
1603 default:
1604 label = "???";
1605 }
1606 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1607 BatteryStatsHelper.makemAh(bs.value));
1608 }
1609 }
1610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 for (int iu = 0; iu < NU; iu++) {
1612 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001613 if (reqUid >= 0 && uid != reqUid) {
1614 continue;
1615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 Uid u = uidStats.valueAt(iu);
1617 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001618 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1619 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1620 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1621 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1622 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1623 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001624 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1625 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001626 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1627 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001628 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001629 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001630 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001631
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001632 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1633 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001634 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001635 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1636 wifiBytesRx, wifiBytesTx,
1637 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001638 wifiPacketsRx, wifiPacketsTx,
1639 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001640 }
1641
Nick Pelly6ccaa542012-06-15 15:22:47 -07001642 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001643 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001644 dumpLine(pw, uid, category, WIFI_DATA,
1645 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647
Dianne Hackborn617f8772009-03-31 15:04:46 -07001648 if (u.hasUserActivity()) {
1649 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1650 boolean hasData = false;
1651 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1652 int val = u.getUserActivityCount(i, which);
1653 args[i] = val;
1654 if (val != 0) hasData = true;
1655 }
1656 if (hasData) {
1657 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1658 }
1659 }
1660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1662 if (wakelocks.size() > 0) {
1663 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1664 : wakelocks.entrySet()) {
1665 Uid.Wakelock wl = ent.getValue();
1666 String linePrefix = "";
1667 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001668 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1669 batteryRealtime, "f", which, linePrefix);
1670 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1671 batteryRealtime, "p", which, linePrefix);
1672 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1673 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674
1675 // Only log if we had at lease one wakelock...
1676 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001677 String name = ent.getKey();
1678 if (name.indexOf(',') >= 0) {
1679 name = name.replace(',', '_');
1680 }
1681 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
1684 }
1685
1686 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1687 if (sensors.size() > 0) {
1688 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1689 : sensors.entrySet()) {
1690 Uid.Sensor se = ent.getValue();
1691 int sensorNumber = ent.getKey();
1692 Timer timer = se.getSensorTime();
1693 if (timer != null) {
1694 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001695 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1696 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 if (totalTime != 0) {
1698 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1699 }
1700 }
1701 }
1702 }
1703
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001704 Timer vibTimer = u.getVibratorOnTimer();
1705 if (vibTimer != null) {
1706 // Convert from microseconds to milliseconds with rounding
1707 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1708 int count = vibTimer.getCountLocked(which);
1709 if (totalTime != 0) {
1710 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1711 }
1712 }
1713
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001714 Timer fgTimer = u.getForegroundActivityTimer();
1715 if (fgTimer != null) {
1716 // Convert from microseconds to milliseconds with rounding
1717 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1718 int count = fgTimer.getCountLocked(which);
1719 if (totalTime != 0) {
1720 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1721 }
1722 }
1723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1725 if (processStats.size() > 0) {
1726 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1727 : processStats.entrySet()) {
1728 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001729
1730 final long userMillis = ps.getUserTime(which) * 10;
1731 final long systemMillis = ps.getSystemTime(which) * 10;
1732 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1733 final long starts = ps.getStarts(which);
1734
1735 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1736 || starts != 0) {
1737 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1738 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740 }
1741 }
1742
1743 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1744 if (packageStats.size() > 0) {
1745 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1746 : packageStats.entrySet()) {
1747
1748 Uid.Pkg ps = ent.getValue();
1749 int wakeups = ps.getWakeups(which);
1750 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1751 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1752 : serviceStats.entrySet()) {
1753 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1754 long startTime = ss.getStartTime(batteryUptime, which);
1755 int starts = ss.getStarts(which);
1756 int launches = ss.getLaunches(which);
1757 if (startTime != 0 || starts != 0 || launches != 0) {
1758 dumpLine(pw, uid, category, APK_DATA,
1759 wakeups, // wakeup alarms
1760 ent.getKey(), // Apk
1761 sent.getKey(), // service
1762 startTime / 1000, // time spent started, in ms
1763 starts,
1764 launches);
1765 }
1766 }
1767 }
1768 }
1769 }
1770 }
1771
Dianne Hackborn81038902012-11-26 17:04:09 -08001772 static final class TimerEntry {
1773 final String mName;
1774 final int mId;
1775 final BatteryStats.Timer mTimer;
1776 final long mTime;
1777 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1778 mName = name;
1779 mId = id;
1780 mTimer = timer;
1781 mTime = time;
1782 }
1783 }
1784
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001785 private void printmAh(PrintWriter printer, double power) {
1786 printer.print(BatteryStatsHelper.makemAh(power));
1787 }
1788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001790 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1791 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1793 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1794 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001795 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
1797 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1798 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1799 final long totalRealtime = computeRealtime(rawRealtime, which);
1800 final long totalUptime = computeUptime(rawUptime, which);
1801
1802 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001803
1804 SparseArray<? extends Uid> uidStats = getUidStats();
1805 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001807 sb.setLength(0);
1808 sb.append(prefix);
1809 sb.append(" Time on battery: ");
1810 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1811 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1812 sb.append(") realtime, ");
1813 formatTimeMs(sb, whichBatteryUptime / 1000);
1814 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1815 sb.append(") uptime");
1816 pw.println(sb.toString());
1817 sb.setLength(0);
1818 sb.append(prefix);
1819 sb.append(" Total run time: ");
1820 formatTimeMs(sb, totalRealtime / 1000);
1821 sb.append("realtime, ");
1822 formatTimeMs(sb, totalUptime / 1000);
1823 sb.append("uptime, ");
1824 pw.println(sb.toString());
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001825 pw.print(" Start clock time: ");
1826 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1827
The Android Open Source Project10592532009-03-18 17:39:46 -07001828 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1829 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001830 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001831 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1832 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001833 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001834 sb.append(prefix);
1835 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1836 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1837 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1838 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1839 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1840 sb.append(")");
1841 pw.println(sb.toString());
1842 sb.setLength(0);
1843 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 sb.append(" Screen brightnesses: ");
1845 boolean didOne = false;
1846 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1847 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1848 if (time == 0) {
1849 continue;
1850 }
1851 if (didOne) sb.append(", ");
1852 didOne = true;
1853 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1854 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001855 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001856 sb.append("(");
1857 sb.append(formatRatioLocked(time, screenOnTime));
1858 sb.append(")");
1859 }
1860 if (!didOne) sb.append("No activity");
1861 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001862
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001863 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001864 long fullWakeLockTimeTotalMicros = 0;
1865 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001866
1867 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1868 @Override
1869 public int compare(TimerEntry lhs, TimerEntry rhs) {
1870 long lhsTime = lhs.mTime;
1871 long rhsTime = rhs.mTime;
1872 if (lhsTime < rhsTime) {
1873 return 1;
1874 }
1875 if (lhsTime > rhsTime) {
1876 return -1;
1877 }
1878 return 0;
1879 }
1880 };
1881
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001882 if (reqUid < 0) {
1883 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1884 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001885 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001886 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001887 BatteryStats.Timer timer = ent.getValue();
1888 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1889 if (totalTimeMillis > 0) {
1890 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1891 }
1892 }
1893 Collections.sort(timers, timerComparator);
1894 for (int i=0; i<timers.size(); i++) {
1895 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001896 String linePrefix = ": ";
1897 sb.setLength(0);
1898 sb.append(prefix);
1899 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001900 sb.append(timer.mName);
1901 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1902 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001903 if (!linePrefix.equals(": ")) {
1904 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001905 // Only print out wake locks that were held
1906 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001907 }
Evan Millarc64edde2009-04-18 12:26:32 -07001908 }
Evan Millarc64edde2009-04-18 12:26:32 -07001909 }
1910 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001911
1912 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1913
Evan Millar22ac0432009-03-31 11:33:18 -07001914 for (int iu = 0; iu < NU; iu++) {
1915 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001916
Evan Millar22ac0432009-03-31 11:33:18 -07001917 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1918 if (wakelocks.size() > 0) {
1919 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1920 : wakelocks.entrySet()) {
1921 Uid.Wakelock wl = ent.getValue();
1922
1923 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1924 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001925 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001926 batteryRealtime, which);
1927 }
1928
1929 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1930 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001931 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001932 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001933 if (totalTimeMicros > 0) {
1934 if (reqUid < 0) {
1935 // Only show the ordered list of all wake
1936 // locks if the caller is not asking for data
1937 // about a specific uid.
1938 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1939 partialWakeTimer, totalTimeMicros));
1940 }
1941 partialWakeLockTimeTotalMicros += totalTimeMicros;
1942 }
Evan Millar22ac0432009-03-31 11:33:18 -07001943 }
1944 }
1945 }
1946 }
1947
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001948 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1949 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1950 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1951 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1952 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1953 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1954 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1955 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1956
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001957 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001958 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1959 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1960 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1961 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001962 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001963 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1964 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1965 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1966 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001967 sb.setLength(0);
1968 sb.append(prefix);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001969 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001970 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001971 sb.append(", Total partial wakelock time: "); formatTimeMsNoSpace(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001972 (partialWakeLockTimeTotalMicros + 500) / 1000);
1973 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001974
Dianne Hackborn627bba72009-03-24 22:32:56 -07001975 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001976 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001977 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07001978 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001979 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001980 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1981 if (time == 0) {
1982 continue;
1983 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001984 sb.append("\n ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001985 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001986 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001987 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001988 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001989 sb.append("(");
1990 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001991 sb.append(") ");
1992 sb.append(getPhoneSignalStrengthCount(i, which));
1993 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001994 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001995 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001996 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001997
1998 sb.setLength(0);
1999 sb.append(prefix);
2000 sb.append(" Signal scanning time: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002001 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002002 pw.println(sb.toString());
2003
Dianne Hackborn627bba72009-03-24 22:32:56 -07002004 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002005 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002006 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002007 didOne = false;
2008 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2009 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
2010 if (time == 0) {
2011 continue;
2012 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002013 sb.append("\n ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002014 didOne = true;
2015 sb.append(DATA_CONNECTION_NAMES[i]);
2016 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002017 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002018 sb.append("(");
2019 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002020 sb.append(") ");
2021 sb.append(getPhoneDataConnectionCount(i, which));
2022 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002023 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002024 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002025 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002026
2027 sb.setLength(0);
2028 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002029 sb.append(" Mobile radio active time: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002030 final long mobileActiveTime = getMobileRadioActiveTime(batteryRealtime, which);
2031 formatTimeMs(sb, mobileActiveTime / 1000);
2032 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2033 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2034 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002035 pw.println(sb.toString());
2036
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002037 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2038 if (mobileActiveUnknownTime != 0) {
2039 sb.setLength(0);
2040 sb.append(prefix);
2041 sb.append(" Mobile radio active unknown time: ");
2042 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2043 sb.append("(");
2044 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2045 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2046 sb.append("x");
2047 pw.println(sb.toString());
2048 }
2049
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002050 sb.setLength(0);
2051 sb.append(prefix);
2052 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2053 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2054 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2055 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002056 sb.append(")");
2057 pw.println(sb.toString());
2058
2059 sb.setLength(0);
2060 sb.append(prefix);
2061 sb.append(" Wifi states:");
2062 didOne = false;
2063 for (int i=0; i<NUM_WIFI_STATES; i++) {
2064 final long time = getWifiStateTime(i, batteryRealtime, which);
2065 if (time == 0) {
2066 continue;
2067 }
2068 sb.append("\n ");
2069 didOne = true;
2070 sb.append(WIFI_STATE_NAMES[i]);
2071 sb.append(" ");
2072 formatTimeMs(sb, time/1000);
2073 sb.append("(");
2074 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2075 sb.append(") ");
2076 sb.append(getPhoneDataConnectionCount(i, which));
2077 sb.append("x");
2078 }
2079 if (!didOne) sb.append(" (no activity)");
2080 pw.println(sb.toString());
2081
2082 sb.setLength(0);
2083 sb.append(prefix);
2084 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002085 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2086 sb.append(")");
2087 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002088
2089 sb.setLength(0);
2090 sb.append(prefix);
2091 sb.append(" Bluetooth states:");
2092 didOne = false;
2093 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2094 final long time = getBluetoothStateTime(i, batteryRealtime, which);
2095 if (time == 0) {
2096 continue;
2097 }
2098 sb.append("\n ");
2099 didOne = true;
2100 sb.append(BLUETOOTH_STATE_NAMES[i]);
2101 sb.append(" ");
2102 formatTimeMs(sb, time/1000);
2103 sb.append("(");
2104 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2105 sb.append(") ");
2106 sb.append(getPhoneDataConnectionCount(i, which));
2107 sb.append("x");
2108 }
2109 if (!didOne) sb.append(" (no activity)");
2110 pw.println(sb.toString());
2111
2112 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002113
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002114 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002115 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002116 pw.print(prefix); pw.println(" Device is currently unplugged");
2117 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2118 pw.println(getDischargeStartLevel());
2119 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2120 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002121 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002122 pw.print(prefix); pw.println(" Device is currently plugged into power");
2123 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2124 pw.println(getDischargeStartLevel());
2125 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2126 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002127 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002128 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2129 pw.println(getDischargeAmountScreenOn());
2130 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2131 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002132 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002133 } else {
2134 pw.print(prefix); pw.println(" Device battery use since last full charge");
2135 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2136 pw.println(getLowDischargeAmountSinceCharge());
2137 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2138 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002139 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2140 pw.println(getDischargeAmountScreenOnSinceCharge());
2141 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2142 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002143 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002144 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002145
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002146 BatteryStatsHelper helper = new BatteryStatsHelper(context);
2147 helper.create(this);
2148 helper.refreshStats(which, UserHandle.USER_ALL);
2149 List<BatterySipper> sippers = helper.getUsageList();
2150 if (sippers != null && sippers.size() > 0) {
2151 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2152 pw.print(prefix); pw.print(" Capacity: ");
2153 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002154 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002155 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2156 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2157 pw.println();
2158 for (int i=0; i<sippers.size(); i++) {
2159 BatterySipper bs = sippers.get(i);
2160 switch (bs.drainType) {
2161 case IDLE:
2162 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2163 pw.println();
2164 break;
2165 case CELL:
2166 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2167 pw.println();
2168 break;
2169 case PHONE:
2170 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2171 pw.println();
2172 break;
2173 case WIFI:
2174 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2175 pw.println();
2176 break;
2177 case BLUETOOTH:
2178 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2179 pw.println();
2180 break;
2181 case SCREEN:
2182 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2183 pw.println();
2184 break;
2185 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002186 pw.print(prefix); pw.print(" Uid ");
2187 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002188 pw.print(": "); printmAh(pw, bs.value); pw.println();
2189 break;
2190 case USER:
2191 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2192 pw.print(": "); printmAh(pw, bs.value); pw.println();
2193 break;
2194 case UNACCOUNTED:
2195 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2196 pw.println();
2197 break;
2198 case OVERCOUNTED:
2199 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2200 pw.println();
2201 break;
2202 }
2203 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002204 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002205 }
2206
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002207 sippers = helper.getMobilemsppList();
2208 if (sippers != null && sippers.size() > 0) {
2209 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
2210 for (int i=0; i<sippers.size(); i++) {
2211 BatterySipper bs = sippers.get(i);
2212 sb.setLength(0);
2213 sb.append(prefix); sb.append(" Uid ");
2214 UserHandle.formatUid(sb, bs.uidObj.getUid());
2215 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2216 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2217 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
2218 sb.append(")");
2219 pw.println(sb.toString());
2220 }
2221 pw.println();
2222 }
2223
Dianne Hackborn81038902012-11-26 17:04:09 -08002224 if (timers.size() > 0) {
2225 Collections.sort(timers, timerComparator);
2226 pw.print(prefix); pw.println(" All partial wake locks:");
2227 for (int i=0; i<timers.size(); i++) {
2228 TimerEntry timer = timers.get(i);
2229 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002230 sb.append(" Wake lock ");
2231 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08002232 sb.append(" ");
2233 sb.append(timer.mName);
2234 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
2235 sb.append(" realtime");
2236 pw.println(sb.toString());
2237 }
2238 timers.clear();
2239 pw.println();
2240 }
Evan Millar22ac0432009-03-31 11:33:18 -07002241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 for (int iu=0; iu<NU; iu++) {
2243 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002244 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002245 continue;
2246 }
2247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002249
2250 pw.print(prefix);
2251 pw.print(" ");
2252 UserHandle.formatUid(pw, uid);
2253 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002255
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002256 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2257 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2258 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2259 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2260 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2261 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002262 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2263 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002264 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2265 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07002266 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07002267 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002268 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002269
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002270 if (mobileRxBytes > 0 || mobileTxBytes > 0
2271 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002272 pw.print(prefix); pw.print(" Mobile network: ");
2273 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002274 pw.print(formatBytesLocked(mobileTxBytes));
2275 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2276 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002278 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2279 sb.setLength(0);
2280 sb.append(prefix); sb.append(" Mobile radio active: ");
2281 formatTimeMs(sb, uidMobileActiveTime / 1000);
2282 sb.append("(");
2283 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2284 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2285 long packets = mobileRxPackets + mobileTxPackets;
2286 if (packets == 0) {
2287 packets = 1;
2288 }
2289 sb.append(" @ ");
2290 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2291 sb.append(" mspp");
2292 pw.println(sb.toString());
2293 }
2294
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002295 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002296 pw.print(prefix); pw.print(" Wi-Fi network: ");
2297 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002298 pw.print(formatBytesLocked(wifiTxBytes));
2299 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2300 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002301 }
2302
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002303 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2304 || uidWifiRunningTime != 0) {
2305 sb.setLength(0);
2306 sb.append(prefix); sb.append(" Wifi Running: ");
2307 formatTimeMs(sb, uidWifiRunningTime / 1000);
2308 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2309 whichBatteryRealtime)); sb.append(")\n");
2310 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2311 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2312 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2313 whichBatteryRealtime)); sb.append(")\n");
2314 sb.append(prefix); sb.append(" Wifi Scan: ");
2315 formatTimeMs(sb, wifiScanTime / 1000);
2316 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2317 whichBatteryRealtime)); sb.append(")");
2318 pw.println(sb.toString());
2319 }
2320
Dianne Hackborn617f8772009-03-31 15:04:46 -07002321 if (u.hasUserActivity()) {
2322 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002323 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002324 int val = u.getUserActivityCount(i, which);
2325 if (val != 0) {
2326 if (!hasData) {
2327 sb.setLength(0);
2328 sb.append(" User activity: ");
2329 hasData = true;
2330 } else {
2331 sb.append(", ");
2332 }
2333 sb.append(val);
2334 sb.append(" ");
2335 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2336 }
2337 }
2338 if (hasData) {
2339 pw.println(sb.toString());
2340 }
2341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342
2343 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2344 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002345 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2346 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2348 : wakelocks.entrySet()) {
2349 Uid.Wakelock wl = ent.getValue();
2350 String linePrefix = ": ";
2351 sb.setLength(0);
2352 sb.append(prefix);
2353 sb.append(" Wake lock ");
2354 sb.append(ent.getKey());
2355 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
2356 "full", which, linePrefix);
2357 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
2358 "partial", which, linePrefix);
2359 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
2360 "window", which, linePrefix);
2361 if (!linePrefix.equals(": ")) {
2362 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002363 // Only print out wake locks that were held
2364 pw.println(sb.toString());
2365 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002366 count++;
2367 }
2368 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
2369 batteryRealtime, which);
2370 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
2371 batteryRealtime, which);
2372 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
2373 batteryRealtime, which);
2374 }
2375 if (count > 1) {
2376 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2377 sb.setLength(0);
2378 sb.append(prefix);
2379 sb.append(" TOTAL wake: ");
2380 boolean needComma = false;
2381 if (totalFull != 0) {
2382 needComma = true;
2383 formatTimeMs(sb, totalFull);
2384 sb.append("full");
2385 }
2386 if (totalPartial != 0) {
2387 if (needComma) {
2388 sb.append(", ");
2389 }
2390 needComma = true;
2391 formatTimeMs(sb, totalPartial);
2392 sb.append("partial");
2393 }
2394 if (totalWindow != 0) {
2395 if (needComma) {
2396 sb.append(", ");
2397 }
2398 needComma = true;
2399 formatTimeMs(sb, totalWindow);
2400 sb.append("window");
2401 }
2402 sb.append(" realtime");
2403 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406 }
2407
2408 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2409 if (sensors.size() > 0) {
2410 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2411 : sensors.entrySet()) {
2412 Uid.Sensor se = ent.getValue();
2413 int sensorNumber = ent.getKey();
2414 sb.setLength(0);
2415 sb.append(prefix);
2416 sb.append(" Sensor ");
2417 int handle = se.getHandle();
2418 if (handle == Uid.Sensor.GPS) {
2419 sb.append("GPS");
2420 } else {
2421 sb.append(handle);
2422 }
2423 sb.append(": ");
2424
2425 Timer timer = se.getSensorTime();
2426 if (timer != null) {
2427 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002428 long totalTime = (timer.getTotalTimeLocked(
2429 batteryRealtime, which) + 500) / 1000;
2430 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 //timer.logState();
2432 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002433 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 sb.append("realtime (");
2435 sb.append(count);
2436 sb.append(" times)");
2437 } else {
2438 sb.append("(not used)");
2439 }
2440 } else {
2441 sb.append("(not used)");
2442 }
2443
2444 pw.println(sb.toString());
2445 uidActivity = true;
2446 }
2447 }
2448
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002449 Timer vibTimer = u.getVibratorOnTimer();
2450 if (vibTimer != null) {
2451 // Convert from microseconds to milliseconds with rounding
2452 long totalTime = (vibTimer.getTotalTimeLocked(
2453 batteryRealtime, which) + 500) / 1000;
2454 int count = vibTimer.getCountLocked(which);
2455 //timer.logState();
2456 if (totalTime != 0) {
2457 sb.setLength(0);
2458 sb.append(prefix);
2459 sb.append(" Vibrator: ");
2460 formatTimeMs(sb, totalTime);
2461 sb.append("realtime (");
2462 sb.append(count);
2463 sb.append(" times)");
2464 pw.println(sb.toString());
2465 uidActivity = true;
2466 }
2467 }
2468
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002469 Timer fgTimer = u.getForegroundActivityTimer();
2470 if (fgTimer != null) {
2471 // Convert from microseconds to milliseconds with rounding
2472 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2473 int count = fgTimer.getCountLocked(which);
2474 if (totalTime != 0) {
2475 sb.setLength(0);
2476 sb.append(prefix);
2477 sb.append(" Foreground activities: ");
2478 formatTimeMs(sb, totalTime);
2479 sb.append("realtime (");
2480 sb.append(count);
2481 sb.append(" times)");
2482 pw.println(sb.toString());
2483 uidActivity = true;
2484 }
2485 }
2486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2488 if (processStats.size() > 0) {
2489 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2490 : processStats.entrySet()) {
2491 Uid.Proc ps = ent.getValue();
2492 long userTime;
2493 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002494 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002496 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497
2498 userTime = ps.getUserTime(which);
2499 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002500 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002502 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002503 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002505 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002506 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002507 sb.setLength(0);
2508 sb.append(prefix); sb.append(" Proc ");
2509 sb.append(ent.getKey()); sb.append(":\n");
2510 sb.append(prefix); sb.append(" CPU: ");
2511 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002512 formatTime(sb, systemTime); sb.append("krn ; ");
2513 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002514 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002515 sb.append("\n"); sb.append(prefix); sb.append(" ");
2516 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002517 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002518 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002519 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002520 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002521 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002522 pw.print(prefix); pw.print(" * Killed for ");
2523 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2524 pw.print("wake lock");
2525 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2526 pw.print("cpu");
2527 } else {
2528 pw.print("unknown");
2529 }
2530 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002531 TimeUtils.formatDuration(ew.usedTime, pw);
2532 pw.print(" over ");
2533 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002534 if (ew.overTime != 0) {
2535 pw.print(" (");
2536 pw.print((ew.usedTime*100)/ew.overTime);
2537 pw.println("%)");
2538 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002539 }
2540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 uidActivity = true;
2542 }
2543 }
2544 }
2545
2546 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2547 if (packageStats.size() > 0) {
2548 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2549 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002550 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 boolean apkActivity = false;
2552 Uid.Pkg ps = ent.getValue();
2553 int wakeups = ps.getWakeups(which);
2554 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002555 pw.print(prefix); pw.print(" ");
2556 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 apkActivity = true;
2558 }
2559 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2560 if (serviceStats.size() > 0) {
2561 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2562 : serviceStats.entrySet()) {
2563 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2564 long startTime = ss.getStartTime(batteryUptime, which);
2565 int starts = ss.getStarts(which);
2566 int launches = ss.getLaunches(which);
2567 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002568 sb.setLength(0);
2569 sb.append(prefix); sb.append(" Service ");
2570 sb.append(sent.getKey()); sb.append(":\n");
2571 sb.append(prefix); sb.append(" Created for: ");
2572 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002573 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002574 sb.append(prefix); sb.append(" Starts: ");
2575 sb.append(starts);
2576 sb.append(", launches: "); sb.append(launches);
2577 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 apkActivity = true;
2579 }
2580 }
2581 }
2582 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002583 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585 uidActivity = true;
2586 }
2587 }
2588 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002589 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
2591 }
2592 }
2593
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002594 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002595 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002596 int diff = oldval ^ newval;
2597 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002598 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002599 for (int i=0; i<descriptions.length; i++) {
2600 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002601 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002602 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002603 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002604 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002605 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002606 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2607 didWake = true;
2608 pw.print("=");
2609 if (longNames) {
2610 UserHandle.formatUid(pw, wakelockTag.uid);
2611 pw.print(":\"");
2612 pw.print(wakelockTag.string);
2613 pw.print("\"");
2614 } else {
2615 pw.print(wakelockTag.poolIdx);
2616 }
2617 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002618 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002619 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002620 pw.print("=");
2621 int val = (newval&bd.mask)>>bd.shift;
2622 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002623 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002624 } else {
2625 pw.print(val);
2626 }
2627 }
2628 }
2629 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002630 if (!didWake && wakelockTag != null) {
2631 pw.print(longNames ? "wake_lock=" : "w=");
2632 if (longNames) {
2633 UserHandle.formatUid(pw, wakelockTag.uid);
2634 pw.print(":\"");
2635 pw.print(wakelockTag.string);
2636 pw.print("\"");
2637 } else {
2638 pw.print(wakelockTag.poolIdx);
2639 }
2640 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002641 }
2642
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002643 public void prepareForDumpLocked() {
2644 }
2645
2646 public static class HistoryPrinter {
2647 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002648 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002649 int oldStatus = -1;
2650 int oldHealth = -1;
2651 int oldPlug = -1;
2652 int oldTemp = -1;
2653 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002654 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002655
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002656 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2657 if (!checkin) {
2658 pw.print(" ");
2659 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002660 pw.print(" (");
2661 pw.print(rec.numReadInts);
2662 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002663 } else {
2664 if (lastTime < 0) {
2665 pw.print("@");
2666 pw.print(rec.time-now);
2667 } else {
2668 pw.print(rec.time-lastTime);
2669 }
2670 lastTime = rec.time;
2671 }
2672 if (rec.cmd == HistoryItem.CMD_START) {
2673 if (checkin) {
2674 pw.print(":");
2675 }
2676 pw.println("START");
2677 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2678 if (checkin) {
2679 pw.print(":");
2680 }
2681 pw.println("*OVERFLOW*");
2682 } else {
2683 if (!checkin) {
2684 if (rec.batteryLevel < 10) pw.print("00");
2685 else if (rec.batteryLevel < 100) pw.print("0");
2686 pw.print(rec.batteryLevel);
2687 pw.print(" ");
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002688 if (rec.states < 0) ;
2689 else if (rec.states < 0x10) pw.print("0000000");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002690 else if (rec.states < 0x100) pw.print("000000");
2691 else if (rec.states < 0x1000) pw.print("00000");
2692 else if (rec.states < 0x10000) pw.print("0000");
2693 else if (rec.states < 0x100000) pw.print("000");
2694 else if (rec.states < 0x1000000) pw.print("00");
2695 else if (rec.states < 0x10000000) pw.print("0");
2696 pw.print(Integer.toHexString(rec.states));
2697 } else {
2698 if (oldLevel != rec.batteryLevel) {
2699 oldLevel = rec.batteryLevel;
2700 pw.print(",Bl="); pw.print(rec.batteryLevel);
2701 }
2702 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002703 if (oldStatus != rec.batteryStatus) {
2704 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002705 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002706 switch (oldStatus) {
2707 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002708 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002709 break;
2710 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002711 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002712 break;
2713 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002714 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002715 break;
2716 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002717 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002718 break;
2719 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002720 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002721 break;
2722 default:
2723 pw.print(oldStatus);
2724 break;
2725 }
2726 }
2727 if (oldHealth != rec.batteryHealth) {
2728 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002729 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002730 switch (oldHealth) {
2731 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002732 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002733 break;
2734 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002735 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002736 break;
2737 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002738 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002739 break;
2740 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002741 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002742 break;
2743 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002744 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002745 break;
2746 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002747 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002748 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002749 case BatteryManager.BATTERY_HEALTH_COLD:
2750 pw.print(checkin ? "c" : "cold");
2751 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002752 default:
2753 pw.print(oldHealth);
2754 break;
2755 }
2756 }
2757 if (oldPlug != rec.batteryPlugType) {
2758 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002759 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002760 switch (oldPlug) {
2761 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002762 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002763 break;
2764 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002765 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002766 break;
2767 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002768 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002769 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002770 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002771 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002772 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002773 default:
2774 pw.print(oldPlug);
2775 break;
2776 }
2777 }
2778 if (oldTemp != rec.batteryTemperature) {
2779 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002780 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002781 pw.print(oldTemp);
2782 }
2783 if (oldVolt != rec.batteryVoltage) {
2784 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002785 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002786 pw.print(oldVolt);
2787 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002788 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002789 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002790 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002791 pw.print(checkin ? "," : " ");
2792 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
2793 pw.print("+");
2794 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
2795 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002796 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002797 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
2798 : HISTORY_EVENT_NAMES;
2799 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
2800 | HistoryItem.EVENT_FLAG_FINISH);
2801 if (idx >= 0 && idx < eventNames.length) {
2802 pw.print(eventNames[idx]);
2803 } else {
2804 pw.print(checkin ? "Ev" : "event");
2805 pw.print(idx);
2806 }
2807 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002808 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002809 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002810 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002811 UserHandle.formatUid(pw, rec.eventTag.uid);
2812 pw.print(":\"");
2813 pw.print(rec.eventTag.string);
2814 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002815 }
2816 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002817 pw.println();
2818 }
2819 oldState = rec.states;
2820 }
2821 }
2822
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002823 private void printSizeValue(PrintWriter pw, long size) {
2824 float result = size;
2825 String suffix = "";
2826 if (result >= 10*1024) {
2827 suffix = "KB";
2828 result = result / 1024;
2829 }
2830 if (result >= 10*1024) {
2831 suffix = "MB";
2832 result = result / 1024;
2833 }
2834 if (result >= 10*1024) {
2835 suffix = "GB";
2836 result = result / 1024;
2837 }
2838 if (result >= 10*1024) {
2839 suffix = "TB";
2840 result = result / 1024;
2841 }
2842 if (result >= 10*1024) {
2843 suffix = "PB";
2844 result = result / 1024;
2845 }
2846 pw.print((int)result);
2847 pw.print(suffix);
2848 }
2849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 /**
2851 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2852 *
2853 * @param pw a Printer to receive the dump output.
2854 */
2855 @SuppressWarnings("unused")
Dianne Hackborn099bc622014-01-22 13:39:16 -08002856 public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid,
2857 boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002858 prepareForDumpLocked();
2859
2860 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2861
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002862 final HistoryItem rec = new HistoryItem();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002863 final long historyTotalSize = getHistoryTotalSize();
2864 final long historyUsedSize = getHistoryUsedSize();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002865 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002866 try {
2867 pw.print("Battery History (");
2868 pw.print((100*historyUsedSize)/historyTotalSize);
2869 pw.print("% used, ");
2870 printSizeValue(pw, historyUsedSize);
2871 pw.print(" used of ");
2872 printSizeValue(pw, historyTotalSize);
2873 pw.print(", ");
2874 pw.print(getHistoryStringPoolSize());
2875 pw.print(" strings using ");
2876 printSizeValue(pw, getHistoryStringPoolBytes());
2877 pw.println("):");
2878 HistoryPrinter hprinter = new HistoryPrinter();
2879 while (getNextHistoryLocked(rec)) {
2880 hprinter.printNextItem(pw, rec, now, false);
2881 }
2882 pw.println();
2883 } finally {
2884 finishIteratingHistoryLocked();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002885 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002886 }
2887
2888 if (startIteratingOldHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002889 try {
2890 pw.println("Old battery History:");
2891 HistoryPrinter hprinter = new HistoryPrinter();
2892 while (getNextOldHistoryLocked(rec)) {
2893 hprinter.printNextItem(pw, rec, now, false);
2894 }
2895 pw.println();
2896 } finally {
2897 finishIteratingOldHistoryLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002898 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002899 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002900
2901 if (historyOnly) {
2902 return;
2903 }
2904
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002905 SparseArray<? extends Uid> uidStats = getUidStats();
2906 final int NU = uidStats.size();
2907 boolean didPid = false;
2908 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002909 for (int i=0; i<NU; i++) {
2910 Uid uid = uidStats.valueAt(i);
2911 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2912 if (pids != null) {
2913 for (int j=0; j<pids.size(); j++) {
2914 Uid.Pid pid = pids.valueAt(j);
2915 if (!didPid) {
2916 pw.println("Per-PID Stats:");
2917 didPid = true;
2918 }
2919 long time = pid.mWakeSum + (pid.mWakeStart != 0
2920 ? (nowRealtime - pid.mWakeStart) : 0);
2921 pw.print(" PID "); pw.print(pids.keyAt(j));
2922 pw.print(" wake time: ");
2923 TimeUtils.formatDuration(time, pw);
2924 pw.println("");
2925 }
2926 }
2927 }
2928 if (didPid) {
2929 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002930 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002931
2932 if (!isUnpluggedOnly) {
2933 pw.println("Statistics since last charge:");
2934 pw.println(" System starts: " + getStartCount()
2935 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002936 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002937 pw.println("");
2938 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002939 pw.println("Statistics since last unplugged:");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002940 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
2942
2943 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002944 public void dumpCheckinLocked(Context context,
Dianne Hackborn49021f52013-09-04 18:03:40 -07002945 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
Dianne Hackborn099bc622014-01-22 13:39:16 -08002946 boolean includeHistory, boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002947 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002949 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2950
Dianne Hackborn099bc622014-01-22 13:39:16 -08002951 if (includeHistory || historyOnly) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07002952 final HistoryItem rec = new HistoryItem();
2953 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002954 try {
2955 for (int i=0; i<getHistoryStringPoolSize(); i++) {
2956 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2957 pw.print(HISTORY_STRING_POOL); pw.print(',');
2958 pw.print(i);
2959 pw.print(',');
2960 pw.print(getHistoryTagPoolString(i));
2961 pw.print(',');
2962 pw.print(getHistoryTagPoolUid(i));
2963 pw.println();
2964 }
2965 HistoryPrinter hprinter = new HistoryPrinter();
2966 while (getNextHistoryLocked(rec)) {
2967 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2968 pw.print(HISTORY_DATA); pw.print(',');
2969 hprinter.printNextItem(pw, rec, now, true);
2970 }
2971 } finally {
2972 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002973 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002974 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002975 }
2976
Dianne Hackborn099bc622014-01-22 13:39:16 -08002977 if (historyOnly) {
2978 return;
2979 }
2980
Dianne Hackborne4a59512010-12-07 11:08:07 -08002981 if (apps != null) {
2982 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2983 for (int i=0; i<apps.size(); i++) {
2984 ApplicationInfo ai = apps.get(i);
2985 ArrayList<String> pkgs = uids.get(ai.uid);
2986 if (pkgs == null) {
2987 pkgs = new ArrayList<String>();
2988 uids.put(ai.uid, pkgs);
2989 }
2990 pkgs.add(ai.packageName);
2991 }
2992 SparseArray<? extends Uid> uidStats = getUidStats();
2993 final int NU = uidStats.size();
2994 String[] lineArgs = new String[2];
2995 for (int i=0; i<NU; i++) {
2996 int uid = uidStats.keyAt(i);
2997 ArrayList<String> pkgs = uids.get(uid);
2998 if (pkgs != null) {
2999 for (int j=0; j<pkgs.size(); j++) {
3000 lineArgs[0] = Integer.toString(uid);
3001 lineArgs[1] = pkgs.get(j);
3002 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3003 (Object[])lineArgs);
3004 }
3005 }
3006 }
3007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 if (isUnpluggedOnly) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003009 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 }
3011 else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003012 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
3013 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
3015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016}