blob: f05ddde64ef799154a7d2c9c54e0a77528dc1412 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborna7c837f2014-01-15 16:20:44 -080027import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080028import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080029import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
38 * A class providing access to battery usage statistics, including information on
39 * wakelocks, processes, packages, and services. All times are represented in microseconds
40 * except where indicated otherwise.
41 * @hide
42 */
43public abstract class BatteryStats implements Parcelable {
44
45 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046
47 /** @hide */
48 public static final String SERVICE_NAME = "batterystats";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * A constant indicating a partial wake lock timer.
52 */
53 public static final int WAKE_TYPE_PARTIAL = 0;
54
55 /**
56 * A constant indicating a full wake lock timer.
57 */
58 public static final int WAKE_TYPE_FULL = 1;
59
60 /**
61 * A constant indicating a window wake lock timer.
62 */
63 public static final int WAKE_TYPE_WINDOW = 2;
64
65 /**
66 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070069
70 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070072 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070074
75 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070076 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070078 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070082 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 /**
86 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 */
88 public static final int WIFI_MULTICAST_ENABLED = 7;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int AUDIO_TURNED_ON = 7;
94
95 /**
96 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070097 */
98 public static final int VIDEO_TURNED_ON = 8;
99
100 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 * A constant indicating a vibrator on timer
102 */
103 public static final int VIBRATOR_ON = 9;
104
105 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700106 * A constant indicating a foreground activity timer
107 */
108 public static final int FOREGROUND_ACTIVITY = 10;
109
110 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700111 * A constant indicating a wifi batched scan is active
112 */
113 public static final int WIFI_BATCHED_SCAN = 11;
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * Include all of the data in the stats, including previously saved data.
117 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700118 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * Include only the current run in the stats.
122 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700123 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 /**
126 * Include only the run since the last time the device was unplugged in the stats.
127 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700128 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700129
130 // NOTE: Update this list if you add/change any stats above.
131 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700132 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700133 private static final String[] STAT_NAMES = { "l", "c", "u" };
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
136 * Bump the version on this if the checkin format changes.
137 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700138 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700139
140 private static final long BYTES_PER_KB = 1024;
141 private static final long BYTES_PER_MB = 1048576; // 1024^2
142 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Dianne Hackborne4a59512010-12-07 11:08:07 -0800145 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700147 private static final String PROCESS_DATA = "pr";
148 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800149 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700150 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700151 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700152 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700153 private static final String WAKEUP_REASON_DATA = "wr";
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 Hackborn099bc622014-01-22 13:39:16 -0800162 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700163 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700164 private static final String SCREEN_BRIGHTNESS_DATA = "br";
165 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700166 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700167 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
168 private static final String DATA_CONNECTION_TIME_DATA = "dct";
169 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800170 private static final String WIFI_STATE_TIME_DATA = "wst";
171 private static final String WIFI_STATE_COUNT_DATA = "wsc";
172 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
173 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800174 private static final String POWER_USE_SUMMARY_DATA = "pws";
175 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700177 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private final Formatter mFormatter = new Formatter(mFormatBuilder);
179
180 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700181 * State for keeping track of counting information.
182 */
183 public static abstract class Counter {
184
185 /**
186 * Returns the count associated with this Counter for the
187 * selected type of statistics.
188 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700189 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700190 */
Evan Millarc64edde2009-04-18 12:26:32 -0700191 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700192
193 /**
194 * Temporary for debugging.
195 */
196 public abstract void logState(Printer pw, String prefix);
197 }
198
199 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700200 * State for keeping track of long counting information.
201 */
202 public static abstract class LongCounter {
203
204 /**
205 * Returns the count associated with this Counter for the
206 * selected type of statistics.
207 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700208 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700209 */
210 public abstract long getCountLocked(int which);
211
212 /**
213 * Temporary for debugging.
214 */
215 public abstract void logState(Printer pw, String prefix);
216 }
217
218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * State for keeping track of timing information.
220 */
221 public static abstract class Timer {
222
223 /**
224 * Returns the count associated with this Timer for the
225 * selected type of statistics.
226 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700227 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Evan Millarc64edde2009-04-18 12:26:32 -0700229 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 /**
232 * Returns the total time in microseconds associated with this Timer for the
233 * selected type of statistics.
234 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800235 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700236 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * @return a time in microseconds
238 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800239 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * Temporary for debugging.
243 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700244 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246
247 /**
248 * The statistics associated with a particular uid.
249 */
250 public static abstract class Uid {
251
252 /**
253 * Returns a mapping containing wakelock statistics.
254 *
255 * @return a Map from Strings to Uid.Wakelock objects.
256 */
257 public abstract Map<String, ? extends Wakelock> getWakelockStats();
258
259 /**
260 * The statistics associated with a particular wake lock.
261 */
262 public static abstract class Wakelock {
263 public abstract Timer getWakeTime(int type);
264 }
265
266 /**
267 * Returns a mapping containing sensor statistics.
268 *
269 * @return a Map from Integer sensor ids to Uid.Sensor objects.
270 */
271 public abstract Map<Integer, ? extends Sensor> getSensorStats();
272
273 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700274 * Returns a mapping containing active process data.
275 */
276 public abstract SparseArray<? extends Pid> getPidStats();
277
278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * Returns a mapping containing process statistics.
280 *
281 * @return a Map from Strings to Uid.Proc objects.
282 */
283 public abstract Map<String, ? extends Proc> getProcessStats();
284
285 /**
286 * Returns a mapping containing package statistics.
287 *
288 * @return a Map from Strings to Uid.Pkg objects.
289 */
290 public abstract Map<String, ? extends Pkg> getPackageStats();
291
292 /**
293 * {@hide}
294 */
295 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700296
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800297 public abstract void noteWifiRunningLocked(long elapsedRealtime);
298 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
299 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
300 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
301 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
302 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
303 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
304 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
305 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
306 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
307 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
308 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
309 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
310 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
311 public abstract void noteActivityResumedLocked(long elapsedRealtime);
312 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800313 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
314 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
315 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
316 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
317 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
318 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
319 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700320 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800321 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
Robert Greenwalta029ea12013-09-25 16:38:12 -0700323 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
324
Dianne Hackborn617f8772009-03-31 15:04:46 -0700325 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700326 * Note that these must match the constants in android.os.PowerManager.
327 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
328 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700329 */
330 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700331 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700332 };
333
Jeff Browndf693de2012-07-27 12:03:38 -0700334 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700335
Dianne Hackborn617f8772009-03-31 15:04:46 -0700336 public abstract void noteUserActivityLocked(int type);
337 public abstract boolean hasUserActivity();
338 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700339
340 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800341 public abstract long getNetworkActivityBytes(int type, int which);
342 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800343 public abstract long getMobileRadioActiveTime(int which);
344 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800347 /*
348 * FIXME: it's not correct to use this magic value because it
349 * could clash with a sensor handle (which are defined by
350 * the sensor HAL, and therefore out of our control
351 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 // Magic sensor number for the GPS.
353 public static final int GPS = -10000;
354
355 public abstract int getHandle();
356
357 public abstract Timer getSensorTime();
358 }
359
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700360 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800361 public int mWakeNesting;
362 public long mWakeSumMs;
363 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700364 }
365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * The statistics associated with a particular process.
368 */
369 public static abstract class Proc {
370
Dianne Hackborn287952c2010-09-22 22:34:31 -0700371 public static class ExcessivePower {
372 public static final int TYPE_WAKE = 1;
373 public static final int TYPE_CPU = 2;
374
375 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700376 public long overTime;
377 public long usedTime;
378 }
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800381 * Returns true if this process is still active in the battery stats.
382 */
383 public abstract boolean isActive();
384
385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * Returns the total time (in 1/100 sec) spent executing in user code.
387 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700388 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 */
390 public abstract long getUserTime(int which);
391
392 /**
393 * Returns the total time (in 1/100 sec) spent executing in system code.
394 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700395 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
397 public abstract long getSystemTime(int which);
398
399 /**
400 * Returns the number of times the process has been started.
401 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700402 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 */
404 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700405
406 /**
407 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700408 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700409 * @return foreground cpu time in microseconds
410 */
411 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700412
413 /**
414 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
415 * @param speedStep the index of the CPU speed. This is not the actual speed of the
416 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700417 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700418 * @see BatteryStats#getCpuSpeedSteps()
419 */
420 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700421
Dianne Hackborn287952c2010-09-22 22:34:31 -0700422 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700423
Dianne Hackborn287952c2010-09-22 22:34:31 -0700424 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
426
427 /**
428 * The statistics associated with a particular package.
429 */
430 public static abstract class Pkg {
431
432 /**
433 * Returns the number of times this package has done something that could wake up the
434 * device from sleep.
435 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700436 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 */
438 public abstract int getWakeups(int which);
439
440 /**
441 * Returns a mapping containing service statistics.
442 */
443 public abstract Map<String, ? extends Serv> getServiceStats();
444
445 /**
446 * The statistics associated with a particular service.
447 */
448 public abstract class Serv {
449
450 /**
451 * Returns the amount of time spent started.
452 *
453 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700454 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * @return
456 */
457 public abstract long getStartTime(long batteryUptime, int which);
458
459 /**
460 * Returns the total number of times startService() has been called.
461 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700462 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
464 public abstract int getStarts(int which);
465
466 /**
467 * Returns the total number times the service has been launched.
468 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700469 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
471 public abstract int getLaunches(int which);
472 }
473 }
474 }
475
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800476 public final static class HistoryTag {
477 public String string;
478 public int uid;
479
480 public int poolIdx;
481
482 public void setTo(HistoryTag o) {
483 string = o.string;
484 uid = o.uid;
485 poolIdx = o.poolIdx;
486 }
487
488 public void setTo(String _string, int _uid) {
489 string = _string;
490 uid = _uid;
491 poolIdx = -1;
492 }
493
494 public void writeToParcel(Parcel dest, int flags) {
495 dest.writeString(string);
496 dest.writeInt(uid);
497 }
498
499 public void readFromParcel(Parcel src) {
500 string = src.readString();
501 uid = src.readInt();
502 poolIdx = -1;
503 }
504
505 @Override
506 public boolean equals(Object o) {
507 if (this == o) return true;
508 if (o == null || getClass() != o.getClass()) return false;
509
510 HistoryTag that = (HistoryTag) o;
511
512 if (uid != that.uid) return false;
513 if (!string.equals(that.string)) return false;
514
515 return true;
516 }
517
518 @Override
519 public int hashCode() {
520 int result = string.hashCode();
521 result = 31 * result + uid;
522 return result;
523 }
524 }
525
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700526 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700527 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700528
529 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800530
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800531 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800532 public static final byte CMD_NULL = -1;
533 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800534 public static final byte CMD_CURRENT_TIME = 5;
535 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800536
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700537 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700538
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800539 /**
540 * Return whether the command code is a delta data update.
541 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800542 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800543 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800544 }
545
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700546 public byte batteryLevel;
547 public byte batteryStatus;
548 public byte batteryHealth;
549 public byte batteryPlugType;
550
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900551 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700552 public char batteryVoltage;
553
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700554 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700555 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800556 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700557 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800558 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
559 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700560 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800561 public static final int STATE_PHONE_STATE_SHIFT = 6;
562 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700563 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800564 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800565 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800566
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700567 // These states always appear directly in the first int token
568 // of a delta change; they should be ones that change relatively
569 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700570 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
571 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800572 public static final int STATE_GPS_ON_FLAG = 1<<29;
573 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800574 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800575 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800576 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800577 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700578 // These are on the lower bits used for the command; if they change
579 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700580 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700581 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700582 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700583 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
584 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
585 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
586 public static final int STATE_WIFI_ON_FLAG = 1<<17;
587 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700588
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700589 public static final int MOST_INTERESTING_STATES =
590 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
591 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
592
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700593 public int states;
594
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700595 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700596 public int states2;
597
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800598 // The wake lock that was acquired at this point.
599 public HistoryTag wakelockTag;
600
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800601 // Kernel wakeup reason at this point.
602 public HistoryTag wakeReasonTag;
603
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800604 public static final int EVENT_FLAG_START = 0x8000;
605 public static final int EVENT_FLAG_FINISH = 0x4000;
606
607 // No event in this item.
608 public static final int EVENT_NONE = 0x0000;
609 // Event is about a process that is running.
610 public static final int EVENT_PROC = 0x0001;
611 // Event is about an application package that is in the foreground.
612 public static final int EVENT_FOREGROUND = 0x0002;
613 // Event is about an application package that is at the top of the screen.
614 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800615 // Event is about an application package that is at the top of the screen.
616 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800617 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800618 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800619
620 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
621 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
622 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
623 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
624 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
625 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800626 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
627 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800628
629 // For CMD_EVENT.
630 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800631 public HistoryTag eventTag;
632
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800633 // Only set for CMD_CURRENT_TIME.
634 public long currentTime;
635
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800636 // Meta-data when reading.
637 public int numReadInts;
638
639 // Pre-allocated objects.
640 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800641 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800642 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800643
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700644 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700645 }
646
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700647 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700648 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800649 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700650 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700651 }
652
653 public int describeContents() {
654 return 0;
655 }
656
657 public void writeToParcel(Parcel dest, int flags) {
658 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700659 int bat = (((int)cmd)&0xff)
660 | ((((int)batteryLevel)<<8)&0xff00)
661 | ((((int)batteryStatus)<<16)&0xf0000)
662 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800663 | ((((int)batteryPlugType)<<24)&0xf000000)
664 | (wakelockTag != null ? 0x10000000 : 0)
665 | (wakeReasonTag != null ? 0x20000000 : 0)
666 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700667 dest.writeInt(bat);
668 bat = (((int)batteryTemperature)&0xffff)
669 | ((((int)batteryVoltage)<<16)&0xffff0000);
670 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700671 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700672 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800673 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800674 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800675 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800676 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800677 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800678 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800679 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800680 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800681 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800682 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800683 if (cmd == CMD_CURRENT_TIME) {
684 dest.writeLong(currentTime);
685 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700686 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700687
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800688 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800689 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700690 int bat = src.readInt();
691 cmd = (byte)(bat&0xff);
692 batteryLevel = (byte)((bat>>8)&0xff);
693 batteryStatus = (byte)((bat>>16)&0xf);
694 batteryHealth = (byte)((bat>>20)&0xf);
695 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800696 int bat2 = src.readInt();
697 batteryTemperature = (short)(bat2&0xffff);
698 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700699 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700700 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800701 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800702 wakelockTag = localWakelockTag;
703 wakelockTag.readFromParcel(src);
704 } else {
705 wakelockTag = null;
706 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800707 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800708 wakeReasonTag = localWakeReasonTag;
709 wakeReasonTag.readFromParcel(src);
710 } else {
711 wakeReasonTag = null;
712 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800713 if ((bat&0x40000000) != 0) {
714 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800715 eventTag = localEventTag;
716 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800717 } else {
718 eventCode = EVENT_NONE;
719 eventTag = null;
720 }
721 if (cmd == CMD_CURRENT_TIME) {
722 currentTime = src.readLong();
723 } else {
724 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700725 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800726 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700727 }
728
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700729 public void clear() {
730 time = 0;
731 cmd = CMD_NULL;
732 batteryLevel = 0;
733 batteryStatus = 0;
734 batteryHealth = 0;
735 batteryPlugType = 0;
736 batteryTemperature = 0;
737 batteryVoltage = 0;
738 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700739 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800740 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800741 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800742 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800743 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700744 }
745
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700746 public void setTo(HistoryItem o) {
747 time = o.time;
748 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800749 setToCommon(o);
750 }
751
752 public void setTo(long time, byte cmd, HistoryItem o) {
753 this.time = time;
754 this.cmd = cmd;
755 setToCommon(o);
756 }
757
758 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700759 batteryLevel = o.batteryLevel;
760 batteryStatus = o.batteryStatus;
761 batteryHealth = o.batteryHealth;
762 batteryPlugType = o.batteryPlugType;
763 batteryTemperature = o.batteryTemperature;
764 batteryVoltage = o.batteryVoltage;
765 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700766 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800767 if (o.wakelockTag != null) {
768 wakelockTag = localWakelockTag;
769 wakelockTag.setTo(o.wakelockTag);
770 } else {
771 wakelockTag = null;
772 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800773 if (o.wakeReasonTag != null) {
774 wakeReasonTag = localWakeReasonTag;
775 wakeReasonTag.setTo(o.wakeReasonTag);
776 } else {
777 wakeReasonTag = null;
778 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800779 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800780 if (o.eventTag != null) {
781 eventTag = localEventTag;
782 eventTag.setTo(o.eventTag);
783 } else {
784 eventTag = null;
785 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800786 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700787 }
788
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800789 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700790 return batteryLevel == o.batteryLevel
791 && batteryStatus == o.batteryStatus
792 && batteryHealth == o.batteryHealth
793 && batteryPlugType == o.batteryPlugType
794 && batteryTemperature == o.batteryTemperature
795 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800796 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700797 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800798 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700799 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800800
801 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800802 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800803 return false;
804 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800805 if (wakelockTag != o.wakelockTag) {
806 if (wakelockTag == null || o.wakelockTag == null) {
807 return false;
808 }
809 if (!wakelockTag.equals(o.wakelockTag)) {
810 return false;
811 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800812 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800813 if (wakeReasonTag != o.wakeReasonTag) {
814 if (wakeReasonTag == null || o.wakeReasonTag == null) {
815 return false;
816 }
817 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
818 return false;
819 }
820 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800821 if (eventTag != o.eventTag) {
822 if (eventTag == null || o.eventTag == null) {
823 return false;
824 }
825 if (!eventTag.equals(o.eventTag)) {
826 return false;
827 }
828 }
829 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800830 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700831 }
832
833 public static final class BitDescription {
834 public final int mask;
835 public final int shift;
836 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800837 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700838 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800839 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700840
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800841 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700842 this.mask = mask;
843 this.shift = -1;
844 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800845 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700846 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800847 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700848 }
849
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800850 public BitDescription(int mask, int shift, String name, String shortName,
851 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700852 this.mask = mask;
853 this.shift = shift;
854 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800855 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700856 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800857 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700858 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700859 }
860
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800861 public abstract int getHistoryTotalSize();
862
863 public abstract int getHistoryUsedSize();
864
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700865 public abstract boolean startIteratingHistoryLocked();
866
Dianne Hackborn099bc622014-01-22 13:39:16 -0800867 public abstract int getHistoryStringPoolSize();
868
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800869 public abstract int getHistoryStringPoolBytes();
870
871 public abstract String getHistoryTagPoolString(int index);
872
873 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800874
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700875 public abstract boolean getNextHistoryLocked(HistoryItem out);
876
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700877 public abstract void finishIteratingHistoryLocked();
878
879 public abstract boolean startIteratingOldHistoryLocked();
880
881 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
882
883 public abstract void finishIteratingOldHistoryLocked();
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700886 * Return the base time offset for the battery history.
887 */
888 public abstract long getHistoryBaseTime();
889
890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 * Returns the number of times the device has been started.
892 */
893 public abstract int getStartCount();
894
895 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700896 * 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 -0800897 * running on battery.
898 *
899 * {@hide}
900 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800901 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800903 /**
904 * Returns the number of times the screen was turned on.
905 *
906 * {@hide}
907 */
908 public abstract int getScreenOnCount(int which);
909
Dianne Hackborn617f8772009-03-31 15:04:46 -0700910 public static final int SCREEN_BRIGHTNESS_DARK = 0;
911 public static final int SCREEN_BRIGHTNESS_DIM = 1;
912 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
913 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
914 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
915
916 static final String[] SCREEN_BRIGHTNESS_NAMES = {
917 "dark", "dim", "medium", "light", "bright"
918 };
919
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800920 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
921 "0", "1", "2", "3", "4"
922 };
923
Dianne Hackborn617f8772009-03-31 15:04:46 -0700924 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
925
926 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700927 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 * the given brightness
929 *
930 * {@hide}
931 */
932 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800933 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700934
935 public abstract int getInputEventCount(int which);
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700938 * 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 -0800939 * running on battery.
940 *
941 * {@hide}
942 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800943 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700944
945 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800946 * Returns the number of times a phone call was activated.
947 *
948 * {@hide}
949 */
950 public abstract int getPhoneOnCount(int which);
951
952 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700953 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700954 * the given signal strength.
955 *
956 * {@hide}
957 */
958 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800959 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700960
Dianne Hackborn617f8772009-03-31 15:04:46 -0700961 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700962 * Returns the time in microseconds that the phone has been trying to
963 * acquire a signal.
964 *
965 * {@hide}
966 */
967 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800968 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -0700969
970 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700971 * Returns the number of times the phone has entered the given signal strength.
972 *
973 * {@hide}
974 */
975 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
976
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800977 /**
978 * Returns the time in microseconds that the mobile network has been active
979 * (in a high power state).
980 *
981 * {@hide}
982 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800983 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800984
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800985 /**
986 * Returns the number of times that the mobile network has transitioned to the
987 * active state.
988 *
989 * {@hide}
990 */
991 public abstract int getMobileRadioActiveCount(int which);
992
993 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700994 * Returns the time in microseconds that is the difference between the mobile radio
995 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
996 * from the radio.
997 *
998 * {@hide}
999 */
1000 public abstract long getMobileRadioActiveAdjustedTime(int which);
1001
1002 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001003 * Returns the time in microseconds that the mobile network has been active
1004 * (in a high power state) but not being able to blame on an app.
1005 *
1006 * {@hide}
1007 */
1008 public abstract long getMobileRadioActiveUnknownTime(int which);
1009
1010 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001011 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001012 *
1013 * {@hide}
1014 */
1015 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001016
Dianne Hackborn627bba72009-03-24 22:32:56 -07001017 public static final int DATA_CONNECTION_NONE = 0;
1018 public static final int DATA_CONNECTION_GPRS = 1;
1019 public static final int DATA_CONNECTION_EDGE = 2;
1020 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001021 public static final int DATA_CONNECTION_CDMA = 4;
1022 public static final int DATA_CONNECTION_EVDO_0 = 5;
1023 public static final int DATA_CONNECTION_EVDO_A = 6;
1024 public static final int DATA_CONNECTION_1xRTT = 7;
1025 public static final int DATA_CONNECTION_HSDPA = 8;
1026 public static final int DATA_CONNECTION_HSUPA = 9;
1027 public static final int DATA_CONNECTION_HSPA = 10;
1028 public static final int DATA_CONNECTION_IDEN = 11;
1029 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001030 public static final int DATA_CONNECTION_LTE = 13;
1031 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001032 public static final int DATA_CONNECTION_HSPAP = 15;
1033 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001034
Dianne Hackborn627bba72009-03-24 22:32:56 -07001035 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001036 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001037 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001038 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001039 };
1040
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001041 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001042
1043 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001044 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001045 * the given data connection.
1046 *
1047 * {@hide}
1048 */
1049 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001050 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001053 * Returns the number of times the phone has entered the given data
1054 * connection type.
1055 *
1056 * {@hide}
1057 */
1058 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001059
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001060 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1061 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001062 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001063 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1064 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001065 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001066 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1067 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1068 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001069 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001070 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1071 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001072 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001073 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1074 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1075 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1076 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1077 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1078 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1079 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1080 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1081 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1082 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1083 new String[] {"in", "out", "emergency", "off"},
1084 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001085 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001086 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1087 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1088 "0", "1", "2", "3", "4"
1089 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001090 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1091 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1092 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001093 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001094
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001095 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1096 = new BitDescription[] {
1097 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1098 };
1099
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001100 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001101 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001102 };
1103
1104 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001105 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001106 };
1107
Dianne Hackborn617f8772009-03-31 15:04:46 -07001108 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001109 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001110 * running on battery.
1111 *
1112 * {@hide}
1113 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001115
1116 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001117 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001118 * been in the running state while the device was running on battery.
1119 *
1120 * {@hide}
1121 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001122 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001123
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001124 public static final int WIFI_STATE_OFF = 0;
1125 public static final int WIFI_STATE_OFF_SCANNING = 1;
1126 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1127 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1128 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1129 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1130 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1131 public static final int WIFI_STATE_SOFT_AP = 7;
1132
1133 static final String[] WIFI_STATE_NAMES = {
1134 "off", "scanning", "no_net", "disconn",
1135 "sta", "p2p", "sta_p2p", "soft_ap"
1136 };
1137
1138 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1139
1140 /**
1141 * Returns the time in microseconds that WiFi has been running in the given state.
1142 *
1143 * {@hide}
1144 */
1145 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001147
1148 /**
1149 * Returns the number of times that WiFi has entered the given state.
1150 *
1151 * {@hide}
1152 */
1153 public abstract int getWifiStateCount(int wifiState, int which);
1154
The Android Open Source Project10592532009-03-18 17:39:46 -07001155 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001156 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001157 * running on battery.
1158 *
1159 * {@hide}
1160 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001161 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001162
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001163 public abstract int getBluetoothPingCount();
1164
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001165 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1166 public static final int BLUETOOTH_STATE_LOW = 1;
1167 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1168 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001169
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001170 static final String[] BLUETOOTH_STATE_NAMES = {
1171 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001172 };
1173
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001174 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001175
1176 /**
1177 * Returns the time in microseconds that Bluetooth has been running in the
1178 * given active state.
1179 *
1180 * {@hide}
1181 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001182 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001183 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001184
1185 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001186 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001187 *
1188 * {@hide}
1189 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001190 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001191
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001192 public static final int NETWORK_MOBILE_RX_DATA = 0;
1193 public static final int NETWORK_MOBILE_TX_DATA = 1;
1194 public static final int NETWORK_WIFI_RX_DATA = 2;
1195 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001196
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001197 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001198
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001199 public abstract long getNetworkActivityBytes(int type, int which);
1200 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001201
The Android Open Source Project10592532009-03-18 17:39:46 -07001202 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001203 * Return the wall clock time when battery stats data collection started.
1204 */
1205 public abstract long getStartClockTime();
1206
1207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 * Return whether we are currently running on battery.
1209 */
1210 public abstract boolean getIsOnBattery();
1211
1212 /**
1213 * Returns a SparseArray containing the statistics for each uid.
1214 */
1215 public abstract SparseArray<? extends Uid> getUidStats();
1216
1217 /**
1218 * Returns the current battery uptime in microseconds.
1219 *
1220 * @param curTime the amount of elapsed realtime in microseconds.
1221 */
1222 public abstract long getBatteryUptime(long curTime);
1223
1224 /**
1225 * Returns the current battery realtime in microseconds.
1226 *
1227 * @param curTime the amount of elapsed realtime in microseconds.
1228 */
1229 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001230
1231 /**
Evan Millar633a1742009-04-02 16:36:33 -07001232 * Returns the battery percentage level at the last time the device was unplugged from power, or
1233 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001234 */
Evan Millar633a1742009-04-02 16:36:33 -07001235 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001236
1237 /**
Evan Millar633a1742009-04-02 16:36:33 -07001238 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1239 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001240 */
Evan Millar633a1742009-04-02 16:36:33 -07001241 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242
1243 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001244 * Get the amount the battery has discharged since the stats were
1245 * last reset after charging, as a lower-end approximation.
1246 */
1247 public abstract int getLowDischargeAmountSinceCharge();
1248
1249 /**
1250 * Get the amount the battery has discharged since the stats were
1251 * last reset after charging, as an upper-end approximation.
1252 */
1253 public abstract int getHighDischargeAmountSinceCharge();
1254
1255 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001256 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1257 */
1258 public abstract int getDischargeAmount(int which);
1259
1260 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001261 * Get the amount the battery has discharged while the screen was on,
1262 * since the last time power was unplugged.
1263 */
1264 public abstract int getDischargeAmountScreenOn();
1265
1266 /**
1267 * Get the amount the battery has discharged while the screen was on,
1268 * since the last time the device was charged.
1269 */
1270 public abstract int getDischargeAmountScreenOnSinceCharge();
1271
1272 /**
1273 * Get the amount the battery has discharged while the screen was off,
1274 * since the last time power was unplugged.
1275 */
1276 public abstract int getDischargeAmountScreenOff();
1277
1278 /**
1279 * Get the amount the battery has discharged while the screen was off,
1280 * since the last time the device was charged.
1281 */
1282 public abstract int getDischargeAmountScreenOffSinceCharge();
1283
1284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 * Returns the total, last, or current battery uptime in microseconds.
1286 *
1287 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001288 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 */
1290 public abstract long computeBatteryUptime(long curTime, int which);
1291
1292 /**
1293 * Returns the total, last, or current battery realtime in microseconds.
1294 *
1295 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001296 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 */
1298 public abstract long computeBatteryRealtime(long curTime, int which);
1299
1300 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001301 * Returns the total, last, or current battery screen off uptime in microseconds.
1302 *
1303 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001304 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001305 */
1306 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1307
1308 /**
1309 * Returns the total, last, or current battery screen off realtime in microseconds.
1310 *
1311 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001312 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001313 */
1314 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1315
1316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 * Returns the total, last, or current uptime in microseconds.
1318 *
1319 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001320 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 */
1322 public abstract long computeUptime(long curTime, int which);
1323
1324 /**
1325 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001328 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 */
1330 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001331
1332 /**
1333 * Compute an approximation for how much run time (in microseconds) is remaining on
1334 * the battery. Returns -1 if no time can be computed: either there is not
1335 * enough current data to make a decision, or the battery is currently
1336 * charging.
1337 *
1338 * @param curTime The current elepsed realtime in microseconds.
1339 */
1340 public abstract long computeBatteryTimeRemaining(long curTime);
1341
1342 /**
1343 * Compute an approximation for how much time (in microseconds) remains until the battery
1344 * is fully charged. Returns -1 if no time can be computed: either there is not
1345 * enough current data to make a decision, or the battery is currently
1346 * discharging.
1347 *
1348 * @param curTime The current elepsed realtime in microseconds.
1349 */
1350 public abstract long computeChargeTimeRemaining(long curTime);
1351
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001352 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1353
Evan Millarc64edde2009-04-18 12:26:32 -07001354 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355
Amith Yamasanie43530a2009-08-21 13:11:37 -07001356 /** Returns the number of different speeds that the CPU can run at */
1357 public abstract int getCpuSpeedSteps();
1358
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001359 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1360
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001361 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 long days = seconds / (60 * 60 * 24);
1363 if (days != 0) {
1364 out.append(days);
1365 out.append("d ");
1366 }
1367 long used = days * 60 * 60 * 24;
1368
1369 long hours = (seconds - used) / (60 * 60);
1370 if (hours != 0 || used != 0) {
1371 out.append(hours);
1372 out.append("h ");
1373 }
1374 used += hours * 60 * 60;
1375
1376 long mins = (seconds-used) / 60;
1377 if (mins != 0 || used != 0) {
1378 out.append(mins);
1379 out.append("m ");
1380 }
1381 used += mins * 60;
1382
1383 if (seconds != 0 || used != 0) {
1384 out.append(seconds-used);
1385 out.append("s ");
1386 }
1387 }
1388
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001389 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001391 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 sb.append((time - (sec * 100)) * 10);
1393 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001396 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001398 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 sb.append(time - (sec * 1000));
1400 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001403 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001404 long sec = time / 1000;
1405 formatTimeRaw(sb, sec);
1406 sb.append(time - (sec * 1000));
1407 sb.append("ms");
1408 }
1409
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001410 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001412 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 float perc = ((float)num) / ((float)den) * 100;
1415 mFormatBuilder.setLength(0);
1416 mFormatter.format("%.1f%%", perc);
1417 return mFormatBuilder.toString();
1418 }
1419
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001420 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001421 mFormatBuilder.setLength(0);
1422
1423 if (bytes < BYTES_PER_KB) {
1424 return bytes + "B";
1425 } else if (bytes < BYTES_PER_MB) {
1426 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1427 return mFormatBuilder.toString();
1428 } else if (bytes < BYTES_PER_GB){
1429 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1430 return mFormatBuilder.toString();
1431 } else {
1432 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1433 return mFormatBuilder.toString();
1434 }
1435 }
1436
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001437 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001438 if (timer != null) {
1439 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001440 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001441 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1442 return totalTimeMillis;
1443 }
1444 return 0;
1445 }
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 /**
1448 *
1449 * @param sb a StringBuilder object.
1450 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001451 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001453 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 * @param linePrefix a String to be prepended to each line of output.
1455 * @return the line prefix
1456 */
1457 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001458 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
1460 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001461 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462
Evan Millarc64edde2009-04-18 12:26:32 -07001463 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 if (totalTimeMillis != 0) {
1465 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001466 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001467 if (name != null) {
1468 sb.append(name);
1469 sb.append(' ');
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 sb.append('(');
1472 sb.append(count);
1473 sb.append(" times)");
1474 return ", ";
1475 }
1476 }
1477 return linePrefix;
1478 }
1479
1480 /**
1481 * Checkin version of wakelock printer. Prints simple comma-separated list.
1482 *
1483 * @param sb a StringBuilder object.
1484 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001485 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001487 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 * @param linePrefix a String to be prepended to each line of output.
1489 * @return the line prefix
1490 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001491 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1492 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 long totalTimeMicros = 0;
1494 int count = 0;
1495 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001496 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001497 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499 sb.append(linePrefix);
1500 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1501 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001502 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 sb.append(count);
1504 return ",";
1505 }
1506
1507 /**
1508 * Dump a comma-separated line of values for terse checkin mode.
1509 *
1510 * @param pw the PageWriter to dump log to
1511 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1512 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1513 * @param args type-dependent data arguments
1514 */
1515 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1516 Object... args ) {
1517 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1518 pw.print(uid); pw.print(',');
1519 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001520 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001523 pw.print(',');
1524 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001526 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
1528
1529 /**
1530 * Checkin server version of dump to produce more compact, computer-readable log.
1531 *
1532 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001534 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1536 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1537 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1539 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001540 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1541 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1542 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 final long totalRealtime = computeRealtime(rawRealtime, which);
1544 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001545 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1546 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1547 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1548 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1549 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
1551 StringBuilder sb = new StringBuilder(128);
1552
Evan Millar22ac0432009-03-31 11:33:18 -07001553 SparseArray<? extends Uid> uidStats = getUidStats();
1554 final int NU = uidStats.size();
1555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 // Dump "battery" stat
1559 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001560 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001561 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001562 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001563 getStartClockTime(),
1564 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001566 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001567 long fullWakeLockTimeTotal = 0;
1568 long partialWakeLockTimeTotal = 0;
1569
1570 for (int iu = 0; iu < NU; iu++) {
1571 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001572
Evan Millar22ac0432009-03-31 11:33:18 -07001573 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1574 if (wakelocks.size() > 0) {
1575 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1576 : wakelocks.entrySet()) {
1577 Uid.Wakelock wl = ent.getValue();
1578
1579 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1580 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001581 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1582 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001583 }
1584
1585 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1586 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001587 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001588 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001589 }
1590 }
1591 }
1592 }
1593
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001594 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1595 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1596 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1597 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1598 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1599 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1600 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1601 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1602
1603 // Dump network stats
1604 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1605 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1606 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 // Dump misc stats
1609 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001610 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001611 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001612 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001613 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001614 getInputEventCount(which), getMobileRadioActiveTime(rawRealtime, which),
1615 getMobileRadioActiveAdjustedTime(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001616
1617 // Dump screen brightness stats
1618 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1619 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001620 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001621 }
1622 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001623
Dianne Hackborn627bba72009-03-24 22:32:56 -07001624 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001625 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1626 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001627 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001628 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001629 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001630 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001631 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001632 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001633 args[i] = getPhoneSignalStrengthCount(i, which);
1634 }
1635 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001636
Dianne Hackborn627bba72009-03-24 22:32:56 -07001637 // Dump network type stats
1638 args = new Object[NUM_DATA_CONNECTION_TYPES];
1639 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001640 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001641 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001642 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1643 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1644 args[i] = getPhoneDataConnectionCount(i, which);
1645 }
1646 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001647
1648 // Dump wifi state stats
1649 args = new Object[NUM_WIFI_STATES];
1650 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001651 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001652 }
1653 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1654 for (int i=0; i<NUM_WIFI_STATES; i++) {
1655 args[i] = getWifiStateCount(i, which);
1656 }
1657 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1658
1659 // Dump bluetooth state stats
1660 args = new Object[NUM_BLUETOOTH_STATES];
1661 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001662 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001663 }
1664 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1665 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1666 args[i] = getBluetoothStateCount(i, which);
1667 }
1668 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1669
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001670 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001671 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001672 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001675 if (which == STATS_SINCE_UNPLUGGED) {
1676 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1677 getDischargeStartLevel()-getDischargeCurrentLevel(),
1678 getDischargeStartLevel()-getDischargeCurrentLevel(),
1679 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1680 } else {
1681 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1682 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1683 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1684 }
1685
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001686 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001687 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001688 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001689 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001690 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001691 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001692 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001693 sb.toString());
1694 }
Evan Millarc64edde2009-04-18 12:26:32 -07001695 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001696 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1697 if (wakeupReasons.size() > 0) {
1698 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1699 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1700 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1701 }
1702 }
Evan Millarc64edde2009-04-18 12:26:32 -07001703 }
1704
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001705 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001706 helper.create(this);
1707 helper.refreshStats(which, UserHandle.USER_ALL);
1708 List<BatterySipper> sippers = helper.getUsageList();
1709 if (sippers != null && sippers.size() > 0) {
1710 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1711 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001712 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001713 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1714 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1715 for (int i=0; i<sippers.size(); i++) {
1716 BatterySipper bs = sippers.get(i);
1717 int uid = 0;
1718 String label;
1719 switch (bs.drainType) {
1720 case IDLE:
1721 label="idle";
1722 break;
1723 case CELL:
1724 label="cell";
1725 break;
1726 case PHONE:
1727 label="phone";
1728 break;
1729 case WIFI:
1730 label="wifi";
1731 break;
1732 case BLUETOOTH:
1733 label="blue";
1734 break;
1735 case SCREEN:
1736 label="scrn";
1737 break;
1738 case APP:
1739 uid = bs.uidObj.getUid();
1740 label = "uid";
1741 break;
1742 case USER:
1743 uid = UserHandle.getUid(bs.userId, 0);
1744 label = "user";
1745 break;
1746 case UNACCOUNTED:
1747 label = "unacc";
1748 break;
1749 case OVERCOUNTED:
1750 label = "over";
1751 break;
1752 default:
1753 label = "???";
1754 }
1755 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1756 BatteryStatsHelper.makemAh(bs.value));
1757 }
1758 }
1759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 for (int iu = 0; iu < NU; iu++) {
1761 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001762 if (reqUid >= 0 && uid != reqUid) {
1763 continue;
1764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 Uid u = uidStats.valueAt(iu);
1766 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001767 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1768 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1769 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1770 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1771 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1772 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001773 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1774 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001775 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1776 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001777 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1778 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1779 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001780
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001781 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1782 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001783 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001784 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1785 wifiBytesRx, wifiBytesTx,
1786 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001787 wifiPacketsRx, wifiPacketsTx,
1788 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001789 }
1790
Nick Pelly6ccaa542012-06-15 15:22:47 -07001791 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001792 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001793 dumpLine(pw, uid, category, WIFI_DATA,
1794 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
Dianne Hackborn617f8772009-03-31 15:04:46 -07001797 if (u.hasUserActivity()) {
1798 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1799 boolean hasData = false;
1800 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1801 int val = u.getUserActivityCount(i, which);
1802 args[i] = val;
1803 if (val != 0) hasData = true;
1804 }
1805 if (hasData) {
1806 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1807 }
1808 }
1809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1811 if (wakelocks.size() > 0) {
1812 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1813 : wakelocks.entrySet()) {
1814 Uid.Wakelock wl = ent.getValue();
1815 String linePrefix = "";
1816 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001817 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001818 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001819 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001820 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001821 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001822 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823
1824 // Only log if we had at lease one wakelock...
1825 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001826 String name = ent.getKey();
1827 if (name.indexOf(',') >= 0) {
1828 name = name.replace(',', '_');
1829 }
1830 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832 }
1833 }
1834
1835 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1836 if (sensors.size() > 0) {
1837 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1838 : sensors.entrySet()) {
1839 Uid.Sensor se = ent.getValue();
1840 int sensorNumber = ent.getKey();
1841 Timer timer = se.getSensorTime();
1842 if (timer != null) {
1843 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001844 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001845 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 if (totalTime != 0) {
1847 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1848 }
1849 }
1850 }
1851 }
1852
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001853 Timer vibTimer = u.getVibratorOnTimer();
1854 if (vibTimer != null) {
1855 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001856 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001857 int count = vibTimer.getCountLocked(which);
1858 if (totalTime != 0) {
1859 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1860 }
1861 }
1862
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001863 Timer fgTimer = u.getForegroundActivityTimer();
1864 if (fgTimer != null) {
1865 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001866 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001867 int count = fgTimer.getCountLocked(which);
1868 if (totalTime != 0) {
1869 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1870 }
1871 }
1872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1874 if (processStats.size() > 0) {
1875 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1876 : processStats.entrySet()) {
1877 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001878
1879 final long userMillis = ps.getUserTime(which) * 10;
1880 final long systemMillis = ps.getSystemTime(which) * 10;
1881 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1882 final long starts = ps.getStarts(which);
1883
1884 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1885 || starts != 0) {
1886 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1887 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889 }
1890 }
1891
1892 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1893 if (packageStats.size() > 0) {
1894 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1895 : packageStats.entrySet()) {
1896
1897 Uid.Pkg ps = ent.getValue();
1898 int wakeups = ps.getWakeups(which);
1899 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1900 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1901 : serviceStats.entrySet()) {
1902 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1903 long startTime = ss.getStartTime(batteryUptime, which);
1904 int starts = ss.getStarts(which);
1905 int launches = ss.getLaunches(which);
1906 if (startTime != 0 || starts != 0 || launches != 0) {
1907 dumpLine(pw, uid, category, APK_DATA,
1908 wakeups, // wakeup alarms
1909 ent.getKey(), // Apk
1910 sent.getKey(), // service
1911 startTime / 1000, // time spent started, in ms
1912 starts,
1913 launches);
1914 }
1915 }
1916 }
1917 }
1918 }
1919 }
1920
Dianne Hackborn81038902012-11-26 17:04:09 -08001921 static final class TimerEntry {
1922 final String mName;
1923 final int mId;
1924 final BatteryStats.Timer mTimer;
1925 final long mTime;
1926 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1927 mName = name;
1928 mId = id;
1929 mTimer = timer;
1930 mTime = time;
1931 }
1932 }
1933
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001934 private void printmAh(PrintWriter printer, double power) {
1935 printer.print(BatteryStatsHelper.makemAh(power));
1936 }
1937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001939 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1940 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1942 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1943 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944
1945 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1946 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1947 final long totalRealtime = computeRealtime(rawRealtime, which);
1948 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001949 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1950 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1951 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001952 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
1953 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001956
1957 SparseArray<? extends Uid> uidStats = getUidStats();
1958 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001960 sb.setLength(0);
1961 sb.append(prefix);
1962 sb.append(" Time on battery: ");
1963 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1964 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1965 sb.append(") realtime, ");
1966 formatTimeMs(sb, whichBatteryUptime / 1000);
1967 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1968 sb.append(") uptime");
1969 pw.println(sb.toString());
1970 sb.setLength(0);
1971 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001972 sb.append(" Time on battery screen off: ");
1973 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
1974 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
1975 sb.append(") realtime, ");
1976 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
1977 sb.append("(");
1978 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
1979 sb.append(") uptime");
1980 pw.println(sb.toString());
1981 sb.setLength(0);
1982 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001983 sb.append(" Total run time: ");
1984 formatTimeMs(sb, totalRealtime / 1000);
1985 sb.append("realtime, ");
1986 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001987 sb.append("uptime");
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001988 if (batteryTimeRemaining >= 0) {
1989 sb.setLength(0);
1990 sb.append(prefix);
1991 sb.append(" Battery time remaining: ");
1992 formatTimeMs(sb, batteryTimeRemaining / 1000);
1993 pw.println(sb.toString());
1994 }
1995 if (chargeTimeRemaining >= 0) {
1996 sb.setLength(0);
1997 sb.append(prefix);
1998 sb.append(" Charge time remaining: ");
1999 formatTimeMs(sb, chargeTimeRemaining / 1000);
2000 pw.println(sb.toString());
2001 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002002 pw.print(" Start clock time: ");
2003 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2004
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002005 final long screenOnTime = getScreenOnTime(rawRealtime, which);
2006 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2007 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2008 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2009 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002010 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002011 sb.append(prefix);
2012 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2013 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002014 sb.append(") "); sb.append(getScreenOnCount(which));
2015 sb.append("x, Input events: "); sb.append(getInputEventCount(which));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002016 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002017 if (phoneOnTime != 0) {
2018 sb.setLength(0);
2019 sb.append(prefix);
2020 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2021 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2022 sb.append(") "); sb.append(getPhoneOnCount(which));
2023 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002024 sb.setLength(0);
2025 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002026 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002027 boolean didOne = false;
2028 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002029 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002030 if (time == 0) {
2031 continue;
2032 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002033 sb.append("\n ");
2034 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002035 didOne = true;
2036 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2037 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002038 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002039 sb.append("(");
2040 sb.append(formatRatioLocked(time, screenOnTime));
2041 sb.append(")");
2042 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002043 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002044 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002045
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002046 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002047 long fullWakeLockTimeTotalMicros = 0;
2048 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002049
Dianne Hackborn81038902012-11-26 17:04:09 -08002050 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2051
Evan Millar22ac0432009-03-31 11:33:18 -07002052 for (int iu = 0; iu < NU; iu++) {
2053 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002054
Evan Millar22ac0432009-03-31 11:33:18 -07002055 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2056 if (wakelocks.size() > 0) {
2057 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2058 : wakelocks.entrySet()) {
2059 Uid.Wakelock wl = ent.getValue();
2060
2061 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2062 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002063 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002064 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002065 }
2066
2067 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2068 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002069 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002070 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002071 if (totalTimeMicros > 0) {
2072 if (reqUid < 0) {
2073 // Only show the ordered list of all wake
2074 // locks if the caller is not asking for data
2075 // about a specific uid.
2076 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2077 partialWakeTimer, totalTimeMicros));
2078 }
2079 partialWakeLockTimeTotalMicros += totalTimeMicros;
2080 }
Evan Millar22ac0432009-03-31 11:33:18 -07002081 }
2082 }
2083 }
2084 }
2085
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002086 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2087 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2088 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2089 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2090 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2091 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2092 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2093 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2094
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002095 if (fullWakeLockTimeTotalMicros != 0) {
2096 sb.setLength(0);
2097 sb.append(prefix);
2098 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2099 (fullWakeLockTimeTotalMicros + 500) / 1000);
2100 pw.println(sb.toString());
2101 }
2102
2103 if (partialWakeLockTimeTotalMicros != 0) {
2104 sb.setLength(0);
2105 sb.append(prefix);
2106 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2107 (partialWakeLockTimeTotalMicros + 500) / 1000);
2108 pw.println(sb.toString());
2109 }
2110
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002111 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002112 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2113 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2114 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2115 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002116 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002117 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002118 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002119 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002120 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002121 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002122 if (time == 0) {
2123 continue;
2124 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002125 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002126 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002127 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002128 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002129 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002130 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002131 sb.append("(");
2132 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002133 sb.append(") ");
2134 sb.append(getPhoneSignalStrengthCount(i, which));
2135 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002136 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002137 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002138 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002139
2140 sb.setLength(0);
2141 sb.append(prefix);
2142 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002143 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002144 pw.println(sb.toString());
2145
Dianne Hackborn627bba72009-03-24 22:32:56 -07002146 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002147 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002148 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002149 didOne = false;
2150 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002151 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002152 if (time == 0) {
2153 continue;
2154 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002155 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002156 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002157 didOne = true;
2158 sb.append(DATA_CONNECTION_NAMES[i]);
2159 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002160 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002161 sb.append("(");
2162 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002163 sb.append(") ");
2164 sb.append(getPhoneDataConnectionCount(i, which));
2165 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002166 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002167 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002168 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002169
2170 sb.setLength(0);
2171 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002172 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002173 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002174 formatTimeMs(sb, mobileActiveTime / 1000);
2175 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2176 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2177 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002178 pw.println(sb.toString());
2179
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002180 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2181 if (mobileActiveUnknownTime != 0) {
2182 sb.setLength(0);
2183 sb.append(prefix);
2184 sb.append(" Mobile radio active unknown time: ");
2185 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2186 sb.append("(");
2187 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2188 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2189 sb.append("x");
2190 pw.println(sb.toString());
2191 }
2192
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002193 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2194 if (mobileActiveAdjustedTime != 0) {
2195 sb.setLength(0);
2196 sb.append(prefix);
2197 sb.append(" Mobile radio active adjusted time: ");
2198 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2199 sb.append("(");
2200 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2201 sb.append(")");
2202 pw.println(sb.toString());
2203 }
2204
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002205 pw.print(prefix);
2206 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2207 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2208 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2209 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002210 sb.setLength(0);
2211 sb.append(prefix);
2212 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2213 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2214 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2215 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002216 sb.append(")");
2217 pw.println(sb.toString());
2218
2219 sb.setLength(0);
2220 sb.append(prefix);
2221 sb.append(" Wifi states:");
2222 didOne = false;
2223 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002224 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002225 if (time == 0) {
2226 continue;
2227 }
2228 sb.append("\n ");
2229 didOne = true;
2230 sb.append(WIFI_STATE_NAMES[i]);
2231 sb.append(" ");
2232 formatTimeMs(sb, time/1000);
2233 sb.append("(");
2234 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2235 sb.append(") ");
2236 sb.append(getPhoneDataConnectionCount(i, which));
2237 sb.append("x");
2238 }
2239 if (!didOne) sb.append(" (no activity)");
2240 pw.println(sb.toString());
2241
2242 sb.setLength(0);
2243 sb.append(prefix);
2244 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002245 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2246 sb.append(")");
2247 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002248
2249 sb.setLength(0);
2250 sb.append(prefix);
2251 sb.append(" Bluetooth states:");
2252 didOne = false;
2253 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002254 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002255 if (time == 0) {
2256 continue;
2257 }
2258 sb.append("\n ");
2259 didOne = true;
2260 sb.append(BLUETOOTH_STATE_NAMES[i]);
2261 sb.append(" ");
2262 formatTimeMs(sb, time/1000);
2263 sb.append("(");
2264 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2265 sb.append(") ");
2266 sb.append(getPhoneDataConnectionCount(i, which));
2267 sb.append("x");
2268 }
2269 if (!didOne) sb.append(" (no activity)");
2270 pw.println(sb.toString());
2271
2272 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002273
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002274 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002275 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002276 pw.print(prefix); pw.println(" Device is currently unplugged");
2277 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2278 pw.println(getDischargeStartLevel());
2279 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2280 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002281 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002282 pw.print(prefix); pw.println(" Device is currently plugged into power");
2283 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2284 pw.println(getDischargeStartLevel());
2285 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2286 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002287 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002288 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2289 pw.println(getDischargeAmountScreenOn());
2290 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2291 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002292 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002293 } else {
2294 pw.print(prefix); pw.println(" Device battery use since last full charge");
2295 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2296 pw.println(getLowDischargeAmountSinceCharge());
2297 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2298 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002299 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2300 pw.println(getDischargeAmountScreenOnSinceCharge());
2301 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2302 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002303 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002304 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002305
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002306 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002307 helper.create(this);
2308 helper.refreshStats(which, UserHandle.USER_ALL);
2309 List<BatterySipper> sippers = helper.getUsageList();
2310 if (sippers != null && sippers.size() > 0) {
2311 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2312 pw.print(prefix); pw.print(" Capacity: ");
2313 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002314 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002315 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2316 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2317 pw.println();
2318 for (int i=0; i<sippers.size(); i++) {
2319 BatterySipper bs = sippers.get(i);
2320 switch (bs.drainType) {
2321 case IDLE:
2322 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2323 pw.println();
2324 break;
2325 case CELL:
2326 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2327 pw.println();
2328 break;
2329 case PHONE:
2330 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2331 pw.println();
2332 break;
2333 case WIFI:
2334 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2335 pw.println();
2336 break;
2337 case BLUETOOTH:
2338 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2339 pw.println();
2340 break;
2341 case SCREEN:
2342 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2343 pw.println();
2344 break;
2345 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002346 pw.print(prefix); pw.print(" Uid ");
2347 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002348 pw.print(": "); printmAh(pw, bs.value); pw.println();
2349 break;
2350 case USER:
2351 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2352 pw.print(": "); printmAh(pw, bs.value); pw.println();
2353 break;
2354 case UNACCOUNTED:
2355 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2356 pw.println();
2357 break;
2358 case OVERCOUNTED:
2359 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2360 pw.println();
2361 break;
2362 }
2363 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002364 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002365 }
2366
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002367 sippers = helper.getMobilemsppList();
2368 if (sippers != null && sippers.size() > 0) {
2369 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002370 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002371 for (int i=0; i<sippers.size(); i++) {
2372 BatterySipper bs = sippers.get(i);
2373 sb.setLength(0);
2374 sb.append(prefix); sb.append(" Uid ");
2375 UserHandle.formatUid(sb, bs.uidObj.getUid());
2376 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2377 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2378 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002379 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002380 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002381 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002382 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002383 sb.setLength(0);
2384 sb.append(prefix);
2385 sb.append(" TOTAL TIME: ");
2386 formatTimeMs(sb, totalTime);
2387 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2388 sb.append(")");
2389 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002390 pw.println();
2391 }
2392
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002393 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2394 @Override
2395 public int compare(TimerEntry lhs, TimerEntry rhs) {
2396 long lhsTime = lhs.mTime;
2397 long rhsTime = rhs.mTime;
2398 if (lhsTime < rhsTime) {
2399 return 1;
2400 }
2401 if (lhsTime > rhsTime) {
2402 return -1;
2403 }
2404 return 0;
2405 }
2406 };
2407
2408 if (reqUid < 0) {
2409 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2410 if (kernelWakelocks.size() > 0) {
2411 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2412 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2413 BatteryStats.Timer timer = ent.getValue();
2414 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2415 if (totalTimeMillis > 0) {
2416 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2417 }
2418 }
2419 if (ktimers.size() > 0) {
2420 Collections.sort(ktimers, timerComparator);
2421 pw.print(prefix); pw.println(" All kernel wake locks:");
2422 for (int i=0; i<ktimers.size(); i++) {
2423 TimerEntry timer = ktimers.get(i);
2424 String linePrefix = ": ";
2425 sb.setLength(0);
2426 sb.append(prefix);
2427 sb.append(" Kernel Wake lock ");
2428 sb.append(timer.mName);
2429 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2430 which, linePrefix);
2431 if (!linePrefix.equals(": ")) {
2432 sb.append(" realtime");
2433 // Only print out wake locks that were held
2434 pw.println(sb.toString());
2435 }
2436 }
2437 pw.println();
2438 }
2439 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002440
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002441 if (timers.size() > 0) {
2442 Collections.sort(timers, timerComparator);
2443 pw.print(prefix); pw.println(" All partial wake locks:");
2444 for (int i=0; i<timers.size(); i++) {
2445 TimerEntry timer = timers.get(i);
2446 sb.setLength(0);
2447 sb.append(" Wake lock ");
2448 UserHandle.formatUid(sb, timer.mId);
2449 sb.append(" ");
2450 sb.append(timer.mName);
2451 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2452 sb.append(" realtime");
2453 pw.println(sb.toString());
2454 }
2455 timers.clear();
2456 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002457 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002458
2459 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2460 if (wakeupReasons.size() > 0) {
2461 pw.print(prefix); pw.println(" All wakeup reasons:");
2462 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2463 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2464 BatteryStats.LongCounter counter = ent.getValue();
2465 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2466 ent.getValue().getCountLocked(which)));
2467 }
2468 Collections.sort(reasons, timerComparator);
2469 for (int i=0; i<reasons.size(); i++) {
2470 TimerEntry timer = reasons.get(i);
2471 String linePrefix = ": ";
2472 sb.setLength(0);
2473 sb.append(prefix);
2474 sb.append(" Wakeup reason ");
2475 sb.append(timer.mName);
2476 sb.append(": ");
2477 formatTimeMs(sb, timer.mTime);
2478 sb.append("realtime");
2479 pw.println(sb.toString());
2480 }
2481 pw.println();
2482 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002483 }
Evan Millar22ac0432009-03-31 11:33:18 -07002484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 for (int iu=0; iu<NU; iu++) {
2486 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002487 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002488 continue;
2489 }
2490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002492
2493 pw.print(prefix);
2494 pw.print(" ");
2495 UserHandle.formatUid(pw, uid);
2496 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002498
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002499 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2500 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2501 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2502 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2503 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2504 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002505 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2506 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002507 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2508 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002509 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2510 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2511 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002512
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002513 if (mobileRxBytes > 0 || mobileTxBytes > 0
2514 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002515 pw.print(prefix); pw.print(" Mobile network: ");
2516 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002517 pw.print(formatBytesLocked(mobileTxBytes));
2518 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2519 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002521 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2522 sb.setLength(0);
2523 sb.append(prefix); sb.append(" Mobile radio active: ");
2524 formatTimeMs(sb, uidMobileActiveTime / 1000);
2525 sb.append("(");
2526 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2527 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2528 long packets = mobileRxPackets + mobileTxPackets;
2529 if (packets == 0) {
2530 packets = 1;
2531 }
2532 sb.append(" @ ");
2533 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2534 sb.append(" mspp");
2535 pw.println(sb.toString());
2536 }
2537
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002538 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002539 pw.print(prefix); pw.print(" Wi-Fi network: ");
2540 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002541 pw.print(formatBytesLocked(wifiTxBytes));
2542 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2543 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002544 }
2545
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002546 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2547 || uidWifiRunningTime != 0) {
2548 sb.setLength(0);
2549 sb.append(prefix); sb.append(" Wifi Running: ");
2550 formatTimeMs(sb, uidWifiRunningTime / 1000);
2551 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2552 whichBatteryRealtime)); sb.append(")\n");
2553 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2554 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2555 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2556 whichBatteryRealtime)); sb.append(")\n");
2557 sb.append(prefix); sb.append(" Wifi Scan: ");
2558 formatTimeMs(sb, wifiScanTime / 1000);
2559 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2560 whichBatteryRealtime)); sb.append(")");
2561 pw.println(sb.toString());
2562 }
2563
Dianne Hackborn617f8772009-03-31 15:04:46 -07002564 if (u.hasUserActivity()) {
2565 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002566 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002567 int val = u.getUserActivityCount(i, which);
2568 if (val != 0) {
2569 if (!hasData) {
2570 sb.setLength(0);
2571 sb.append(" User activity: ");
2572 hasData = true;
2573 } else {
2574 sb.append(", ");
2575 }
2576 sb.append(val);
2577 sb.append(" ");
2578 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2579 }
2580 }
2581 if (hasData) {
2582 pw.println(sb.toString());
2583 }
2584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585
2586 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2587 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002588 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2589 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2591 : wakelocks.entrySet()) {
2592 Uid.Wakelock wl = ent.getValue();
2593 String linePrefix = ": ";
2594 sb.setLength(0);
2595 sb.append(prefix);
2596 sb.append(" Wake lock ");
2597 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002598 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002600 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002602 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 "window", which, linePrefix);
2604 if (!linePrefix.equals(": ")) {
2605 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002606 // Only print out wake locks that were held
2607 pw.println(sb.toString());
2608 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002609 count++;
2610 }
2611 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002612 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002613 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002614 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002615 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002616 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002617 }
2618 if (count > 1) {
2619 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2620 sb.setLength(0);
2621 sb.append(prefix);
2622 sb.append(" TOTAL wake: ");
2623 boolean needComma = false;
2624 if (totalFull != 0) {
2625 needComma = true;
2626 formatTimeMs(sb, totalFull);
2627 sb.append("full");
2628 }
2629 if (totalPartial != 0) {
2630 if (needComma) {
2631 sb.append(", ");
2632 }
2633 needComma = true;
2634 formatTimeMs(sb, totalPartial);
2635 sb.append("partial");
2636 }
2637 if (totalWindow != 0) {
2638 if (needComma) {
2639 sb.append(", ");
2640 }
2641 needComma = true;
2642 formatTimeMs(sb, totalWindow);
2643 sb.append("window");
2644 }
2645 sb.append(" realtime");
2646 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 }
2649 }
2650
2651 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2652 if (sensors.size() > 0) {
2653 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2654 : sensors.entrySet()) {
2655 Uid.Sensor se = ent.getValue();
2656 int sensorNumber = ent.getKey();
2657 sb.setLength(0);
2658 sb.append(prefix);
2659 sb.append(" Sensor ");
2660 int handle = se.getHandle();
2661 if (handle == Uid.Sensor.GPS) {
2662 sb.append("GPS");
2663 } else {
2664 sb.append(handle);
2665 }
2666 sb.append(": ");
2667
2668 Timer timer = se.getSensorTime();
2669 if (timer != null) {
2670 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002671 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002672 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002673 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 //timer.logState();
2675 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002676 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 sb.append("realtime (");
2678 sb.append(count);
2679 sb.append(" times)");
2680 } else {
2681 sb.append("(not used)");
2682 }
2683 } else {
2684 sb.append("(not used)");
2685 }
2686
2687 pw.println(sb.toString());
2688 uidActivity = true;
2689 }
2690 }
2691
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002692 Timer vibTimer = u.getVibratorOnTimer();
2693 if (vibTimer != null) {
2694 // Convert from microseconds to milliseconds with rounding
2695 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002696 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002697 int count = vibTimer.getCountLocked(which);
2698 //timer.logState();
2699 if (totalTime != 0) {
2700 sb.setLength(0);
2701 sb.append(prefix);
2702 sb.append(" Vibrator: ");
2703 formatTimeMs(sb, totalTime);
2704 sb.append("realtime (");
2705 sb.append(count);
2706 sb.append(" times)");
2707 pw.println(sb.toString());
2708 uidActivity = true;
2709 }
2710 }
2711
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002712 Timer fgTimer = u.getForegroundActivityTimer();
2713 if (fgTimer != null) {
2714 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002715 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002716 int count = fgTimer.getCountLocked(which);
2717 if (totalTime != 0) {
2718 sb.setLength(0);
2719 sb.append(prefix);
2720 sb.append(" Foreground activities: ");
2721 formatTimeMs(sb, totalTime);
2722 sb.append("realtime (");
2723 sb.append(count);
2724 sb.append(" times)");
2725 pw.println(sb.toString());
2726 uidActivity = true;
2727 }
2728 }
2729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2731 if (processStats.size() > 0) {
2732 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2733 : processStats.entrySet()) {
2734 Uid.Proc ps = ent.getValue();
2735 long userTime;
2736 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002737 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002739 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740
2741 userTime = ps.getUserTime(which);
2742 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002743 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002745 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002746 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002748 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002749 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002750 sb.setLength(0);
2751 sb.append(prefix); sb.append(" Proc ");
2752 sb.append(ent.getKey()); sb.append(":\n");
2753 sb.append(prefix); sb.append(" CPU: ");
2754 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002755 formatTime(sb, systemTime); sb.append("krn ; ");
2756 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002757 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002758 sb.append("\n"); sb.append(prefix); sb.append(" ");
2759 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002760 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002761 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002762 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002763 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002764 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002765 pw.print(prefix); pw.print(" * Killed for ");
2766 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2767 pw.print("wake lock");
2768 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2769 pw.print("cpu");
2770 } else {
2771 pw.print("unknown");
2772 }
2773 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002774 TimeUtils.formatDuration(ew.usedTime, pw);
2775 pw.print(" over ");
2776 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002777 if (ew.overTime != 0) {
2778 pw.print(" (");
2779 pw.print((ew.usedTime*100)/ew.overTime);
2780 pw.println("%)");
2781 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002782 }
2783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 uidActivity = true;
2785 }
2786 }
2787 }
2788
2789 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2790 if (packageStats.size() > 0) {
2791 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2792 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002793 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 boolean apkActivity = false;
2795 Uid.Pkg ps = ent.getValue();
2796 int wakeups = ps.getWakeups(which);
2797 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002798 pw.print(prefix); pw.print(" ");
2799 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 apkActivity = true;
2801 }
2802 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2803 if (serviceStats.size() > 0) {
2804 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2805 : serviceStats.entrySet()) {
2806 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2807 long startTime = ss.getStartTime(batteryUptime, which);
2808 int starts = ss.getStarts(which);
2809 int launches = ss.getLaunches(which);
2810 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002811 sb.setLength(0);
2812 sb.append(prefix); sb.append(" Service ");
2813 sb.append(sent.getKey()); sb.append(":\n");
2814 sb.append(prefix); sb.append(" Created for: ");
2815 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002816 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002817 sb.append(prefix); sb.append(" Starts: ");
2818 sb.append(starts);
2819 sb.append(", launches: "); sb.append(launches);
2820 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 apkActivity = true;
2822 }
2823 }
2824 }
2825 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002826 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
2828 uidActivity = true;
2829 }
2830 }
2831 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002832 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
2834 }
2835 }
2836
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002837 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002838 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002839 int diff = oldval ^ newval;
2840 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002841 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002842 for (int i=0; i<descriptions.length; i++) {
2843 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002844 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002845 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002846 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002847 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002848 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002849 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2850 didWake = true;
2851 pw.print("=");
2852 if (longNames) {
2853 UserHandle.formatUid(pw, wakelockTag.uid);
2854 pw.print(":\"");
2855 pw.print(wakelockTag.string);
2856 pw.print("\"");
2857 } else {
2858 pw.print(wakelockTag.poolIdx);
2859 }
2860 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002861 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002862 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002863 pw.print("=");
2864 int val = (newval&bd.mask)>>bd.shift;
2865 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002866 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002867 } else {
2868 pw.print(val);
2869 }
2870 }
2871 }
2872 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002873 if (!didWake && wakelockTag != null) {
2874 pw.print(longNames ? "wake_lock=" : "w=");
2875 if (longNames) {
2876 UserHandle.formatUid(pw, wakelockTag.uid);
2877 pw.print(":\"");
2878 pw.print(wakelockTag.string);
2879 pw.print("\"");
2880 } else {
2881 pw.print(wakelockTag.poolIdx);
2882 }
2883 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002884 }
2885
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002886 public void prepareForDumpLocked() {
2887 }
2888
2889 public static class HistoryPrinter {
2890 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002891 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002892 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002893 int oldStatus = -1;
2894 int oldHealth = -1;
2895 int oldPlug = -1;
2896 int oldTemp = -1;
2897 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002898 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002899 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002900
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002901 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002902 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002903 if (!checkin) {
2904 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002905 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002906 pw.print(" (");
2907 pw.print(rec.numReadInts);
2908 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002909 } else {
2910 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002911 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002912 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002913 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002914 }
2915 lastTime = rec.time;
2916 }
2917 if (rec.cmd == HistoryItem.CMD_START) {
2918 if (checkin) {
2919 pw.print(":");
2920 }
2921 pw.println("START");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002922 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
2923 if (checkin) {
2924 pw.print(":");
2925 }
2926 pw.print("TIME:");
2927 if (checkin) {
2928 pw.println(rec.currentTime);
2929 } else {
2930 pw.print(" ");
2931 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
2932 rec.currentTime).toString());
2933 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002934 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2935 if (checkin) {
2936 pw.print(":");
2937 }
2938 pw.println("*OVERFLOW*");
2939 } else {
2940 if (!checkin) {
2941 if (rec.batteryLevel < 10) pw.print("00");
2942 else if (rec.batteryLevel < 100) pw.print("0");
2943 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002944 if (verbose) {
2945 pw.print(" ");
2946 if (rec.states < 0) ;
2947 else if (rec.states < 0x10) pw.print("0000000");
2948 else if (rec.states < 0x100) pw.print("000000");
2949 else if (rec.states < 0x1000) pw.print("00000");
2950 else if (rec.states < 0x10000) pw.print("0000");
2951 else if (rec.states < 0x100000) pw.print("000");
2952 else if (rec.states < 0x1000000) pw.print("00");
2953 else if (rec.states < 0x10000000) pw.print("0");
2954 pw.print(Integer.toHexString(rec.states));
2955 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002956 } else {
2957 if (oldLevel != rec.batteryLevel) {
2958 oldLevel = rec.batteryLevel;
2959 pw.print(",Bl="); pw.print(rec.batteryLevel);
2960 }
2961 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002962 if (oldStatus != rec.batteryStatus) {
2963 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002964 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002965 switch (oldStatus) {
2966 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002967 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002968 break;
2969 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002970 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002971 break;
2972 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002973 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002974 break;
2975 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002976 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002977 break;
2978 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002979 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002980 break;
2981 default:
2982 pw.print(oldStatus);
2983 break;
2984 }
2985 }
2986 if (oldHealth != rec.batteryHealth) {
2987 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002988 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002989 switch (oldHealth) {
2990 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002991 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002992 break;
2993 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002994 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002995 break;
2996 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002997 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002998 break;
2999 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003000 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003001 break;
3002 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003003 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003004 break;
3005 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003006 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003007 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003008 case BatteryManager.BATTERY_HEALTH_COLD:
3009 pw.print(checkin ? "c" : "cold");
3010 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003011 default:
3012 pw.print(oldHealth);
3013 break;
3014 }
3015 }
3016 if (oldPlug != rec.batteryPlugType) {
3017 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003018 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003019 switch (oldPlug) {
3020 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003021 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003022 break;
3023 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003024 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003025 break;
3026 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003027 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003028 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003029 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003030 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003031 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003032 default:
3033 pw.print(oldPlug);
3034 break;
3035 }
3036 }
3037 if (oldTemp != rec.batteryTemperature) {
3038 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003039 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003040 pw.print(oldTemp);
3041 }
3042 if (oldVolt != rec.batteryVoltage) {
3043 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003044 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003045 pw.print(oldVolt);
3046 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003047 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003048 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003049 printBitDescriptions(pw, oldState2, rec.states2, null,
3050 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003051 if (rec.wakeReasonTag != null) {
3052 if (checkin) {
3053 pw.print(",Wr=");
3054 pw.print(rec.wakeReasonTag.poolIdx);
3055 } else {
3056 pw.print(" wake_reason=");
3057 pw.print(rec.wakeReasonTag.uid);
3058 pw.print(":\"");
3059 pw.print(rec.wakeReasonTag.string);
3060 pw.print("\"");
3061 }
3062 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003063 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003064 pw.print(checkin ? "," : " ");
3065 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3066 pw.print("+");
3067 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3068 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003069 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003070 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3071 : HISTORY_EVENT_NAMES;
3072 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3073 | HistoryItem.EVENT_FLAG_FINISH);
3074 if (idx >= 0 && idx < eventNames.length) {
3075 pw.print(eventNames[idx]);
3076 } else {
3077 pw.print(checkin ? "Ev" : "event");
3078 pw.print(idx);
3079 }
3080 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003081 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003082 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003083 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003084 UserHandle.formatUid(pw, rec.eventTag.uid);
3085 pw.print(":\"");
3086 pw.print(rec.eventTag.string);
3087 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003088 }
3089 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003090 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003091 oldState = rec.states;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003092 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003093 }
3094 }
3095
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003096 private void printSizeValue(PrintWriter pw, long size) {
3097 float result = size;
3098 String suffix = "";
3099 if (result >= 10*1024) {
3100 suffix = "KB";
3101 result = result / 1024;
3102 }
3103 if (result >= 10*1024) {
3104 suffix = "MB";
3105 result = result / 1024;
3106 }
3107 if (result >= 10*1024) {
3108 suffix = "GB";
3109 result = result / 1024;
3110 }
3111 if (result >= 10*1024) {
3112 suffix = "TB";
3113 result = result / 1024;
3114 }
3115 if (result >= 10*1024) {
3116 suffix = "PB";
3117 result = result / 1024;
3118 }
3119 pw.print((int)result);
3120 pw.print(suffix);
3121 }
3122
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003123 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3124 public static final int DUMP_CHARGED_ONLY = 1<<1;
3125 public static final int DUMP_HISTORY_ONLY = 1<<2;
3126 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003127 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 /**
3130 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3131 *
3132 * @param pw a Printer to receive the dump output.
3133 */
3134 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003135 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003136 prepareForDumpLocked();
3137
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003138 final boolean filtering =
3139 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003140
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003141 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
3142 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3143
3144 final HistoryItem rec = new HistoryItem();
3145 final long historyTotalSize = getHistoryTotalSize();
3146 final long historyUsedSize = getHistoryUsedSize();
3147 if (startIteratingHistoryLocked()) {
3148 try {
3149 pw.print("Battery History (");
3150 pw.print((100*historyUsedSize)/historyTotalSize);
3151 pw.print("% used, ");
3152 printSizeValue(pw, historyUsedSize);
3153 pw.print(" used of ");
3154 printSizeValue(pw, historyTotalSize);
3155 pw.print(", ");
3156 pw.print(getHistoryStringPoolSize());
3157 pw.print(" strings using ");
3158 printSizeValue(pw, getHistoryStringPoolBytes());
3159 pw.println("):");
3160 HistoryPrinter hprinter = new HistoryPrinter();
3161 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003162 long baseTime = -1;
3163 boolean printed = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003164 while (getNextHistoryLocked(rec)) {
3165 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003166 if (baseTime < 0) {
3167 baseTime = lastTime;
3168 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003169 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003170 if (histStart >= 0 && !printed) {
3171 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3172 printed = true;
3173 } else if (rec.currentTime != 0) {
3174 printed = true;
3175 byte cmd = rec.cmd;
3176 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3177 hprinter.printNextItem(pw, rec, baseTime, false,
3178 (flags&DUMP_VERBOSE) != 0);
3179 rec.cmd = cmd;
3180 }
3181 }
3182 hprinter.printNextItem(pw, rec, baseTime, false,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003183 (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003184 }
3185 }
3186 if (histStart >= 0) {
3187 pw.print(" NEXT: "); pw.println(lastTime+1);
3188 }
3189 pw.println();
3190 } finally {
3191 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003192 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003193 }
3194
3195 if (startIteratingOldHistoryLocked()) {
3196 try {
3197 pw.println("Old battery History:");
3198 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003199 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003200 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003201 if (baseTime < 0) {
3202 baseTime = rec.time;
3203 }
3204 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003205 }
3206 pw.println();
3207 } finally {
3208 finishIteratingOldHistoryLocked();
3209 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003210 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003211 }
3212
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003213 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003214 return;
3215 }
3216
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003217 if (!filtering) {
3218 SparseArray<? extends Uid> uidStats = getUidStats();
3219 final int NU = uidStats.size();
3220 boolean didPid = false;
3221 long nowRealtime = SystemClock.elapsedRealtime();
3222 for (int i=0; i<NU; i++) {
3223 Uid uid = uidStats.valueAt(i);
3224 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3225 if (pids != null) {
3226 for (int j=0; j<pids.size(); j++) {
3227 Uid.Pid pid = pids.valueAt(j);
3228 if (!didPid) {
3229 pw.println("Per-PID Stats:");
3230 didPid = true;
3231 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003232 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3233 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003234 pw.print(" PID "); pw.print(pids.keyAt(j));
3235 pw.print(" wake time: ");
3236 TimeUtils.formatDuration(time, pw);
3237 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003238 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003239 }
3240 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003241 if (didPid) {
3242 pw.println("");
3243 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003244 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003245
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003246 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003247 pw.println("Statistics since last charge:");
3248 pw.println(" System starts: " + getStartCount()
3249 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003250 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003251 pw.println("");
3252 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003253 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3254 pw.println("Statistics since last unplugged:");
3255 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
3258
3259 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003260 public void dumpCheckinLocked(Context context, PrintWriter pw,
3261 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003262 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003264 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3265
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003266 final boolean filtering =
3267 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3268
3269 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003270 final HistoryItem rec = new HistoryItem();
3271 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003272 try {
3273 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3274 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3275 pw.print(HISTORY_STRING_POOL); pw.print(',');
3276 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003277 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003278 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003279 pw.print(",\"");
3280 String str = getHistoryTagPoolString(i);
3281 str = str.replace("\\", "\\\\");
3282 str = str.replace("\"", "\\\"");
3283 pw.print(str);
3284 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003285 pw.println();
3286 }
3287 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003288 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003289 long baseTime = -1;
3290 boolean printed = false;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003291 while (getNextHistoryLocked(rec)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003292 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003293 if (baseTime < 0) {
3294 baseTime = lastTime;
3295 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003296 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003297 if (histStart >= 0 && !printed) {
3298 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3299 printed = true;
3300 } else if (rec.currentTime != 0) {
3301 printed = true;
3302 byte cmd = rec.cmd;
3303 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3304 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3305 pw.print(HISTORY_DATA); pw.print(',');
3306 hprinter.printNextItem(pw, rec, baseTime, true, false);
3307 rec.cmd = cmd;
3308 }
3309 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003310 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3311 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003312 hprinter.printNextItem(pw, rec, baseTime, true, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003313 }
3314 }
3315 if (histStart >= 0) {
3316 pw.print("NEXT: "); pw.println(lastTime+1);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003317 }
3318 } finally {
3319 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003320 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003321 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003322 }
3323
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003324 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003325 return;
3326 }
3327
Dianne Hackborne4a59512010-12-07 11:08:07 -08003328 if (apps != null) {
3329 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3330 for (int i=0; i<apps.size(); i++) {
3331 ApplicationInfo ai = apps.get(i);
3332 ArrayList<String> pkgs = uids.get(ai.uid);
3333 if (pkgs == null) {
3334 pkgs = new ArrayList<String>();
3335 uids.put(ai.uid, pkgs);
3336 }
3337 pkgs.add(ai.packageName);
3338 }
3339 SparseArray<? extends Uid> uidStats = getUidStats();
3340 final int NU = uidStats.size();
3341 String[] lineArgs = new String[2];
3342 for (int i=0; i<NU; i++) {
3343 int uid = uidStats.keyAt(i);
3344 ArrayList<String> pkgs = uids.get(uid);
3345 if (pkgs != null) {
3346 for (int j=0; j<pkgs.size(); j++) {
3347 lineArgs[0] = Integer.toString(uid);
3348 lineArgs[1] = pkgs.get(j);
3349 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3350 (Object[])lineArgs);
3351 }
3352 }
3353 }
3354 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003355 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003356 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003357 }
3358 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003359 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
3361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362}