blob: f1ad1f892b0acdeb47319074754bd5fe9ad86923 [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";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700176 private static final String DISCHARGE_STEP_DATA = "dsd";
177 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700178 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
179 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700181 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private final Formatter mFormatter = new Formatter(mFormatBuilder);
183
184 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700185 * State for keeping track of counting information.
186 */
187 public static abstract class Counter {
188
189 /**
190 * Returns the count associated with this Counter for the
191 * selected type of statistics.
192 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700193 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700194 */
Evan Millarc64edde2009-04-18 12:26:32 -0700195 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700196
197 /**
198 * Temporary for debugging.
199 */
200 public abstract void logState(Printer pw, String prefix);
201 }
202
203 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700204 * State for keeping track of long counting information.
205 */
206 public static abstract class LongCounter {
207
208 /**
209 * Returns the count associated with this Counter for the
210 * selected type of statistics.
211 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700212 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700213 */
214 public abstract long getCountLocked(int which);
215
216 /**
217 * Temporary for debugging.
218 */
219 public abstract void logState(Printer pw, String prefix);
220 }
221
222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * State for keeping track of timing information.
224 */
225 public static abstract class Timer {
226
227 /**
228 * Returns the count associated with this Timer for the
229 * selected type of statistics.
230 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700231 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 */
Evan Millarc64edde2009-04-18 12:26:32 -0700233 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
235 /**
236 * Returns the total time in microseconds associated with this Timer for the
237 * selected type of statistics.
238 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800239 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700240 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * @return a time in microseconds
242 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800243 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Temporary for debugging.
247 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700248 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
251 /**
252 * The statistics associated with a particular uid.
253 */
254 public static abstract class Uid {
255
256 /**
257 * Returns a mapping containing wakelock statistics.
258 *
259 * @return a Map from Strings to Uid.Wakelock objects.
260 */
261 public abstract Map<String, ? extends Wakelock> getWakelockStats();
262
263 /**
264 * The statistics associated with a particular wake lock.
265 */
266 public static abstract class Wakelock {
267 public abstract Timer getWakeTime(int type);
268 }
269
270 /**
271 * Returns a mapping containing sensor statistics.
272 *
273 * @return a Map from Integer sensor ids to Uid.Sensor objects.
274 */
275 public abstract Map<Integer, ? extends Sensor> getSensorStats();
276
277 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700278 * Returns a mapping containing active process data.
279 */
280 public abstract SparseArray<? extends Pid> getPidStats();
281
282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * Returns a mapping containing process statistics.
284 *
285 * @return a Map from Strings to Uid.Proc objects.
286 */
287 public abstract Map<String, ? extends Proc> getProcessStats();
288
289 /**
290 * Returns a mapping containing package statistics.
291 *
292 * @return a Map from Strings to Uid.Pkg objects.
293 */
294 public abstract Map<String, ? extends Pkg> getPackageStats();
295
296 /**
297 * {@hide}
298 */
299 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700300
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800301 public abstract void noteWifiRunningLocked(long elapsedRealtime);
302 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
303 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
304 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
305 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
306 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
307 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
308 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
309 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
310 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
311 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
312 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
313 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
314 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
315 public abstract void noteActivityResumedLocked(long elapsedRealtime);
316 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800317 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
318 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
319 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
320 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
321 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
322 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
323 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700324 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800325 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Robert Greenwalta029ea12013-09-25 16:38:12 -0700327 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
328
Dianne Hackborn617f8772009-03-31 15:04:46 -0700329 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700330 * Note that these must match the constants in android.os.PowerManager.
331 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
332 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700333 */
334 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700335 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700336 };
337
Jeff Browndf693de2012-07-27 12:03:38 -0700338 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700339
Dianne Hackborn617f8772009-03-31 15:04:46 -0700340 public abstract void noteUserActivityLocked(int type);
341 public abstract boolean hasUserActivity();
342 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700343
344 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800345 public abstract long getNetworkActivityBytes(int type, int which);
346 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800347 public abstract long getMobileRadioActiveTime(int which);
348 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800351 /*
352 * FIXME: it's not correct to use this magic value because it
353 * could clash with a sensor handle (which are defined by
354 * the sensor HAL, and therefore out of our control
355 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 // Magic sensor number for the GPS.
357 public static final int GPS = -10000;
358
359 public abstract int getHandle();
360
361 public abstract Timer getSensorTime();
362 }
363
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700364 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800365 public int mWakeNesting;
366 public long mWakeSumMs;
367 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700368 }
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * The statistics associated with a particular process.
372 */
373 public static abstract class Proc {
374
Dianne Hackborn287952c2010-09-22 22:34:31 -0700375 public static class ExcessivePower {
376 public static final int TYPE_WAKE = 1;
377 public static final int TYPE_CPU = 2;
378
379 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700380 public long overTime;
381 public long usedTime;
382 }
383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800385 * Returns true if this process is still active in the battery stats.
386 */
387 public abstract boolean isActive();
388
389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * Returns the total time (in 1/100 sec) spent executing in user code.
391 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700392 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
394 public abstract long getUserTime(int which);
395
396 /**
397 * Returns the total time (in 1/100 sec) spent executing in system code.
398 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700399 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
401 public abstract long getSystemTime(int which);
402
403 /**
404 * Returns the number of times the process has been started.
405 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700406 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 */
408 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700409
410 /**
411 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700413 * @return foreground cpu time in microseconds
414 */
415 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700416
417 /**
418 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
419 * @param speedStep the index of the CPU speed. This is not the actual speed of the
420 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700421 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700422 * @see BatteryStats#getCpuSpeedSteps()
423 */
424 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700425
Dianne Hackborn287952c2010-09-22 22:34:31 -0700426 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700427
Dianne Hackborn287952c2010-09-22 22:34:31 -0700428 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
430
431 /**
432 * The statistics associated with a particular package.
433 */
434 public static abstract class Pkg {
435
436 /**
437 * Returns the number of times this package has done something that could wake up the
438 * device from sleep.
439 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700440 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
442 public abstract int getWakeups(int which);
443
444 /**
445 * Returns a mapping containing service statistics.
446 */
447 public abstract Map<String, ? extends Serv> getServiceStats();
448
449 /**
450 * The statistics associated with a particular service.
451 */
452 public abstract class Serv {
453
454 /**
455 * Returns the amount of time spent started.
456 *
457 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700458 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * @return
460 */
461 public abstract long getStartTime(long batteryUptime, int which);
462
463 /**
464 * Returns the total number of times startService() has been called.
465 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700466 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 */
468 public abstract int getStarts(int which);
469
470 /**
471 * Returns the total number times the service has been launched.
472 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700473 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 */
475 public abstract int getLaunches(int which);
476 }
477 }
478 }
479
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800480 public final static class HistoryTag {
481 public String string;
482 public int uid;
483
484 public int poolIdx;
485
486 public void setTo(HistoryTag o) {
487 string = o.string;
488 uid = o.uid;
489 poolIdx = o.poolIdx;
490 }
491
492 public void setTo(String _string, int _uid) {
493 string = _string;
494 uid = _uid;
495 poolIdx = -1;
496 }
497
498 public void writeToParcel(Parcel dest, int flags) {
499 dest.writeString(string);
500 dest.writeInt(uid);
501 }
502
503 public void readFromParcel(Parcel src) {
504 string = src.readString();
505 uid = src.readInt();
506 poolIdx = -1;
507 }
508
509 @Override
510 public boolean equals(Object o) {
511 if (this == o) return true;
512 if (o == null || getClass() != o.getClass()) return false;
513
514 HistoryTag that = (HistoryTag) o;
515
516 if (uid != that.uid) return false;
517 if (!string.equals(that.string)) return false;
518
519 return true;
520 }
521
522 @Override
523 public int hashCode() {
524 int result = string.hashCode();
525 result = 31 * result + uid;
526 return result;
527 }
528 }
529
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700530 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700531 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700532
533 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800534
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800535 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800536 public static final byte CMD_NULL = -1;
537 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800538 public static final byte CMD_CURRENT_TIME = 5;
539 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800540
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700541 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700542
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800543 /**
544 * Return whether the command code is a delta data update.
545 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800546 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800547 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800548 }
549
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700550 public byte batteryLevel;
551 public byte batteryStatus;
552 public byte batteryHealth;
553 public byte batteryPlugType;
554
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900555 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700556 public char batteryVoltage;
557
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700558 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700559 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800560 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700561 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800562 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
563 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700564 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800565 public static final int STATE_PHONE_STATE_SHIFT = 6;
566 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700567 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800568 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800569 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800570
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700571 // These states always appear directly in the first int token
572 // of a delta change; they should be ones that change relatively
573 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700574 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
575 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800576 public static final int STATE_GPS_ON_FLAG = 1<<29;
577 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800578 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800579 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800580 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800581 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700582 // These are on the lower bits used for the command; if they change
583 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700584 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700585 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700586 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700587 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
588 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
589 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
590 public static final int STATE_WIFI_ON_FLAG = 1<<17;
591 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700592
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700593 public static final int MOST_INTERESTING_STATES =
594 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
595 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
596
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700597 public int states;
598
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700599 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700600 public int states2;
601
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800602 // The wake lock that was acquired at this point.
603 public HistoryTag wakelockTag;
604
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800605 // Kernel wakeup reason at this point.
606 public HistoryTag wakeReasonTag;
607
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800608 public static final int EVENT_FLAG_START = 0x8000;
609 public static final int EVENT_FLAG_FINISH = 0x4000;
610
611 // No event in this item.
612 public static final int EVENT_NONE = 0x0000;
613 // Event is about a process that is running.
614 public static final int EVENT_PROC = 0x0001;
615 // Event is about an application package that is in the foreground.
616 public static final int EVENT_FOREGROUND = 0x0002;
617 // Event is about an application package that is at the top of the screen.
618 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800619 // Event is about an application package that is at the top of the screen.
620 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800621 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800622 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800623
624 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
625 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
626 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
627 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
628 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
629 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800630 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
631 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800632
633 // For CMD_EVENT.
634 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800635 public HistoryTag eventTag;
636
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800637 // Only set for CMD_CURRENT_TIME.
638 public long currentTime;
639
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800640 // Meta-data when reading.
641 public int numReadInts;
642
643 // Pre-allocated objects.
644 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800645 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800646 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800647
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700648 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700649 }
650
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700651 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700652 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800653 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700654 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700655 }
656
657 public int describeContents() {
658 return 0;
659 }
660
661 public void writeToParcel(Parcel dest, int flags) {
662 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700663 int bat = (((int)cmd)&0xff)
664 | ((((int)batteryLevel)<<8)&0xff00)
665 | ((((int)batteryStatus)<<16)&0xf0000)
666 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800667 | ((((int)batteryPlugType)<<24)&0xf000000)
668 | (wakelockTag != null ? 0x10000000 : 0)
669 | (wakeReasonTag != null ? 0x20000000 : 0)
670 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700671 dest.writeInt(bat);
672 bat = (((int)batteryTemperature)&0xffff)
673 | ((((int)batteryVoltage)<<16)&0xffff0000);
674 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700675 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700676 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800677 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800678 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800679 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800680 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800681 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800682 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800683 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800684 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800685 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800686 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800687 if (cmd == CMD_CURRENT_TIME) {
688 dest.writeLong(currentTime);
689 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700690 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700691
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800692 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800693 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700694 int bat = src.readInt();
695 cmd = (byte)(bat&0xff);
696 batteryLevel = (byte)((bat>>8)&0xff);
697 batteryStatus = (byte)((bat>>16)&0xf);
698 batteryHealth = (byte)((bat>>20)&0xf);
699 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800700 int bat2 = src.readInt();
701 batteryTemperature = (short)(bat2&0xffff);
702 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700703 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700704 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800705 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800706 wakelockTag = localWakelockTag;
707 wakelockTag.readFromParcel(src);
708 } else {
709 wakelockTag = null;
710 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800711 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800712 wakeReasonTag = localWakeReasonTag;
713 wakeReasonTag.readFromParcel(src);
714 } else {
715 wakeReasonTag = null;
716 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800717 if ((bat&0x40000000) != 0) {
718 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800719 eventTag = localEventTag;
720 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800721 } else {
722 eventCode = EVENT_NONE;
723 eventTag = null;
724 }
725 if (cmd == CMD_CURRENT_TIME) {
726 currentTime = src.readLong();
727 } else {
728 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700729 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800730 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700731 }
732
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700733 public void clear() {
734 time = 0;
735 cmd = CMD_NULL;
736 batteryLevel = 0;
737 batteryStatus = 0;
738 batteryHealth = 0;
739 batteryPlugType = 0;
740 batteryTemperature = 0;
741 batteryVoltage = 0;
742 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700743 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800744 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800745 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800746 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800747 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700748 }
749
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700750 public void setTo(HistoryItem o) {
751 time = o.time;
752 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800753 setToCommon(o);
754 }
755
756 public void setTo(long time, byte cmd, HistoryItem o) {
757 this.time = time;
758 this.cmd = cmd;
759 setToCommon(o);
760 }
761
762 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700763 batteryLevel = o.batteryLevel;
764 batteryStatus = o.batteryStatus;
765 batteryHealth = o.batteryHealth;
766 batteryPlugType = o.batteryPlugType;
767 batteryTemperature = o.batteryTemperature;
768 batteryVoltage = o.batteryVoltage;
769 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700770 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800771 if (o.wakelockTag != null) {
772 wakelockTag = localWakelockTag;
773 wakelockTag.setTo(o.wakelockTag);
774 } else {
775 wakelockTag = null;
776 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800777 if (o.wakeReasonTag != null) {
778 wakeReasonTag = localWakeReasonTag;
779 wakeReasonTag.setTo(o.wakeReasonTag);
780 } else {
781 wakeReasonTag = null;
782 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800783 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800784 if (o.eventTag != null) {
785 eventTag = localEventTag;
786 eventTag.setTo(o.eventTag);
787 } else {
788 eventTag = null;
789 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800790 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700791 }
792
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800793 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700794 return batteryLevel == o.batteryLevel
795 && batteryStatus == o.batteryStatus
796 && batteryHealth == o.batteryHealth
797 && batteryPlugType == o.batteryPlugType
798 && batteryTemperature == o.batteryTemperature
799 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800800 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700801 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800802 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700803 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800804
805 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800806 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800807 return false;
808 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800809 if (wakelockTag != o.wakelockTag) {
810 if (wakelockTag == null || o.wakelockTag == null) {
811 return false;
812 }
813 if (!wakelockTag.equals(o.wakelockTag)) {
814 return false;
815 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800816 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800817 if (wakeReasonTag != o.wakeReasonTag) {
818 if (wakeReasonTag == null || o.wakeReasonTag == null) {
819 return false;
820 }
821 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
822 return false;
823 }
824 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800825 if (eventTag != o.eventTag) {
826 if (eventTag == null || o.eventTag == null) {
827 return false;
828 }
829 if (!eventTag.equals(o.eventTag)) {
830 return false;
831 }
832 }
833 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800834 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700835 }
836
837 public static final class BitDescription {
838 public final int mask;
839 public final int shift;
840 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800841 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700842 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800843 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700844
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800845 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700846 this.mask = mask;
847 this.shift = -1;
848 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800849 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700850 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800851 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700852 }
853
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800854 public BitDescription(int mask, int shift, String name, String shortName,
855 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700856 this.mask = mask;
857 this.shift = shift;
858 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800859 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700860 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800861 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700862 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700863 }
864
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800865 public abstract int getHistoryTotalSize();
866
867 public abstract int getHistoryUsedSize();
868
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700869 public abstract boolean startIteratingHistoryLocked();
870
Dianne Hackborn099bc622014-01-22 13:39:16 -0800871 public abstract int getHistoryStringPoolSize();
872
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800873 public abstract int getHistoryStringPoolBytes();
874
875 public abstract String getHistoryTagPoolString(int index);
876
877 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800878
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700879 public abstract boolean getNextHistoryLocked(HistoryItem out);
880
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700881 public abstract void finishIteratingHistoryLocked();
882
883 public abstract boolean startIteratingOldHistoryLocked();
884
885 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
886
887 public abstract void finishIteratingOldHistoryLocked();
888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700890 * Return the base time offset for the battery history.
891 */
892 public abstract long getHistoryBaseTime();
893
894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 * Returns the number of times the device has been started.
896 */
897 public abstract int getStartCount();
898
899 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700900 * 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 -0800901 * running on battery.
902 *
903 * {@hide}
904 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800905 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800907 /**
908 * Returns the number of times the screen was turned on.
909 *
910 * {@hide}
911 */
912 public abstract int getScreenOnCount(int which);
913
Dianne Hackborn617f8772009-03-31 15:04:46 -0700914 public static final int SCREEN_BRIGHTNESS_DARK = 0;
915 public static final int SCREEN_BRIGHTNESS_DIM = 1;
916 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
917 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
918 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
919
920 static final String[] SCREEN_BRIGHTNESS_NAMES = {
921 "dark", "dim", "medium", "light", "bright"
922 };
923
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800924 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
925 "0", "1", "2", "3", "4"
926 };
927
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
929
930 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700931 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700932 * the given brightness
933 *
934 * {@hide}
935 */
936 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800937 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700938
939 public abstract int getInputEventCount(int which);
940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700942 * 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 -0800943 * running on battery.
944 *
945 * {@hide}
946 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700948
949 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800950 * Returns the number of times a phone call was activated.
951 *
952 * {@hide}
953 */
954 public abstract int getPhoneOnCount(int which);
955
956 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700957 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700958 * the given signal strength.
959 *
960 * {@hide}
961 */
962 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800963 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700964
Dianne Hackborn617f8772009-03-31 15:04:46 -0700965 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700966 * Returns the time in microseconds that the phone has been trying to
967 * acquire a signal.
968 *
969 * {@hide}
970 */
971 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800972 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -0700973
974 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700975 * Returns the number of times the phone has entered the given signal strength.
976 *
977 * {@hide}
978 */
979 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
980
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800981 /**
982 * Returns the time in microseconds that the mobile network has been active
983 * (in a high power state).
984 *
985 * {@hide}
986 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800987 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800988
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800989 /**
990 * Returns the number of times that the mobile network has transitioned to the
991 * active state.
992 *
993 * {@hide}
994 */
995 public abstract int getMobileRadioActiveCount(int which);
996
997 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700998 * Returns the time in microseconds that is the difference between the mobile radio
999 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1000 * from the radio.
1001 *
1002 * {@hide}
1003 */
1004 public abstract long getMobileRadioActiveAdjustedTime(int which);
1005
1006 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001007 * Returns the time in microseconds that the mobile network has been active
1008 * (in a high power state) but not being able to blame on an app.
1009 *
1010 * {@hide}
1011 */
1012 public abstract long getMobileRadioActiveUnknownTime(int which);
1013
1014 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001015 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001016 *
1017 * {@hide}
1018 */
1019 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001020
Dianne Hackborn627bba72009-03-24 22:32:56 -07001021 public static final int DATA_CONNECTION_NONE = 0;
1022 public static final int DATA_CONNECTION_GPRS = 1;
1023 public static final int DATA_CONNECTION_EDGE = 2;
1024 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001025 public static final int DATA_CONNECTION_CDMA = 4;
1026 public static final int DATA_CONNECTION_EVDO_0 = 5;
1027 public static final int DATA_CONNECTION_EVDO_A = 6;
1028 public static final int DATA_CONNECTION_1xRTT = 7;
1029 public static final int DATA_CONNECTION_HSDPA = 8;
1030 public static final int DATA_CONNECTION_HSUPA = 9;
1031 public static final int DATA_CONNECTION_HSPA = 10;
1032 public static final int DATA_CONNECTION_IDEN = 11;
1033 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001034 public static final int DATA_CONNECTION_LTE = 13;
1035 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001036 public static final int DATA_CONNECTION_HSPAP = 15;
1037 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001038
Dianne Hackborn627bba72009-03-24 22:32:56 -07001039 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001040 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001041 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001042 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001043 };
1044
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001045 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001046
1047 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001048 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001049 * the given data connection.
1050 *
1051 * {@hide}
1052 */
1053 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001054 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001057 * Returns the number of times the phone has entered the given data
1058 * connection type.
1059 *
1060 * {@hide}
1061 */
1062 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001063
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001064 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1065 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001066 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001067 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1068 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001069 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001070 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1071 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1072 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001073 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001074 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1075 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001076 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001077 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1078 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1079 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1080 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1081 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1082 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1083 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1084 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1085 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1086 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1087 new String[] {"in", "out", "emergency", "off"},
1088 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001089 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001090 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1091 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1092 "0", "1", "2", "3", "4"
1093 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001094 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1095 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1096 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001097 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001098
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001099 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1100 = new BitDescription[] {
1101 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1102 };
1103
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001104 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001105 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001106 };
1107
1108 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001109 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001110 };
1111
Dianne Hackborn617f8772009-03-31 15:04:46 -07001112 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001113 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001114 * running on battery.
1115 *
1116 * {@hide}
1117 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001118 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001119
1120 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001121 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001122 * been in the running state while the device was running on battery.
1123 *
1124 * {@hide}
1125 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001126 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001127
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001128 public static final int WIFI_STATE_OFF = 0;
1129 public static final int WIFI_STATE_OFF_SCANNING = 1;
1130 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1131 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1132 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1133 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1134 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1135 public static final int WIFI_STATE_SOFT_AP = 7;
1136
1137 static final String[] WIFI_STATE_NAMES = {
1138 "off", "scanning", "no_net", "disconn",
1139 "sta", "p2p", "sta_p2p", "soft_ap"
1140 };
1141
1142 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1143
1144 /**
1145 * Returns the time in microseconds that WiFi has been running in the given state.
1146 *
1147 * {@hide}
1148 */
1149 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001150 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001151
1152 /**
1153 * Returns the number of times that WiFi has entered the given state.
1154 *
1155 * {@hide}
1156 */
1157 public abstract int getWifiStateCount(int wifiState, int which);
1158
The Android Open Source Project10592532009-03-18 17:39:46 -07001159 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001160 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001161 * running on battery.
1162 *
1163 * {@hide}
1164 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001166
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001167 public abstract int getBluetoothPingCount();
1168
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001169 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1170 public static final int BLUETOOTH_STATE_LOW = 1;
1171 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1172 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001173
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001174 static final String[] BLUETOOTH_STATE_NAMES = {
1175 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001176 };
1177
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001178 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001179
1180 /**
1181 * Returns the time in microseconds that Bluetooth has been running in the
1182 * given active state.
1183 *
1184 * {@hide}
1185 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001186 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001187 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001188
1189 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001190 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001191 *
1192 * {@hide}
1193 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001194 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001195
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001196 public static final int NETWORK_MOBILE_RX_DATA = 0;
1197 public static final int NETWORK_MOBILE_TX_DATA = 1;
1198 public static final int NETWORK_WIFI_RX_DATA = 2;
1199 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001200
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001201 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001202
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001203 public abstract long getNetworkActivityBytes(int type, int which);
1204 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001205
The Android Open Source Project10592532009-03-18 17:39:46 -07001206 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001207 * Return the wall clock time when battery stats data collection started.
1208 */
1209 public abstract long getStartClockTime();
1210
1211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 * Return whether we are currently running on battery.
1213 */
1214 public abstract boolean getIsOnBattery();
1215
1216 /**
1217 * Returns a SparseArray containing the statistics for each uid.
1218 */
1219 public abstract SparseArray<? extends Uid> getUidStats();
1220
1221 /**
1222 * Returns the current battery uptime in microseconds.
1223 *
1224 * @param curTime the amount of elapsed realtime in microseconds.
1225 */
1226 public abstract long getBatteryUptime(long curTime);
1227
1228 /**
1229 * Returns the current battery realtime in microseconds.
1230 *
1231 * @param curTime the amount of elapsed realtime in microseconds.
1232 */
1233 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001234
1235 /**
Evan Millar633a1742009-04-02 16:36:33 -07001236 * Returns the battery percentage level at the last time the device was unplugged from power, or
1237 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001238 */
Evan Millar633a1742009-04-02 16:36:33 -07001239 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001240
1241 /**
Evan Millar633a1742009-04-02 16:36:33 -07001242 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1243 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001244 */
Evan Millar633a1742009-04-02 16:36:33 -07001245 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246
1247 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001248 * Get the amount the battery has discharged since the stats were
1249 * last reset after charging, as a lower-end approximation.
1250 */
1251 public abstract int getLowDischargeAmountSinceCharge();
1252
1253 /**
1254 * Get the amount the battery has discharged since the stats were
1255 * last reset after charging, as an upper-end approximation.
1256 */
1257 public abstract int getHighDischargeAmountSinceCharge();
1258
1259 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001260 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1261 */
1262 public abstract int getDischargeAmount(int which);
1263
1264 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001265 * Get the amount the battery has discharged while the screen was on,
1266 * since the last time power was unplugged.
1267 */
1268 public abstract int getDischargeAmountScreenOn();
1269
1270 /**
1271 * Get the amount the battery has discharged while the screen was on,
1272 * since the last time the device was charged.
1273 */
1274 public abstract int getDischargeAmountScreenOnSinceCharge();
1275
1276 /**
1277 * Get the amount the battery has discharged while the screen was off,
1278 * since the last time power was unplugged.
1279 */
1280 public abstract int getDischargeAmountScreenOff();
1281
1282 /**
1283 * Get the amount the battery has discharged while the screen was off,
1284 * since the last time the device was charged.
1285 */
1286 public abstract int getDischargeAmountScreenOffSinceCharge();
1287
1288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 * Returns the total, last, or current battery uptime in microseconds.
1290 *
1291 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001292 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 */
1294 public abstract long computeBatteryUptime(long curTime, int which);
1295
1296 /**
1297 * Returns the total, last, or current battery realtime in microseconds.
1298 *
1299 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001300 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 */
1302 public abstract long computeBatteryRealtime(long curTime, int which);
1303
1304 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001305 * Returns the total, last, or current battery screen off uptime in microseconds.
1306 *
1307 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001308 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001309 */
1310 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1311
1312 /**
1313 * Returns the total, last, or current battery screen off realtime in microseconds.
1314 *
1315 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001316 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001317 */
1318 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1319
1320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 * Returns the total, last, or current uptime in microseconds.
1322 *
1323 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001324 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 */
1326 public abstract long computeUptime(long curTime, int which);
1327
1328 /**
1329 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001332 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 */
1334 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001335
1336 /**
1337 * Compute an approximation for how much run time (in microseconds) is remaining on
1338 * the battery. Returns -1 if no time can be computed: either there is not
1339 * enough current data to make a decision, or the battery is currently
1340 * charging.
1341 *
1342 * @param curTime The current elepsed realtime in microseconds.
1343 */
1344 public abstract long computeBatteryTimeRemaining(long curTime);
1345
1346 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001347 * Return the historical number of discharge steps we currently have.
1348 */
1349 public abstract int getNumDischargeStepDurations();
1350
1351 /**
1352 * Return the array of discharge step durations; the number of valid
1353 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1354 * These values are in milliseconds.
1355 */
1356 public abstract long[] getDischargeStepDurationsArray();
1357
1358 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001359 * Compute an approximation for how much time (in microseconds) remains until the battery
1360 * is fully charged. Returns -1 if no time can be computed: either there is not
1361 * enough current data to make a decision, or the battery is currently
1362 * discharging.
1363 *
1364 * @param curTime The current elepsed realtime in microseconds.
1365 */
1366 public abstract long computeChargeTimeRemaining(long curTime);
1367
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001368 /**
1369 * Return the historical number of charge steps we currently have.
1370 */
1371 public abstract int getNumChargeStepDurations();
1372
1373 /**
1374 * Return the array of charge step durations; the number of valid
1375 * items in it is returned by {@link #getNumChargeStepDurations()}.
1376 * These values are in milliseconds.
1377 */
1378 public abstract long[] getChargeStepDurationsArray();
1379
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001380 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1381
Evan Millarc64edde2009-04-18 12:26:32 -07001382 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383
Amith Yamasanie43530a2009-08-21 13:11:37 -07001384 /** Returns the number of different speeds that the CPU can run at */
1385 public abstract int getCpuSpeedSteps();
1386
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001387 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1388
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001389 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 long days = seconds / (60 * 60 * 24);
1391 if (days != 0) {
1392 out.append(days);
1393 out.append("d ");
1394 }
1395 long used = days * 60 * 60 * 24;
1396
1397 long hours = (seconds - used) / (60 * 60);
1398 if (hours != 0 || used != 0) {
1399 out.append(hours);
1400 out.append("h ");
1401 }
1402 used += hours * 60 * 60;
1403
1404 long mins = (seconds-used) / 60;
1405 if (mins != 0 || used != 0) {
1406 out.append(mins);
1407 out.append("m ");
1408 }
1409 used += mins * 60;
1410
1411 if (seconds != 0 || used != 0) {
1412 out.append(seconds-used);
1413 out.append("s ");
1414 }
1415 }
1416
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001417 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001419 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 sb.append((time - (sec * 100)) * 10);
1421 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001424 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001426 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 sb.append(time - (sec * 1000));
1428 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001431 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001432 long sec = time / 1000;
1433 formatTimeRaw(sb, sec);
1434 sb.append(time - (sec * 1000));
1435 sb.append("ms");
1436 }
1437
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001438 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001440 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 float perc = ((float)num) / ((float)den) * 100;
1443 mFormatBuilder.setLength(0);
1444 mFormatter.format("%.1f%%", perc);
1445 return mFormatBuilder.toString();
1446 }
1447
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001448 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001449 mFormatBuilder.setLength(0);
1450
1451 if (bytes < BYTES_PER_KB) {
1452 return bytes + "B";
1453 } else if (bytes < BYTES_PER_MB) {
1454 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1455 return mFormatBuilder.toString();
1456 } else if (bytes < BYTES_PER_GB){
1457 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1458 return mFormatBuilder.toString();
1459 } else {
1460 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1461 return mFormatBuilder.toString();
1462 }
1463 }
1464
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001465 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001466 if (timer != null) {
1467 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001468 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001469 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1470 return totalTimeMillis;
1471 }
1472 return 0;
1473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 /**
1476 *
1477 * @param sb a StringBuilder object.
1478 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001479 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001481 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 * @param linePrefix a String to be prepended to each line of output.
1483 * @return the line prefix
1484 */
1485 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001486 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487
1488 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001489 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490
Evan Millarc64edde2009-04-18 12:26:32 -07001491 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (totalTimeMillis != 0) {
1493 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001494 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001495 if (name != null) {
1496 sb.append(name);
1497 sb.append(' ');
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 sb.append('(');
1500 sb.append(count);
1501 sb.append(" times)");
1502 return ", ";
1503 }
1504 }
1505 return linePrefix;
1506 }
1507
1508 /**
1509 * Checkin version of wakelock printer. Prints simple comma-separated list.
1510 *
1511 * @param sb a StringBuilder object.
1512 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001513 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001515 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * @param linePrefix a String to be prepended to each line of output.
1517 * @return the line prefix
1518 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001519 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1520 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 long totalTimeMicros = 0;
1522 int count = 0;
1523 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001524 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001525 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 }
1527 sb.append(linePrefix);
1528 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1529 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001530 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 sb.append(count);
1532 return ",";
1533 }
1534
1535 /**
1536 * Dump a comma-separated line of values for terse checkin mode.
1537 *
1538 * @param pw the PageWriter to dump log to
1539 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1540 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1541 * @param args type-dependent data arguments
1542 */
1543 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1544 Object... args ) {
1545 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1546 pw.print(uid); pw.print(',');
1547 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001548 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549
1550 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001551 pw.print(',');
1552 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001554 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
1557 /**
1558 * Checkin server version of dump to produce more compact, computer-readable log.
1559 *
1560 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001562 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1564 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1565 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1567 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001568 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1569 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1570 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 final long totalRealtime = computeRealtime(rawRealtime, which);
1572 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001573 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1574 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1575 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1576 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1577 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
1579 StringBuilder sb = new StringBuilder(128);
1580
Evan Millar22ac0432009-03-31 11:33:18 -07001581 SparseArray<? extends Uid> uidStats = getUidStats();
1582 final int NU = uidStats.size();
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 // Dump "battery" stat
1587 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001588 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001589 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001590 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001591 getStartClockTime(),
1592 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001594 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001595 long fullWakeLockTimeTotal = 0;
1596 long partialWakeLockTimeTotal = 0;
1597
1598 for (int iu = 0; iu < NU; iu++) {
1599 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001600
Evan Millar22ac0432009-03-31 11:33:18 -07001601 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1602 if (wakelocks.size() > 0) {
1603 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1604 : wakelocks.entrySet()) {
1605 Uid.Wakelock wl = ent.getValue();
1606
1607 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1608 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001609 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1610 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001611 }
1612
1613 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1614 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001615 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001616 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001617 }
1618 }
1619 }
1620 }
1621
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001622 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1623 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1624 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1625 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1626 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1627 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1628 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1629 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1630
1631 // Dump network stats
1632 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1633 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1634 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 // Dump misc stats
1637 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001638 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001639 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001640 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001641 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001642 getInputEventCount(which), getMobileRadioActiveTime(rawRealtime, which),
1643 getMobileRadioActiveAdjustedTime(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001644
1645 // Dump screen brightness stats
1646 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1647 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001648 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001649 }
1650 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001651
Dianne Hackborn627bba72009-03-24 22:32:56 -07001652 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001653 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1654 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001655 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001656 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001657 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001658 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001659 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001660 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001661 args[i] = getPhoneSignalStrengthCount(i, which);
1662 }
1663 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001664
Dianne Hackborn627bba72009-03-24 22:32:56 -07001665 // Dump network type stats
1666 args = new Object[NUM_DATA_CONNECTION_TYPES];
1667 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001668 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001669 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001670 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1671 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1672 args[i] = getPhoneDataConnectionCount(i, which);
1673 }
1674 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001675
1676 // Dump wifi state stats
1677 args = new Object[NUM_WIFI_STATES];
1678 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001679 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001680 }
1681 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1682 for (int i=0; i<NUM_WIFI_STATES; i++) {
1683 args[i] = getWifiStateCount(i, which);
1684 }
1685 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1686
1687 // Dump bluetooth state stats
1688 args = new Object[NUM_BLUETOOTH_STATES];
1689 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001690 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001691 }
1692 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1693 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1694 args[i] = getBluetoothStateCount(i, which);
1695 }
1696 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1697
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001698 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001699 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001700 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001703 if (which == STATS_SINCE_UNPLUGGED) {
1704 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1705 getDischargeStartLevel()-getDischargeCurrentLevel(),
1706 getDischargeStartLevel()-getDischargeCurrentLevel(),
1707 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1708 } else {
1709 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1710 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1711 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1712 }
1713
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001714 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001715 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001716 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001717 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001718 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001719 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001720 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001721 sb.toString());
1722 }
Evan Millarc64edde2009-04-18 12:26:32 -07001723 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001724 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1725 if (wakeupReasons.size() > 0) {
1726 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1727 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1728 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1729 }
1730 }
Evan Millarc64edde2009-04-18 12:26:32 -07001731 }
1732
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001733 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001734 helper.create(this);
1735 helper.refreshStats(which, UserHandle.USER_ALL);
1736 List<BatterySipper> sippers = helper.getUsageList();
1737 if (sippers != null && sippers.size() > 0) {
1738 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1739 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001740 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001741 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1742 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1743 for (int i=0; i<sippers.size(); i++) {
1744 BatterySipper bs = sippers.get(i);
1745 int uid = 0;
1746 String label;
1747 switch (bs.drainType) {
1748 case IDLE:
1749 label="idle";
1750 break;
1751 case CELL:
1752 label="cell";
1753 break;
1754 case PHONE:
1755 label="phone";
1756 break;
1757 case WIFI:
1758 label="wifi";
1759 break;
1760 case BLUETOOTH:
1761 label="blue";
1762 break;
1763 case SCREEN:
1764 label="scrn";
1765 break;
1766 case APP:
1767 uid = bs.uidObj.getUid();
1768 label = "uid";
1769 break;
1770 case USER:
1771 uid = UserHandle.getUid(bs.userId, 0);
1772 label = "user";
1773 break;
1774 case UNACCOUNTED:
1775 label = "unacc";
1776 break;
1777 case OVERCOUNTED:
1778 label = "over";
1779 break;
1780 default:
1781 label = "???";
1782 }
1783 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1784 BatteryStatsHelper.makemAh(bs.value));
1785 }
1786 }
1787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 for (int iu = 0; iu < NU; iu++) {
1789 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001790 if (reqUid >= 0 && uid != reqUid) {
1791 continue;
1792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 Uid u = uidStats.valueAt(iu);
1794 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001795 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1796 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1797 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1798 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1799 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1800 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001801 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1802 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1804 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001805 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1806 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1807 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001808
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001809 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1810 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001811 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001812 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1813 wifiBytesRx, wifiBytesTx,
1814 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001815 wifiPacketsRx, wifiPacketsTx,
1816 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001817 }
1818
Nick Pelly6ccaa542012-06-15 15:22:47 -07001819 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001820 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001821 dumpLine(pw, uid, category, WIFI_DATA,
1822 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824
Dianne Hackborn617f8772009-03-31 15:04:46 -07001825 if (u.hasUserActivity()) {
1826 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1827 boolean hasData = false;
1828 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1829 int val = u.getUserActivityCount(i, which);
1830 args[i] = val;
1831 if (val != 0) hasData = true;
1832 }
1833 if (hasData) {
1834 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1835 }
1836 }
1837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1839 if (wakelocks.size() > 0) {
1840 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1841 : wakelocks.entrySet()) {
1842 Uid.Wakelock wl = ent.getValue();
1843 String linePrefix = "";
1844 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001845 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001846 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001847 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001848 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001849 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001850 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
1852 // Only log if we had at lease one wakelock...
1853 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001854 String name = ent.getKey();
1855 if (name.indexOf(',') >= 0) {
1856 name = name.replace(',', '_');
1857 }
1858 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
1860 }
1861 }
1862
1863 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1864 if (sensors.size() > 0) {
1865 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1866 : sensors.entrySet()) {
1867 Uid.Sensor se = ent.getValue();
1868 int sensorNumber = ent.getKey();
1869 Timer timer = se.getSensorTime();
1870 if (timer != null) {
1871 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001872 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001873 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if (totalTime != 0) {
1875 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1876 }
1877 }
1878 }
1879 }
1880
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001881 Timer vibTimer = u.getVibratorOnTimer();
1882 if (vibTimer != null) {
1883 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001884 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001885 int count = vibTimer.getCountLocked(which);
1886 if (totalTime != 0) {
1887 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1888 }
1889 }
1890
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001891 Timer fgTimer = u.getForegroundActivityTimer();
1892 if (fgTimer != null) {
1893 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001894 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001895 int count = fgTimer.getCountLocked(which);
1896 if (totalTime != 0) {
1897 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1898 }
1899 }
1900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1902 if (processStats.size() > 0) {
1903 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1904 : processStats.entrySet()) {
1905 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001906
1907 final long userMillis = ps.getUserTime(which) * 10;
1908 final long systemMillis = ps.getSystemTime(which) * 10;
1909 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1910 final long starts = ps.getStarts(which);
1911
1912 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1913 || starts != 0) {
1914 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1915 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917 }
1918 }
1919
1920 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1921 if (packageStats.size() > 0) {
1922 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1923 : packageStats.entrySet()) {
1924
1925 Uid.Pkg ps = ent.getValue();
1926 int wakeups = ps.getWakeups(which);
1927 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1928 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1929 : serviceStats.entrySet()) {
1930 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1931 long startTime = ss.getStartTime(batteryUptime, which);
1932 int starts = ss.getStarts(which);
1933 int launches = ss.getLaunches(which);
1934 if (startTime != 0 || starts != 0 || launches != 0) {
1935 dumpLine(pw, uid, category, APK_DATA,
1936 wakeups, // wakeup alarms
1937 ent.getKey(), // Apk
1938 sent.getKey(), // service
1939 startTime / 1000, // time spent started, in ms
1940 starts,
1941 launches);
1942 }
1943 }
1944 }
1945 }
1946 }
1947 }
1948
Dianne Hackborn81038902012-11-26 17:04:09 -08001949 static final class TimerEntry {
1950 final String mName;
1951 final int mId;
1952 final BatteryStats.Timer mTimer;
1953 final long mTime;
1954 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1955 mName = name;
1956 mId = id;
1957 mTimer = timer;
1958 mTime = time;
1959 }
1960 }
1961
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001962 private void printmAh(PrintWriter printer, double power) {
1963 printer.print(BatteryStatsHelper.makemAh(power));
1964 }
1965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001967 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1968 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1970 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1971 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972
1973 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1974 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1975 final long totalRealtime = computeRealtime(rawRealtime, which);
1976 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001977 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1978 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1979 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001980 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
1981 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001984
1985 SparseArray<? extends Uid> uidStats = getUidStats();
1986 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001988 sb.setLength(0);
1989 sb.append(prefix);
1990 sb.append(" Time on battery: ");
1991 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1992 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1993 sb.append(") realtime, ");
1994 formatTimeMs(sb, whichBatteryUptime / 1000);
1995 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1996 sb.append(") uptime");
1997 pw.println(sb.toString());
1998 sb.setLength(0);
1999 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002000 sb.append(" Time on battery screen off: ");
2001 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2002 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2003 sb.append(") realtime, ");
2004 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2005 sb.append("(");
2006 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2007 sb.append(") uptime");
2008 pw.println(sb.toString());
2009 sb.setLength(0);
2010 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002011 sb.append(" Total run time: ");
2012 formatTimeMs(sb, totalRealtime / 1000);
2013 sb.append("realtime, ");
2014 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002015 sb.append("uptime");
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002016 if (batteryTimeRemaining >= 0) {
2017 sb.setLength(0);
2018 sb.append(prefix);
2019 sb.append(" Battery time remaining: ");
2020 formatTimeMs(sb, batteryTimeRemaining / 1000);
2021 pw.println(sb.toString());
2022 }
2023 if (chargeTimeRemaining >= 0) {
2024 sb.setLength(0);
2025 sb.append(prefix);
2026 sb.append(" Charge time remaining: ");
2027 formatTimeMs(sb, chargeTimeRemaining / 1000);
2028 pw.println(sb.toString());
2029 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002030 pw.print(" Start clock time: ");
2031 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2032
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002033 final long screenOnTime = getScreenOnTime(rawRealtime, which);
2034 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2035 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2036 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2037 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002038 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002039 sb.append(prefix);
2040 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2041 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002042 sb.append(") "); sb.append(getScreenOnCount(which));
2043 sb.append("x, Input events: "); sb.append(getInputEventCount(which));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002044 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002045 if (phoneOnTime != 0) {
2046 sb.setLength(0);
2047 sb.append(prefix);
2048 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2049 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2050 sb.append(") "); sb.append(getPhoneOnCount(which));
2051 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002052 sb.setLength(0);
2053 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002054 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002055 boolean didOne = false;
2056 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002057 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002058 if (time == 0) {
2059 continue;
2060 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002061 sb.append("\n ");
2062 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002063 didOne = true;
2064 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2065 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002066 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002067 sb.append("(");
2068 sb.append(formatRatioLocked(time, screenOnTime));
2069 sb.append(")");
2070 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002071 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002072 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002073
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002074 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002075 long fullWakeLockTimeTotalMicros = 0;
2076 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002077
Dianne Hackborn81038902012-11-26 17:04:09 -08002078 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2079
Evan Millar22ac0432009-03-31 11:33:18 -07002080 for (int iu = 0; iu < NU; iu++) {
2081 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002082
Evan Millar22ac0432009-03-31 11:33:18 -07002083 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2084 if (wakelocks.size() > 0) {
2085 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2086 : wakelocks.entrySet()) {
2087 Uid.Wakelock wl = ent.getValue();
2088
2089 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2090 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002091 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002092 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002093 }
2094
2095 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2096 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002097 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002098 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002099 if (totalTimeMicros > 0) {
2100 if (reqUid < 0) {
2101 // Only show the ordered list of all wake
2102 // locks if the caller is not asking for data
2103 // about a specific uid.
2104 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2105 partialWakeTimer, totalTimeMicros));
2106 }
2107 partialWakeLockTimeTotalMicros += totalTimeMicros;
2108 }
Evan Millar22ac0432009-03-31 11:33:18 -07002109 }
2110 }
2111 }
2112 }
2113
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002114 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2115 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2116 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2117 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2118 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2119 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2120 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2121 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2122
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002123 if (fullWakeLockTimeTotalMicros != 0) {
2124 sb.setLength(0);
2125 sb.append(prefix);
2126 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2127 (fullWakeLockTimeTotalMicros + 500) / 1000);
2128 pw.println(sb.toString());
2129 }
2130
2131 if (partialWakeLockTimeTotalMicros != 0) {
2132 sb.setLength(0);
2133 sb.append(prefix);
2134 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2135 (partialWakeLockTimeTotalMicros + 500) / 1000);
2136 pw.println(sb.toString());
2137 }
2138
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002139 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002140 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2141 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2142 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2143 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002144 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002145 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002146 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002147 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002148 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002149 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002150 if (time == 0) {
2151 continue;
2152 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002153 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002154 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002155 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002156 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002157 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002158 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002159 sb.append("(");
2160 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002161 sb.append(") ");
2162 sb.append(getPhoneSignalStrengthCount(i, which));
2163 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002164 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002165 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002166 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002167
2168 sb.setLength(0);
2169 sb.append(prefix);
2170 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002171 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002172 pw.println(sb.toString());
2173
Dianne Hackborn627bba72009-03-24 22:32:56 -07002174 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002175 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002176 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002177 didOne = false;
2178 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002179 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002180 if (time == 0) {
2181 continue;
2182 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002183 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002184 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002185 didOne = true;
2186 sb.append(DATA_CONNECTION_NAMES[i]);
2187 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002188 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002189 sb.append("(");
2190 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002191 sb.append(") ");
2192 sb.append(getPhoneDataConnectionCount(i, which));
2193 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002194 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002195 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002196 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002197
2198 sb.setLength(0);
2199 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002200 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002201 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002202 formatTimeMs(sb, mobileActiveTime / 1000);
2203 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2204 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2205 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002206 pw.println(sb.toString());
2207
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002208 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2209 if (mobileActiveUnknownTime != 0) {
2210 sb.setLength(0);
2211 sb.append(prefix);
2212 sb.append(" Mobile radio active unknown time: ");
2213 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2214 sb.append("(");
2215 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2216 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2217 sb.append("x");
2218 pw.println(sb.toString());
2219 }
2220
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002221 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2222 if (mobileActiveAdjustedTime != 0) {
2223 sb.setLength(0);
2224 sb.append(prefix);
2225 sb.append(" Mobile radio active adjusted time: ");
2226 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2227 sb.append("(");
2228 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2229 sb.append(")");
2230 pw.println(sb.toString());
2231 }
2232
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002233 pw.print(prefix);
2234 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2235 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2236 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2237 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002238 sb.setLength(0);
2239 sb.append(prefix);
2240 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2241 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2242 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2243 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002244 sb.append(")");
2245 pw.println(sb.toString());
2246
2247 sb.setLength(0);
2248 sb.append(prefix);
2249 sb.append(" Wifi states:");
2250 didOne = false;
2251 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002252 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002253 if (time == 0) {
2254 continue;
2255 }
2256 sb.append("\n ");
2257 didOne = true;
2258 sb.append(WIFI_STATE_NAMES[i]);
2259 sb.append(" ");
2260 formatTimeMs(sb, time/1000);
2261 sb.append("(");
2262 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2263 sb.append(") ");
2264 sb.append(getPhoneDataConnectionCount(i, which));
2265 sb.append("x");
2266 }
2267 if (!didOne) sb.append(" (no activity)");
2268 pw.println(sb.toString());
2269
2270 sb.setLength(0);
2271 sb.append(prefix);
2272 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002273 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2274 sb.append(")");
2275 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002276
2277 sb.setLength(0);
2278 sb.append(prefix);
2279 sb.append(" Bluetooth states:");
2280 didOne = false;
2281 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002282 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002283 if (time == 0) {
2284 continue;
2285 }
2286 sb.append("\n ");
2287 didOne = true;
2288 sb.append(BLUETOOTH_STATE_NAMES[i]);
2289 sb.append(" ");
2290 formatTimeMs(sb, time/1000);
2291 sb.append("(");
2292 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2293 sb.append(") ");
2294 sb.append(getPhoneDataConnectionCount(i, which));
2295 sb.append("x");
2296 }
2297 if (!didOne) sb.append(" (no activity)");
2298 pw.println(sb.toString());
2299
2300 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002301
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002302 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002303 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002304 pw.print(prefix); pw.println(" Device is currently unplugged");
2305 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2306 pw.println(getDischargeStartLevel());
2307 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2308 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002309 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002310 pw.print(prefix); pw.println(" Device is currently plugged into power");
2311 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2312 pw.println(getDischargeStartLevel());
2313 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2314 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002315 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002316 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2317 pw.println(getDischargeAmountScreenOn());
2318 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2319 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002320 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002321 } else {
2322 pw.print(prefix); pw.println(" Device battery use since last full charge");
2323 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2324 pw.println(getLowDischargeAmountSinceCharge());
2325 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2326 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002327 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2328 pw.println(getDischargeAmountScreenOnSinceCharge());
2329 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2330 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002331 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002332 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002333
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002334 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002335 helper.create(this);
2336 helper.refreshStats(which, UserHandle.USER_ALL);
2337 List<BatterySipper> sippers = helper.getUsageList();
2338 if (sippers != null && sippers.size() > 0) {
2339 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2340 pw.print(prefix); pw.print(" Capacity: ");
2341 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002342 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002343 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2344 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2345 pw.println();
2346 for (int i=0; i<sippers.size(); i++) {
2347 BatterySipper bs = sippers.get(i);
2348 switch (bs.drainType) {
2349 case IDLE:
2350 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2351 pw.println();
2352 break;
2353 case CELL:
2354 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2355 pw.println();
2356 break;
2357 case PHONE:
2358 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2359 pw.println();
2360 break;
2361 case WIFI:
2362 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2363 pw.println();
2364 break;
2365 case BLUETOOTH:
2366 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2367 pw.println();
2368 break;
2369 case SCREEN:
2370 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2371 pw.println();
2372 break;
2373 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002374 pw.print(prefix); pw.print(" Uid ");
2375 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002376 pw.print(": "); printmAh(pw, bs.value); pw.println();
2377 break;
2378 case USER:
2379 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2380 pw.print(": "); printmAh(pw, bs.value); pw.println();
2381 break;
2382 case UNACCOUNTED:
2383 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2384 pw.println();
2385 break;
2386 case OVERCOUNTED:
2387 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2388 pw.println();
2389 break;
2390 }
2391 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002392 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002393 }
2394
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002395 sippers = helper.getMobilemsppList();
2396 if (sippers != null && sippers.size() > 0) {
2397 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002398 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002399 for (int i=0; i<sippers.size(); i++) {
2400 BatterySipper bs = sippers.get(i);
2401 sb.setLength(0);
2402 sb.append(prefix); sb.append(" Uid ");
2403 UserHandle.formatUid(sb, bs.uidObj.getUid());
2404 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2405 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2406 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002407 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002408 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002409 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002410 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002411 sb.setLength(0);
2412 sb.append(prefix);
2413 sb.append(" TOTAL TIME: ");
2414 formatTimeMs(sb, totalTime);
2415 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2416 sb.append(")");
2417 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002418 pw.println();
2419 }
2420
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002421 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2422 @Override
2423 public int compare(TimerEntry lhs, TimerEntry rhs) {
2424 long lhsTime = lhs.mTime;
2425 long rhsTime = rhs.mTime;
2426 if (lhsTime < rhsTime) {
2427 return 1;
2428 }
2429 if (lhsTime > rhsTime) {
2430 return -1;
2431 }
2432 return 0;
2433 }
2434 };
2435
2436 if (reqUid < 0) {
2437 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2438 if (kernelWakelocks.size() > 0) {
2439 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2440 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2441 BatteryStats.Timer timer = ent.getValue();
2442 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2443 if (totalTimeMillis > 0) {
2444 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2445 }
2446 }
2447 if (ktimers.size() > 0) {
2448 Collections.sort(ktimers, timerComparator);
2449 pw.print(prefix); pw.println(" All kernel wake locks:");
2450 for (int i=0; i<ktimers.size(); i++) {
2451 TimerEntry timer = ktimers.get(i);
2452 String linePrefix = ": ";
2453 sb.setLength(0);
2454 sb.append(prefix);
2455 sb.append(" Kernel Wake lock ");
2456 sb.append(timer.mName);
2457 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2458 which, linePrefix);
2459 if (!linePrefix.equals(": ")) {
2460 sb.append(" realtime");
2461 // Only print out wake locks that were held
2462 pw.println(sb.toString());
2463 }
2464 }
2465 pw.println();
2466 }
2467 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002468
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002469 if (timers.size() > 0) {
2470 Collections.sort(timers, timerComparator);
2471 pw.print(prefix); pw.println(" All partial wake locks:");
2472 for (int i=0; i<timers.size(); i++) {
2473 TimerEntry timer = timers.get(i);
2474 sb.setLength(0);
2475 sb.append(" Wake lock ");
2476 UserHandle.formatUid(sb, timer.mId);
2477 sb.append(" ");
2478 sb.append(timer.mName);
2479 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2480 sb.append(" realtime");
2481 pw.println(sb.toString());
2482 }
2483 timers.clear();
2484 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002485 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002486
2487 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2488 if (wakeupReasons.size() > 0) {
2489 pw.print(prefix); pw.println(" All wakeup reasons:");
2490 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2491 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2492 BatteryStats.LongCounter counter = ent.getValue();
2493 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2494 ent.getValue().getCountLocked(which)));
2495 }
2496 Collections.sort(reasons, timerComparator);
2497 for (int i=0; i<reasons.size(); i++) {
2498 TimerEntry timer = reasons.get(i);
2499 String linePrefix = ": ";
2500 sb.setLength(0);
2501 sb.append(prefix);
2502 sb.append(" Wakeup reason ");
2503 sb.append(timer.mName);
2504 sb.append(": ");
2505 formatTimeMs(sb, timer.mTime);
2506 sb.append("realtime");
2507 pw.println(sb.toString());
2508 }
2509 pw.println();
2510 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002511 }
Evan Millar22ac0432009-03-31 11:33:18 -07002512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 for (int iu=0; iu<NU; iu++) {
2514 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002515 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002516 continue;
2517 }
2518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002520
2521 pw.print(prefix);
2522 pw.print(" ");
2523 UserHandle.formatUid(pw, uid);
2524 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002526
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002527 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2528 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2529 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2530 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2531 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2532 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002533 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2534 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002535 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2536 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002537 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2538 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2539 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002540
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002541 if (mobileRxBytes > 0 || mobileTxBytes > 0
2542 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002543 pw.print(prefix); pw.print(" Mobile network: ");
2544 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002545 pw.print(formatBytesLocked(mobileTxBytes));
2546 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2547 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002549 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2550 sb.setLength(0);
2551 sb.append(prefix); sb.append(" Mobile radio active: ");
2552 formatTimeMs(sb, uidMobileActiveTime / 1000);
2553 sb.append("(");
2554 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2555 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2556 long packets = mobileRxPackets + mobileTxPackets;
2557 if (packets == 0) {
2558 packets = 1;
2559 }
2560 sb.append(" @ ");
2561 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2562 sb.append(" mspp");
2563 pw.println(sb.toString());
2564 }
2565
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002566 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002567 pw.print(prefix); pw.print(" Wi-Fi network: ");
2568 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002569 pw.print(formatBytesLocked(wifiTxBytes));
2570 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2571 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002572 }
2573
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002574 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2575 || uidWifiRunningTime != 0) {
2576 sb.setLength(0);
2577 sb.append(prefix); sb.append(" Wifi Running: ");
2578 formatTimeMs(sb, uidWifiRunningTime / 1000);
2579 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2580 whichBatteryRealtime)); sb.append(")\n");
2581 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2582 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2583 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2584 whichBatteryRealtime)); sb.append(")\n");
2585 sb.append(prefix); sb.append(" Wifi Scan: ");
2586 formatTimeMs(sb, wifiScanTime / 1000);
2587 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2588 whichBatteryRealtime)); sb.append(")");
2589 pw.println(sb.toString());
2590 }
2591
Dianne Hackborn617f8772009-03-31 15:04:46 -07002592 if (u.hasUserActivity()) {
2593 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002594 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002595 int val = u.getUserActivityCount(i, which);
2596 if (val != 0) {
2597 if (!hasData) {
2598 sb.setLength(0);
2599 sb.append(" User activity: ");
2600 hasData = true;
2601 } else {
2602 sb.append(", ");
2603 }
2604 sb.append(val);
2605 sb.append(" ");
2606 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2607 }
2608 }
2609 if (hasData) {
2610 pw.println(sb.toString());
2611 }
2612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613
2614 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2615 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002616 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2617 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2619 : wakelocks.entrySet()) {
2620 Uid.Wakelock wl = ent.getValue();
2621 String linePrefix = ": ";
2622 sb.setLength(0);
2623 sb.append(prefix);
2624 sb.append(" Wake lock ");
2625 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002626 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002628 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002630 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 "window", which, linePrefix);
2632 if (!linePrefix.equals(": ")) {
2633 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002634 // Only print out wake locks that were held
2635 pw.println(sb.toString());
2636 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002637 count++;
2638 }
2639 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002640 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002641 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002643 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002644 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002645 }
2646 if (count > 1) {
2647 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2648 sb.setLength(0);
2649 sb.append(prefix);
2650 sb.append(" TOTAL wake: ");
2651 boolean needComma = false;
2652 if (totalFull != 0) {
2653 needComma = true;
2654 formatTimeMs(sb, totalFull);
2655 sb.append("full");
2656 }
2657 if (totalPartial != 0) {
2658 if (needComma) {
2659 sb.append(", ");
2660 }
2661 needComma = true;
2662 formatTimeMs(sb, totalPartial);
2663 sb.append("partial");
2664 }
2665 if (totalWindow != 0) {
2666 if (needComma) {
2667 sb.append(", ");
2668 }
2669 needComma = true;
2670 formatTimeMs(sb, totalWindow);
2671 sb.append("window");
2672 }
2673 sb.append(" realtime");
2674 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
2677 }
2678
2679 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2680 if (sensors.size() > 0) {
2681 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2682 : sensors.entrySet()) {
2683 Uid.Sensor se = ent.getValue();
2684 int sensorNumber = ent.getKey();
2685 sb.setLength(0);
2686 sb.append(prefix);
2687 sb.append(" Sensor ");
2688 int handle = se.getHandle();
2689 if (handle == Uid.Sensor.GPS) {
2690 sb.append("GPS");
2691 } else {
2692 sb.append(handle);
2693 }
2694 sb.append(": ");
2695
2696 Timer timer = se.getSensorTime();
2697 if (timer != null) {
2698 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002699 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002700 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002701 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 //timer.logState();
2703 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002704 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 sb.append("realtime (");
2706 sb.append(count);
2707 sb.append(" times)");
2708 } else {
2709 sb.append("(not used)");
2710 }
2711 } else {
2712 sb.append("(not used)");
2713 }
2714
2715 pw.println(sb.toString());
2716 uidActivity = true;
2717 }
2718 }
2719
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002720 Timer vibTimer = u.getVibratorOnTimer();
2721 if (vibTimer != null) {
2722 // Convert from microseconds to milliseconds with rounding
2723 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002724 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002725 int count = vibTimer.getCountLocked(which);
2726 //timer.logState();
2727 if (totalTime != 0) {
2728 sb.setLength(0);
2729 sb.append(prefix);
2730 sb.append(" Vibrator: ");
2731 formatTimeMs(sb, totalTime);
2732 sb.append("realtime (");
2733 sb.append(count);
2734 sb.append(" times)");
2735 pw.println(sb.toString());
2736 uidActivity = true;
2737 }
2738 }
2739
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002740 Timer fgTimer = u.getForegroundActivityTimer();
2741 if (fgTimer != null) {
2742 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002743 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002744 int count = fgTimer.getCountLocked(which);
2745 if (totalTime != 0) {
2746 sb.setLength(0);
2747 sb.append(prefix);
2748 sb.append(" Foreground activities: ");
2749 formatTimeMs(sb, totalTime);
2750 sb.append("realtime (");
2751 sb.append(count);
2752 sb.append(" times)");
2753 pw.println(sb.toString());
2754 uidActivity = true;
2755 }
2756 }
2757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2759 if (processStats.size() > 0) {
2760 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2761 : processStats.entrySet()) {
2762 Uid.Proc ps = ent.getValue();
2763 long userTime;
2764 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002765 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002767 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768
2769 userTime = ps.getUserTime(which);
2770 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002771 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002773 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002774 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002776 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002777 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002778 sb.setLength(0);
2779 sb.append(prefix); sb.append(" Proc ");
2780 sb.append(ent.getKey()); sb.append(":\n");
2781 sb.append(prefix); sb.append(" CPU: ");
2782 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002783 formatTime(sb, systemTime); sb.append("krn ; ");
2784 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002785 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002786 sb.append("\n"); sb.append(prefix); sb.append(" ");
2787 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002788 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002789 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002790 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002791 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002792 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002793 pw.print(prefix); pw.print(" * Killed for ");
2794 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2795 pw.print("wake lock");
2796 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2797 pw.print("cpu");
2798 } else {
2799 pw.print("unknown");
2800 }
2801 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002802 TimeUtils.formatDuration(ew.usedTime, pw);
2803 pw.print(" over ");
2804 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002805 if (ew.overTime != 0) {
2806 pw.print(" (");
2807 pw.print((ew.usedTime*100)/ew.overTime);
2808 pw.println("%)");
2809 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002810 }
2811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 uidActivity = true;
2813 }
2814 }
2815 }
2816
2817 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2818 if (packageStats.size() > 0) {
2819 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2820 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002821 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 boolean apkActivity = false;
2823 Uid.Pkg ps = ent.getValue();
2824 int wakeups = ps.getWakeups(which);
2825 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002826 pw.print(prefix); pw.print(" ");
2827 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 apkActivity = true;
2829 }
2830 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2831 if (serviceStats.size() > 0) {
2832 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2833 : serviceStats.entrySet()) {
2834 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2835 long startTime = ss.getStartTime(batteryUptime, which);
2836 int starts = ss.getStarts(which);
2837 int launches = ss.getLaunches(which);
2838 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002839 sb.setLength(0);
2840 sb.append(prefix); sb.append(" Service ");
2841 sb.append(sent.getKey()); sb.append(":\n");
2842 sb.append(prefix); sb.append(" Created for: ");
2843 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002844 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002845 sb.append(prefix); sb.append(" Starts: ");
2846 sb.append(starts);
2847 sb.append(", launches: "); sb.append(launches);
2848 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 apkActivity = true;
2850 }
2851 }
2852 }
2853 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002854 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
2856 uidActivity = true;
2857 }
2858 }
2859 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002860 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 }
2862 }
2863 }
2864
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002865 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002866 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002867 int diff = oldval ^ newval;
2868 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002869 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002870 for (int i=0; i<descriptions.length; i++) {
2871 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002872 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002873 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002874 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002875 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002876 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002877 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2878 didWake = true;
2879 pw.print("=");
2880 if (longNames) {
2881 UserHandle.formatUid(pw, wakelockTag.uid);
2882 pw.print(":\"");
2883 pw.print(wakelockTag.string);
2884 pw.print("\"");
2885 } else {
2886 pw.print(wakelockTag.poolIdx);
2887 }
2888 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002889 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002890 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002891 pw.print("=");
2892 int val = (newval&bd.mask)>>bd.shift;
2893 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002894 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002895 } else {
2896 pw.print(val);
2897 }
2898 }
2899 }
2900 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002901 if (!didWake && wakelockTag != null) {
2902 pw.print(longNames ? "wake_lock=" : "w=");
2903 if (longNames) {
2904 UserHandle.formatUid(pw, wakelockTag.uid);
2905 pw.print(":\"");
2906 pw.print(wakelockTag.string);
2907 pw.print("\"");
2908 } else {
2909 pw.print(wakelockTag.poolIdx);
2910 }
2911 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002912 }
2913
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002914 public void prepareForDumpLocked() {
2915 }
2916
2917 public static class HistoryPrinter {
2918 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002919 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002920 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002921 int oldStatus = -1;
2922 int oldHealth = -1;
2923 int oldPlug = -1;
2924 int oldTemp = -1;
2925 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002926 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002927 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002928
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002929 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002930 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002931 if (!checkin) {
2932 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002933 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002934 pw.print(" (");
2935 pw.print(rec.numReadInts);
2936 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002937 } else {
2938 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002939 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002940 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07002941 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002942 }
2943 lastTime = rec.time;
2944 }
2945 if (rec.cmd == HistoryItem.CMD_START) {
2946 if (checkin) {
2947 pw.print(":");
2948 }
2949 pw.println("START");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002950 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
2951 if (checkin) {
2952 pw.print(":");
2953 }
2954 pw.print("TIME:");
2955 if (checkin) {
2956 pw.println(rec.currentTime);
2957 } else {
2958 pw.print(" ");
2959 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
2960 rec.currentTime).toString());
2961 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002962 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2963 if (checkin) {
2964 pw.print(":");
2965 }
2966 pw.println("*OVERFLOW*");
2967 } else {
2968 if (!checkin) {
2969 if (rec.batteryLevel < 10) pw.print("00");
2970 else if (rec.batteryLevel < 100) pw.print("0");
2971 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002972 if (verbose) {
2973 pw.print(" ");
2974 if (rec.states < 0) ;
2975 else if (rec.states < 0x10) pw.print("0000000");
2976 else if (rec.states < 0x100) pw.print("000000");
2977 else if (rec.states < 0x1000) pw.print("00000");
2978 else if (rec.states < 0x10000) pw.print("0000");
2979 else if (rec.states < 0x100000) pw.print("000");
2980 else if (rec.states < 0x1000000) pw.print("00");
2981 else if (rec.states < 0x10000000) pw.print("0");
2982 pw.print(Integer.toHexString(rec.states));
2983 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002984 } else {
2985 if (oldLevel != rec.batteryLevel) {
2986 oldLevel = rec.batteryLevel;
2987 pw.print(",Bl="); pw.print(rec.batteryLevel);
2988 }
2989 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002990 if (oldStatus != rec.batteryStatus) {
2991 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002992 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002993 switch (oldStatus) {
2994 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002995 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002996 break;
2997 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002998 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002999 break;
3000 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003001 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003002 break;
3003 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003004 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003005 break;
3006 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003007 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003008 break;
3009 default:
3010 pw.print(oldStatus);
3011 break;
3012 }
3013 }
3014 if (oldHealth != rec.batteryHealth) {
3015 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003016 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003017 switch (oldHealth) {
3018 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003019 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003020 break;
3021 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003022 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003023 break;
3024 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003025 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003026 break;
3027 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003028 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003029 break;
3030 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003031 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003032 break;
3033 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003034 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003035 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003036 case BatteryManager.BATTERY_HEALTH_COLD:
3037 pw.print(checkin ? "c" : "cold");
3038 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003039 default:
3040 pw.print(oldHealth);
3041 break;
3042 }
3043 }
3044 if (oldPlug != rec.batteryPlugType) {
3045 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003046 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003047 switch (oldPlug) {
3048 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003049 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003050 break;
3051 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003052 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003053 break;
3054 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003055 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003056 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003057 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003058 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003059 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003060 default:
3061 pw.print(oldPlug);
3062 break;
3063 }
3064 }
3065 if (oldTemp != rec.batteryTemperature) {
3066 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003067 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003068 pw.print(oldTemp);
3069 }
3070 if (oldVolt != rec.batteryVoltage) {
3071 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003072 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003073 pw.print(oldVolt);
3074 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003075 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003076 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003077 printBitDescriptions(pw, oldState2, rec.states2, null,
3078 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003079 if (rec.wakeReasonTag != null) {
3080 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003081 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003082 pw.print(rec.wakeReasonTag.poolIdx);
3083 } else {
3084 pw.print(" wake_reason=");
3085 pw.print(rec.wakeReasonTag.uid);
3086 pw.print(":\"");
3087 pw.print(rec.wakeReasonTag.string);
3088 pw.print("\"");
3089 }
3090 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003091 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003092 pw.print(checkin ? "," : " ");
3093 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3094 pw.print("+");
3095 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3096 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003097 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003098 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3099 : HISTORY_EVENT_NAMES;
3100 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3101 | HistoryItem.EVENT_FLAG_FINISH);
3102 if (idx >= 0 && idx < eventNames.length) {
3103 pw.print(eventNames[idx]);
3104 } else {
3105 pw.print(checkin ? "Ev" : "event");
3106 pw.print(idx);
3107 }
3108 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003109 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003110 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003111 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003112 UserHandle.formatUid(pw, rec.eventTag.uid);
3113 pw.print(":\"");
3114 pw.print(rec.eventTag.string);
3115 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003116 }
3117 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003118 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003119 oldState = rec.states;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003120 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003121 }
3122 }
3123
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003124 private void printSizeValue(PrintWriter pw, long size) {
3125 float result = size;
3126 String suffix = "";
3127 if (result >= 10*1024) {
3128 suffix = "KB";
3129 result = result / 1024;
3130 }
3131 if (result >= 10*1024) {
3132 suffix = "MB";
3133 result = result / 1024;
3134 }
3135 if (result >= 10*1024) {
3136 suffix = "GB";
3137 result = result / 1024;
3138 }
3139 if (result >= 10*1024) {
3140 suffix = "TB";
3141 result = result / 1024;
3142 }
3143 if (result >= 10*1024) {
3144 suffix = "PB";
3145 result = result / 1024;
3146 }
3147 pw.print((int)result);
3148 pw.print(suffix);
3149 }
3150
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003151 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3152 int count, boolean checkin) {
3153 if (count <= 0) {
3154 return false;
3155 }
3156 if (!checkin) {
3157 pw.println(header);
3158 }
3159 String[] lineArgs = new String[1];
3160 for (int i=0; i<count; i++) {
3161 if (checkin) {
3162 lineArgs[0] = Long.toString(steps[i]);
3163 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3164 } else {
3165 pw.print(" #"); pw.print(i); pw.print(": ");
3166 TimeUtils.formatDuration(steps[i], pw);
3167 pw.println();
3168 }
3169 }
3170 return true;
3171 }
3172
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003173 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3174 public static final int DUMP_CHARGED_ONLY = 1<<1;
3175 public static final int DUMP_HISTORY_ONLY = 1<<2;
3176 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003177 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 /**
3180 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3181 *
3182 * @param pw a Printer to receive the dump output.
3183 */
3184 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003185 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003186 prepareForDumpLocked();
3187
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003188 final boolean filtering =
3189 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003190
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003191 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
3192 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3193
3194 final HistoryItem rec = new HistoryItem();
3195 final long historyTotalSize = getHistoryTotalSize();
3196 final long historyUsedSize = getHistoryUsedSize();
3197 if (startIteratingHistoryLocked()) {
3198 try {
3199 pw.print("Battery History (");
3200 pw.print((100*historyUsedSize)/historyTotalSize);
3201 pw.print("% used, ");
3202 printSizeValue(pw, historyUsedSize);
3203 pw.print(" used of ");
3204 printSizeValue(pw, historyTotalSize);
3205 pw.print(", ");
3206 pw.print(getHistoryStringPoolSize());
3207 pw.print(" strings using ");
3208 printSizeValue(pw, getHistoryStringPoolBytes());
3209 pw.println("):");
3210 HistoryPrinter hprinter = new HistoryPrinter();
3211 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003212 long baseTime = -1;
3213 boolean printed = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003214 while (getNextHistoryLocked(rec)) {
3215 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003216 if (baseTime < 0) {
3217 baseTime = lastTime;
3218 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003219 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003220 if (histStart >= 0 && !printed) {
3221 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3222 printed = true;
3223 } else if (rec.currentTime != 0) {
3224 printed = true;
3225 byte cmd = rec.cmd;
3226 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3227 hprinter.printNextItem(pw, rec, baseTime, false,
3228 (flags&DUMP_VERBOSE) != 0);
3229 rec.cmd = cmd;
3230 }
3231 }
3232 hprinter.printNextItem(pw, rec, baseTime, false,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003233 (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003234 }
3235 }
3236 if (histStart >= 0) {
3237 pw.print(" NEXT: "); pw.println(lastTime+1);
3238 }
3239 pw.println();
3240 } finally {
3241 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003242 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003243 }
3244
3245 if (startIteratingOldHistoryLocked()) {
3246 try {
3247 pw.println("Old battery History:");
3248 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003249 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003250 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003251 if (baseTime < 0) {
3252 baseTime = rec.time;
3253 }
3254 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003255 }
3256 pw.println();
3257 } finally {
3258 finishIteratingOldHistoryLocked();
3259 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003260 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003261 }
3262
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003263 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003264 return;
3265 }
3266
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003267 if (!filtering) {
3268 SparseArray<? extends Uid> uidStats = getUidStats();
3269 final int NU = uidStats.size();
3270 boolean didPid = false;
3271 long nowRealtime = SystemClock.elapsedRealtime();
3272 for (int i=0; i<NU; i++) {
3273 Uid uid = uidStats.valueAt(i);
3274 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3275 if (pids != null) {
3276 for (int j=0; j<pids.size(); j++) {
3277 Uid.Pid pid = pids.valueAt(j);
3278 if (!didPid) {
3279 pw.println("Per-PID Stats:");
3280 didPid = true;
3281 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003282 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3283 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003284 pw.print(" PID "); pw.print(pids.keyAt(j));
3285 pw.print(" wake time: ");
3286 TimeUtils.formatDuration(time, pw);
3287 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003288 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003289 }
3290 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003291 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003292 pw.println();
3293 }
3294 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3295 getNumDischargeStepDurations(), false)) {
3296 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3297 if (timeRemaining >= 0) {
3298 pw.print(" Estimated discharge time remaining: ");
3299 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3300 pw.println();
3301 }
3302 pw.println();
3303 }
3304 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3305 getNumChargeStepDurations(), false)) {
3306 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3307 if (timeRemaining >= 0) {
3308 pw.print(" Estimated charge time remaining: ");
3309 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3310 pw.println();
3311 }
3312 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003313 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003314 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003315
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003316 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003317 pw.println("Statistics since last charge:");
3318 pw.println(" System starts: " + getStartCount()
3319 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003320 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003321 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003322 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003323 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3324 pw.println("Statistics since last unplugged:");
3325 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
3328
3329 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003330 public void dumpCheckinLocked(Context context, PrintWriter pw,
3331 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003332 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003334 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3335
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003336 final boolean filtering =
3337 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3338
3339 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003340 final HistoryItem rec = new HistoryItem();
3341 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003342 try {
3343 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3344 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3345 pw.print(HISTORY_STRING_POOL); pw.print(',');
3346 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003347 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003348 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003349 pw.print(",\"");
3350 String str = getHistoryTagPoolString(i);
3351 str = str.replace("\\", "\\\\");
3352 str = str.replace("\"", "\\\"");
3353 pw.print(str);
3354 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003355 pw.println();
3356 }
3357 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003358 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003359 long baseTime = -1;
3360 boolean printed = false;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003361 while (getNextHistoryLocked(rec)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003362 lastTime = rec.time;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003363 if (baseTime < 0) {
3364 baseTime = lastTime;
3365 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003366 if (rec.time >= histStart) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003367 if (histStart >= 0 && !printed) {
3368 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
3369 printed = true;
3370 } else if (rec.currentTime != 0) {
3371 printed = true;
3372 byte cmd = rec.cmd;
3373 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3374 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3375 pw.print(HISTORY_DATA); pw.print(',');
3376 hprinter.printNextItem(pw, rec, baseTime, true, false);
3377 rec.cmd = cmd;
3378 }
3379 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003380 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3381 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003382 hprinter.printNextItem(pw, rec, baseTime, true, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003383 }
3384 }
3385 if (histStart >= 0) {
3386 pw.print("NEXT: "); pw.println(lastTime+1);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003387 }
3388 } finally {
3389 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003390 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003391 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003392 }
3393
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003394 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003395 return;
3396 }
3397
Dianne Hackborne4a59512010-12-07 11:08:07 -08003398 if (apps != null) {
3399 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3400 for (int i=0; i<apps.size(); i++) {
3401 ApplicationInfo ai = apps.get(i);
3402 ArrayList<String> pkgs = uids.get(ai.uid);
3403 if (pkgs == null) {
3404 pkgs = new ArrayList<String>();
3405 uids.put(ai.uid, pkgs);
3406 }
3407 pkgs.add(ai.packageName);
3408 }
3409 SparseArray<? extends Uid> uidStats = getUidStats();
3410 final int NU = uidStats.size();
3411 String[] lineArgs = new String[2];
3412 for (int i=0; i<NU; i++) {
3413 int uid = uidStats.keyAt(i);
3414 ArrayList<String> pkgs = uids.get(uid);
3415 if (pkgs != null) {
3416 for (int j=0; j<pkgs.size(); j++) {
3417 lineArgs[0] = Integer.toString(uid);
3418 lineArgs[1] = pkgs.get(j);
3419 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3420 (Object[])lineArgs);
3421 }
3422 }
3423 }
3424 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003425 if (!filtering) {
3426 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
3427 getNumDischargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003428 String[] lineArgs = new String[1];
3429 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3430 if (timeRemaining >= 0) {
3431 lineArgs[0] = Long.toString(timeRemaining);
3432 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
3433 (Object[])lineArgs);
3434 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003435 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
3436 getNumChargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003437 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3438 if (timeRemaining >= 0) {
3439 lineArgs[0] = Long.toString(timeRemaining);
3440 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
3441 (Object[])lineArgs);
3442 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003443 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003444 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003445 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003446 }
3447 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003448 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 }
3450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451}