blob: 1ce958bfef1664c0e7ca7fd9a88025b8705bb0e4 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Printer;
31import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070032import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080033import com.android.internal.os.BatterySipper;
34import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36/**
37 * A class providing access to battery usage statistics, including information on
38 * wakelocks, processes, packages, and services. All times are represented in microseconds
39 * except where indicated otherwise.
40 * @hide
41 */
42public abstract class BatteryStats implements Parcelable {
43
44 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070045
46 /** @hide */
47 public static final String SERVICE_NAME = "batterystats";
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 /**
50 * A constant indicating a partial wake lock timer.
51 */
52 public static final int WAKE_TYPE_PARTIAL = 0;
53
54 /**
55 * A constant indicating a full wake lock timer.
56 */
57 public static final int WAKE_TYPE_FULL = 1;
58
59 /**
60 * A constant indicating a window wake lock timer.
61 */
62 public static final int WAKE_TYPE_WINDOW = 2;
63
64 /**
65 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 */
67 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070068
69 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070070 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070071 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070072 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070073
74 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070075 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070076 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070077 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070080 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070081 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070082 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
Robert Greenwalt5347bd42009-05-13 15:10:16 -070084 /**
85 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070086 */
87 public static final int WIFI_MULTICAST_ENABLED = 7;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070090 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 */
92 public static final int AUDIO_TURNED_ON = 7;
93
94 /**
95 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070096 */
97 public static final int VIDEO_TURNED_ON = 8;
98
99 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800100 * A constant indicating a vibrator on timer
101 */
102 public static final int VIBRATOR_ON = 9;
103
104 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700105 * A constant indicating a foreground activity timer
106 */
107 public static final int FOREGROUND_ACTIVITY = 10;
108
109 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700110 * A constant indicating a wifi batched scan is active
111 */
112 public static final int WIFI_BATCHED_SCAN = 11;
113
114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * Include all of the data in the stats, including previously saved data.
116 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700117 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 /**
120 * Include only the last run in the stats.
121 */
122 public static final int STATS_LAST = 1;
123
124 /**
125 * Include only the current run in the stats.
126 */
127 public static final int STATS_CURRENT = 2;
128
129 /**
130 * Include only the run since the last time the device was unplugged in the stats.
131 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700132 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700133
134 // NOTE: Update this list if you add/change any stats above.
135 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700136 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700137 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139 /**
140 * Bump the version on this if the checkin format changes.
141 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700142 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700143
144 private static final long BYTES_PER_KB = 1024;
145 private static final long BYTES_PER_MB = 1048576; // 1024^2
146 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Dianne Hackborne4a59512010-12-07 11:08:07 -0800149 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700151 private static final String PROCESS_DATA = "pr";
152 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800153 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700154 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700155 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700156 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700157 private static final String NETWORK_DATA = "nt";
158 private static final String USER_ACTIVITY_DATA = "ua";
159 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800160 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700161 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700162 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700163 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800164 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800165 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700166 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700167 private static final String SCREEN_BRIGHTNESS_DATA = "br";
168 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700169 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700170 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
171 private static final String DATA_CONNECTION_TIME_DATA = "dct";
172 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800173 private static final String POWER_USE_SUMMARY_DATA = "pws";
174 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700176 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private final Formatter mFormatter = new Formatter(mFormatBuilder);
178
179 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700180 * State for keeping track of counting information.
181 */
182 public static abstract class Counter {
183
184 /**
185 * Returns the count associated with this Counter for the
186 * selected type of statistics.
187 *
188 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
189 */
Evan Millarc64edde2009-04-18 12:26:32 -0700190 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700191
192 /**
193 * Temporary for debugging.
194 */
195 public abstract void logState(Printer pw, String prefix);
196 }
197
198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * State for keeping track of timing information.
200 */
201 public static abstract class Timer {
202
203 /**
204 * Returns the count associated with this Timer for the
205 * selected type of statistics.
206 *
207 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
208 */
Evan Millarc64edde2009-04-18 12:26:32 -0700209 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 /**
212 * Returns the total time in microseconds associated with this Timer for the
213 * selected type of statistics.
214 *
215 * @param batteryRealtime system realtime on battery in microseconds
216 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
217 * @return a time in microseconds
218 */
Evan Millarc64edde2009-04-18 12:26:32 -0700219 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
222 * Temporary for debugging.
223 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700224 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
226
227 /**
228 * The statistics associated with a particular uid.
229 */
230 public static abstract class Uid {
231
232 /**
233 * Returns a mapping containing wakelock statistics.
234 *
235 * @return a Map from Strings to Uid.Wakelock objects.
236 */
237 public abstract Map<String, ? extends Wakelock> getWakelockStats();
238
239 /**
240 * The statistics associated with a particular wake lock.
241 */
242 public static abstract class Wakelock {
243 public abstract Timer getWakeTime(int type);
244 }
245
246 /**
247 * Returns a mapping containing sensor statistics.
248 *
249 * @return a Map from Integer sensor ids to Uid.Sensor objects.
250 */
251 public abstract Map<Integer, ? extends Sensor> getSensorStats();
252
253 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700254 * Returns a mapping containing active process data.
255 */
256 public abstract SparseArray<? extends Pid> getPidStats();
257
258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * Returns a mapping containing process statistics.
260 *
261 * @return a Map from Strings to Uid.Proc objects.
262 */
263 public abstract Map<String, ? extends Proc> getProcessStats();
264
265 /**
266 * Returns a mapping containing package statistics.
267 *
268 * @return a Map from Strings to Uid.Pkg objects.
269 */
270 public abstract Map<String, ? extends Pkg> getPackageStats();
271
272 /**
273 * {@hide}
274 */
275 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700276
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700277 public abstract void noteWifiRunningLocked();
278 public abstract void noteWifiStoppedLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700279 public abstract void noteFullWifiLockAcquiredLocked();
280 public abstract void noteFullWifiLockReleasedLocked();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700281 public abstract void noteWifiScanStartedLocked();
282 public abstract void noteWifiScanStoppedLocked();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700283 public abstract void noteWifiBatchedScanStartedLocked(int csph);
284 public abstract void noteWifiBatchedScanStoppedLocked();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700285 public abstract void noteWifiMulticastEnabledLocked();
286 public abstract void noteWifiMulticastDisabledLocked();
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700287 public abstract void noteAudioTurnedOnLocked();
288 public abstract void noteAudioTurnedOffLocked();
289 public abstract void noteVideoTurnedOnLocked();
290 public abstract void noteVideoTurnedOffLocked();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700291 public abstract void noteActivityResumedLocked();
292 public abstract void noteActivityPausedLocked();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700293 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700294 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700295 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalta029ea12013-09-25 16:38:12 -0700296 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700297 public abstract long getWifiMulticastTime(long batteryRealtime,
298 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700299 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
300 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700301 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800302 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Robert Greenwalta029ea12013-09-25 16:38:12 -0700304 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
305
Dianne Hackborn617f8772009-03-31 15:04:46 -0700306 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700307 * Note that these must match the constants in android.os.PowerManager.
308 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
309 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700310 */
311 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700312 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700313 };
314
Jeff Browndf693de2012-07-27 12:03:38 -0700315 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700316
Dianne Hackborn617f8772009-03-31 15:04:46 -0700317 public abstract void noteUserActivityLocked(int type);
318 public abstract boolean hasUserActivity();
319 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700320
321 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800322 public abstract long getNetworkActivityBytes(int type, int which);
323 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800326 /*
327 * FIXME: it's not correct to use this magic value because it
328 * could clash with a sensor handle (which are defined by
329 * the sensor HAL, and therefore out of our control
330 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 // Magic sensor number for the GPS.
332 public static final int GPS = -10000;
333
334 public abstract int getHandle();
335
336 public abstract Timer getSensorTime();
337 }
338
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700339 public class Pid {
340 public long mWakeSum;
341 public long mWakeStart;
342 }
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The statistics associated with a particular process.
346 */
347 public static abstract class Proc {
348
Dianne Hackborn287952c2010-09-22 22:34:31 -0700349 public static class ExcessivePower {
350 public static final int TYPE_WAKE = 1;
351 public static final int TYPE_CPU = 2;
352
353 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700354 public long overTime;
355 public long usedTime;
356 }
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800359 * Returns true if this process is still active in the battery stats.
360 */
361 public abstract boolean isActive();
362
363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * Returns the total time (in 1/100 sec) spent executing in user code.
365 *
366 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
367 */
368 public abstract long getUserTime(int which);
369
370 /**
371 * Returns the total time (in 1/100 sec) spent executing in system code.
372 *
373 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
374 */
375 public abstract long getSystemTime(int which);
376
377 /**
378 * Returns the number of times the process has been started.
379 *
380 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
381 */
382 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700383
384 /**
385 * Returns the cpu time spent in microseconds while the process was in the foreground.
386 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
387 * @return foreground cpu time in microseconds
388 */
389 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700390
391 /**
392 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
393 * @param speedStep the index of the CPU speed. This is not the actual speed of the
394 * CPU.
395 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
396 * @see BatteryStats#getCpuSpeedSteps()
397 */
398 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700399
Dianne Hackborn287952c2010-09-22 22:34:31 -0700400 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700401
Dianne Hackborn287952c2010-09-22 22:34:31 -0700402 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404
405 /**
406 * The statistics associated with a particular package.
407 */
408 public static abstract class Pkg {
409
410 /**
411 * Returns the number of times this package has done something that could wake up the
412 * device from sleep.
413 *
414 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
415 */
416 public abstract int getWakeups(int which);
417
418 /**
419 * Returns a mapping containing service statistics.
420 */
421 public abstract Map<String, ? extends Serv> getServiceStats();
422
423 /**
424 * The statistics associated with a particular service.
425 */
426 public abstract class Serv {
427
428 /**
429 * Returns the amount of time spent started.
430 *
431 * @param batteryUptime elapsed uptime on battery in microseconds.
432 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
433 * @return
434 */
435 public abstract long getStartTime(long batteryUptime, int which);
436
437 /**
438 * Returns the total number of times startService() has been called.
439 *
440 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
441 */
442 public abstract int getStarts(int which);
443
444 /**
445 * Returns the total number times the service has been launched.
446 *
447 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
448 */
449 public abstract int getLaunches(int which);
450 }
451 }
452 }
453
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700454 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700455 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700456
457 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800458
459 // The command codes 0-3 can be written with delta updates; all others require
460 // that a full entry be written.
461 public static final byte CMD_UPDATE = 0;
462 public static final byte CMD_EVENT = 1;
463 public static final byte CMD_NULL = -1;
464 public static final byte CMD_START = 4;
465 public static final byte CMD_OVERFLOW = 5;
466
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700467 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700468
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800469 /**
470 * Return whether the command code is a delta data update.
471 */
472 public static boolean isDeltaData(byte cmd) {
473 return cmd >= 0 && cmd <= 3;
474 }
475
476 /**
477 * Return whether the command code is a delta data update.
478 */
479 public boolean isDeltaData() {
480 return cmd >= 0 && cmd <= 3;
481 }
482
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700483 public byte batteryLevel;
484 public byte batteryStatus;
485 public byte batteryHealth;
486 public byte batteryPlugType;
487
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900488 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700489 public char batteryVoltage;
490
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700491 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700492 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800493 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700494 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800495 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
496 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700497 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800498 public static final int STATE_PHONE_STATE_SHIFT = 6;
499 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700500 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800501 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
502 public static final int STATE_DATA_CONNECTION_MASK = 0x1f;
503
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700504 // These states always appear directly in the first int token
505 // of a delta change; they should be ones that change relatively
506 // frequently.
507 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
508 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700509 public static final int STATE_GPS_ON_FLAG = 1<<28;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700510 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
511 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
512 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
Nick Pelly6ccaa542012-06-15 15:22:47 -0700513 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700514 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
515 // These are on the lower bits used for the command; if they change
516 // we need to write another int of data.
517 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
518 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
519 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
520 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
521 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
522 public static final int STATE_WIFI_ON_FLAG = 1<<17;
523 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700524
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700525 public static final int MOST_INTERESTING_STATES =
526 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
527 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
528
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700529 public int states;
530
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800531 public static final int EVENT_NONE = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800532 public static final int EVENT_PROC_STARTED = 1;
533 public static final int EVENT_PROC_FINISHED = 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800534
535 // For CMD_EVENT.
536 public int eventCode;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800537 public String eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800538 public int eventNameIdx; // only filled in when iterating.
539 public int eventUid;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800540
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700541 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700542 }
543
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700544 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700545 this.time = time;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700546 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700547 }
548
549 public int describeContents() {
550 return 0;
551 }
552
553 public void writeToParcel(Parcel dest, int flags) {
554 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700555 int bat = (((int)cmd)&0xff)
556 | ((((int)batteryLevel)<<8)&0xff00)
557 | ((((int)batteryStatus)<<16)&0xf0000)
558 | ((((int)batteryHealth)<<20)&0xf00000)
559 | ((((int)batteryPlugType)<<24)&0xf000000);
560 dest.writeInt(bat);
561 bat = (((int)batteryTemperature)&0xffff)
562 | ((((int)batteryVoltage)<<16)&0xffff0000);
563 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700564 dest.writeInt(states);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800565 if (cmd == CMD_EVENT) {
566 dest.writeInt(eventCode);
567 dest.writeInt(eventUid);
568 dest.writeString(eventName);
569 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700570 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700571
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800572 public void readFromParcel(Parcel src) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700573 int bat = src.readInt();
574 cmd = (byte)(bat&0xff);
575 batteryLevel = (byte)((bat>>8)&0xff);
576 batteryStatus = (byte)((bat>>16)&0xf);
577 batteryHealth = (byte)((bat>>20)&0xf);
578 batteryPlugType = (byte)((bat>>24)&0xf);
579 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900580 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700581 batteryVoltage = (char)((bat>>16)&0xffff);
582 states = src.readInt();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800583 if (cmd == CMD_EVENT) {
584 eventCode = src.readInt();
585 eventUid = src.readInt();
586 eventName = src.readString();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800587 eventNameIdx = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700588 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800589 eventCode = EVENT_NONE;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700590 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700591 }
592
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700593 public void clear() {
594 time = 0;
595 cmd = CMD_NULL;
596 batteryLevel = 0;
597 batteryStatus = 0;
598 batteryHealth = 0;
599 batteryPlugType = 0;
600 batteryTemperature = 0;
601 batteryVoltage = 0;
602 states = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800603 eventCode = EVENT_NONE;
604 eventUid = 0;
605 eventName = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700606 }
607
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700608 public void setTo(HistoryItem o) {
609 time = o.time;
610 cmd = o.cmd;
611 batteryLevel = o.batteryLevel;
612 batteryStatus = o.batteryStatus;
613 batteryHealth = o.batteryHealth;
614 batteryPlugType = o.batteryPlugType;
615 batteryTemperature = o.batteryTemperature;
616 batteryVoltage = o.batteryVoltage;
617 states = o.states;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800618 eventCode = o.eventCode;
619 eventUid = o.eventUid;
620 eventName = o.eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800621 eventNameIdx = o.eventNameIdx;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700622 }
623
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800624 public void setTo(long time, byte cmd, int eventCode, int eventUid, String eventName,
625 HistoryItem o) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700626 this.time = time;
627 this.cmd = cmd;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800628 this.eventCode = eventCode;
629 this.eventUid = eventUid;
630 this.eventName = eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800631 this.eventNameIdx = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700632 batteryLevel = o.batteryLevel;
633 batteryStatus = o.batteryStatus;
634 batteryHealth = o.batteryHealth;
635 batteryPlugType = o.batteryPlugType;
636 batteryTemperature = o.batteryTemperature;
637 batteryVoltage = o.batteryVoltage;
638 states = o.states;
639 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700640
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800641 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700642 return batteryLevel == o.batteryLevel
643 && batteryStatus == o.batteryStatus
644 && batteryHealth == o.batteryHealth
645 && batteryPlugType == o.batteryPlugType
646 && batteryTemperature == o.batteryTemperature
647 && batteryVoltage == o.batteryVoltage
648 && states == o.states;
649 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800650
651 public boolean same(HistoryItem o) {
652 if (!sameNonEvent(o) || eventCode != o.eventCode || eventUid != o.eventUid) {
653 return false;
654 }
655 if (eventName == o.eventName) {
656 return true;
657 }
658 return eventName != null && o.eventName != null && eventName.equals(o.eventName);
659 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700660 }
661
662 public static final class BitDescription {
663 public final int mask;
664 public final int shift;
665 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800666 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700667 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800668 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700669
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800670 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700671 this.mask = mask;
672 this.shift = -1;
673 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800674 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700675 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800676 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700677 }
678
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800679 public BitDescription(int mask, int shift, String name, String shortName,
680 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700681 this.mask = mask;
682 this.shift = shift;
683 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800684 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700685 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800686 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700687 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700688 }
689
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700690 public abstract boolean startIteratingHistoryLocked();
691
Dianne Hackborn099bc622014-01-22 13:39:16 -0800692 public abstract int getHistoryStringPoolSize();
693
694 public abstract String getHistoryStringPoolItem(int index);
695
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700696 public abstract boolean getNextHistoryLocked(HistoryItem out);
697
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700698 public abstract void finishIteratingHistoryLocked();
699
700 public abstract boolean startIteratingOldHistoryLocked();
701
702 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
703
704 public abstract void finishIteratingOldHistoryLocked();
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700707 * Return the base time offset for the battery history.
708 */
709 public abstract long getHistoryBaseTime();
710
711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * Returns the number of times the device has been started.
713 */
714 public abstract int getStartCount();
715
716 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700717 * 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 -0800718 * running on battery.
719 *
720 * {@hide}
721 */
722 public abstract long getScreenOnTime(long batteryRealtime, int which);
723
Dianne Hackborn617f8772009-03-31 15:04:46 -0700724 public static final int SCREEN_BRIGHTNESS_DARK = 0;
725 public static final int SCREEN_BRIGHTNESS_DIM = 1;
726 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
727 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
728 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
729
730 static final String[] SCREEN_BRIGHTNESS_NAMES = {
731 "dark", "dim", "medium", "light", "bright"
732 };
733
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800734 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
735 "0", "1", "2", "3", "4"
736 };
737
Dianne Hackborn617f8772009-03-31 15:04:46 -0700738 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
739
740 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700741 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700742 * the given brightness
743 *
744 * {@hide}
745 */
746 public abstract long getScreenBrightnessTime(int brightnessBin,
747 long batteryRealtime, int which);
748
749 public abstract int getInputEventCount(int which);
750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700752 * 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 -0800753 * running on battery.
754 *
755 * {@hide}
756 */
757 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700758
759 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700760 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700761 * the given signal strength.
762 *
763 * {@hide}
764 */
765 public abstract long getPhoneSignalStrengthTime(int strengthBin,
766 long batteryRealtime, int which);
767
Dianne Hackborn617f8772009-03-31 15:04:46 -0700768 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700769 * Returns the time in microseconds that the phone has been trying to
770 * acquire a signal.
771 *
772 * {@hide}
773 */
774 public abstract long getPhoneSignalScanningTime(
775 long batteryRealtime, int which);
776
777 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700778 * Returns the number of times the phone has entered the given signal strength.
779 *
780 * {@hide}
781 */
782 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
783
Dianne Hackborn627bba72009-03-24 22:32:56 -0700784 public static final int DATA_CONNECTION_NONE = 0;
785 public static final int DATA_CONNECTION_GPRS = 1;
786 public static final int DATA_CONNECTION_EDGE = 2;
787 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700788 public static final int DATA_CONNECTION_CDMA = 4;
789 public static final int DATA_CONNECTION_EVDO_0 = 5;
790 public static final int DATA_CONNECTION_EVDO_A = 6;
791 public static final int DATA_CONNECTION_1xRTT = 7;
792 public static final int DATA_CONNECTION_HSDPA = 8;
793 public static final int DATA_CONNECTION_HSUPA = 9;
794 public static final int DATA_CONNECTION_HSPA = 10;
795 public static final int DATA_CONNECTION_IDEN = 11;
796 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700797 public static final int DATA_CONNECTION_LTE = 13;
798 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800799 public static final int DATA_CONNECTION_HSPAP = 15;
800 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700801
Dianne Hackborn627bba72009-03-24 22:32:56 -0700802 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700803 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700804 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800805 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700806 };
807
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700808 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700809
810 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700811 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700812 * the given data connection.
813 *
814 * {@hide}
815 */
816 public abstract long getPhoneDataConnectionTime(int dataType,
817 long batteryRealtime, int which);
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700820 * Returns the number of times the phone has entered the given data
821 * connection type.
822 *
823 * {@hide}
824 */
825 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700826
827 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
828 = new BitDescription[] {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800829 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
830 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
831 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
832 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
833 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
834 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
835 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
836 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
837 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
838 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
839 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
840 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
841 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
842 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
843 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700844 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800845 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
846 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700847 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800848 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
849 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
850 "0", "1", "2", "3", "4"
851 }),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700852 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800853 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
854 new String[] {"in", "out", "emergency", "off"},
855 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700856 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800857 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
858 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700859 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700860
861 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700862 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700863 * running on battery.
864 *
865 * {@hide}
866 */
867 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700868
869 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700870 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700871 * been in the running state while the device was running on battery.
872 *
873 * {@hide}
874 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700875 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700876
The Android Open Source Project10592532009-03-18 17:39:46 -0700877 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700878 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700879 * running on battery.
880 *
881 * {@hide}
882 */
883 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
884
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800885 public abstract int getBluetoothPingCount();
886
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800887 public static final int NETWORK_MOBILE_RX_DATA = 0;
888 public static final int NETWORK_MOBILE_TX_DATA = 1;
889 public static final int NETWORK_WIFI_RX_DATA = 2;
890 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700891
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800892 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700893
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800894 public abstract long getNetworkActivityBytes(int type, int which);
895 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700896
The Android Open Source Project10592532009-03-18 17:39:46 -0700897 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 * Return whether we are currently running on battery.
899 */
900 public abstract boolean getIsOnBattery();
901
902 /**
903 * Returns a SparseArray containing the statistics for each uid.
904 */
905 public abstract SparseArray<? extends Uid> getUidStats();
906
907 /**
908 * Returns the current battery uptime in microseconds.
909 *
910 * @param curTime the amount of elapsed realtime in microseconds.
911 */
912 public abstract long getBatteryUptime(long curTime);
913
914 /**
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700915 * @deprecated use getRadioDataUptime
916 */
917 public long getRadioDataUptimeMs() {
918 return getRadioDataUptime() / 1000;
919 }
920
921 /**
922 * Returns the time that the radio was on for data transfers.
923 * @return the uptime in microseconds while unplugged
924 */
925 public abstract long getRadioDataUptime();
926
927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * Returns the current battery realtime in microseconds.
929 *
930 * @param curTime the amount of elapsed realtime in microseconds.
931 */
932 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -0700933
934 /**
Evan Millar633a1742009-04-02 16:36:33 -0700935 * Returns the battery percentage level at the last time the device was unplugged from power, or
936 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -0700937 */
Evan Millar633a1742009-04-02 16:36:33 -0700938 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -0700939
940 /**
Evan Millar633a1742009-04-02 16:36:33 -0700941 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
942 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -0700943 */
Evan Millar633a1742009-04-02 16:36:33 -0700944 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945
946 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700947 * Get the amount the battery has discharged since the stats were
948 * last reset after charging, as a lower-end approximation.
949 */
950 public abstract int getLowDischargeAmountSinceCharge();
951
952 /**
953 * Get the amount the battery has discharged since the stats were
954 * last reset after charging, as an upper-end approximation.
955 */
956 public abstract int getHighDischargeAmountSinceCharge();
957
958 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800959 * Get the amount the battery has discharged while the screen was on,
960 * since the last time power was unplugged.
961 */
962 public abstract int getDischargeAmountScreenOn();
963
964 /**
965 * Get the amount the battery has discharged while the screen was on,
966 * since the last time the device was charged.
967 */
968 public abstract int getDischargeAmountScreenOnSinceCharge();
969
970 /**
971 * Get the amount the battery has discharged while the screen was off,
972 * since the last time power was unplugged.
973 */
974 public abstract int getDischargeAmountScreenOff();
975
976 /**
977 * Get the amount the battery has discharged while the screen was off,
978 * since the last time the device was charged.
979 */
980 public abstract int getDischargeAmountScreenOffSinceCharge();
981
982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 * Returns the total, last, or current battery uptime in microseconds.
984 *
985 * @param curTime the elapsed realtime in microseconds.
986 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
987 */
988 public abstract long computeBatteryUptime(long curTime, int which);
989
990 /**
991 * Returns the total, last, or current battery realtime in microseconds.
992 *
993 * @param curTime the current elapsed realtime in microseconds.
994 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
995 */
996 public abstract long computeBatteryRealtime(long curTime, int which);
997
998 /**
999 * Returns the total, last, or current uptime in microseconds.
1000 *
1001 * @param curTime the current elapsed realtime in microseconds.
1002 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1003 */
1004 public abstract long computeUptime(long curTime, int which);
1005
1006 /**
1007 * Returns the total, last, or current realtime in microseconds.
1008 * *
1009 * @param curTime the current elapsed realtime in microseconds.
1010 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1011 */
1012 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001013
1014 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
Amith Yamasanie43530a2009-08-21 13:11:37 -07001016 /** Returns the number of different speeds that the CPU can run at */
1017 public abstract int getCpuSpeedSteps();
1018
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001019 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1020
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001021 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 long days = seconds / (60 * 60 * 24);
1023 if (days != 0) {
1024 out.append(days);
1025 out.append("d ");
1026 }
1027 long used = days * 60 * 60 * 24;
1028
1029 long hours = (seconds - used) / (60 * 60);
1030 if (hours != 0 || used != 0) {
1031 out.append(hours);
1032 out.append("h ");
1033 }
1034 used += hours * 60 * 60;
1035
1036 long mins = (seconds-used) / 60;
1037 if (mins != 0 || used != 0) {
1038 out.append(mins);
1039 out.append("m ");
1040 }
1041 used += mins * 60;
1042
1043 if (seconds != 0 || used != 0) {
1044 out.append(seconds-used);
1045 out.append("s ");
1046 }
1047 }
1048
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001049 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001051 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 sb.append((time - (sec * 100)) * 10);
1053 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001056 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001058 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 sb.append(time - (sec * 1000));
1060 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
1063 private final String formatRatioLocked(long num, long den) {
1064 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001065 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 float perc = ((float)num) / ((float)den) * 100;
1068 mFormatBuilder.setLength(0);
1069 mFormatter.format("%.1f%%", perc);
1070 return mFormatBuilder.toString();
1071 }
1072
Evan Millar22ac0432009-03-31 11:33:18 -07001073 private final String formatBytesLocked(long bytes) {
1074 mFormatBuilder.setLength(0);
1075
1076 if (bytes < BYTES_PER_KB) {
1077 return bytes + "B";
1078 } else if (bytes < BYTES_PER_MB) {
1079 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1080 return mFormatBuilder.toString();
1081 } else if (bytes < BYTES_PER_GB){
1082 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1083 return mFormatBuilder.toString();
1084 } else {
1085 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1086 return mFormatBuilder.toString();
1087 }
1088 }
1089
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001090 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1091 if (timer != null) {
1092 // Convert from microseconds to milliseconds with rounding
1093 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1094 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1095 return totalTimeMillis;
1096 }
1097 return 0;
1098 }
1099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 *
1102 * @param sb a StringBuilder object.
1103 * @param timer a Timer object contining the wakelock times.
1104 * @param batteryRealtime the current on-battery time in microseconds.
1105 * @param name the name of the wakelock.
1106 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1107 * @param linePrefix a String to be prepended to each line of output.
1108 * @return the line prefix
1109 */
1110 private static final String printWakeLock(StringBuilder sb, Timer timer,
1111 long batteryRealtime, String name, int which, String linePrefix) {
1112
1113 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001114 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115
Evan Millarc64edde2009-04-18 12:26:32 -07001116 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (totalTimeMillis != 0) {
1118 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001119 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001120 if (name != null) {
1121 sb.append(name);
1122 sb.append(' ');
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 sb.append('(');
1125 sb.append(count);
1126 sb.append(" times)");
1127 return ", ";
1128 }
1129 }
1130 return linePrefix;
1131 }
1132
1133 /**
1134 * Checkin version of wakelock printer. Prints simple comma-separated list.
1135 *
1136 * @param sb a StringBuilder object.
1137 * @param timer a Timer object contining the wakelock times.
1138 * @param now the current time in microseconds.
1139 * @param name the name of the wakelock.
1140 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1141 * @param linePrefix a String to be prepended to each line of output.
1142 * @return the line prefix
1143 */
1144 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001145 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 long totalTimeMicros = 0;
1147 int count = 0;
1148 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001149 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1150 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 sb.append(linePrefix);
1153 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1154 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001155 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 sb.append(count);
1157 return ",";
1158 }
1159
1160 /**
1161 * Dump a comma-separated line of values for terse checkin mode.
1162 *
1163 * @param pw the PageWriter to dump log to
1164 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1165 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1166 * @param args type-dependent data arguments
1167 */
1168 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1169 Object... args ) {
1170 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1171 pw.print(uid); pw.print(',');
1172 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001173 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174
1175 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001176 pw.print(',');
1177 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001179 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
1182 /**
1183 * Checkin server version of dump to produce more compact, computer-readable log.
1184 *
1185 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001187 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1189 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1190 final long batteryUptime = getBatteryUptime(rawUptime);
1191 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1192 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1193 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1194 final long totalRealtime = computeRealtime(rawRealtime, which);
1195 final long totalUptime = computeUptime(rawUptime, which);
1196 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1197 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001198 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001199 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001200 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201
1202 StringBuilder sb = new StringBuilder(128);
1203
Evan Millar22ac0432009-03-31 11:33:18 -07001204 SparseArray<? extends Uid> uidStats = getUidStats();
1205 final int NU = uidStats.size();
1206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 // Dump "battery" stat
1210 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001211 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001212 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1213 totalRealtime / 1000, totalUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001215 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001216 long fullWakeLockTimeTotal = 0;
1217 long partialWakeLockTimeTotal = 0;
1218
1219 for (int iu = 0; iu < NU; iu++) {
1220 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001221
Evan Millar22ac0432009-03-31 11:33:18 -07001222 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1223 if (wakelocks.size() > 0) {
1224 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1225 : wakelocks.entrySet()) {
1226 Uid.Wakelock wl = ent.getValue();
1227
1228 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1229 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001230 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001231 }
1232
1233 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1234 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001235 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001236 batteryRealtime, which);
1237 }
1238 }
1239 }
1240 }
1241
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1243 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1244 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1245 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1246 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1247 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1248 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1249 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1250
1251 // Dump network stats
1252 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1253 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1254 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 // Dump misc stats
1257 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001258 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001259 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001260 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001261 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1262 getInputEventCount(which));
1263
1264 // Dump screen brightness stats
1265 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1266 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1267 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1268 }
1269 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001270
Dianne Hackborn627bba72009-03-24 22:32:56 -07001271 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001272 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1273 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001274 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1275 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001276 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001277 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1278 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001279 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001280 args[i] = getPhoneSignalStrengthCount(i, which);
1281 }
1282 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001283
1284 // Dump network type stats
1285 args = new Object[NUM_DATA_CONNECTION_TYPES];
1286 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1287 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1288 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001289 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1290 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1291 args[i] = getPhoneDataConnectionCount(i, which);
1292 }
1293 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001294
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001295 if (which == STATS_SINCE_UNPLUGGED) {
Evan Millare84de8d2009-04-02 22:16:12 -07001296 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001297 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001300 if (which == STATS_SINCE_UNPLUGGED) {
1301 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1302 getDischargeStartLevel()-getDischargeCurrentLevel(),
1303 getDischargeStartLevel()-getDischargeCurrentLevel(),
1304 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1305 } else {
1306 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1307 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1308 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1309 }
1310
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001311 if (reqUid < 0) {
1312 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1313 if (kernelWakelocks.size() > 0) {
1314 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1315 sb.setLength(0);
1316 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1317
1318 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1319 sb.toString());
1320 }
Evan Millarc64edde2009-04-18 12:26:32 -07001321 }
1322 }
1323
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001324 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1325 helper.create(this);
1326 helper.refreshStats(which, UserHandle.USER_ALL);
1327 List<BatterySipper> sippers = helper.getUsageList();
1328 if (sippers != null && sippers.size() > 0) {
1329 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1330 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001331 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001332 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1333 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1334 for (int i=0; i<sippers.size(); i++) {
1335 BatterySipper bs = sippers.get(i);
1336 int uid = 0;
1337 String label;
1338 switch (bs.drainType) {
1339 case IDLE:
1340 label="idle";
1341 break;
1342 case CELL:
1343 label="cell";
1344 break;
1345 case PHONE:
1346 label="phone";
1347 break;
1348 case WIFI:
1349 label="wifi";
1350 break;
1351 case BLUETOOTH:
1352 label="blue";
1353 break;
1354 case SCREEN:
1355 label="scrn";
1356 break;
1357 case APP:
1358 uid = bs.uidObj.getUid();
1359 label = "uid";
1360 break;
1361 case USER:
1362 uid = UserHandle.getUid(bs.userId, 0);
1363 label = "user";
1364 break;
1365 case UNACCOUNTED:
1366 label = "unacc";
1367 break;
1368 case OVERCOUNTED:
1369 label = "over";
1370 break;
1371 default:
1372 label = "???";
1373 }
1374 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1375 BatteryStatsHelper.makemAh(bs.value));
1376 }
1377 }
1378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 for (int iu = 0; iu < NU; iu++) {
1380 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001381 if (reqUid >= 0 && uid != reqUid) {
1382 continue;
1383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 Uid u = uidStats.valueAt(iu);
1385 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001386 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1387 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1388 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1389 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1390 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1391 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1392 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1393 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001394 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001395 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001396 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001397
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001398 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1399 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
1400 || wifiPacketsTx > 0) {
1401 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1402 wifiBytesRx, wifiBytesTx,
1403 mobilePacketsRx, mobilePacketsTx,
1404 wifiPacketsRx, wifiPacketsTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001405 }
1406
Nick Pelly6ccaa542012-06-15 15:22:47 -07001407 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001408 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001409 dumpLine(pw, uid, category, WIFI_DATA,
1410 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412
Dianne Hackborn617f8772009-03-31 15:04:46 -07001413 if (u.hasUserActivity()) {
1414 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1415 boolean hasData = false;
1416 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1417 int val = u.getUserActivityCount(i, which);
1418 args[i] = val;
1419 if (val != 0) hasData = true;
1420 }
1421 if (hasData) {
1422 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1423 }
1424 }
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1427 if (wakelocks.size() > 0) {
1428 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1429 : wakelocks.entrySet()) {
1430 Uid.Wakelock wl = ent.getValue();
1431 String linePrefix = "";
1432 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001433 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1434 batteryRealtime, "f", which, linePrefix);
1435 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1436 batteryRealtime, "p", which, linePrefix);
1437 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1438 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439
1440 // Only log if we had at lease one wakelock...
1441 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001442 String name = ent.getKey();
1443 if (name.indexOf(',') >= 0) {
1444 name = name.replace(',', '_');
1445 }
1446 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 }
1449 }
1450
1451 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1452 if (sensors.size() > 0) {
1453 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1454 : sensors.entrySet()) {
1455 Uid.Sensor se = ent.getValue();
1456 int sensorNumber = ent.getKey();
1457 Timer timer = se.getSensorTime();
1458 if (timer != null) {
1459 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001460 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1461 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (totalTime != 0) {
1463 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1464 }
1465 }
1466 }
1467 }
1468
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001469 Timer vibTimer = u.getVibratorOnTimer();
1470 if (vibTimer != null) {
1471 // Convert from microseconds to milliseconds with rounding
1472 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1473 int count = vibTimer.getCountLocked(which);
1474 if (totalTime != 0) {
1475 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1476 }
1477 }
1478
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001479 Timer fgTimer = u.getForegroundActivityTimer();
1480 if (fgTimer != null) {
1481 // Convert from microseconds to milliseconds with rounding
1482 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1483 int count = fgTimer.getCountLocked(which);
1484 if (totalTime != 0) {
1485 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1486 }
1487 }
1488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1490 if (processStats.size() > 0) {
1491 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1492 : processStats.entrySet()) {
1493 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001494
1495 final long userMillis = ps.getUserTime(which) * 10;
1496 final long systemMillis = ps.getSystemTime(which) * 10;
1497 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1498 final long starts = ps.getStarts(which);
1499
1500 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1501 || starts != 0) {
1502 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1503 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
1505 }
1506 }
1507
1508 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1509 if (packageStats.size() > 0) {
1510 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1511 : packageStats.entrySet()) {
1512
1513 Uid.Pkg ps = ent.getValue();
1514 int wakeups = ps.getWakeups(which);
1515 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1516 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1517 : serviceStats.entrySet()) {
1518 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1519 long startTime = ss.getStartTime(batteryUptime, which);
1520 int starts = ss.getStarts(which);
1521 int launches = ss.getLaunches(which);
1522 if (startTime != 0 || starts != 0 || launches != 0) {
1523 dumpLine(pw, uid, category, APK_DATA,
1524 wakeups, // wakeup alarms
1525 ent.getKey(), // Apk
1526 sent.getKey(), // service
1527 startTime / 1000, // time spent started, in ms
1528 starts,
1529 launches);
1530 }
1531 }
1532 }
1533 }
1534 }
1535 }
1536
Dianne Hackborn81038902012-11-26 17:04:09 -08001537 static final class TimerEntry {
1538 final String mName;
1539 final int mId;
1540 final BatteryStats.Timer mTimer;
1541 final long mTime;
1542 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1543 mName = name;
1544 mId = id;
1545 mTimer = timer;
1546 mTime = time;
1547 }
1548 }
1549
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001550 private void printmAh(PrintWriter printer, double power) {
1551 printer.print(BatteryStatsHelper.makemAh(power));
1552 }
1553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001555 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1556 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1558 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1559 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001560 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561
1562 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1563 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1564 final long totalRealtime = computeRealtime(rawRealtime, which);
1565 final long totalUptime = computeUptime(rawUptime, which);
1566
1567 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001568
1569 SparseArray<? extends Uid> uidStats = getUidStats();
1570 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001572 sb.setLength(0);
1573 sb.append(prefix);
1574 sb.append(" Time on battery: ");
1575 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1576 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1577 sb.append(") realtime, ");
1578 formatTimeMs(sb, whichBatteryUptime / 1000);
1579 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1580 sb.append(") uptime");
1581 pw.println(sb.toString());
1582 sb.setLength(0);
1583 sb.append(prefix);
1584 sb.append(" Total run time: ");
1585 formatTimeMs(sb, totalRealtime / 1000);
1586 sb.append("realtime, ");
1587 formatTimeMs(sb, totalUptime / 1000);
1588 sb.append("uptime, ");
1589 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590
The Android Open Source Project10592532009-03-18 17:39:46 -07001591 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1592 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001593 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001594 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1595 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001596 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001597 sb.append(prefix);
1598 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1599 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1600 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1601 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1602 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1603 sb.append(")");
1604 pw.println(sb.toString());
1605 sb.setLength(0);
1606 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001607 sb.append(" Screen brightnesses: ");
1608 boolean didOne = false;
1609 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1610 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1611 if (time == 0) {
1612 continue;
1613 }
1614 if (didOne) sb.append(", ");
1615 didOne = true;
1616 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1617 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001618 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001619 sb.append("(");
1620 sb.append(formatRatioLocked(time, screenOnTime));
1621 sb.append(")");
1622 }
1623 if (!didOne) sb.append("No activity");
1624 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001625
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001626 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001627 long fullWakeLockTimeTotalMicros = 0;
1628 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001629
1630 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1631 @Override
1632 public int compare(TimerEntry lhs, TimerEntry rhs) {
1633 long lhsTime = lhs.mTime;
1634 long rhsTime = rhs.mTime;
1635 if (lhsTime < rhsTime) {
1636 return 1;
1637 }
1638 if (lhsTime > rhsTime) {
1639 return -1;
1640 }
1641 return 0;
1642 }
1643 };
1644
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001645 if (reqUid < 0) {
1646 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1647 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001648 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001649 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001650 BatteryStats.Timer timer = ent.getValue();
1651 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1652 if (totalTimeMillis > 0) {
1653 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1654 }
1655 }
1656 Collections.sort(timers, timerComparator);
1657 for (int i=0; i<timers.size(); i++) {
1658 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001659 String linePrefix = ": ";
1660 sb.setLength(0);
1661 sb.append(prefix);
1662 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001663 sb.append(timer.mName);
1664 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1665 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001666 if (!linePrefix.equals(": ")) {
1667 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001668 // Only print out wake locks that were held
1669 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001670 }
Evan Millarc64edde2009-04-18 12:26:32 -07001671 }
Evan Millarc64edde2009-04-18 12:26:32 -07001672 }
1673 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001674
1675 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1676
Evan Millar22ac0432009-03-31 11:33:18 -07001677 for (int iu = 0; iu < NU; iu++) {
1678 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001679
Evan Millar22ac0432009-03-31 11:33:18 -07001680 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1681 if (wakelocks.size() > 0) {
1682 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1683 : wakelocks.entrySet()) {
1684 Uid.Wakelock wl = ent.getValue();
1685
1686 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1687 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001688 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001689 batteryRealtime, which);
1690 }
1691
1692 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1693 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001694 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001695 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001696 if (totalTimeMicros > 0) {
1697 if (reqUid < 0) {
1698 // Only show the ordered list of all wake
1699 // locks if the caller is not asking for data
1700 // about a specific uid.
1701 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1702 partialWakeTimer, totalTimeMicros));
1703 }
1704 partialWakeLockTimeTotalMicros += totalTimeMicros;
1705 }
Evan Millar22ac0432009-03-31 11:33:18 -07001706 }
1707 }
1708 }
1709 }
1710
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001711 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1712 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1713 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1714 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1715 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1716 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1717 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1718 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1719
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001720 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001721 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1722 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1723 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1724 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001725 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001726 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1727 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1728 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1729 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001730 sb.setLength(0);
1731 sb.append(prefix);
1732 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1733 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001734 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001735 (partialWakeLockTimeTotalMicros + 500) / 1000);
1736 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001737
Dianne Hackborn627bba72009-03-24 22:32:56 -07001738 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001739 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001740 sb.append(" Signal levels: ");
1741 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001742 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001743 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1744 if (time == 0) {
1745 continue;
1746 }
1747 if (didOne) sb.append(", ");
1748 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001749 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001750 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001751 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001752 sb.append("(");
1753 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001754 sb.append(") ");
1755 sb.append(getPhoneSignalStrengthCount(i, which));
1756 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001757 }
1758 if (!didOne) sb.append("No activity");
1759 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001760
1761 sb.setLength(0);
1762 sb.append(prefix);
1763 sb.append(" Signal scanning time: ");
1764 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1765 pw.println(sb.toString());
1766
Dianne Hackborn627bba72009-03-24 22:32:56 -07001767 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001768 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001769 sb.append(" Radio types: ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001770 didOne = false;
1771 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1772 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1773 if (time == 0) {
1774 continue;
1775 }
1776 if (didOne) sb.append(", ");
1777 didOne = true;
1778 sb.append(DATA_CONNECTION_NAMES[i]);
1779 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001780 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001781 sb.append("(");
1782 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001783 sb.append(") ");
1784 sb.append(getPhoneDataConnectionCount(i, which));
1785 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001786 }
1787 if (!didOne) sb.append("No activity");
1788 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001789
1790 sb.setLength(0);
1791 sb.append(prefix);
1792 sb.append(" Radio data uptime when unplugged: ");
1793 sb.append(getRadioDataUptime() / 1000);
1794 sb.append(" ms");
1795 pw.println(sb.toString());
1796
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001797 sb.setLength(0);
1798 sb.append(prefix);
1799 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1800 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1801 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1802 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1803 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1804 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1805 sb.append(")");
1806 pw.println(sb.toString());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001807
The Android Open Source Project10592532009-03-18 17:39:46 -07001808 pw.println(" ");
1809
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001810 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001811 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001812 pw.print(prefix); pw.println(" Device is currently unplugged");
1813 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1814 pw.println(getDischargeStartLevel());
1815 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1816 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07001817 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001818 pw.print(prefix); pw.println(" Device is currently plugged into power");
1819 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1820 pw.println(getDischargeStartLevel());
1821 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1822 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001823 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001824 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1825 pw.println(getDischargeAmountScreenOn());
1826 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1827 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001828 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001829 } else {
1830 pw.print(prefix); pw.println(" Device battery use since last full charge");
1831 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1832 pw.println(getLowDischargeAmountSinceCharge());
1833 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1834 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001835 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1836 pw.println(getDischargeAmountScreenOnSinceCharge());
1837 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1838 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08001839 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07001840 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001841
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001842 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1843 helper.create(this);
1844 helper.refreshStats(which, UserHandle.USER_ALL);
1845 List<BatterySipper> sippers = helper.getUsageList();
1846 if (sippers != null && sippers.size() > 0) {
1847 pw.print(prefix); pw.println(" Estimated power use (mAh):");
1848 pw.print(prefix); pw.print(" Capacity: ");
1849 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08001850 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001851 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
1852 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
1853 pw.println();
1854 for (int i=0; i<sippers.size(); i++) {
1855 BatterySipper bs = sippers.get(i);
1856 switch (bs.drainType) {
1857 case IDLE:
1858 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
1859 pw.println();
1860 break;
1861 case CELL:
1862 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
1863 pw.println();
1864 break;
1865 case PHONE:
1866 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
1867 pw.println();
1868 break;
1869 case WIFI:
1870 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
1871 pw.println();
1872 break;
1873 case BLUETOOTH:
1874 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
1875 pw.println();
1876 break;
1877 case SCREEN:
1878 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
1879 pw.println();
1880 break;
1881 case APP:
1882 pw.print(prefix); pw.print(" Uid "); pw.print(bs.uidObj.getUid());
1883 pw.print(": "); printmAh(pw, bs.value); pw.println();
1884 break;
1885 case USER:
1886 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
1887 pw.print(": "); printmAh(pw, bs.value); pw.println();
1888 break;
1889 case UNACCOUNTED:
1890 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
1891 pw.println();
1892 break;
1893 case OVERCOUNTED:
1894 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
1895 pw.println();
1896 break;
1897 }
1898 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08001899 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001900 }
1901
Dianne Hackborn81038902012-11-26 17:04:09 -08001902 if (timers.size() > 0) {
1903 Collections.sort(timers, timerComparator);
1904 pw.print(prefix); pw.println(" All partial wake locks:");
1905 for (int i=0; i<timers.size(); i++) {
1906 TimerEntry timer = timers.get(i);
1907 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001908 sb.append(" Wake lock ");
1909 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08001910 sb.append(" ");
1911 sb.append(timer.mName);
1912 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1913 sb.append(" realtime");
1914 pw.println(sb.toString());
1915 }
1916 timers.clear();
1917 pw.println();
1918 }
Evan Millar22ac0432009-03-31 11:33:18 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 for (int iu=0; iu<NU; iu++) {
1921 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08001922 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001923 continue;
1924 }
1925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001927
1928 pw.print(prefix);
1929 pw.print(" ");
1930 UserHandle.formatUid(pw, uid);
1931 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 boolean uidActivity = false;
1933
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001934 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1935 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1936 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1937 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1938 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1939 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1940 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1941 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001942 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001943 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001944 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001945
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001946 if (mobileRxBytes > 0 || mobileTxBytes > 0
1947 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001948 pw.print(prefix); pw.print(" Mobile network: ");
1949 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001950 pw.print(formatBytesLocked(mobileTxBytes));
1951 pw.print(" sent (packets "); pw.print(mobileRxPackets);
1952 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001954 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001955 pw.print(prefix); pw.print(" Wi-Fi network: ");
1956 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001957 pw.print(formatBytesLocked(wifiTxBytes));
1958 pw.print(" sent (packets "); pw.print(wifiRxPackets);
1959 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001960 }
1961
Dianne Hackborn617f8772009-03-31 15:04:46 -07001962 if (u.hasUserActivity()) {
1963 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07001964 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001965 int val = u.getUserActivityCount(i, which);
1966 if (val != 0) {
1967 if (!hasData) {
1968 sb.setLength(0);
1969 sb.append(" User activity: ");
1970 hasData = true;
1971 } else {
1972 sb.append(", ");
1973 }
1974 sb.append(val);
1975 sb.append(" ");
1976 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1977 }
1978 }
1979 if (hasData) {
1980 pw.println(sb.toString());
1981 }
1982 }
1983
Nick Pelly6ccaa542012-06-15 15:22:47 -07001984 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001985 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001986 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001987 sb.append(prefix); sb.append(" Wifi Running: ");
1988 formatTimeMs(sb, uidWifiRunningTime / 1000);
1989 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001990 whichBatteryRealtime)); sb.append(")\n");
1991 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001992 formatTimeMs(sb, fullWifiLockOnTime / 1000);
1993 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001994 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001995 sb.append(prefix); sb.append(" Wifi Scan: ");
1996 formatTimeMs(sb, wifiScanTime / 1000);
1997 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001998 whichBatteryRealtime)); sb.append(")");
1999 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001
2002 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2003 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002004 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2005 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2007 : wakelocks.entrySet()) {
2008 Uid.Wakelock wl = ent.getValue();
2009 String linePrefix = ": ";
2010 sb.setLength(0);
2011 sb.append(prefix);
2012 sb.append(" Wake lock ");
2013 sb.append(ent.getKey());
2014 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
2015 "full", which, linePrefix);
2016 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
2017 "partial", which, linePrefix);
2018 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
2019 "window", which, linePrefix);
2020 if (!linePrefix.equals(": ")) {
2021 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002022 // Only print out wake locks that were held
2023 pw.println(sb.toString());
2024 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002025 count++;
2026 }
2027 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
2028 batteryRealtime, which);
2029 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
2030 batteryRealtime, which);
2031 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
2032 batteryRealtime, which);
2033 }
2034 if (count > 1) {
2035 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2036 sb.setLength(0);
2037 sb.append(prefix);
2038 sb.append(" TOTAL wake: ");
2039 boolean needComma = false;
2040 if (totalFull != 0) {
2041 needComma = true;
2042 formatTimeMs(sb, totalFull);
2043 sb.append("full");
2044 }
2045 if (totalPartial != 0) {
2046 if (needComma) {
2047 sb.append(", ");
2048 }
2049 needComma = true;
2050 formatTimeMs(sb, totalPartial);
2051 sb.append("partial");
2052 }
2053 if (totalWindow != 0) {
2054 if (needComma) {
2055 sb.append(", ");
2056 }
2057 needComma = true;
2058 formatTimeMs(sb, totalWindow);
2059 sb.append("window");
2060 }
2061 sb.append(" realtime");
2062 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
2065 }
2066
2067 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2068 if (sensors.size() > 0) {
2069 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2070 : sensors.entrySet()) {
2071 Uid.Sensor se = ent.getValue();
2072 int sensorNumber = ent.getKey();
2073 sb.setLength(0);
2074 sb.append(prefix);
2075 sb.append(" Sensor ");
2076 int handle = se.getHandle();
2077 if (handle == Uid.Sensor.GPS) {
2078 sb.append("GPS");
2079 } else {
2080 sb.append(handle);
2081 }
2082 sb.append(": ");
2083
2084 Timer timer = se.getSensorTime();
2085 if (timer != null) {
2086 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002087 long totalTime = (timer.getTotalTimeLocked(
2088 batteryRealtime, which) + 500) / 1000;
2089 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 //timer.logState();
2091 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002092 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 sb.append("realtime (");
2094 sb.append(count);
2095 sb.append(" times)");
2096 } else {
2097 sb.append("(not used)");
2098 }
2099 } else {
2100 sb.append("(not used)");
2101 }
2102
2103 pw.println(sb.toString());
2104 uidActivity = true;
2105 }
2106 }
2107
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002108 Timer vibTimer = u.getVibratorOnTimer();
2109 if (vibTimer != null) {
2110 // Convert from microseconds to milliseconds with rounding
2111 long totalTime = (vibTimer.getTotalTimeLocked(
2112 batteryRealtime, which) + 500) / 1000;
2113 int count = vibTimer.getCountLocked(which);
2114 //timer.logState();
2115 if (totalTime != 0) {
2116 sb.setLength(0);
2117 sb.append(prefix);
2118 sb.append(" Vibrator: ");
2119 formatTimeMs(sb, totalTime);
2120 sb.append("realtime (");
2121 sb.append(count);
2122 sb.append(" times)");
2123 pw.println(sb.toString());
2124 uidActivity = true;
2125 }
2126 }
2127
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002128 Timer fgTimer = u.getForegroundActivityTimer();
2129 if (fgTimer != null) {
2130 // Convert from microseconds to milliseconds with rounding
2131 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2132 int count = fgTimer.getCountLocked(which);
2133 if (totalTime != 0) {
2134 sb.setLength(0);
2135 sb.append(prefix);
2136 sb.append(" Foreground activities: ");
2137 formatTimeMs(sb, totalTime);
2138 sb.append("realtime (");
2139 sb.append(count);
2140 sb.append(" times)");
2141 pw.println(sb.toString());
2142 uidActivity = true;
2143 }
2144 }
2145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2147 if (processStats.size() > 0) {
2148 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2149 : processStats.entrySet()) {
2150 Uid.Proc ps = ent.getValue();
2151 long userTime;
2152 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002153 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002155 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156
2157 userTime = ps.getUserTime(which);
2158 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002159 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002161 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002162 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002164 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002165 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002166 sb.setLength(0);
2167 sb.append(prefix); sb.append(" Proc ");
2168 sb.append(ent.getKey()); sb.append(":\n");
2169 sb.append(prefix); sb.append(" CPU: ");
2170 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002171 formatTime(sb, systemTime); sb.append("krn ; ");
2172 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002173 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002174 sb.append("\n"); sb.append(prefix); sb.append(" ");
2175 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002176 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002177 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002178 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002179 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002180 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002181 pw.print(prefix); pw.print(" * Killed for ");
2182 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2183 pw.print("wake lock");
2184 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2185 pw.print("cpu");
2186 } else {
2187 pw.print("unknown");
2188 }
2189 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002190 TimeUtils.formatDuration(ew.usedTime, pw);
2191 pw.print(" over ");
2192 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002193 if (ew.overTime != 0) {
2194 pw.print(" (");
2195 pw.print((ew.usedTime*100)/ew.overTime);
2196 pw.println("%)");
2197 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002198 }
2199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 uidActivity = true;
2201 }
2202 }
2203 }
2204
2205 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2206 if (packageStats.size() > 0) {
2207 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2208 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002209 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 boolean apkActivity = false;
2211 Uid.Pkg ps = ent.getValue();
2212 int wakeups = ps.getWakeups(which);
2213 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002214 pw.print(prefix); pw.print(" ");
2215 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 apkActivity = true;
2217 }
2218 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2219 if (serviceStats.size() > 0) {
2220 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2221 : serviceStats.entrySet()) {
2222 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2223 long startTime = ss.getStartTime(batteryUptime, which);
2224 int starts = ss.getStarts(which);
2225 int launches = ss.getLaunches(which);
2226 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002227 sb.setLength(0);
2228 sb.append(prefix); sb.append(" Service ");
2229 sb.append(sent.getKey()); sb.append(":\n");
2230 sb.append(prefix); sb.append(" Created for: ");
2231 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002232 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002233 sb.append(prefix); sb.append(" Starts: ");
2234 sb.append(starts);
2235 sb.append(", launches: "); sb.append(launches);
2236 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 apkActivity = true;
2238 }
2239 }
2240 }
2241 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002242 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
2244 uidActivity = true;
2245 }
2246 }
2247 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002248 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
2251 }
2252
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002253 static void printBitDescriptions(PrintWriter pw, int oldval, int newval,
2254 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002255 int diff = oldval ^ newval;
2256 if (diff == 0) return;
2257 for (int i=0; i<descriptions.length; i++) {
2258 BitDescription bd = descriptions[i];
Dianne Hackborn099bc622014-01-22 13:39:16 -08002259 int mask = bd.mask;
2260 if (bd.shift > 0) {
2261 mask <<= bd.shift;
2262 }
2263 if ((diff&mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002264 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002265 if (bd.shift < 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002266 pw.print((newval&mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002267 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002268 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002269 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002270 pw.print("=");
2271 int val = (newval&bd.mask)>>bd.shift;
2272 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002274 } else {
2275 pw.print(val);
2276 }
2277 }
2278 }
2279 }
2280 }
2281
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002282 public void prepareForDumpLocked() {
2283 }
2284
2285 public static class HistoryPrinter {
2286 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002287 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002288 int oldStatus = -1;
2289 int oldHealth = -1;
2290 int oldPlug = -1;
2291 int oldTemp = -1;
2292 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002293 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002294
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002295 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2296 if (!checkin) {
2297 pw.print(" ");
2298 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002299 pw.print(" ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002300 } else {
2301 if (lastTime < 0) {
2302 pw.print("@");
2303 pw.print(rec.time-now);
2304 } else {
2305 pw.print(rec.time-lastTime);
2306 }
2307 lastTime = rec.time;
2308 }
2309 if (rec.cmd == HistoryItem.CMD_START) {
2310 if (checkin) {
2311 pw.print(":");
2312 }
2313 pw.println("START");
2314 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2315 if (checkin) {
2316 pw.print(":");
2317 }
2318 pw.println("*OVERFLOW*");
2319 } else {
2320 if (!checkin) {
2321 if (rec.batteryLevel < 10) pw.print("00");
2322 else if (rec.batteryLevel < 100) pw.print("0");
2323 pw.print(rec.batteryLevel);
2324 pw.print(" ");
2325 if (rec.states < 0x10) pw.print("0000000");
2326 else if (rec.states < 0x100) pw.print("000000");
2327 else if (rec.states < 0x1000) pw.print("00000");
2328 else if (rec.states < 0x10000) pw.print("0000");
2329 else if (rec.states < 0x100000) pw.print("000");
2330 else if (rec.states < 0x1000000) pw.print("00");
2331 else if (rec.states < 0x10000000) pw.print("0");
2332 pw.print(Integer.toHexString(rec.states));
2333 } else {
2334 if (oldLevel != rec.batteryLevel) {
2335 oldLevel = rec.batteryLevel;
2336 pw.print(",Bl="); pw.print(rec.batteryLevel);
2337 }
2338 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002339 if (oldStatus != rec.batteryStatus) {
2340 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002341 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002342 switch (oldStatus) {
2343 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002344 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002345 break;
2346 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002347 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002348 break;
2349 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002350 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002351 break;
2352 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002353 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002354 break;
2355 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002356 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002357 break;
2358 default:
2359 pw.print(oldStatus);
2360 break;
2361 }
2362 }
2363 if (oldHealth != rec.batteryHealth) {
2364 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002365 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002366 switch (oldHealth) {
2367 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002368 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002369 break;
2370 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002371 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002372 break;
2373 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002374 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002375 break;
2376 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002377 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002378 break;
2379 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002380 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002381 break;
2382 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002383 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002384 break;
2385 default:
2386 pw.print(oldHealth);
2387 break;
2388 }
2389 }
2390 if (oldPlug != rec.batteryPlugType) {
2391 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002392 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002393 switch (oldPlug) {
2394 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002395 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002396 break;
2397 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002398 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002399 break;
2400 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002401 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002402 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002403 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002404 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002405 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002406 default:
2407 pw.print(oldPlug);
2408 break;
2409 }
2410 }
2411 if (oldTemp != rec.batteryTemperature) {
2412 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002413 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002414 pw.print(oldTemp);
2415 }
2416 if (oldVolt != rec.batteryVoltage) {
2417 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002418 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002419 pw.print(oldVolt);
2420 }
2421 printBitDescriptions(pw, oldState, rec.states,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002422 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002423 if (rec.eventCode != HistoryItem.EVENT_NONE) {
2424 switch (rec.eventCode) {
2425 case HistoryItem.EVENT_PROC_STARTED:
2426 pw.print(checkin ? ",PrSt=" : " procstart=");
2427 break;
2428 case HistoryItem.EVENT_PROC_FINISHED:
2429 pw.print(checkin ? ",PrFn=" : " procfin=");
2430 break;
2431 default:
2432 if (checkin) {
2433 pw.print(",?cmd_");
2434 pw.print(rec.eventCode);
2435 pw.print("=");
2436 } else {
2437 pw.print(" cmd_");
2438 pw.print(rec.eventCode);
2439 pw.print("=");
2440 }
2441 break;
2442 }
2443 if (checkin) {
2444 pw.print(rec.eventUid);
2445 } else {
2446 UserHandle.formatUid(pw, rec.eventUid);
2447 }
2448 pw.print(":");
2449 if (checkin) {
2450 pw.print(rec.eventNameIdx);
2451 } else {
2452 pw.print(rec.eventName);
2453 }
2454 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002455 pw.println();
2456 }
2457 oldState = rec.states;
2458 }
2459 }
2460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 /**
2462 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2463 *
2464 * @param pw a Printer to receive the dump output.
2465 */
2466 @SuppressWarnings("unused")
Dianne Hackborn099bc622014-01-22 13:39:16 -08002467 public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid,
2468 boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002469 prepareForDumpLocked();
2470
2471 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2472
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002473 final HistoryItem rec = new HistoryItem();
2474 if (startIteratingHistoryLocked()) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002475 pw.println("Battery History:");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002476 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002477 while (getNextHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002478 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002479 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002480 finishIteratingHistoryLocked();
2481 pw.println("");
2482 }
2483
2484 if (startIteratingOldHistoryLocked()) {
2485 pw.println("Old battery History:");
2486 HistoryPrinter hprinter = new HistoryPrinter();
2487 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002488 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002489 }
2490 finishIteratingOldHistoryLocked();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002491 pw.println("");
2492 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002493
2494 if (historyOnly) {
2495 return;
2496 }
2497
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002498 SparseArray<? extends Uid> uidStats = getUidStats();
2499 final int NU = uidStats.size();
2500 boolean didPid = false;
2501 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002502 for (int i=0; i<NU; i++) {
2503 Uid uid = uidStats.valueAt(i);
2504 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2505 if (pids != null) {
2506 for (int j=0; j<pids.size(); j++) {
2507 Uid.Pid pid = pids.valueAt(j);
2508 if (!didPid) {
2509 pw.println("Per-PID Stats:");
2510 didPid = true;
2511 }
2512 long time = pid.mWakeSum + (pid.mWakeStart != 0
2513 ? (nowRealtime - pid.mWakeStart) : 0);
2514 pw.print(" PID "); pw.print(pids.keyAt(j));
2515 pw.print(" wake time: ");
2516 TimeUtils.formatDuration(time, pw);
2517 pw.println("");
2518 }
2519 }
2520 }
2521 if (didPid) {
2522 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002523 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002524
2525 if (!isUnpluggedOnly) {
2526 pw.println("Statistics since last charge:");
2527 pw.println(" System starts: " + getStartCount()
2528 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002529 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002530 pw.println("");
2531 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002532 pw.println("Statistics since last unplugged:");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002533 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
2535
2536 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002537 public void dumpCheckinLocked(Context context,
Dianne Hackborn49021f52013-09-04 18:03:40 -07002538 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
Dianne Hackborn099bc622014-01-22 13:39:16 -08002539 boolean includeHistory, boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002540 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002542 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2543
Dianne Hackborn099bc622014-01-22 13:39:16 -08002544 if (includeHistory || historyOnly) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07002545 final HistoryItem rec = new HistoryItem();
2546 if (startIteratingHistoryLocked()) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002547 for (int i=0; i<getHistoryStringPoolSize(); i++) {
2548 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2549 pw.print(HISTORY_STRING_POOL); pw.print(',');
2550 pw.print(i);
2551 pw.print(',');
2552 pw.print(getHistoryStringPoolItem(i));
2553 pw.println();
2554 }
Dianne Hackborn49021f52013-09-04 18:03:40 -07002555 HistoryPrinter hprinter = new HistoryPrinter();
2556 while (getNextHistoryLocked(rec)) {
2557 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
Dianne Hackborn49021f52013-09-04 18:03:40 -07002558 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002559 hprinter.printNextItem(pw, rec, now, true);
Dianne Hackborn49021f52013-09-04 18:03:40 -07002560 }
2561 finishIteratingHistoryLocked();
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002562 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002563 }
2564
Dianne Hackborn099bc622014-01-22 13:39:16 -08002565 if (historyOnly) {
2566 return;
2567 }
2568
Dianne Hackborne4a59512010-12-07 11:08:07 -08002569 if (apps != null) {
2570 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2571 for (int i=0; i<apps.size(); i++) {
2572 ApplicationInfo ai = apps.get(i);
2573 ArrayList<String> pkgs = uids.get(ai.uid);
2574 if (pkgs == null) {
2575 pkgs = new ArrayList<String>();
2576 uids.put(ai.uid, pkgs);
2577 }
2578 pkgs.add(ai.packageName);
2579 }
2580 SparseArray<? extends Uid> uidStats = getUidStats();
2581 final int NU = uidStats.size();
2582 String[] lineArgs = new String[2];
2583 for (int i=0; i<NU; i++) {
2584 int uid = uidStats.keyAt(i);
2585 ArrayList<String> pkgs = uids.get(uid);
2586 if (pkgs != null) {
2587 for (int j=0; j<pkgs.size(); j++) {
2588 lineArgs[0] = Integer.toString(uid);
2589 lineArgs[1] = pkgs.get(j);
2590 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2591 (Object[])lineArgs);
2592 }
2593 }
2594 }
2595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 if (isUnpluggedOnly) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002597 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599 else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002600 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
2601 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
2603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604}