blob: 58b94ca8cb1ce98c24551f28fc29a2b5c4f4d872 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborne4a59512010-12-07 11:08:07 -080027import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080028import android.telephony.SignalStrength;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.Printer;
30import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070031import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33/**
34 * A class providing access to battery usage statistics, including information on
35 * wakelocks, processes, packages, and services. All times are represented in microseconds
36 * except where indicated otherwise.
37 * @hide
38 */
39public abstract class BatteryStats implements Parcelable {
40
41 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070042
43 /** @hide */
44 public static final String SERVICE_NAME = "batterystats";
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 /**
47 * A constant indicating a partial wake lock timer.
48 */
49 public static final int WAKE_TYPE_PARTIAL = 0;
50
51 /**
52 * A constant indicating a full wake lock timer.
53 */
54 public static final int WAKE_TYPE_FULL = 1;
55
56 /**
57 * A constant indicating a window wake lock timer.
58 */
59 public static final int WAKE_TYPE_WINDOW = 2;
60
61 /**
62 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
64 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070065
66 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070067 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070068 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070069 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070070
71 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070072 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070073 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070074 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070075
76 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070077 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070078 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070079 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Robert Greenwalt5347bd42009-05-13 15:10:16 -070081 /**
82 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070083 */
84 public static final int WIFI_MULTICAST_ENABLED = 7;
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070087 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070088 */
89 public static final int AUDIO_TURNED_ON = 7;
90
91 /**
92 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070093 */
94 public static final int VIDEO_TURNED_ON = 8;
95
96 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -080097 * A constant indicating a vibrator on timer
98 */
99 public static final int VIBRATOR_ON = 9;
100
101 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700102 * A constant indicating a foreground activity timer
103 */
104 public static final int FOREGROUND_ACTIVITY = 10;
105
106 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700107 * A constant indicating a wifi batched scan is active
108 */
109 public static final int WIFI_BATCHED_SCAN = 11;
110
111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * Include all of the data in the stats, including previously saved data.
113 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700114 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 /**
117 * Include only the last run in the stats.
118 */
119 public static final int STATS_LAST = 1;
120
121 /**
122 * Include only the current run in the stats.
123 */
124 public static final int STATS_CURRENT = 2;
125
126 /**
127 * Include only the run since the last time the device was unplugged in the stats.
128 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700129 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700130
131 // NOTE: Update this list if you add/change any stats above.
132 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700133 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700134 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 /**
137 * Bump the version on this if the checkin format changes.
138 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700139 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700140
141 private static final long BYTES_PER_KB = 1024;
142 private static final long BYTES_PER_MB = 1048576; // 1024^2
143 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Dianne Hackborne4a59512010-12-07 11:08:07 -0800146 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700148 private static final String PROCESS_DATA = "pr";
149 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800150 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700151 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700152 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700153 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700154 private static final String NETWORK_DATA = "nt";
155 private static final String USER_ACTIVITY_DATA = "ua";
156 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800157 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700159 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700160 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800161 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700162 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700163 private static final String SCREEN_BRIGHTNESS_DATA = "br";
164 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700165 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700166 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
167 private static final String DATA_CONNECTION_TIME_DATA = "dct";
168 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700170 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private final Formatter mFormatter = new Formatter(mFormatBuilder);
172
173 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700174 * State for keeping track of counting information.
175 */
176 public static abstract class Counter {
177
178 /**
179 * Returns the count associated with this Counter for the
180 * selected type of statistics.
181 *
182 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
183 */
Evan Millarc64edde2009-04-18 12:26:32 -0700184 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700185
186 /**
187 * Temporary for debugging.
188 */
189 public abstract void logState(Printer pw, String prefix);
190 }
191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * State for keeping track of timing information.
194 */
195 public static abstract class Timer {
196
197 /**
198 * Returns the count associated with this Timer for the
199 * selected type of statistics.
200 *
201 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
202 */
Evan Millarc64edde2009-04-18 12:26:32 -0700203 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
205 /**
206 * Returns the total time in microseconds associated with this Timer for the
207 * selected type of statistics.
208 *
209 * @param batteryRealtime system realtime on battery in microseconds
210 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
211 * @return a time in microseconds
212 */
Evan Millarc64edde2009-04-18 12:26:32 -0700213 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
216 * Temporary for debugging.
217 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700218 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220
221 /**
222 * The statistics associated with a particular uid.
223 */
224 public static abstract class Uid {
225
226 /**
227 * Returns a mapping containing wakelock statistics.
228 *
229 * @return a Map from Strings to Uid.Wakelock objects.
230 */
231 public abstract Map<String, ? extends Wakelock> getWakelockStats();
232
233 /**
234 * The statistics associated with a particular wake lock.
235 */
236 public static abstract class Wakelock {
237 public abstract Timer getWakeTime(int type);
238 }
239
240 /**
241 * Returns a mapping containing sensor statistics.
242 *
243 * @return a Map from Integer sensor ids to Uid.Sensor objects.
244 */
245 public abstract Map<Integer, ? extends Sensor> getSensorStats();
246
247 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700248 * Returns a mapping containing active process data.
249 */
250 public abstract SparseArray<? extends Pid> getPidStats();
251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * Returns a mapping containing process statistics.
254 *
255 * @return a Map from Strings to Uid.Proc objects.
256 */
257 public abstract Map<String, ? extends Proc> getProcessStats();
258
259 /**
260 * Returns a mapping containing package statistics.
261 *
262 * @return a Map from Strings to Uid.Pkg objects.
263 */
264 public abstract Map<String, ? extends Pkg> getPackageStats();
265
266 /**
267 * {@hide}
268 */
269 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700270
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700271 public abstract void noteWifiRunningLocked();
272 public abstract void noteWifiStoppedLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700273 public abstract void noteFullWifiLockAcquiredLocked();
274 public abstract void noteFullWifiLockReleasedLocked();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700275 public abstract void noteWifiScanStartedLocked();
276 public abstract void noteWifiScanStoppedLocked();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700277 public abstract void noteWifiBatchedScanStartedLocked(int csph);
278 public abstract void noteWifiBatchedScanStoppedLocked();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700279 public abstract void noteWifiMulticastEnabledLocked();
280 public abstract void noteWifiMulticastDisabledLocked();
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700281 public abstract void noteAudioTurnedOnLocked();
282 public abstract void noteAudioTurnedOffLocked();
283 public abstract void noteVideoTurnedOnLocked();
284 public abstract void noteVideoTurnedOffLocked();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700285 public abstract void noteActivityResumedLocked();
286 public abstract void noteActivityPausedLocked();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700287 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700288 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700289 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalta029ea12013-09-25 16:38:12 -0700290 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700291 public abstract long getWifiMulticastTime(long batteryRealtime,
292 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700293 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
294 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700295 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800296 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Robert Greenwalta029ea12013-09-25 16:38:12 -0700298 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
299
Dianne Hackborn617f8772009-03-31 15:04:46 -0700300 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700301 * Note that these must match the constants in android.os.PowerManager.
302 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
303 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700304 */
305 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700306 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700307 };
308
Jeff Browndf693de2012-07-27 12:03:38 -0700309 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700310
Dianne Hackborn617f8772009-03-31 15:04:46 -0700311 public abstract void noteUserActivityLocked(int type);
312 public abstract boolean hasUserActivity();
313 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700314
315 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800316 public abstract long getNetworkActivityBytes(int type, int which);
317 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800320 /*
321 * FIXME: it's not correct to use this magic value because it
322 * could clash with a sensor handle (which are defined by
323 * the sensor HAL, and therefore out of our control
324 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 // Magic sensor number for the GPS.
326 public static final int GPS = -10000;
327
328 public abstract int getHandle();
329
330 public abstract Timer getSensorTime();
331 }
332
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700333 public class Pid {
334 public long mWakeSum;
335 public long mWakeStart;
336 }
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * The statistics associated with a particular process.
340 */
341 public static abstract class Proc {
342
Dianne Hackborn287952c2010-09-22 22:34:31 -0700343 public static class ExcessivePower {
344 public static final int TYPE_WAKE = 1;
345 public static final int TYPE_CPU = 2;
346
347 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700348 public long overTime;
349 public long usedTime;
350 }
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * Returns the total time (in 1/100 sec) spent executing in user code.
354 *
355 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
356 */
357 public abstract long getUserTime(int which);
358
359 /**
360 * Returns the total time (in 1/100 sec) spent executing in system code.
361 *
362 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
363 */
364 public abstract long getSystemTime(int which);
365
366 /**
367 * Returns the number of times the process has been started.
368 *
369 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
370 */
371 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700372
373 /**
374 * Returns the cpu time spent in microseconds while the process was in the foreground.
375 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
376 * @return foreground cpu time in microseconds
377 */
378 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700379
380 /**
381 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
382 * @param speedStep the index of the CPU speed. This is not the actual speed of the
383 * CPU.
384 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
385 * @see BatteryStats#getCpuSpeedSteps()
386 */
387 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700388
Dianne Hackborn287952c2010-09-22 22:34:31 -0700389 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700390
Dianne Hackborn287952c2010-09-22 22:34:31 -0700391 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
393
394 /**
395 * The statistics associated with a particular package.
396 */
397 public static abstract class Pkg {
398
399 /**
400 * Returns the number of times this package has done something that could wake up the
401 * device from sleep.
402 *
403 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
404 */
405 public abstract int getWakeups(int which);
406
407 /**
408 * Returns a mapping containing service statistics.
409 */
410 public abstract Map<String, ? extends Serv> getServiceStats();
411
412 /**
413 * The statistics associated with a particular service.
414 */
415 public abstract class Serv {
416
417 /**
418 * Returns the amount of time spent started.
419 *
420 * @param batteryUptime elapsed uptime on battery in microseconds.
421 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
422 * @return
423 */
424 public abstract long getStartTime(long batteryUptime, int which);
425
426 /**
427 * Returns the total number of times startService() has been called.
428 *
429 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
430 */
431 public abstract int getStarts(int which);
432
433 /**
434 * Returns the total number times the service has been launched.
435 *
436 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
437 */
438 public abstract int getLaunches(int which);
439 }
440 }
441 }
442
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700443 public final static class HistoryItem implements Parcelable {
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700444 static final String TAG = "HistoryItem";
445 static final boolean DEBUG = false;
446
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700447 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700448
449 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800450
451 // The command codes 0-3 can be written with delta updates; all others require
452 // that a full entry be written.
453 public static final byte CMD_UPDATE = 0;
454 public static final byte CMD_EVENT = 1;
455 public static final byte CMD_NULL = -1;
456 public static final byte CMD_START = 4;
457 public static final byte CMD_OVERFLOW = 5;
458
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700459 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700460
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800461 /**
462 * Return whether the command code is a delta data update.
463 */
464 public static boolean isDeltaData(byte cmd) {
465 return cmd >= 0 && cmd <= 3;
466 }
467
468 /**
469 * Return whether the command code is a delta data update.
470 */
471 public boolean isDeltaData() {
472 return cmd >= 0 && cmd <= 3;
473 }
474
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700475 public byte batteryLevel;
476 public byte batteryStatus;
477 public byte batteryHealth;
478 public byte batteryPlugType;
479
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900480 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700481 public char batteryVoltage;
482
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700483 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700484 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800485 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700486 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800487 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
488 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700489 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800490 public static final int STATE_PHONE_STATE_SHIFT = 6;
491 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700492 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800493 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
494 public static final int STATE_DATA_CONNECTION_MASK = 0x1f;
495
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700496 // These states always appear directly in the first int token
497 // of a delta change; they should be ones that change relatively
498 // frequently.
499 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
500 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700501 public static final int STATE_GPS_ON_FLAG = 1<<28;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700502 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
503 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
504 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
Nick Pelly6ccaa542012-06-15 15:22:47 -0700505 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700506 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
507 // These are on the lower bits used for the command; if they change
508 // we need to write another int of data.
509 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
510 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
511 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
512 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
513 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
514 public static final int STATE_WIFI_ON_FLAG = 1<<17;
515 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700516
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700517 public static final int MOST_INTERESTING_STATES =
518 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
519 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
520
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700521 public int states;
522
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800523 public static final int EVENT_NONE = 0;
524 public static final int EVENT_APP_FOREGROUND = 1;
525
526 // For CMD_EVENT.
527 public int eventCode;
528 public int eventUid;
529 public String eventName;
530
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700531 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700532 }
533
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700534 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700535 this.time = time;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700536 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700537 }
538
539 public int describeContents() {
540 return 0;
541 }
542
543 public void writeToParcel(Parcel dest, int flags) {
544 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700545 int bat = (((int)cmd)&0xff)
546 | ((((int)batteryLevel)<<8)&0xff00)
547 | ((((int)batteryStatus)<<16)&0xf0000)
548 | ((((int)batteryHealth)<<20)&0xf00000)
549 | ((((int)batteryPlugType)<<24)&0xf000000);
550 dest.writeInt(bat);
551 bat = (((int)batteryTemperature)&0xffff)
552 | ((((int)batteryVoltage)<<16)&0xffff0000);
553 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700554 dest.writeInt(states);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800555 if (cmd == CMD_EVENT) {
556 dest.writeInt(eventCode);
557 dest.writeInt(eventUid);
558 dest.writeString(eventName);
559 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700560 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700561
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800562 public void readFromParcel(Parcel src) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700563 int bat = src.readInt();
564 cmd = (byte)(bat&0xff);
565 batteryLevel = (byte)((bat>>8)&0xff);
566 batteryStatus = (byte)((bat>>16)&0xf);
567 batteryHealth = (byte)((bat>>20)&0xf);
568 batteryPlugType = (byte)((bat>>24)&0xf);
569 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900570 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700571 batteryVoltage = (char)((bat>>16)&0xffff);
572 states = src.readInt();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800573 if (cmd == CMD_EVENT) {
574 eventCode = src.readInt();
575 eventUid = src.readInt();
576 eventName = src.readString();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700577 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800578 eventCode = EVENT_NONE;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700579 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700580 }
581
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700582 public void clear() {
583 time = 0;
584 cmd = CMD_NULL;
585 batteryLevel = 0;
586 batteryStatus = 0;
587 batteryHealth = 0;
588 batteryPlugType = 0;
589 batteryTemperature = 0;
590 batteryVoltage = 0;
591 states = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800592 eventCode = EVENT_NONE;
593 eventUid = 0;
594 eventName = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700595 }
596
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700597 public void setTo(HistoryItem o) {
598 time = o.time;
599 cmd = o.cmd;
600 batteryLevel = o.batteryLevel;
601 batteryStatus = o.batteryStatus;
602 batteryHealth = o.batteryHealth;
603 batteryPlugType = o.batteryPlugType;
604 batteryTemperature = o.batteryTemperature;
605 batteryVoltage = o.batteryVoltage;
606 states = o.states;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800607 eventCode = o.eventCode;
608 eventUid = o.eventUid;
609 eventName = o.eventName;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700610 }
611
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800612 public void setTo(long time, byte cmd, int eventCode, int eventUid, String eventName,
613 HistoryItem o) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700614 this.time = time;
615 this.cmd = cmd;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800616 this.eventCode = eventCode;
617 this.eventUid = eventUid;
618 this.eventName = eventName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700619 batteryLevel = o.batteryLevel;
620 batteryStatus = o.batteryStatus;
621 batteryHealth = o.batteryHealth;
622 batteryPlugType = o.batteryPlugType;
623 batteryTemperature = o.batteryTemperature;
624 batteryVoltage = o.batteryVoltage;
625 states = o.states;
626 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700627
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800628 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700629 return batteryLevel == o.batteryLevel
630 && batteryStatus == o.batteryStatus
631 && batteryHealth == o.batteryHealth
632 && batteryPlugType == o.batteryPlugType
633 && batteryTemperature == o.batteryTemperature
634 && batteryVoltage == o.batteryVoltage
635 && states == o.states;
636 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800637
638 public boolean same(HistoryItem o) {
639 if (!sameNonEvent(o) || eventCode != o.eventCode || eventUid != o.eventUid) {
640 return false;
641 }
642 if (eventName == o.eventName) {
643 return true;
644 }
645 return eventName != null && o.eventName != null && eventName.equals(o.eventName);
646 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700647 }
648
649 public static final class BitDescription {
650 public final int mask;
651 public final int shift;
652 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800653 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700654 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800655 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700656
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800657 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700658 this.mask = mask;
659 this.shift = -1;
660 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800661 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700662 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800663 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700664 }
665
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800666 public BitDescription(int mask, int shift, String name, String shortName,
667 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700668 this.mask = mask;
669 this.shift = shift;
670 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800671 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700672 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800673 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700674 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700675 }
676
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700677 public abstract boolean startIteratingHistoryLocked();
678
679 public abstract boolean getNextHistoryLocked(HistoryItem out);
680
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700681 public abstract void finishIteratingHistoryLocked();
682
683 public abstract boolean startIteratingOldHistoryLocked();
684
685 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
686
687 public abstract void finishIteratingOldHistoryLocked();
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700690 * Return the base time offset for the battery history.
691 */
692 public abstract long getHistoryBaseTime();
693
694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 * Returns the number of times the device has been started.
696 */
697 public abstract int getStartCount();
698
699 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700700 * 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 -0800701 * running on battery.
702 *
703 * {@hide}
704 */
705 public abstract long getScreenOnTime(long batteryRealtime, int which);
706
Dianne Hackborn617f8772009-03-31 15:04:46 -0700707 public static final int SCREEN_BRIGHTNESS_DARK = 0;
708 public static final int SCREEN_BRIGHTNESS_DIM = 1;
709 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
710 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
711 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
712
713 static final String[] SCREEN_BRIGHTNESS_NAMES = {
714 "dark", "dim", "medium", "light", "bright"
715 };
716
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800717 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
718 "0", "1", "2", "3", "4"
719 };
720
Dianne Hackborn617f8772009-03-31 15:04:46 -0700721 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
722
723 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700724 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700725 * the given brightness
726 *
727 * {@hide}
728 */
729 public abstract long getScreenBrightnessTime(int brightnessBin,
730 long batteryRealtime, int which);
731
732 public abstract int getInputEventCount(int which);
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700735 * 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 -0800736 * running on battery.
737 *
738 * {@hide}
739 */
740 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700741
742 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700743 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700744 * the given signal strength.
745 *
746 * {@hide}
747 */
748 public abstract long getPhoneSignalStrengthTime(int strengthBin,
749 long batteryRealtime, int which);
750
Dianne Hackborn617f8772009-03-31 15:04:46 -0700751 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700752 * Returns the time in microseconds that the phone has been trying to
753 * acquire a signal.
754 *
755 * {@hide}
756 */
757 public abstract long getPhoneSignalScanningTime(
758 long batteryRealtime, int which);
759
760 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700761 * Returns the number of times the phone has entered the given signal strength.
762 *
763 * {@hide}
764 */
765 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
766
Dianne Hackborn627bba72009-03-24 22:32:56 -0700767 public static final int DATA_CONNECTION_NONE = 0;
768 public static final int DATA_CONNECTION_GPRS = 1;
769 public static final int DATA_CONNECTION_EDGE = 2;
770 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700771 public static final int DATA_CONNECTION_CDMA = 4;
772 public static final int DATA_CONNECTION_EVDO_0 = 5;
773 public static final int DATA_CONNECTION_EVDO_A = 6;
774 public static final int DATA_CONNECTION_1xRTT = 7;
775 public static final int DATA_CONNECTION_HSDPA = 8;
776 public static final int DATA_CONNECTION_HSUPA = 9;
777 public static final int DATA_CONNECTION_HSPA = 10;
778 public static final int DATA_CONNECTION_IDEN = 11;
779 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700780 public static final int DATA_CONNECTION_LTE = 13;
781 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800782 public static final int DATA_CONNECTION_HSPAP = 15;
783 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700784
Dianne Hackborn627bba72009-03-24 22:32:56 -0700785 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700786 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700787 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800788 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700789 };
790
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700791 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700792
793 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700794 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700795 * the given data connection.
796 *
797 * {@hide}
798 */
799 public abstract long getPhoneDataConnectionTime(int dataType,
800 long batteryRealtime, int which);
801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700803 * Returns the number of times the phone has entered the given data
804 * connection type.
805 *
806 * {@hide}
807 */
808 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700809
810 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
811 = new BitDescription[] {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800812 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
813 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
814 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
815 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
816 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
817 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
818 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
819 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
820 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
821 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
822 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
823 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
824 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
825 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
826 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700827 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800828 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
829 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700830 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800831 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
832 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
833 "0", "1", "2", "3", "4"
834 }),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700835 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800836 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
837 new String[] {"in", "out", "emergency", "off"},
838 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700839 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800840 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
841 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700842 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700843
844 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700845 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700846 * running on battery.
847 *
848 * {@hide}
849 */
850 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700851
852 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700853 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700854 * been in the running state while the device was running on battery.
855 *
856 * {@hide}
857 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700858 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700859
The Android Open Source Project10592532009-03-18 17:39:46 -0700860 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700861 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700862 * running on battery.
863 *
864 * {@hide}
865 */
866 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
867
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800868 public static final int NETWORK_MOBILE_RX_DATA = 0;
869 public static final int NETWORK_MOBILE_TX_DATA = 1;
870 public static final int NETWORK_WIFI_RX_DATA = 2;
871 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700872
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800873 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700874
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800875 public abstract long getNetworkActivityBytes(int type, int which);
876 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700877
The Android Open Source Project10592532009-03-18 17:39:46 -0700878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 * Return whether we are currently running on battery.
880 */
881 public abstract boolean getIsOnBattery();
882
883 /**
884 * Returns a SparseArray containing the statistics for each uid.
885 */
886 public abstract SparseArray<? extends Uid> getUidStats();
887
888 /**
889 * Returns the current battery uptime in microseconds.
890 *
891 * @param curTime the amount of elapsed realtime in microseconds.
892 */
893 public abstract long getBatteryUptime(long curTime);
894
895 /**
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700896 * @deprecated use getRadioDataUptime
897 */
898 public long getRadioDataUptimeMs() {
899 return getRadioDataUptime() / 1000;
900 }
901
902 /**
903 * Returns the time that the radio was on for data transfers.
904 * @return the uptime in microseconds while unplugged
905 */
906 public abstract long getRadioDataUptime();
907
908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * Returns the current battery realtime in microseconds.
910 *
911 * @param curTime the amount of elapsed realtime in microseconds.
912 */
913 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -0700914
915 /**
Evan Millar633a1742009-04-02 16:36:33 -0700916 * Returns the battery percentage level at the last time the device was unplugged from power, or
917 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -0700918 */
Evan Millar633a1742009-04-02 16:36:33 -0700919 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -0700920
921 /**
Evan Millar633a1742009-04-02 16:36:33 -0700922 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
923 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -0700924 */
Evan Millar633a1742009-04-02 16:36:33 -0700925 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700928 * Get the amount the battery has discharged since the stats were
929 * last reset after charging, as a lower-end approximation.
930 */
931 public abstract int getLowDischargeAmountSinceCharge();
932
933 /**
934 * Get the amount the battery has discharged since the stats were
935 * last reset after charging, as an upper-end approximation.
936 */
937 public abstract int getHighDischargeAmountSinceCharge();
938
939 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800940 * Get the amount the battery has discharged while the screen was on,
941 * since the last time power was unplugged.
942 */
943 public abstract int getDischargeAmountScreenOn();
944
945 /**
946 * Get the amount the battery has discharged while the screen was on,
947 * since the last time the device was charged.
948 */
949 public abstract int getDischargeAmountScreenOnSinceCharge();
950
951 /**
952 * Get the amount the battery has discharged while the screen was off,
953 * since the last time power was unplugged.
954 */
955 public abstract int getDischargeAmountScreenOff();
956
957 /**
958 * Get the amount the battery has discharged while the screen was off,
959 * since the last time the device was charged.
960 */
961 public abstract int getDischargeAmountScreenOffSinceCharge();
962
963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * Returns the total, last, or current battery uptime in microseconds.
965 *
966 * @param curTime the elapsed realtime in microseconds.
967 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
968 */
969 public abstract long computeBatteryUptime(long curTime, int which);
970
971 /**
972 * Returns the total, last, or current battery realtime in microseconds.
973 *
974 * @param curTime the current elapsed realtime in microseconds.
975 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
976 */
977 public abstract long computeBatteryRealtime(long curTime, int which);
978
979 /**
980 * Returns the total, last, or current uptime in microseconds.
981 *
982 * @param curTime the current elapsed realtime in microseconds.
983 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
984 */
985 public abstract long computeUptime(long curTime, int which);
986
987 /**
988 * Returns the total, last, or current realtime in microseconds.
989 * *
990 * @param curTime the current elapsed realtime in microseconds.
991 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
992 */
993 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -0700994
995 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
Amith Yamasanie43530a2009-08-21 13:11:37 -0700997 /** Returns the number of different speeds that the CPU can run at */
998 public abstract int getCpuSpeedSteps();
999
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001000 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 long days = seconds / (60 * 60 * 24);
1002 if (days != 0) {
1003 out.append(days);
1004 out.append("d ");
1005 }
1006 long used = days * 60 * 60 * 24;
1007
1008 long hours = (seconds - used) / (60 * 60);
1009 if (hours != 0 || used != 0) {
1010 out.append(hours);
1011 out.append("h ");
1012 }
1013 used += hours * 60 * 60;
1014
1015 long mins = (seconds-used) / 60;
1016 if (mins != 0 || used != 0) {
1017 out.append(mins);
1018 out.append("m ");
1019 }
1020 used += mins * 60;
1021
1022 if (seconds != 0 || used != 0) {
1023 out.append(seconds-used);
1024 out.append("s ");
1025 }
1026 }
1027
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001028 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001030 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 sb.append((time - (sec * 100)) * 10);
1032 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001035 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001037 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 sb.append(time - (sec * 1000));
1039 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
1042 private final String formatRatioLocked(long num, long den) {
1043 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001044 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 float perc = ((float)num) / ((float)den) * 100;
1047 mFormatBuilder.setLength(0);
1048 mFormatter.format("%.1f%%", perc);
1049 return mFormatBuilder.toString();
1050 }
1051
Evan Millar22ac0432009-03-31 11:33:18 -07001052 private final String formatBytesLocked(long bytes) {
1053 mFormatBuilder.setLength(0);
1054
1055 if (bytes < BYTES_PER_KB) {
1056 return bytes + "B";
1057 } else if (bytes < BYTES_PER_MB) {
1058 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1059 return mFormatBuilder.toString();
1060 } else if (bytes < BYTES_PER_GB){
1061 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1062 return mFormatBuilder.toString();
1063 } else {
1064 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1065 return mFormatBuilder.toString();
1066 }
1067 }
1068
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001069 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1070 if (timer != null) {
1071 // Convert from microseconds to milliseconds with rounding
1072 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1073 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1074 return totalTimeMillis;
1075 }
1076 return 0;
1077 }
1078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 /**
1080 *
1081 * @param sb a StringBuilder object.
1082 * @param timer a Timer object contining the wakelock times.
1083 * @param batteryRealtime the current on-battery time in microseconds.
1084 * @param name the name of the wakelock.
1085 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1086 * @param linePrefix a String to be prepended to each line of output.
1087 * @return the line prefix
1088 */
1089 private static final String printWakeLock(StringBuilder sb, Timer timer,
1090 long batteryRealtime, String name, int which, String linePrefix) {
1091
1092 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001093 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094
Evan Millarc64edde2009-04-18 12:26:32 -07001095 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (totalTimeMillis != 0) {
1097 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001098 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001099 if (name != null) {
1100 sb.append(name);
1101 sb.append(' ');
1102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 sb.append('(');
1104 sb.append(count);
1105 sb.append(" times)");
1106 return ", ";
1107 }
1108 }
1109 return linePrefix;
1110 }
1111
1112 /**
1113 * Checkin version of wakelock printer. Prints simple comma-separated list.
1114 *
1115 * @param sb a StringBuilder object.
1116 * @param timer a Timer object contining the wakelock times.
1117 * @param now the current time in microseconds.
1118 * @param name the name of the wakelock.
1119 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1120 * @param linePrefix a String to be prepended to each line of output.
1121 * @return the line prefix
1122 */
1123 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001124 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 long totalTimeMicros = 0;
1126 int count = 0;
1127 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001128 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1129 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131 sb.append(linePrefix);
1132 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1133 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001134 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 sb.append(count);
1136 return ",";
1137 }
1138
1139 /**
1140 * Dump a comma-separated line of values for terse checkin mode.
1141 *
1142 * @param pw the PageWriter to dump log to
1143 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1144 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1145 * @param args type-dependent data arguments
1146 */
1147 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1148 Object... args ) {
1149 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1150 pw.print(uid); pw.print(',');
1151 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001152 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153
1154 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001155 pw.print(',');
1156 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001158 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
1161 /**
1162 * Checkin server version of dump to produce more compact, computer-readable log.
1163 *
1164 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001166 public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1168 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1169 final long batteryUptime = getBatteryUptime(rawUptime);
1170 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1171 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1172 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1173 final long totalRealtime = computeRealtime(rawRealtime, which);
1174 final long totalUptime = computeUptime(rawUptime, which);
1175 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1176 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001177 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001178 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001179 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
1181 StringBuilder sb = new StringBuilder(128);
1182
Evan Millar22ac0432009-03-31 11:33:18 -07001183 SparseArray<? extends Uid> uidStats = getUidStats();
1184 final int NU = uidStats.size();
1185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 // Dump "battery" stat
1189 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001190 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001191 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1192 totalRealtime / 1000, totalUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001194 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001195 long fullWakeLockTimeTotal = 0;
1196 long partialWakeLockTimeTotal = 0;
1197
1198 for (int iu = 0; iu < NU; iu++) {
1199 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001200
Evan Millar22ac0432009-03-31 11:33:18 -07001201 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1202 if (wakelocks.size() > 0) {
1203 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1204 : wakelocks.entrySet()) {
1205 Uid.Wakelock wl = ent.getValue();
1206
1207 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1208 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001209 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001210 }
1211
1212 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1213 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001214 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001215 batteryRealtime, which);
1216 }
1217 }
1218 }
1219 }
1220
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001221 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1222 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1223 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1224 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1225 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1226 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1227 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1228 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1229
1230 // Dump network stats
1231 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1232 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1233 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 // Dump misc stats
1236 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001237 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001238 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001239 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001240 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1241 getInputEventCount(which));
1242
1243 // Dump screen brightness stats
1244 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1245 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1246 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1247 }
1248 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001249
Dianne Hackborn627bba72009-03-24 22:32:56 -07001250 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001251 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1252 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001253 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1254 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001255 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001256 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1257 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001258 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001259 args[i] = getPhoneSignalStrengthCount(i, which);
1260 }
1261 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001262
1263 // Dump network type stats
1264 args = new Object[NUM_DATA_CONNECTION_TYPES];
1265 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1266 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1267 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001268 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1269 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1270 args[i] = getPhoneDataConnectionCount(i, which);
1271 }
1272 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001273
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001274 if (which == STATS_SINCE_UNPLUGGED) {
Evan Millare84de8d2009-04-02 22:16:12 -07001275 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001276 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001279 if (which == STATS_SINCE_UNPLUGGED) {
1280 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1281 getDischargeStartLevel()-getDischargeCurrentLevel(),
1282 getDischargeStartLevel()-getDischargeCurrentLevel(),
1283 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1284 } else {
1285 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1286 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1287 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1288 }
1289
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001290 if (reqUid < 0) {
1291 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1292 if (kernelWakelocks.size() > 0) {
1293 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1294 sb.setLength(0);
1295 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1296
1297 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1298 sb.toString());
1299 }
Evan Millarc64edde2009-04-18 12:26:32 -07001300 }
1301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 for (int iu = 0; iu < NU; iu++) {
1304 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001305 if (reqUid >= 0 && uid != reqUid) {
1306 continue;
1307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 Uid u = uidStats.valueAt(iu);
1309 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001310 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1311 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1312 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1313 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1314 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1315 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1316 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1317 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001318 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001319 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001320 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001321
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001322 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1323 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
1324 || wifiPacketsTx > 0) {
1325 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1326 wifiBytesRx, wifiBytesTx,
1327 mobilePacketsRx, mobilePacketsTx,
1328 wifiPacketsRx, wifiPacketsTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001329 }
1330
Nick Pelly6ccaa542012-06-15 15:22:47 -07001331 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001332 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001333 dumpLine(pw, uid, category, WIFI_DATA,
1334 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336
Dianne Hackborn617f8772009-03-31 15:04:46 -07001337 if (u.hasUserActivity()) {
1338 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1339 boolean hasData = false;
1340 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1341 int val = u.getUserActivityCount(i, which);
1342 args[i] = val;
1343 if (val != 0) hasData = true;
1344 }
1345 if (hasData) {
1346 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1347 }
1348 }
1349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1351 if (wakelocks.size() > 0) {
1352 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1353 : wakelocks.entrySet()) {
1354 Uid.Wakelock wl = ent.getValue();
1355 String linePrefix = "";
1356 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001357 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1358 batteryRealtime, "f", which, linePrefix);
1359 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1360 batteryRealtime, "p", which, linePrefix);
1361 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1362 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
1364 // Only log if we had at lease one wakelock...
1365 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001366 String name = ent.getKey();
1367 if (name.indexOf(',') >= 0) {
1368 name = name.replace(',', '_');
1369 }
1370 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373 }
1374
1375 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1376 if (sensors.size() > 0) {
1377 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1378 : sensors.entrySet()) {
1379 Uid.Sensor se = ent.getValue();
1380 int sensorNumber = ent.getKey();
1381 Timer timer = se.getSensorTime();
1382 if (timer != null) {
1383 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001384 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1385 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 if (totalTime != 0) {
1387 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1388 }
1389 }
1390 }
1391 }
1392
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001393 Timer vibTimer = u.getVibratorOnTimer();
1394 if (vibTimer != null) {
1395 // Convert from microseconds to milliseconds with rounding
1396 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1397 int count = vibTimer.getCountLocked(which);
1398 if (totalTime != 0) {
1399 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1400 }
1401 }
1402
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001403 Timer fgTimer = u.getForegroundActivityTimer();
1404 if (fgTimer != null) {
1405 // Convert from microseconds to milliseconds with rounding
1406 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1407 int count = fgTimer.getCountLocked(which);
1408 if (totalTime != 0) {
1409 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1410 }
1411 }
1412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1414 if (processStats.size() > 0) {
1415 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1416 : processStats.entrySet()) {
1417 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001418
1419 final long userMillis = ps.getUserTime(which) * 10;
1420 final long systemMillis = ps.getSystemTime(which) * 10;
1421 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1422 final long starts = ps.getStarts(which);
1423
1424 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1425 || starts != 0) {
1426 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1427 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429 }
1430 }
1431
1432 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1433 if (packageStats.size() > 0) {
1434 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1435 : packageStats.entrySet()) {
1436
1437 Uid.Pkg ps = ent.getValue();
1438 int wakeups = ps.getWakeups(which);
1439 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1440 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1441 : serviceStats.entrySet()) {
1442 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1443 long startTime = ss.getStartTime(batteryUptime, which);
1444 int starts = ss.getStarts(which);
1445 int launches = ss.getLaunches(which);
1446 if (startTime != 0 || starts != 0 || launches != 0) {
1447 dumpLine(pw, uid, category, APK_DATA,
1448 wakeups, // wakeup alarms
1449 ent.getKey(), // Apk
1450 sent.getKey(), // service
1451 startTime / 1000, // time spent started, in ms
1452 starts,
1453 launches);
1454 }
1455 }
1456 }
1457 }
1458 }
1459 }
1460
Dianne Hackborn81038902012-11-26 17:04:09 -08001461 static final class TimerEntry {
1462 final String mName;
1463 final int mId;
1464 final BatteryStats.Timer mTimer;
1465 final long mTime;
1466 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1467 mName = name;
1468 mId = id;
1469 mTimer = timer;
1470 mTime = time;
1471 }
1472 }
1473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 @SuppressWarnings("unused")
Dianne Hackborn81038902012-11-26 17:04:09 -08001475 public final void dumpLocked(PrintWriter pw, String prefix, final int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1477 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1478 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001479 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480
1481 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1482 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1483 final long totalRealtime = computeRealtime(rawRealtime, which);
1484 final long totalUptime = computeUptime(rawUptime, which);
1485
1486 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001487
1488 SparseArray<? extends Uid> uidStats = getUidStats();
1489 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001491 sb.setLength(0);
1492 sb.append(prefix);
1493 sb.append(" Time on battery: ");
1494 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1495 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1496 sb.append(") realtime, ");
1497 formatTimeMs(sb, whichBatteryUptime / 1000);
1498 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1499 sb.append(") uptime");
1500 pw.println(sb.toString());
1501 sb.setLength(0);
1502 sb.append(prefix);
1503 sb.append(" Total run time: ");
1504 formatTimeMs(sb, totalRealtime / 1000);
1505 sb.append("realtime, ");
1506 formatTimeMs(sb, totalUptime / 1000);
1507 sb.append("uptime, ");
1508 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509
The Android Open Source Project10592532009-03-18 17:39:46 -07001510 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1511 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001512 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001513 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1514 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001515 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001516 sb.append(prefix);
1517 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1518 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1519 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1520 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1521 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1522 sb.append(")");
1523 pw.println(sb.toString());
1524 sb.setLength(0);
1525 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001526 sb.append(" Screen brightnesses: ");
1527 boolean didOne = false;
1528 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1529 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1530 if (time == 0) {
1531 continue;
1532 }
1533 if (didOne) sb.append(", ");
1534 didOne = true;
1535 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1536 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001537 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001538 sb.append("(");
1539 sb.append(formatRatioLocked(time, screenOnTime));
1540 sb.append(")");
1541 }
1542 if (!didOne) sb.append("No activity");
1543 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001544
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001545 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001546 long fullWakeLockTimeTotalMicros = 0;
1547 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001548
1549 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1550 @Override
1551 public int compare(TimerEntry lhs, TimerEntry rhs) {
1552 long lhsTime = lhs.mTime;
1553 long rhsTime = rhs.mTime;
1554 if (lhsTime < rhsTime) {
1555 return 1;
1556 }
1557 if (lhsTime > rhsTime) {
1558 return -1;
1559 }
1560 return 0;
1561 }
1562 };
1563
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001564 if (reqUid < 0) {
1565 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1566 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001567 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001568 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001569 BatteryStats.Timer timer = ent.getValue();
1570 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1571 if (totalTimeMillis > 0) {
1572 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1573 }
1574 }
1575 Collections.sort(timers, timerComparator);
1576 for (int i=0; i<timers.size(); i++) {
1577 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001578 String linePrefix = ": ";
1579 sb.setLength(0);
1580 sb.append(prefix);
1581 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001582 sb.append(timer.mName);
1583 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1584 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001585 if (!linePrefix.equals(": ")) {
1586 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001587 // Only print out wake locks that were held
1588 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001589 }
Evan Millarc64edde2009-04-18 12:26:32 -07001590 }
Evan Millarc64edde2009-04-18 12:26:32 -07001591 }
1592 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001593
1594 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1595
Evan Millar22ac0432009-03-31 11:33:18 -07001596 for (int iu = 0; iu < NU; iu++) {
1597 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001598
Evan Millar22ac0432009-03-31 11:33:18 -07001599 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1600 if (wakelocks.size() > 0) {
1601 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1602 : wakelocks.entrySet()) {
1603 Uid.Wakelock wl = ent.getValue();
1604
1605 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1606 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001607 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001608 batteryRealtime, which);
1609 }
1610
1611 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1612 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001613 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001614 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001615 if (totalTimeMicros > 0) {
1616 if (reqUid < 0) {
1617 // Only show the ordered list of all wake
1618 // locks if the caller is not asking for data
1619 // about a specific uid.
1620 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1621 partialWakeTimer, totalTimeMicros));
1622 }
1623 partialWakeLockTimeTotalMicros += totalTimeMicros;
1624 }
Evan Millar22ac0432009-03-31 11:33:18 -07001625 }
1626 }
1627 }
1628 }
1629
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001630 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1631 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1632 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1633 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1634 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1635 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1636 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1637 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1638
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001639 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001640 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1641 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1642 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1643 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001644 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001645 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1646 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1647 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1648 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001649 sb.setLength(0);
1650 sb.append(prefix);
1651 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1652 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001653 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001654 (partialWakeLockTimeTotalMicros + 500) / 1000);
1655 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001656
Dianne Hackborn627bba72009-03-24 22:32:56 -07001657 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001658 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001659 sb.append(" Signal levels: ");
1660 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001661 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001662 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1663 if (time == 0) {
1664 continue;
1665 }
1666 if (didOne) sb.append(", ");
1667 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001668 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001669 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001670 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001671 sb.append("(");
1672 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001673 sb.append(") ");
1674 sb.append(getPhoneSignalStrengthCount(i, which));
1675 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001676 }
1677 if (!didOne) sb.append("No activity");
1678 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001679
1680 sb.setLength(0);
1681 sb.append(prefix);
1682 sb.append(" Signal scanning time: ");
1683 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1684 pw.println(sb.toString());
1685
Dianne Hackborn627bba72009-03-24 22:32:56 -07001686 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001687 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001688 sb.append(" Radio types: ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001689 didOne = false;
1690 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1691 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1692 if (time == 0) {
1693 continue;
1694 }
1695 if (didOne) sb.append(", ");
1696 didOne = true;
1697 sb.append(DATA_CONNECTION_NAMES[i]);
1698 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001699 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001700 sb.append("(");
1701 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001702 sb.append(") ");
1703 sb.append(getPhoneDataConnectionCount(i, which));
1704 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001705 }
1706 if (!didOne) sb.append("No activity");
1707 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001708
1709 sb.setLength(0);
1710 sb.append(prefix);
1711 sb.append(" Radio data uptime when unplugged: ");
1712 sb.append(getRadioDataUptime() / 1000);
1713 sb.append(" ms");
1714 pw.println(sb.toString());
1715
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001716 sb.setLength(0);
1717 sb.append(prefix);
1718 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1719 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1720 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1721 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1722 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1723 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1724 sb.append(")");
1725 pw.println(sb.toString());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001726
The Android Open Source Project10592532009-03-18 17:39:46 -07001727 pw.println(" ");
1728
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001729 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001730 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001731 pw.print(prefix); pw.println(" Device is currently unplugged");
1732 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1733 pw.println(getDischargeStartLevel());
1734 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1735 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07001736 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001737 pw.print(prefix); pw.println(" Device is currently plugged into power");
1738 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1739 pw.println(getDischargeStartLevel());
1740 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1741 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001742 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001743 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1744 pw.println(getDischargeAmountScreenOn());
1745 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1746 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001747 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001748 } else {
1749 pw.print(prefix); pw.println(" Device battery use since last full charge");
1750 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1751 pw.println(getLowDischargeAmountSinceCharge());
1752 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1753 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001754 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1755 pw.println(getDischargeAmountScreenOnSinceCharge());
1756 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1757 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08001758 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001760
1761 if (timers.size() > 0) {
1762 Collections.sort(timers, timerComparator);
1763 pw.print(prefix); pw.println(" All partial wake locks:");
1764 for (int i=0; i<timers.size(); i++) {
1765 TimerEntry timer = timers.get(i);
1766 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001767 sb.append(" Wake lock ");
1768 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08001769 sb.append(" ");
1770 sb.append(timer.mName);
1771 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1772 sb.append(" realtime");
1773 pw.println(sb.toString());
1774 }
1775 timers.clear();
1776 pw.println();
1777 }
Evan Millar22ac0432009-03-31 11:33:18 -07001778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 for (int iu=0; iu<NU; iu++) {
1780 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08001781 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001782 continue;
1783 }
1784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001786
1787 pw.print(prefix);
1788 pw.print(" ");
1789 UserHandle.formatUid(pw, uid);
1790 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 boolean uidActivity = false;
1792
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001793 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1794 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1795 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1796 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1797 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1798 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1799 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1800 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001801 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001802 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001803 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001804
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001805 if (mobileRxBytes > 0 || mobileTxBytes > 0
1806 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001807 pw.print(prefix); pw.print(" Mobile network: ");
1808 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001809 pw.print(formatBytesLocked(mobileTxBytes));
1810 pw.print(" sent (packets "); pw.print(mobileRxPackets);
1811 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001813 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001814 pw.print(prefix); pw.print(" Wi-Fi network: ");
1815 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001816 pw.print(formatBytesLocked(wifiTxBytes));
1817 pw.print(" sent (packets "); pw.print(wifiRxPackets);
1818 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001819 }
1820
Dianne Hackborn617f8772009-03-31 15:04:46 -07001821 if (u.hasUserActivity()) {
1822 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07001823 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001824 int val = u.getUserActivityCount(i, which);
1825 if (val != 0) {
1826 if (!hasData) {
1827 sb.setLength(0);
1828 sb.append(" User activity: ");
1829 hasData = true;
1830 } else {
1831 sb.append(", ");
1832 }
1833 sb.append(val);
1834 sb.append(" ");
1835 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1836 }
1837 }
1838 if (hasData) {
1839 pw.println(sb.toString());
1840 }
1841 }
1842
Nick Pelly6ccaa542012-06-15 15:22:47 -07001843 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001844 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001845 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001846 sb.append(prefix); sb.append(" Wifi Running: ");
1847 formatTimeMs(sb, uidWifiRunningTime / 1000);
1848 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001849 whichBatteryRealtime)); sb.append(")\n");
1850 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001851 formatTimeMs(sb, fullWifiLockOnTime / 1000);
1852 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001853 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07001854 sb.append(prefix); sb.append(" Wifi Scan: ");
1855 formatTimeMs(sb, wifiScanTime / 1000);
1856 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001857 whichBatteryRealtime)); sb.append(")");
1858 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860
1861 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1862 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001863 long totalFull = 0, totalPartial = 0, totalWindow = 0;
1864 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1866 : wakelocks.entrySet()) {
1867 Uid.Wakelock wl = ent.getValue();
1868 String linePrefix = ": ";
1869 sb.setLength(0);
1870 sb.append(prefix);
1871 sb.append(" Wake lock ");
1872 sb.append(ent.getKey());
1873 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
1874 "full", which, linePrefix);
1875 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
1876 "partial", which, linePrefix);
1877 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
1878 "window", which, linePrefix);
1879 if (!linePrefix.equals(": ")) {
1880 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001881 // Only print out wake locks that were held
1882 pw.println(sb.toString());
1883 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001884 count++;
1885 }
1886 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
1887 batteryRealtime, which);
1888 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
1889 batteryRealtime, which);
1890 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
1891 batteryRealtime, which);
1892 }
1893 if (count > 1) {
1894 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
1895 sb.setLength(0);
1896 sb.append(prefix);
1897 sb.append(" TOTAL wake: ");
1898 boolean needComma = false;
1899 if (totalFull != 0) {
1900 needComma = true;
1901 formatTimeMs(sb, totalFull);
1902 sb.append("full");
1903 }
1904 if (totalPartial != 0) {
1905 if (needComma) {
1906 sb.append(", ");
1907 }
1908 needComma = true;
1909 formatTimeMs(sb, totalPartial);
1910 sb.append("partial");
1911 }
1912 if (totalWindow != 0) {
1913 if (needComma) {
1914 sb.append(", ");
1915 }
1916 needComma = true;
1917 formatTimeMs(sb, totalWindow);
1918 sb.append("window");
1919 }
1920 sb.append(" realtime");
1921 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924 }
1925
1926 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1927 if (sensors.size() > 0) {
1928 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1929 : sensors.entrySet()) {
1930 Uid.Sensor se = ent.getValue();
1931 int sensorNumber = ent.getKey();
1932 sb.setLength(0);
1933 sb.append(prefix);
1934 sb.append(" Sensor ");
1935 int handle = se.getHandle();
1936 if (handle == Uid.Sensor.GPS) {
1937 sb.append("GPS");
1938 } else {
1939 sb.append(handle);
1940 }
1941 sb.append(": ");
1942
1943 Timer timer = se.getSensorTime();
1944 if (timer != null) {
1945 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001946 long totalTime = (timer.getTotalTimeLocked(
1947 batteryRealtime, which) + 500) / 1000;
1948 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 //timer.logState();
1950 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001951 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 sb.append("realtime (");
1953 sb.append(count);
1954 sb.append(" times)");
1955 } else {
1956 sb.append("(not used)");
1957 }
1958 } else {
1959 sb.append("(not used)");
1960 }
1961
1962 pw.println(sb.toString());
1963 uidActivity = true;
1964 }
1965 }
1966
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001967 Timer vibTimer = u.getVibratorOnTimer();
1968 if (vibTimer != null) {
1969 // Convert from microseconds to milliseconds with rounding
1970 long totalTime = (vibTimer.getTotalTimeLocked(
1971 batteryRealtime, which) + 500) / 1000;
1972 int count = vibTimer.getCountLocked(which);
1973 //timer.logState();
1974 if (totalTime != 0) {
1975 sb.setLength(0);
1976 sb.append(prefix);
1977 sb.append(" Vibrator: ");
1978 formatTimeMs(sb, totalTime);
1979 sb.append("realtime (");
1980 sb.append(count);
1981 sb.append(" times)");
1982 pw.println(sb.toString());
1983 uidActivity = true;
1984 }
1985 }
1986
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001987 Timer fgTimer = u.getForegroundActivityTimer();
1988 if (fgTimer != null) {
1989 // Convert from microseconds to milliseconds with rounding
1990 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1991 int count = fgTimer.getCountLocked(which);
1992 if (totalTime != 0) {
1993 sb.setLength(0);
1994 sb.append(prefix);
1995 sb.append(" Foreground activities: ");
1996 formatTimeMs(sb, totalTime);
1997 sb.append("realtime (");
1998 sb.append(count);
1999 sb.append(" times)");
2000 pw.println(sb.toString());
2001 uidActivity = true;
2002 }
2003 }
2004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2006 if (processStats.size() > 0) {
2007 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2008 : processStats.entrySet()) {
2009 Uid.Proc ps = ent.getValue();
2010 long userTime;
2011 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002012 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002014 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015
2016 userTime = ps.getUserTime(which);
2017 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002018 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002020 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002021 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002023 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002024 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002025 sb.setLength(0);
2026 sb.append(prefix); sb.append(" Proc ");
2027 sb.append(ent.getKey()); sb.append(":\n");
2028 sb.append(prefix); sb.append(" CPU: ");
2029 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002030 formatTime(sb, systemTime); sb.append("krn ; ");
2031 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002032 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002033 sb.append("\n"); sb.append(prefix); sb.append(" ");
2034 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002035 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002036 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002037 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002038 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002039 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002040 pw.print(prefix); pw.print(" * Killed for ");
2041 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2042 pw.print("wake lock");
2043 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2044 pw.print("cpu");
2045 } else {
2046 pw.print("unknown");
2047 }
2048 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002049 TimeUtils.formatDuration(ew.usedTime, pw);
2050 pw.print(" over ");
2051 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002052 if (ew.overTime != 0) {
2053 pw.print(" (");
2054 pw.print((ew.usedTime*100)/ew.overTime);
2055 pw.println("%)");
2056 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002057 }
2058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 uidActivity = true;
2060 }
2061 }
2062 }
2063
2064 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2065 if (packageStats.size() > 0) {
2066 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2067 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002068 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 boolean apkActivity = false;
2070 Uid.Pkg ps = ent.getValue();
2071 int wakeups = ps.getWakeups(which);
2072 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002073 pw.print(prefix); pw.print(" ");
2074 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 apkActivity = true;
2076 }
2077 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2078 if (serviceStats.size() > 0) {
2079 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2080 : serviceStats.entrySet()) {
2081 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2082 long startTime = ss.getStartTime(batteryUptime, which);
2083 int starts = ss.getStarts(which);
2084 int launches = ss.getLaunches(which);
2085 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002086 sb.setLength(0);
2087 sb.append(prefix); sb.append(" Service ");
2088 sb.append(sent.getKey()); sb.append(":\n");
2089 sb.append(prefix); sb.append(" Created for: ");
2090 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002091 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002092 sb.append(prefix); sb.append(" Starts: ");
2093 sb.append(starts);
2094 sb.append(", launches: "); sb.append(launches);
2095 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 apkActivity = true;
2097 }
2098 }
2099 }
2100 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002101 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103 uidActivity = true;
2104 }
2105 }
2106 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002107 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109 }
2110 }
2111
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002112 static void printBitDescriptions(PrintWriter pw, int oldval, int newval,
2113 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002114 int diff = oldval ^ newval;
2115 if (diff == 0) return;
2116 for (int i=0; i<descriptions.length; i++) {
2117 BitDescription bd = descriptions[i];
2118 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002119 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002120 if (bd.shift < 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002121 pw.print((newval&bd.mask) != 0 ? "+" : "-");
2122 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002123 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002124 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002125 pw.print("=");
2126 int val = (newval&bd.mask)>>bd.shift;
2127 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002128 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002129 } else {
2130 pw.print(val);
2131 }
2132 }
2133 }
2134 }
2135 }
2136
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002137 public void prepareForDumpLocked() {
2138 }
2139
2140 public static class HistoryPrinter {
2141 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002142 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002143 int oldStatus = -1;
2144 int oldHealth = -1;
2145 int oldPlug = -1;
2146 int oldTemp = -1;
2147 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002148 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002149
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002150 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2151 if (!checkin) {
2152 pw.print(" ");
2153 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002154 pw.print(" ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002155 } else {
2156 if (lastTime < 0) {
2157 pw.print("@");
2158 pw.print(rec.time-now);
2159 } else {
2160 pw.print(rec.time-lastTime);
2161 }
2162 lastTime = rec.time;
2163 }
2164 if (rec.cmd == HistoryItem.CMD_START) {
2165 if (checkin) {
2166 pw.print(":");
2167 }
2168 pw.println("START");
2169 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2170 if (checkin) {
2171 pw.print(":");
2172 }
2173 pw.println("*OVERFLOW*");
2174 } else {
2175 if (!checkin) {
2176 if (rec.batteryLevel < 10) pw.print("00");
2177 else if (rec.batteryLevel < 100) pw.print("0");
2178 pw.print(rec.batteryLevel);
2179 pw.print(" ");
2180 if (rec.states < 0x10) pw.print("0000000");
2181 else if (rec.states < 0x100) pw.print("000000");
2182 else if (rec.states < 0x1000) pw.print("00000");
2183 else if (rec.states < 0x10000) pw.print("0000");
2184 else if (rec.states < 0x100000) pw.print("000");
2185 else if (rec.states < 0x1000000) pw.print("00");
2186 else if (rec.states < 0x10000000) pw.print("0");
2187 pw.print(Integer.toHexString(rec.states));
2188 } else {
2189 if (oldLevel != rec.batteryLevel) {
2190 oldLevel = rec.batteryLevel;
2191 pw.print(",Bl="); pw.print(rec.batteryLevel);
2192 }
2193 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002194 if (oldStatus != rec.batteryStatus) {
2195 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002196 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002197 switch (oldStatus) {
2198 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002199 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002200 break;
2201 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002202 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002203 break;
2204 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002205 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002206 break;
2207 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002208 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002209 break;
2210 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002211 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002212 break;
2213 default:
2214 pw.print(oldStatus);
2215 break;
2216 }
2217 }
2218 if (oldHealth != rec.batteryHealth) {
2219 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002220 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002221 switch (oldHealth) {
2222 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002223 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002224 break;
2225 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002226 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002227 break;
2228 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002229 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002230 break;
2231 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002232 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002233 break;
2234 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002235 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002236 break;
2237 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002238 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002239 break;
2240 default:
2241 pw.print(oldHealth);
2242 break;
2243 }
2244 }
2245 if (oldPlug != rec.batteryPlugType) {
2246 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002247 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002248 switch (oldPlug) {
2249 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002250 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002251 break;
2252 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002253 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002254 break;
2255 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002256 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002257 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002258 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002259 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002260 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002261 default:
2262 pw.print(oldPlug);
2263 break;
2264 }
2265 }
2266 if (oldTemp != rec.batteryTemperature) {
2267 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002268 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002269 pw.print(oldTemp);
2270 }
2271 if (oldVolt != rec.batteryVoltage) {
2272 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002274 pw.print(oldVolt);
2275 }
2276 printBitDescriptions(pw, oldState, rec.states,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002277 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002278 pw.println();
2279 }
2280 oldState = rec.states;
2281 }
2282 }
2283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 /**
2285 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2286 *
2287 * @param pw a Printer to receive the dump output.
2288 */
2289 @SuppressWarnings("unused")
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002290 public void dumpLocked(PrintWriter pw, boolean isUnpluggedOnly, int reqUid) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002291 prepareForDumpLocked();
2292
2293 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2294
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002295 final HistoryItem rec = new HistoryItem();
2296 if (startIteratingHistoryLocked()) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002297 pw.println("Battery History:");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002298 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002299 while (getNextHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002300 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002301 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002302 finishIteratingHistoryLocked();
2303 pw.println("");
2304 }
2305
2306 if (startIteratingOldHistoryLocked()) {
2307 pw.println("Old battery History:");
2308 HistoryPrinter hprinter = new HistoryPrinter();
2309 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002310 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002311 }
2312 finishIteratingOldHistoryLocked();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002313 pw.println("");
2314 }
2315
2316 SparseArray<? extends Uid> uidStats = getUidStats();
2317 final int NU = uidStats.size();
2318 boolean didPid = false;
2319 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002320 for (int i=0; i<NU; i++) {
2321 Uid uid = uidStats.valueAt(i);
2322 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2323 if (pids != null) {
2324 for (int j=0; j<pids.size(); j++) {
2325 Uid.Pid pid = pids.valueAt(j);
2326 if (!didPid) {
2327 pw.println("Per-PID Stats:");
2328 didPid = true;
2329 }
2330 long time = pid.mWakeSum + (pid.mWakeStart != 0
2331 ? (nowRealtime - pid.mWakeStart) : 0);
2332 pw.print(" PID "); pw.print(pids.keyAt(j));
2333 pw.print(" wake time: ");
2334 TimeUtils.formatDuration(time, pw);
2335 pw.println("");
2336 }
2337 }
2338 }
2339 if (didPid) {
2340 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002341 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002342
2343 if (!isUnpluggedOnly) {
2344 pw.println("Statistics since last charge:");
2345 pw.println(" System starts: " + getStartCount()
2346 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002347 dumpLocked(pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002348 pw.println("");
2349 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002350 pw.println("Statistics since last unplugged:");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002351 dumpLocked(pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353
2354 @SuppressWarnings("unused")
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002355 public void dumpCheckinLocked(
Dianne Hackborn49021f52013-09-04 18:03:40 -07002356 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
2357 boolean includeHistory) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002358 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002360 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2361
Dianne Hackborn49021f52013-09-04 18:03:40 -07002362 if (includeHistory) {
2363 final HistoryItem rec = new HistoryItem();
2364 if (startIteratingHistoryLocked()) {
2365 HistoryPrinter hprinter = new HistoryPrinter();
2366 while (getNextHistoryLocked(rec)) {
2367 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
Dianne Hackborn49021f52013-09-04 18:03:40 -07002368 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002369 hprinter.printNextItem(pw, rec, now, true);
Dianne Hackborn49021f52013-09-04 18:03:40 -07002370 }
2371 finishIteratingHistoryLocked();
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002372 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002373 }
2374
Dianne Hackborne4a59512010-12-07 11:08:07 -08002375 if (apps != null) {
2376 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2377 for (int i=0; i<apps.size(); i++) {
2378 ApplicationInfo ai = apps.get(i);
2379 ArrayList<String> pkgs = uids.get(ai.uid);
2380 if (pkgs == null) {
2381 pkgs = new ArrayList<String>();
2382 uids.put(ai.uid, pkgs);
2383 }
2384 pkgs.add(ai.packageName);
2385 }
2386 SparseArray<? extends Uid> uidStats = getUidStats();
2387 final int NU = uidStats.size();
2388 String[] lineArgs = new String[2];
2389 for (int i=0; i<NU; i++) {
2390 int uid = uidStats.keyAt(i);
2391 ArrayList<String> pkgs = uids.get(uid);
2392 if (pkgs != null) {
2393 for (int j=0; j<pkgs.size(); j++) {
2394 lineArgs[0] = Integer.toString(uid);
2395 lineArgs[1] = pkgs.get(j);
2396 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2397 (Object[])lineArgs);
2398 }
2399 }
2400 }
2401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 if (isUnpluggedOnly) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002403 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405 else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002406 dumpCheckinLocked(pw, STATS_SINCE_CHARGED, -1);
2407 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410}