blob: cf0caed0df6ac928997ab75a53d63006af288db7 [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 Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Printer;
33import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070034import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070035import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080036import com.android.internal.os.BatterySipper;
37import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
40 * A class providing access to battery usage statistics, including information on
41 * wakelocks, processes, packages, and services. All times are represented in microseconds
42 * except where indicated otherwise.
43 * @hide
44 */
45public abstract class BatteryStats implements Parcelable {
46
47 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070048
49 /** @hide */
50 public static final String SERVICE_NAME = "batterystats";
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 /**
53 * A constant indicating a partial wake lock timer.
54 */
55 public static final int WAKE_TYPE_PARTIAL = 0;
56
57 /**
58 * A constant indicating a full wake lock timer.
59 */
60 public static final int WAKE_TYPE_FULL = 1;
61
62 /**
63 * A constant indicating a window wake lock timer.
64 */
65 public static final int WAKE_TYPE_WINDOW = 2;
66
67 /**
68 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 */
70 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070071
72 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070074 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070075 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070076
77 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070078 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070079 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070080 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070081
82 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070084 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070085 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 /**
88 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070089 */
90 public static final int WIFI_MULTICAST_ENABLED = 7;
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070093 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070094 */
95 public static final int AUDIO_TURNED_ON = 7;
96
97 /**
98 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070099 */
100 public static final int VIDEO_TURNED_ON = 8;
101
102 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800103 * A constant indicating a vibrator on timer
104 */
105 public static final int VIBRATOR_ON = 9;
106
107 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700108 * A constant indicating a foreground activity timer
109 */
110 public static final int FOREGROUND_ACTIVITY = 10;
111
112 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700113 * A constant indicating a wifi batched scan is active
114 */
115 public static final int WIFI_BATCHED_SCAN = 11;
116
117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * Include all of the data in the stats, including previously saved data.
119 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700120 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * Include only the current run in the stats.
124 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700125 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 /**
128 * Include only the run since the last time the device was unplugged in the stats.
129 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700130 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700131
132 // NOTE: Update this list if you add/change any stats above.
133 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700134 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700135 private static final String[] STAT_NAMES = { "l", "c", "u" };
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /**
138 * Bump the version on this if the checkin format changes.
139 */
Ashish Sharmabffcf1c2014-05-08 17:11:28 -0700140 private static final int BATTERY_STATS_CHECKIN_VERSION = 8;
Evan Millar22ac0432009-03-31 11:33:18 -0700141
142 private static final long BYTES_PER_KB = 1024;
143 private static final long BYTES_PER_MB = 1048576; // 1024^2
144 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
Dianne Hackborne4a59512010-12-07 11:08:07 -0800147 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700149 private static final String PROCESS_DATA = "pr";
150 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800151 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700152 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700153 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700154 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700155 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700156 private static final String NETWORK_DATA = "nt";
157 private static final String USER_ACTIVITY_DATA = "ua";
158 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800159 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700160 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700161 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700162 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800163 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800164 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700165 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700166 private static final String SCREEN_BRIGHTNESS_DATA = "br";
167 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700168 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700169 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
170 private static final String DATA_CONNECTION_TIME_DATA = "dct";
171 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800172 private static final String WIFI_STATE_TIME_DATA = "wst";
173 private static final String WIFI_STATE_COUNT_DATA = "wsc";
174 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
175 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800176 private static final String POWER_USE_SUMMARY_DATA = "pws";
177 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700178 private static final String DISCHARGE_STEP_DATA = "dsd";
179 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700180 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
181 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700183 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private final Formatter mFormatter = new Formatter(mFormatBuilder);
185
186 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700187 * State for keeping track of counting information.
188 */
189 public static abstract class Counter {
190
191 /**
192 * Returns the count associated with this Counter for the
193 * selected type of statistics.
194 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700195 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700196 */
Evan Millarc64edde2009-04-18 12:26:32 -0700197 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700198
199 /**
200 * Temporary for debugging.
201 */
202 public abstract void logState(Printer pw, String prefix);
203 }
204
205 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700206 * State for keeping track of long counting information.
207 */
208 public static abstract class LongCounter {
209
210 /**
211 * Returns the count associated with this Counter for the
212 * selected type of statistics.
213 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700214 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700215 */
216 public abstract long getCountLocked(int which);
217
218 /**
219 * Temporary for debugging.
220 */
221 public abstract void logState(Printer pw, String prefix);
222 }
223
224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * State for keeping track of timing information.
226 */
227 public static abstract class Timer {
228
229 /**
230 * Returns the count associated with this Timer for the
231 * selected type of statistics.
232 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700233 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
Evan Millarc64edde2009-04-18 12:26:32 -0700235 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 /**
238 * Returns the total time in microseconds associated with this Timer for the
239 * selected type of statistics.
240 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800241 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700242 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * @return a time in microseconds
244 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800245 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
248 * Temporary for debugging.
249 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700250 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252
253 /**
254 * The statistics associated with a particular uid.
255 */
256 public static abstract class Uid {
257
258 /**
259 * Returns a mapping containing wakelock statistics.
260 *
261 * @return a Map from Strings to Uid.Wakelock objects.
262 */
263 public abstract Map<String, ? extends Wakelock> getWakelockStats();
264
265 /**
266 * The statistics associated with a particular wake lock.
267 */
268 public static abstract class Wakelock {
269 public abstract Timer getWakeTime(int type);
270 }
271
272 /**
273 * Returns a mapping containing sensor statistics.
274 *
275 * @return a Map from Integer sensor ids to Uid.Sensor objects.
276 */
277 public abstract Map<Integer, ? extends Sensor> getSensorStats();
278
279 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700280 * Returns a mapping containing active process data.
281 */
282 public abstract SparseArray<? extends Pid> getPidStats();
283
284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * Returns a mapping containing process statistics.
286 *
287 * @return a Map from Strings to Uid.Proc objects.
288 */
289 public abstract Map<String, ? extends Proc> getProcessStats();
290
291 /**
292 * Returns a mapping containing package statistics.
293 *
294 * @return a Map from Strings to Uid.Pkg objects.
295 */
296 public abstract Map<String, ? extends Pkg> getPackageStats();
297
298 /**
299 * {@hide}
300 */
301 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700302
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800303 public abstract void noteWifiRunningLocked(long elapsedRealtime);
304 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
305 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
306 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
307 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
308 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
309 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
310 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
311 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
312 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
313 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
314 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
315 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
316 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
317 public abstract void noteActivityResumedLocked(long elapsedRealtime);
318 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800319 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
320 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
321 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
322 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
323 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
324 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
325 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700326 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800327 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
Robert Greenwalta029ea12013-09-25 16:38:12 -0700329 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
330
Dianne Hackborn617f8772009-03-31 15:04:46 -0700331 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700332 * Note that these must match the constants in android.os.PowerManager.
333 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
334 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700335 */
336 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700337 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700338 };
339
Jeff Browndf693de2012-07-27 12:03:38 -0700340 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700341
Dianne Hackborn617f8772009-03-31 15:04:46 -0700342 public abstract void noteUserActivityLocked(int type);
343 public abstract boolean hasUserActivity();
344 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700345
346 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800347 public abstract long getNetworkActivityBytes(int type, int which);
348 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800349 public abstract long getMobileRadioActiveTime(int which);
350 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800353 /*
354 * FIXME: it's not correct to use this magic value because it
355 * could clash with a sensor handle (which are defined by
356 * the sensor HAL, and therefore out of our control
357 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 // Magic sensor number for the GPS.
359 public static final int GPS = -10000;
360
361 public abstract int getHandle();
362
363 public abstract Timer getSensorTime();
364 }
365
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700366 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800367 public int mWakeNesting;
368 public long mWakeSumMs;
369 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700370 }
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
373 * The statistics associated with a particular process.
374 */
375 public static abstract class Proc {
376
Dianne Hackborn287952c2010-09-22 22:34:31 -0700377 public static class ExcessivePower {
378 public static final int TYPE_WAKE = 1;
379 public static final int TYPE_CPU = 2;
380
381 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700382 public long overTime;
383 public long usedTime;
384 }
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800387 * Returns true if this process is still active in the battery stats.
388 */
389 public abstract boolean isActive();
390
391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 * Returns the total time (in 1/100 sec) spent executing in user code.
393 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700394 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
396 public abstract long getUserTime(int which);
397
398 /**
399 * Returns the total time (in 1/100 sec) spent executing in system code.
400 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700401 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
403 public abstract long getSystemTime(int which);
404
405 /**
406 * Returns the number of times the process has been started.
407 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700408 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
410 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700411
412 /**
413 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700414 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700415 * @return foreground cpu time in microseconds
416 */
417 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700418
419 /**
420 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
421 * @param speedStep the index of the CPU speed. This is not the actual speed of the
422 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700423 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700424 * @see BatteryStats#getCpuSpeedSteps()
425 */
426 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700427
Dianne Hackborn287952c2010-09-22 22:34:31 -0700428 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700429
Dianne Hackborn287952c2010-09-22 22:34:31 -0700430 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432
433 /**
434 * The statistics associated with a particular package.
435 */
436 public static abstract class Pkg {
437
438 /**
439 * Returns the number of times this package has done something that could wake up the
440 * device from sleep.
441 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700442 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
444 public abstract int getWakeups(int which);
445
446 /**
447 * Returns a mapping containing service statistics.
448 */
449 public abstract Map<String, ? extends Serv> getServiceStats();
450
451 /**
452 * The statistics associated with a particular service.
453 */
454 public abstract class Serv {
455
456 /**
457 * Returns the amount of time spent started.
458 *
459 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700460 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 * @return
462 */
463 public abstract long getStartTime(long batteryUptime, int which);
464
465 /**
466 * Returns the total number of times startService() has been called.
467 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700468 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
470 public abstract int getStarts(int which);
471
472 /**
473 * Returns the total number times the service has been launched.
474 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700475 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 */
477 public abstract int getLaunches(int which);
478 }
479 }
480 }
481
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800482 public final static class HistoryTag {
483 public String string;
484 public int uid;
485
486 public int poolIdx;
487
488 public void setTo(HistoryTag o) {
489 string = o.string;
490 uid = o.uid;
491 poolIdx = o.poolIdx;
492 }
493
494 public void setTo(String _string, int _uid) {
495 string = _string;
496 uid = _uid;
497 poolIdx = -1;
498 }
499
500 public void writeToParcel(Parcel dest, int flags) {
501 dest.writeString(string);
502 dest.writeInt(uid);
503 }
504
505 public void readFromParcel(Parcel src) {
506 string = src.readString();
507 uid = src.readInt();
508 poolIdx = -1;
509 }
510
511 @Override
512 public boolean equals(Object o) {
513 if (this == o) return true;
514 if (o == null || getClass() != o.getClass()) return false;
515
516 HistoryTag that = (HistoryTag) o;
517
518 if (uid != that.uid) return false;
519 if (!string.equals(that.string)) return false;
520
521 return true;
522 }
523
524 @Override
525 public int hashCode() {
526 int result = string.hashCode();
527 result = 31 * result + uid;
528 return result;
529 }
530 }
531
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700532 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700533 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700534
535 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800536
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800537 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800538 public static final byte CMD_NULL = -1;
539 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800540 public static final byte CMD_CURRENT_TIME = 5;
541 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700542 public static final byte CMD_RESET = 7;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800543
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700544 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700545
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800546 /**
547 * Return whether the command code is a delta data update.
548 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800549 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800550 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800551 }
552
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700553 public byte batteryLevel;
554 public byte batteryStatus;
555 public byte batteryHealth;
556 public byte batteryPlugType;
557
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900558 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700559 public char batteryVoltage;
560
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700561 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700562 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800563 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700564 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800565 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
566 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700567 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800568 public static final int STATE_PHONE_STATE_SHIFT = 6;
569 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700570 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800571 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800572 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800573
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700574 // These states always appear directly in the first int token
575 // of a delta change; they should be ones that change relatively
576 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700577 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
578 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800579 public static final int STATE_GPS_ON_FLAG = 1<<29;
580 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800581 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800582 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800583 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800584 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700585 // These are on the lower bits used for the command; if they change
586 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700587 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700588 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700589 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700590 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
591 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
592 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
593 public static final int STATE_WIFI_ON_FLAG = 1<<17;
594 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700595
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700596 public static final int MOST_INTERESTING_STATES =
597 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
598 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
599
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700600 public int states;
601
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700602 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700603 public static final int STATE2_LOW_POWER_FLAG = 1<<1;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700604 public int states2;
605
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800606 // The wake lock that was acquired at this point.
607 public HistoryTag wakelockTag;
608
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800609 // Kernel wakeup reason at this point.
610 public HistoryTag wakeReasonTag;
611
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800612 public static final int EVENT_FLAG_START = 0x8000;
613 public static final int EVENT_FLAG_FINISH = 0x4000;
614
615 // No event in this item.
616 public static final int EVENT_NONE = 0x0000;
617 // Event is about a process that is running.
618 public static final int EVENT_PROC = 0x0001;
619 // Event is about an application package that is in the foreground.
620 public static final int EVENT_FOREGROUND = 0x0002;
621 // Event is about an application package that is at the top of the screen.
622 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800623 // Event is about an application package that is at the top of the screen.
624 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700625 // Events for all additional wake locks aquired/release within a wake block.
626 // These are not generated by default.
627 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800628 // Number of event types.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700629 public static final int EVENT_COUNT = 0x0006;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700630 // Mask to extract out only the type part of the event.
631 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800632
633 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
634 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
635 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
636 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
637 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
638 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800639 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
640 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700641 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
642 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800643
644 // For CMD_EVENT.
645 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800646 public HistoryTag eventTag;
647
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800648 // Only set for CMD_CURRENT_TIME.
649 public long currentTime;
650
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800651 // Meta-data when reading.
652 public int numReadInts;
653
654 // Pre-allocated objects.
655 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800656 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800657 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800658
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700659 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700660 }
661
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700662 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700663 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800664 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700665 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700666 }
667
668 public int describeContents() {
669 return 0;
670 }
671
672 public void writeToParcel(Parcel dest, int flags) {
673 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700674 int bat = (((int)cmd)&0xff)
675 | ((((int)batteryLevel)<<8)&0xff00)
676 | ((((int)batteryStatus)<<16)&0xf0000)
677 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800678 | ((((int)batteryPlugType)<<24)&0xf000000)
679 | (wakelockTag != null ? 0x10000000 : 0)
680 | (wakeReasonTag != null ? 0x20000000 : 0)
681 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700682 dest.writeInt(bat);
683 bat = (((int)batteryTemperature)&0xffff)
684 | ((((int)batteryVoltage)<<16)&0xffff0000);
685 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700686 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700687 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800688 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800689 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800690 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800691 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800692 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800693 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800694 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800695 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800696 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800697 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700698 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800699 dest.writeLong(currentTime);
700 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700701 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700702
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800703 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800704 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700705 int bat = src.readInt();
706 cmd = (byte)(bat&0xff);
707 batteryLevel = (byte)((bat>>8)&0xff);
708 batteryStatus = (byte)((bat>>16)&0xf);
709 batteryHealth = (byte)((bat>>20)&0xf);
710 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800711 int bat2 = src.readInt();
712 batteryTemperature = (short)(bat2&0xffff);
713 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700714 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700715 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800716 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800717 wakelockTag = localWakelockTag;
718 wakelockTag.readFromParcel(src);
719 } else {
720 wakelockTag = null;
721 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800722 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800723 wakeReasonTag = localWakeReasonTag;
724 wakeReasonTag.readFromParcel(src);
725 } else {
726 wakeReasonTag = null;
727 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800728 if ((bat&0x40000000) != 0) {
729 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800730 eventTag = localEventTag;
731 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800732 } else {
733 eventCode = EVENT_NONE;
734 eventTag = null;
735 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700736 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800737 currentTime = src.readLong();
738 } else {
739 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700740 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800741 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700742 }
743
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700744 public void clear() {
745 time = 0;
746 cmd = CMD_NULL;
747 batteryLevel = 0;
748 batteryStatus = 0;
749 batteryHealth = 0;
750 batteryPlugType = 0;
751 batteryTemperature = 0;
752 batteryVoltage = 0;
753 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700754 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800755 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800756 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800757 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800758 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700759 }
760
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700761 public void setTo(HistoryItem o) {
762 time = o.time;
763 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800764 setToCommon(o);
765 }
766
767 public void setTo(long time, byte cmd, HistoryItem o) {
768 this.time = time;
769 this.cmd = cmd;
770 setToCommon(o);
771 }
772
773 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700774 batteryLevel = o.batteryLevel;
775 batteryStatus = o.batteryStatus;
776 batteryHealth = o.batteryHealth;
777 batteryPlugType = o.batteryPlugType;
778 batteryTemperature = o.batteryTemperature;
779 batteryVoltage = o.batteryVoltage;
780 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700781 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800782 if (o.wakelockTag != null) {
783 wakelockTag = localWakelockTag;
784 wakelockTag.setTo(o.wakelockTag);
785 } else {
786 wakelockTag = null;
787 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800788 if (o.wakeReasonTag != null) {
789 wakeReasonTag = localWakeReasonTag;
790 wakeReasonTag.setTo(o.wakeReasonTag);
791 } else {
792 wakeReasonTag = null;
793 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800794 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800795 if (o.eventTag != null) {
796 eventTag = localEventTag;
797 eventTag.setTo(o.eventTag);
798 } else {
799 eventTag = null;
800 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800801 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700802 }
803
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800804 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700805 return batteryLevel == o.batteryLevel
806 && batteryStatus == o.batteryStatus
807 && batteryHealth == o.batteryHealth
808 && batteryPlugType == o.batteryPlugType
809 && batteryTemperature == o.batteryTemperature
810 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800811 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700812 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800813 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700814 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800815
816 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800817 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800818 return false;
819 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800820 if (wakelockTag != o.wakelockTag) {
821 if (wakelockTag == null || o.wakelockTag == null) {
822 return false;
823 }
824 if (!wakelockTag.equals(o.wakelockTag)) {
825 return false;
826 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800827 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800828 if (wakeReasonTag != o.wakeReasonTag) {
829 if (wakeReasonTag == null || o.wakeReasonTag == null) {
830 return false;
831 }
832 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
833 return false;
834 }
835 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800836 if (eventTag != o.eventTag) {
837 if (eventTag == null || o.eventTag == null) {
838 return false;
839 }
840 if (!eventTag.equals(o.eventTag)) {
841 return false;
842 }
843 }
844 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800845 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700846 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700847
848 public final static class HistoryEventTracker {
849 private final HashMap<String, SparseIntArray>[] mActiveEvents
850 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
851
852 public boolean updateState(int code, String name, int uid, int poolIdx) {
853 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
854 int idx = code&HistoryItem.EVENT_TYPE_MASK;
855 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
856 if (active == null) {
857 active = new HashMap<String, SparseIntArray>();
858 mActiveEvents[idx] = active;
859 }
860 SparseIntArray uids = active.get(name);
861 if (uids == null) {
862 uids = new SparseIntArray();
863 active.put(name, uids);
864 }
865 if (uids.indexOfKey(uid) >= 0) {
866 // Already set, nothing to do!
867 return false;
868 }
869 uids.put(uid, poolIdx);
870 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
871 int idx = code&HistoryItem.EVENT_TYPE_MASK;
872 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
873 if (active == null) {
874 // not currently active, nothing to do.
875 return false;
876 }
877 SparseIntArray uids = active.get(name);
878 if (uids == null) {
879 // not currently active, nothing to do.
880 return false;
881 }
882 idx = uids.indexOfKey(uid);
883 if (idx < 0) {
884 // not currently active, nothing to do.
885 return false;
886 }
887 uids.removeAt(idx);
888 if (uids.size() <= 0) {
889 active.remove(name);
890 }
891 }
892 return true;
893 }
894
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700895 public void removeEvents(int code) {
896 int idx = code&HistoryItem.EVENT_TYPE_MASK;
897 mActiveEvents[idx] = null;
898 }
899
Dianne Hackborn37de0982014-05-09 09:32:18 -0700900 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
901 return mActiveEvents[code];
902 }
903 }
904
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700905 public static final class BitDescription {
906 public final int mask;
907 public final int shift;
908 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800909 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700910 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800911 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700912
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800913 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700914 this.mask = mask;
915 this.shift = -1;
916 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800917 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700918 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800919 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700920 }
921
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800922 public BitDescription(int mask, int shift, String name, String shortName,
923 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700924 this.mask = mask;
925 this.shift = shift;
926 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800927 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700928 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800929 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700930 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700931 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700932
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800933 public abstract int getHistoryTotalSize();
934
935 public abstract int getHistoryUsedSize();
936
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700937 public abstract boolean startIteratingHistoryLocked();
938
Dianne Hackborn099bc622014-01-22 13:39:16 -0800939 public abstract int getHistoryStringPoolSize();
940
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800941 public abstract int getHistoryStringPoolBytes();
942
943 public abstract String getHistoryTagPoolString(int index);
944
945 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800946
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700947 public abstract boolean getNextHistoryLocked(HistoryItem out);
948
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700949 public abstract void finishIteratingHistoryLocked();
950
951 public abstract boolean startIteratingOldHistoryLocked();
952
953 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
954
955 public abstract void finishIteratingOldHistoryLocked();
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700958 * Return the base time offset for the battery history.
959 */
960 public abstract long getHistoryBaseTime();
961
962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 * Returns the number of times the device has been started.
964 */
965 public abstract int getStartCount();
966
967 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700968 * 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 -0800969 * running on battery.
970 *
971 * {@hide}
972 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800973 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800975 /**
976 * Returns the number of times the screen was turned on.
977 *
978 * {@hide}
979 */
980 public abstract int getScreenOnCount(int which);
981
Jeff Browne95c3cd2014-05-02 16:59:26 -0700982 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
983
Dianne Hackborn617f8772009-03-31 15:04:46 -0700984 public static final int SCREEN_BRIGHTNESS_DARK = 0;
985 public static final int SCREEN_BRIGHTNESS_DIM = 1;
986 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
987 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
988 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
989
990 static final String[] SCREEN_BRIGHTNESS_NAMES = {
991 "dark", "dim", "medium", "light", "bright"
992 };
993
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800994 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
995 "0", "1", "2", "3", "4"
996 };
997
Dianne Hackborn617f8772009-03-31 15:04:46 -0700998 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
999
1000 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001001 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001002 * the given brightness
1003 *
1004 * {@hide}
1005 */
1006 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001007 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 /**
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001010 * Returns the time in microseconds that low power mode has been enabled while the device was
1011 * running on battery.
1012 *
1013 * {@hide}
1014 */
1015 public abstract long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which);
1016
1017 /**
1018 * Returns the number of times that low power mode was enabled.
1019 *
1020 * {@hide}
1021 */
1022 public abstract int getLowPowerModeEnabledCount(int which);
1023
1024 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001025 * 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 -08001026 * running on battery.
1027 *
1028 * {@hide}
1029 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001030 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001031
1032 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001033 * Returns the number of times a phone call was activated.
1034 *
1035 * {@hide}
1036 */
1037 public abstract int getPhoneOnCount(int which);
1038
1039 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001040 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001041 * the given signal strength.
1042 *
1043 * {@hide}
1044 */
1045 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001046 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001047
Dianne Hackborn617f8772009-03-31 15:04:46 -07001048 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001049 * Returns the time in microseconds that the phone has been trying to
1050 * acquire a signal.
1051 *
1052 * {@hide}
1053 */
1054 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001055 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001056
1057 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001058 * Returns the number of times the phone has entered the given signal strength.
1059 *
1060 * {@hide}
1061 */
1062 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1063
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001064 /**
1065 * Returns the time in microseconds that the mobile network has been active
1066 * (in a high power state).
1067 *
1068 * {@hide}
1069 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001070 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001071
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001072 /**
1073 * Returns the number of times that the mobile network has transitioned to the
1074 * active state.
1075 *
1076 * {@hide}
1077 */
1078 public abstract int getMobileRadioActiveCount(int which);
1079
1080 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001081 * Returns the time in microseconds that is the difference between the mobile radio
1082 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1083 * from the radio.
1084 *
1085 * {@hide}
1086 */
1087 public abstract long getMobileRadioActiveAdjustedTime(int which);
1088
1089 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001090 * Returns the time in microseconds that the mobile network has been active
1091 * (in a high power state) but not being able to blame on an app.
1092 *
1093 * {@hide}
1094 */
1095 public abstract long getMobileRadioActiveUnknownTime(int which);
1096
1097 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001098 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001099 *
1100 * {@hide}
1101 */
1102 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001103
Dianne Hackborn627bba72009-03-24 22:32:56 -07001104 public static final int DATA_CONNECTION_NONE = 0;
1105 public static final int DATA_CONNECTION_GPRS = 1;
1106 public static final int DATA_CONNECTION_EDGE = 2;
1107 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001108 public static final int DATA_CONNECTION_CDMA = 4;
1109 public static final int DATA_CONNECTION_EVDO_0 = 5;
1110 public static final int DATA_CONNECTION_EVDO_A = 6;
1111 public static final int DATA_CONNECTION_1xRTT = 7;
1112 public static final int DATA_CONNECTION_HSDPA = 8;
1113 public static final int DATA_CONNECTION_HSUPA = 9;
1114 public static final int DATA_CONNECTION_HSPA = 10;
1115 public static final int DATA_CONNECTION_IDEN = 11;
1116 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001117 public static final int DATA_CONNECTION_LTE = 13;
1118 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001119 public static final int DATA_CONNECTION_HSPAP = 15;
1120 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001121
Dianne Hackborn627bba72009-03-24 22:32:56 -07001122 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001123 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001124 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001125 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001126 };
1127
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001128 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001129
1130 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001131 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001132 * the given data connection.
1133 *
1134 * {@hide}
1135 */
1136 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001137 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001140 * Returns the number of times the phone has entered the given data
1141 * connection type.
1142 *
1143 * {@hide}
1144 */
1145 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001146
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001147 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1148 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001149 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001150 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1151 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001152 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001153 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1154 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1155 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001156 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001157 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1158 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001159 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001160 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1161 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1162 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1163 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1164 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1165 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1166 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1167 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1168 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1169 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1170 new String[] {"in", "out", "emergency", "off"},
1171 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001172 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001173 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1174 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1175 "0", "1", "2", "3", "4"
1176 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001177 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1178 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1179 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001180 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001181
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001182 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1183 = new BitDescription[] {
1184 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001185 new BitDescription(HistoryItem.STATE2_LOW_POWER_FLAG, "low_power", "lp"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001186 };
1187
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001188 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001189 "null", "proc", "fg", "top", "sync", "wake_lock_in"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001190 };
1191
1192 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001193 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001194 };
1195
Dianne Hackborn617f8772009-03-31 15:04:46 -07001196 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001197 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001198 * running on battery.
1199 *
1200 * {@hide}
1201 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001202 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001203
1204 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001205 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001206 * been in the running state while the device was running on battery.
1207 *
1208 * {@hide}
1209 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001210 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001211
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001212 public static final int WIFI_STATE_OFF = 0;
1213 public static final int WIFI_STATE_OFF_SCANNING = 1;
1214 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1215 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1216 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1217 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1218 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1219 public static final int WIFI_STATE_SOFT_AP = 7;
1220
1221 static final String[] WIFI_STATE_NAMES = {
1222 "off", "scanning", "no_net", "disconn",
1223 "sta", "p2p", "sta_p2p", "soft_ap"
1224 };
1225
1226 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1227
1228 /**
1229 * Returns the time in microseconds that WiFi has been running in the given state.
1230 *
1231 * {@hide}
1232 */
1233 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001234 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001235
1236 /**
1237 * Returns the number of times that WiFi has entered the given state.
1238 *
1239 * {@hide}
1240 */
1241 public abstract int getWifiStateCount(int wifiState, int which);
1242
The Android Open Source Project10592532009-03-18 17:39:46 -07001243 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001244 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001245 * running on battery.
1246 *
1247 * {@hide}
1248 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001249 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001250
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001251 public abstract int getBluetoothPingCount();
1252
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001253 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1254 public static final int BLUETOOTH_STATE_LOW = 1;
1255 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1256 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001257
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001258 static final String[] BLUETOOTH_STATE_NAMES = {
1259 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001260 };
1261
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001262 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001263
1264 /**
1265 * Returns the time in microseconds that Bluetooth has been running in the
1266 * given active state.
1267 *
1268 * {@hide}
1269 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001270 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001271 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001272
1273 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001274 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001275 *
1276 * {@hide}
1277 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001278 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001279
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001280 public static final int NETWORK_MOBILE_RX_DATA = 0;
1281 public static final int NETWORK_MOBILE_TX_DATA = 1;
1282 public static final int NETWORK_WIFI_RX_DATA = 2;
1283 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001284
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001285 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001286
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001287 public abstract long getNetworkActivityBytes(int type, int which);
1288 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001289
The Android Open Source Project10592532009-03-18 17:39:46 -07001290 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001291 * Return the wall clock time when battery stats data collection started.
1292 */
1293 public abstract long getStartClockTime();
1294
1295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * Return whether we are currently running on battery.
1297 */
1298 public abstract boolean getIsOnBattery();
1299
1300 /**
1301 * Returns a SparseArray containing the statistics for each uid.
1302 */
1303 public abstract SparseArray<? extends Uid> getUidStats();
1304
1305 /**
1306 * Returns the current battery uptime in microseconds.
1307 *
1308 * @param curTime the amount of elapsed realtime in microseconds.
1309 */
1310 public abstract long getBatteryUptime(long curTime);
1311
1312 /**
1313 * Returns the current battery realtime in microseconds.
1314 *
1315 * @param curTime the amount of elapsed realtime in microseconds.
1316 */
1317 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001318
1319 /**
Evan Millar633a1742009-04-02 16:36:33 -07001320 * Returns the battery percentage level at the last time the device was unplugged from power, or
1321 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001322 */
Evan Millar633a1742009-04-02 16:36:33 -07001323 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001324
1325 /**
Evan Millar633a1742009-04-02 16:36:33 -07001326 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1327 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001328 */
Evan Millar633a1742009-04-02 16:36:33 -07001329 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
1331 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001332 * Get the amount the battery has discharged since the stats were
1333 * last reset after charging, as a lower-end approximation.
1334 */
1335 public abstract int getLowDischargeAmountSinceCharge();
1336
1337 /**
1338 * Get the amount the battery has discharged since the stats were
1339 * last reset after charging, as an upper-end approximation.
1340 */
1341 public abstract int getHighDischargeAmountSinceCharge();
1342
1343 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001344 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1345 */
1346 public abstract int getDischargeAmount(int which);
1347
1348 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001349 * Get the amount the battery has discharged while the screen was on,
1350 * since the last time power was unplugged.
1351 */
1352 public abstract int getDischargeAmountScreenOn();
1353
1354 /**
1355 * Get the amount the battery has discharged while the screen was on,
1356 * since the last time the device was charged.
1357 */
1358 public abstract int getDischargeAmountScreenOnSinceCharge();
1359
1360 /**
1361 * Get the amount the battery has discharged while the screen was off,
1362 * since the last time power was unplugged.
1363 */
1364 public abstract int getDischargeAmountScreenOff();
1365
1366 /**
1367 * Get the amount the battery has discharged while the screen was off,
1368 * since the last time the device was charged.
1369 */
1370 public abstract int getDischargeAmountScreenOffSinceCharge();
1371
1372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 * Returns the total, last, or current battery uptime in microseconds.
1374 *
1375 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001376 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 */
1378 public abstract long computeBatteryUptime(long curTime, int which);
1379
1380 /**
1381 * Returns the total, last, or current battery realtime in microseconds.
1382 *
1383 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001384 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 */
1386 public abstract long computeBatteryRealtime(long curTime, int which);
1387
1388 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001389 * Returns the total, last, or current battery screen off uptime in microseconds.
1390 *
1391 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001392 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001393 */
1394 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1395
1396 /**
1397 * Returns the total, last, or current battery screen off realtime in microseconds.
1398 *
1399 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001400 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001401 */
1402 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1403
1404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * Returns the total, last, or current uptime in microseconds.
1406 *
1407 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001408 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 */
1410 public abstract long computeUptime(long curTime, int which);
1411
1412 /**
1413 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001416 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 */
1418 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001419
1420 /**
1421 * Compute an approximation for how much run time (in microseconds) is remaining on
1422 * the battery. Returns -1 if no time can be computed: either there is not
1423 * enough current data to make a decision, or the battery is currently
1424 * charging.
1425 *
1426 * @param curTime The current elepsed realtime in microseconds.
1427 */
1428 public abstract long computeBatteryTimeRemaining(long curTime);
1429
1430 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001431 * Return the historical number of discharge steps we currently have.
1432 */
1433 public abstract int getNumDischargeStepDurations();
1434
1435 /**
1436 * Return the array of discharge step durations; the number of valid
1437 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1438 * These values are in milliseconds.
1439 */
1440 public abstract long[] getDischargeStepDurationsArray();
1441
1442 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001443 * Compute an approximation for how much time (in microseconds) remains until the battery
1444 * is fully charged. Returns -1 if no time can be computed: either there is not
1445 * enough current data to make a decision, or the battery is currently
1446 * discharging.
1447 *
1448 * @param curTime The current elepsed realtime in microseconds.
1449 */
1450 public abstract long computeChargeTimeRemaining(long curTime);
1451
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001452 /**
1453 * Return the historical number of charge steps we currently have.
1454 */
1455 public abstract int getNumChargeStepDurations();
1456
1457 /**
1458 * Return the array of charge step durations; the number of valid
1459 * items in it is returned by {@link #getNumChargeStepDurations()}.
1460 * These values are in milliseconds.
1461 */
1462 public abstract long[] getChargeStepDurationsArray();
1463
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001464 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1465
Evan Millarc64edde2009-04-18 12:26:32 -07001466 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
Amith Yamasanie43530a2009-08-21 13:11:37 -07001468 /** Returns the number of different speeds that the CPU can run at */
1469 public abstract int getCpuSpeedSteps();
1470
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001471 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1472
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001473 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 long days = seconds / (60 * 60 * 24);
1475 if (days != 0) {
1476 out.append(days);
1477 out.append("d ");
1478 }
1479 long used = days * 60 * 60 * 24;
1480
1481 long hours = (seconds - used) / (60 * 60);
1482 if (hours != 0 || used != 0) {
1483 out.append(hours);
1484 out.append("h ");
1485 }
1486 used += hours * 60 * 60;
1487
1488 long mins = (seconds-used) / 60;
1489 if (mins != 0 || used != 0) {
1490 out.append(mins);
1491 out.append("m ");
1492 }
1493 used += mins * 60;
1494
1495 if (seconds != 0 || used != 0) {
1496 out.append(seconds-used);
1497 out.append("s ");
1498 }
1499 }
1500
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001501 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001503 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 sb.append((time - (sec * 100)) * 10);
1505 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001508 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001510 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 sb.append(time - (sec * 1000));
1512 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001515 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001516 long sec = time / 1000;
1517 formatTimeRaw(sb, sec);
1518 sb.append(time - (sec * 1000));
1519 sb.append("ms");
1520 }
1521
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001522 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001524 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526 float perc = ((float)num) / ((float)den) * 100;
1527 mFormatBuilder.setLength(0);
1528 mFormatter.format("%.1f%%", perc);
1529 return mFormatBuilder.toString();
1530 }
1531
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001532 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001533 mFormatBuilder.setLength(0);
1534
1535 if (bytes < BYTES_PER_KB) {
1536 return bytes + "B";
1537 } else if (bytes < BYTES_PER_MB) {
1538 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1539 return mFormatBuilder.toString();
1540 } else if (bytes < BYTES_PER_GB){
1541 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1542 return mFormatBuilder.toString();
1543 } else {
1544 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1545 return mFormatBuilder.toString();
1546 }
1547 }
1548
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001549 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001550 if (timer != null) {
1551 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001552 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001553 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1554 return totalTimeMillis;
1555 }
1556 return 0;
1557 }
1558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 /**
1560 *
1561 * @param sb a StringBuilder object.
1562 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001563 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001565 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 * @param linePrefix a String to be prepended to each line of output.
1567 * @return the line prefix
1568 */
1569 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001570 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571
1572 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001573 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
Evan Millarc64edde2009-04-18 12:26:32 -07001575 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 if (totalTimeMillis != 0) {
1577 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001578 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001579 if (name != null) {
1580 sb.append(name);
1581 sb.append(' ');
1582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 sb.append('(');
1584 sb.append(count);
1585 sb.append(" times)");
1586 return ", ";
1587 }
1588 }
1589 return linePrefix;
1590 }
1591
1592 /**
1593 * Checkin version of wakelock printer. Prints simple comma-separated list.
1594 *
1595 * @param sb a StringBuilder object.
1596 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001597 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001599 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 * @param linePrefix a String to be prepended to each line of output.
1601 * @return the line prefix
1602 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001603 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1604 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 long totalTimeMicros = 0;
1606 int count = 0;
1607 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001608 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001609 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
1611 sb.append(linePrefix);
1612 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1613 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001614 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 sb.append(count);
1616 return ",";
1617 }
1618
1619 /**
1620 * Dump a comma-separated line of values for terse checkin mode.
1621 *
1622 * @param pw the PageWriter to dump log to
1623 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1624 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1625 * @param args type-dependent data arguments
1626 */
1627 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1628 Object... args ) {
1629 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1630 pw.print(uid); pw.print(',');
1631 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001632 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633
1634 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001635 pw.print(',');
1636 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001638 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640
1641 /**
1642 * Checkin server version of dump to produce more compact, computer-readable log.
1643 *
1644 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001646 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1648 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1649 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1651 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001652 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1653 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1654 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 final long totalRealtime = computeRealtime(rawRealtime, which);
1656 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001657 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07001658 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001659 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001660 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1661 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1662 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1663 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 StringBuilder sb = new StringBuilder(128);
1666
Evan Millar22ac0432009-03-31 11:33:18 -07001667 SparseArray<? extends Uid> uidStats = getUidStats();
1668 final int NU = uidStats.size();
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 // Dump "battery" stat
1673 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001674 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001675 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001676 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001677 getStartClockTime(),
1678 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001680 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001681 long fullWakeLockTimeTotal = 0;
1682 long partialWakeLockTimeTotal = 0;
1683
1684 for (int iu = 0; iu < NU; iu++) {
1685 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001686
Evan Millar22ac0432009-03-31 11:33:18 -07001687 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1688 if (wakelocks.size() > 0) {
1689 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1690 : wakelocks.entrySet()) {
1691 Uid.Wakelock wl = ent.getValue();
1692
1693 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1694 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001695 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1696 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001697 }
1698
1699 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1700 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001701 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001702 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001703 }
1704 }
1705 }
1706 }
1707
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001708 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1709 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1710 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1711 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1712 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1713 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1714 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1715 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1716
1717 // Dump network stats
1718 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1719 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1720 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 // Dump misc stats
1723 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001724 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001725 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001726 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001727 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Jeff Browne95c3cd2014-05-02 16:59:26 -07001728 0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which),
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001729 getMobileRadioActiveAdjustedTime(which), interactiveTime / 1000,
1730 lowPowerModeEnabledTime / 1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001731
1732 // Dump screen brightness stats
1733 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1734 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001735 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001736 }
1737 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001738
Dianne Hackborn627bba72009-03-24 22:32:56 -07001739 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001740 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1741 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001742 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001743 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001744 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001745 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001746 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001747 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001748 args[i] = getPhoneSignalStrengthCount(i, which);
1749 }
1750 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001751
Dianne Hackborn627bba72009-03-24 22:32:56 -07001752 // Dump network type stats
1753 args = new Object[NUM_DATA_CONNECTION_TYPES];
1754 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001755 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001756 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001757 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1758 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1759 args[i] = getPhoneDataConnectionCount(i, which);
1760 }
1761 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001762
1763 // Dump wifi state stats
1764 args = new Object[NUM_WIFI_STATES];
1765 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001766 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001767 }
1768 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1769 for (int i=0; i<NUM_WIFI_STATES; i++) {
1770 args[i] = getWifiStateCount(i, which);
1771 }
1772 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1773
1774 // Dump bluetooth state stats
1775 args = new Object[NUM_BLUETOOTH_STATES];
1776 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001777 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001778 }
1779 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1780 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1781 args[i] = getBluetoothStateCount(i, which);
1782 }
1783 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1784
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001785 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001786 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001787 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001790 if (which == STATS_SINCE_UNPLUGGED) {
1791 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1792 getDischargeStartLevel()-getDischargeCurrentLevel(),
1793 getDischargeStartLevel()-getDischargeCurrentLevel(),
1794 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1795 } else {
1796 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1797 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1798 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1799 }
1800
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001801 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001802 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001803 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001804 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001805 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001806 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001807 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001808 sb.toString());
1809 }
Evan Millarc64edde2009-04-18 12:26:32 -07001810 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001811 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1812 if (wakeupReasons.size() > 0) {
1813 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1814 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1815 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1816 }
1817 }
Evan Millarc64edde2009-04-18 12:26:32 -07001818 }
1819
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001820 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001821 helper.create(this);
1822 helper.refreshStats(which, UserHandle.USER_ALL);
1823 List<BatterySipper> sippers = helper.getUsageList();
1824 if (sippers != null && sippers.size() > 0) {
1825 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1826 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001827 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001828 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1829 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1830 for (int i=0; i<sippers.size(); i++) {
1831 BatterySipper bs = sippers.get(i);
1832 int uid = 0;
1833 String label;
1834 switch (bs.drainType) {
1835 case IDLE:
1836 label="idle";
1837 break;
1838 case CELL:
1839 label="cell";
1840 break;
1841 case PHONE:
1842 label="phone";
1843 break;
1844 case WIFI:
1845 label="wifi";
1846 break;
1847 case BLUETOOTH:
1848 label="blue";
1849 break;
1850 case SCREEN:
1851 label="scrn";
1852 break;
1853 case APP:
1854 uid = bs.uidObj.getUid();
1855 label = "uid";
1856 break;
1857 case USER:
1858 uid = UserHandle.getUid(bs.userId, 0);
1859 label = "user";
1860 break;
1861 case UNACCOUNTED:
1862 label = "unacc";
1863 break;
1864 case OVERCOUNTED:
1865 label = "over";
1866 break;
1867 default:
1868 label = "???";
1869 }
1870 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1871 BatteryStatsHelper.makemAh(bs.value));
1872 }
1873 }
1874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 for (int iu = 0; iu < NU; iu++) {
1876 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001877 if (reqUid >= 0 && uid != reqUid) {
1878 continue;
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 Uid u = uidStats.valueAt(iu);
1881 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001882 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1883 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1884 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1885 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1886 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1887 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001888 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1889 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001890 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1891 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001892 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1893 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1894 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001895
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1897 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001898 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001899 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1900 wifiBytesRx, wifiBytesTx,
1901 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001902 wifiPacketsRx, wifiPacketsTx,
1903 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001904 }
1905
Nick Pelly6ccaa542012-06-15 15:22:47 -07001906 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001907 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001908 dumpLine(pw, uid, category, WIFI_DATA,
1909 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911
Dianne Hackborn617f8772009-03-31 15:04:46 -07001912 if (u.hasUserActivity()) {
1913 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1914 boolean hasData = false;
1915 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1916 int val = u.getUserActivityCount(i, which);
1917 args[i] = val;
1918 if (val != 0) hasData = true;
1919 }
1920 if (hasData) {
1921 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1922 }
1923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1926 if (wakelocks.size() > 0) {
1927 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1928 : wakelocks.entrySet()) {
1929 Uid.Wakelock wl = ent.getValue();
1930 String linePrefix = "";
1931 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001932 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001933 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001934 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001935 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001936 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001937 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938
1939 // Only log if we had at lease one wakelock...
1940 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001941 String name = ent.getKey();
1942 if (name.indexOf(',') >= 0) {
1943 name = name.replace(',', '_');
1944 }
1945 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
1947 }
1948 }
1949
1950 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1951 if (sensors.size() > 0) {
1952 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1953 : sensors.entrySet()) {
1954 Uid.Sensor se = ent.getValue();
1955 int sensorNumber = ent.getKey();
1956 Timer timer = se.getSensorTime();
1957 if (timer != null) {
1958 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001959 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001960 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 if (totalTime != 0) {
1962 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1963 }
1964 }
1965 }
1966 }
1967
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001968 Timer vibTimer = u.getVibratorOnTimer();
1969 if (vibTimer != null) {
1970 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001971 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001972 int count = vibTimer.getCountLocked(which);
1973 if (totalTime != 0) {
1974 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1975 }
1976 }
1977
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001978 Timer fgTimer = u.getForegroundActivityTimer();
1979 if (fgTimer != null) {
1980 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001981 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001982 int count = fgTimer.getCountLocked(which);
1983 if (totalTime != 0) {
1984 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1985 }
1986 }
1987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1989 if (processStats.size() > 0) {
1990 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1991 : processStats.entrySet()) {
1992 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001993
1994 final long userMillis = ps.getUserTime(which) * 10;
1995 final long systemMillis = ps.getSystemTime(which) * 10;
1996 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1997 final long starts = ps.getStarts(which);
1998
1999 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2000 || starts != 0) {
2001 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
2002 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004 }
2005 }
2006
2007 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2008 if (packageStats.size() > 0) {
2009 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2010 : packageStats.entrySet()) {
2011
2012 Uid.Pkg ps = ent.getValue();
2013 int wakeups = ps.getWakeups(which);
2014 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2015 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2016 : serviceStats.entrySet()) {
2017 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2018 long startTime = ss.getStartTime(batteryUptime, which);
2019 int starts = ss.getStarts(which);
2020 int launches = ss.getLaunches(which);
2021 if (startTime != 0 || starts != 0 || launches != 0) {
2022 dumpLine(pw, uid, category, APK_DATA,
2023 wakeups, // wakeup alarms
2024 ent.getKey(), // Apk
2025 sent.getKey(), // service
2026 startTime / 1000, // time spent started, in ms
2027 starts,
2028 launches);
2029 }
2030 }
2031 }
2032 }
2033 }
2034 }
2035
Dianne Hackborn81038902012-11-26 17:04:09 -08002036 static final class TimerEntry {
2037 final String mName;
2038 final int mId;
2039 final BatteryStats.Timer mTimer;
2040 final long mTime;
2041 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2042 mName = name;
2043 mId = id;
2044 mTimer = timer;
2045 mTime = time;
2046 }
2047 }
2048
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002049 private void printmAh(PrintWriter printer, double power) {
2050 printer.print(BatteryStatsHelper.makemAh(power));
2051 }
2052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002054 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
2055 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2057 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2058 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059
2060 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2061 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2062 final long totalRealtime = computeRealtime(rawRealtime, which);
2063 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002064 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2065 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2066 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002067 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2068 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07002071
2072 SparseArray<? extends Uid> uidStats = getUidStats();
2073 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002075 sb.setLength(0);
2076 sb.append(prefix);
2077 sb.append(" Time on battery: ");
2078 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2079 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2080 sb.append(") realtime, ");
2081 formatTimeMs(sb, whichBatteryUptime / 1000);
2082 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2083 sb.append(") uptime");
2084 pw.println(sb.toString());
2085 sb.setLength(0);
2086 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002087 sb.append(" Time on battery screen off: ");
2088 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2089 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2090 sb.append(") realtime, ");
2091 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2092 sb.append("(");
2093 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2094 sb.append(") uptime");
2095 pw.println(sb.toString());
2096 sb.setLength(0);
2097 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002098 sb.append(" Total run time: ");
2099 formatTimeMs(sb, totalRealtime / 1000);
2100 sb.append("realtime, ");
2101 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002102 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07002103 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002104 if (batteryTimeRemaining >= 0) {
2105 sb.setLength(0);
2106 sb.append(prefix);
2107 sb.append(" Battery time remaining: ");
2108 formatTimeMs(sb, batteryTimeRemaining / 1000);
2109 pw.println(sb.toString());
2110 }
2111 if (chargeTimeRemaining >= 0) {
2112 sb.setLength(0);
2113 sb.append(prefix);
2114 sb.append(" Charge time remaining: ");
2115 formatTimeMs(sb, chargeTimeRemaining / 1000);
2116 pw.println(sb.toString());
2117 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002118 pw.print(" Start clock time: ");
2119 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2120
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002121 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002122 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002123 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002124 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2125 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2126 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2127 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002128 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002129 sb.append(prefix);
2130 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2131 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002132 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002133 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2134 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07002135 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002136 pw.println(sb.toString());
2137 sb.setLength(0);
2138 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002139 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002140 boolean didOne = false;
2141 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002142 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002143 if (time == 0) {
2144 continue;
2145 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002146 sb.append("\n ");
2147 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002148 didOne = true;
2149 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2150 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002151 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002152 sb.append("(");
2153 sb.append(formatRatioLocked(time, screenOnTime));
2154 sb.append(")");
2155 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002156 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002157 pw.println(sb.toString());
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002158 if (lowPowerModeEnabledTime != 0) {
2159 sb.setLength(0);
2160 sb.append(prefix);
2161 sb.append(" Low power mode enabled: ");
2162 formatTimeMs(sb, lowPowerModeEnabledTime / 1000);
2163 sb.append("(");
2164 sb.append(formatRatioLocked(lowPowerModeEnabledTime, whichBatteryRealtime));
2165 sb.append(")");
2166 pw.println(sb.toString());
2167 }
2168 if (phoneOnTime != 0) {
2169 sb.setLength(0);
2170 sb.append(prefix);
2171 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2172 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2173 sb.append(") "); sb.append(getPhoneOnCount(which));
2174 }
2175
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002176 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002177 long fullWakeLockTimeTotalMicros = 0;
2178 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002179
Dianne Hackborn81038902012-11-26 17:04:09 -08002180 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2181
Evan Millar22ac0432009-03-31 11:33:18 -07002182 for (int iu = 0; iu < NU; iu++) {
2183 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002184
Evan Millar22ac0432009-03-31 11:33:18 -07002185 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2186 if (wakelocks.size() > 0) {
2187 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2188 : wakelocks.entrySet()) {
2189 Uid.Wakelock wl = ent.getValue();
2190
2191 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2192 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002193 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002194 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002195 }
2196
2197 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2198 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002199 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002200 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002201 if (totalTimeMicros > 0) {
2202 if (reqUid < 0) {
2203 // Only show the ordered list of all wake
2204 // locks if the caller is not asking for data
2205 // about a specific uid.
2206 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2207 partialWakeTimer, totalTimeMicros));
2208 }
2209 partialWakeLockTimeTotalMicros += totalTimeMicros;
2210 }
Evan Millar22ac0432009-03-31 11:33:18 -07002211 }
2212 }
2213 }
2214 }
2215
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002216 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2217 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2218 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2219 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2220 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2221 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2222 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2223 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2224
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002225 if (fullWakeLockTimeTotalMicros != 0) {
2226 sb.setLength(0);
2227 sb.append(prefix);
2228 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2229 (fullWakeLockTimeTotalMicros + 500) / 1000);
2230 pw.println(sb.toString());
2231 }
2232
2233 if (partialWakeLockTimeTotalMicros != 0) {
2234 sb.setLength(0);
2235 sb.append(prefix);
2236 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2237 (partialWakeLockTimeTotalMicros + 500) / 1000);
2238 pw.println(sb.toString());
2239 }
2240
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002241 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002242 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2243 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2244 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2245 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002246 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002247 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002248 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002249 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002250 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002251 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002252 if (time == 0) {
2253 continue;
2254 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002255 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002256 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002257 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002258 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002259 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002260 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002261 sb.append("(");
2262 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002263 sb.append(") ");
2264 sb.append(getPhoneSignalStrengthCount(i, which));
2265 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002266 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002267 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002268 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002269
2270 sb.setLength(0);
2271 sb.append(prefix);
2272 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002273 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002274 pw.println(sb.toString());
2275
Dianne Hackborn627bba72009-03-24 22:32:56 -07002276 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002277 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002278 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002279 didOne = false;
2280 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002281 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002282 if (time == 0) {
2283 continue;
2284 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002285 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002286 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002287 didOne = true;
2288 sb.append(DATA_CONNECTION_NAMES[i]);
2289 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002290 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002291 sb.append("(");
2292 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002293 sb.append(") ");
2294 sb.append(getPhoneDataConnectionCount(i, which));
2295 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002296 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002297 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002298 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002299
2300 sb.setLength(0);
2301 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002302 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002303 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002304 formatTimeMs(sb, mobileActiveTime / 1000);
2305 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2306 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2307 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002308 pw.println(sb.toString());
2309
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002310 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2311 if (mobileActiveUnknownTime != 0) {
2312 sb.setLength(0);
2313 sb.append(prefix);
2314 sb.append(" Mobile radio active unknown time: ");
2315 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2316 sb.append("(");
2317 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2318 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2319 sb.append("x");
2320 pw.println(sb.toString());
2321 }
2322
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002323 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2324 if (mobileActiveAdjustedTime != 0) {
2325 sb.setLength(0);
2326 sb.append(prefix);
2327 sb.append(" Mobile radio active adjusted time: ");
2328 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2329 sb.append("(");
2330 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2331 sb.append(")");
2332 pw.println(sb.toString());
2333 }
2334
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002335 pw.print(prefix);
2336 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2337 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2338 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2339 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002340 sb.setLength(0);
2341 sb.append(prefix);
2342 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2343 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2344 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2345 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002346 sb.append(")");
2347 pw.println(sb.toString());
2348
2349 sb.setLength(0);
2350 sb.append(prefix);
2351 sb.append(" Wifi states:");
2352 didOne = false;
2353 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002354 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002355 if (time == 0) {
2356 continue;
2357 }
2358 sb.append("\n ");
2359 didOne = true;
2360 sb.append(WIFI_STATE_NAMES[i]);
2361 sb.append(" ");
2362 formatTimeMs(sb, time/1000);
2363 sb.append("(");
2364 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2365 sb.append(") ");
2366 sb.append(getPhoneDataConnectionCount(i, which));
2367 sb.append("x");
2368 }
2369 if (!didOne) sb.append(" (no activity)");
2370 pw.println(sb.toString());
2371
2372 sb.setLength(0);
2373 sb.append(prefix);
2374 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002375 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2376 sb.append(")");
2377 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002378
2379 sb.setLength(0);
2380 sb.append(prefix);
2381 sb.append(" Bluetooth states:");
2382 didOne = false;
2383 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002384 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002385 if (time == 0) {
2386 continue;
2387 }
2388 sb.append("\n ");
2389 didOne = true;
2390 sb.append(BLUETOOTH_STATE_NAMES[i]);
2391 sb.append(" ");
2392 formatTimeMs(sb, time/1000);
2393 sb.append("(");
2394 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2395 sb.append(") ");
2396 sb.append(getPhoneDataConnectionCount(i, which));
2397 sb.append("x");
2398 }
2399 if (!didOne) sb.append(" (no activity)");
2400 pw.println(sb.toString());
2401
2402 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002403
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002404 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002405 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002406 pw.print(prefix); pw.println(" Device is currently unplugged");
2407 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2408 pw.println(getDischargeStartLevel());
2409 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2410 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002411 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002412 pw.print(prefix); pw.println(" Device is currently plugged into power");
2413 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2414 pw.println(getDischargeStartLevel());
2415 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2416 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002417 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002418 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2419 pw.println(getDischargeAmountScreenOn());
2420 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2421 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002422 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002423 } else {
2424 pw.print(prefix); pw.println(" Device battery use since last full charge");
2425 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2426 pw.println(getLowDischargeAmountSinceCharge());
2427 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2428 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002429 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2430 pw.println(getDischargeAmountScreenOnSinceCharge());
2431 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2432 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002433 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002434 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002435
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002436 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002437 helper.create(this);
2438 helper.refreshStats(which, UserHandle.USER_ALL);
2439 List<BatterySipper> sippers = helper.getUsageList();
2440 if (sippers != null && sippers.size() > 0) {
2441 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2442 pw.print(prefix); pw.print(" Capacity: ");
2443 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002444 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002445 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2446 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2447 pw.println();
2448 for (int i=0; i<sippers.size(); i++) {
2449 BatterySipper bs = sippers.get(i);
2450 switch (bs.drainType) {
2451 case IDLE:
2452 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2453 pw.println();
2454 break;
2455 case CELL:
2456 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2457 pw.println();
2458 break;
2459 case PHONE:
2460 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2461 pw.println();
2462 break;
2463 case WIFI:
2464 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2465 pw.println();
2466 break;
2467 case BLUETOOTH:
2468 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2469 pw.println();
2470 break;
2471 case SCREEN:
2472 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2473 pw.println();
2474 break;
2475 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002476 pw.print(prefix); pw.print(" Uid ");
2477 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002478 pw.print(": "); printmAh(pw, bs.value); pw.println();
2479 break;
2480 case USER:
2481 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2482 pw.print(": "); printmAh(pw, bs.value); pw.println();
2483 break;
2484 case UNACCOUNTED:
2485 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2486 pw.println();
2487 break;
2488 case OVERCOUNTED:
2489 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2490 pw.println();
2491 break;
2492 }
2493 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002494 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002495 }
2496
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002497 sippers = helper.getMobilemsppList();
2498 if (sippers != null && sippers.size() > 0) {
2499 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002500 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002501 for (int i=0; i<sippers.size(); i++) {
2502 BatterySipper bs = sippers.get(i);
2503 sb.setLength(0);
2504 sb.append(prefix); sb.append(" Uid ");
2505 UserHandle.formatUid(sb, bs.uidObj.getUid());
2506 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2507 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2508 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002509 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002510 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002511 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002512 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002513 sb.setLength(0);
2514 sb.append(prefix);
2515 sb.append(" TOTAL TIME: ");
2516 formatTimeMs(sb, totalTime);
2517 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2518 sb.append(")");
2519 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002520 pw.println();
2521 }
2522
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002523 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2524 @Override
2525 public int compare(TimerEntry lhs, TimerEntry rhs) {
2526 long lhsTime = lhs.mTime;
2527 long rhsTime = rhs.mTime;
2528 if (lhsTime < rhsTime) {
2529 return 1;
2530 }
2531 if (lhsTime > rhsTime) {
2532 return -1;
2533 }
2534 return 0;
2535 }
2536 };
2537
2538 if (reqUid < 0) {
2539 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2540 if (kernelWakelocks.size() > 0) {
2541 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2542 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2543 BatteryStats.Timer timer = ent.getValue();
2544 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2545 if (totalTimeMillis > 0) {
2546 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2547 }
2548 }
2549 if (ktimers.size() > 0) {
2550 Collections.sort(ktimers, timerComparator);
2551 pw.print(prefix); pw.println(" All kernel wake locks:");
2552 for (int i=0; i<ktimers.size(); i++) {
2553 TimerEntry timer = ktimers.get(i);
2554 String linePrefix = ": ";
2555 sb.setLength(0);
2556 sb.append(prefix);
2557 sb.append(" Kernel Wake lock ");
2558 sb.append(timer.mName);
2559 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2560 which, linePrefix);
2561 if (!linePrefix.equals(": ")) {
2562 sb.append(" realtime");
2563 // Only print out wake locks that were held
2564 pw.println(sb.toString());
2565 }
2566 }
2567 pw.println();
2568 }
2569 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002570
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002571 if (timers.size() > 0) {
2572 Collections.sort(timers, timerComparator);
2573 pw.print(prefix); pw.println(" All partial wake locks:");
2574 for (int i=0; i<timers.size(); i++) {
2575 TimerEntry timer = timers.get(i);
2576 sb.setLength(0);
2577 sb.append(" Wake lock ");
2578 UserHandle.formatUid(sb, timer.mId);
2579 sb.append(" ");
2580 sb.append(timer.mName);
2581 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2582 sb.append(" realtime");
2583 pw.println(sb.toString());
2584 }
2585 timers.clear();
2586 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002587 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002588
2589 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2590 if (wakeupReasons.size() > 0) {
2591 pw.print(prefix); pw.println(" All wakeup reasons:");
2592 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2593 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2594 BatteryStats.LongCounter counter = ent.getValue();
2595 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2596 ent.getValue().getCountLocked(which)));
2597 }
2598 Collections.sort(reasons, timerComparator);
2599 for (int i=0; i<reasons.size(); i++) {
2600 TimerEntry timer = reasons.get(i);
2601 String linePrefix = ": ";
2602 sb.setLength(0);
2603 sb.append(prefix);
2604 sb.append(" Wakeup reason ");
2605 sb.append(timer.mName);
2606 sb.append(": ");
2607 formatTimeMs(sb, timer.mTime);
2608 sb.append("realtime");
2609 pw.println(sb.toString());
2610 }
2611 pw.println();
2612 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002613 }
Evan Millar22ac0432009-03-31 11:33:18 -07002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 for (int iu=0; iu<NU; iu++) {
2616 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002617 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002618 continue;
2619 }
2620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002622
2623 pw.print(prefix);
2624 pw.print(" ");
2625 UserHandle.formatUid(pw, uid);
2626 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002628
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002629 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2630 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2631 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2632 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2633 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2634 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002635 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2636 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002637 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2638 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002639 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2640 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2641 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002642
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002643 if (mobileRxBytes > 0 || mobileTxBytes > 0
2644 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002645 pw.print(prefix); pw.print(" Mobile network: ");
2646 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002647 pw.print(formatBytesLocked(mobileTxBytes));
2648 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2649 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002651 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2652 sb.setLength(0);
2653 sb.append(prefix); sb.append(" Mobile radio active: ");
2654 formatTimeMs(sb, uidMobileActiveTime / 1000);
2655 sb.append("(");
2656 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2657 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2658 long packets = mobileRxPackets + mobileTxPackets;
2659 if (packets == 0) {
2660 packets = 1;
2661 }
2662 sb.append(" @ ");
2663 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2664 sb.append(" mspp");
2665 pw.println(sb.toString());
2666 }
2667
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002668 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002669 pw.print(prefix); pw.print(" Wi-Fi network: ");
2670 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002671 pw.print(formatBytesLocked(wifiTxBytes));
2672 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2673 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002674 }
2675
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002676 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2677 || uidWifiRunningTime != 0) {
2678 sb.setLength(0);
2679 sb.append(prefix); sb.append(" Wifi Running: ");
2680 formatTimeMs(sb, uidWifiRunningTime / 1000);
2681 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2682 whichBatteryRealtime)); sb.append(")\n");
2683 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2684 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2685 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2686 whichBatteryRealtime)); sb.append(")\n");
2687 sb.append(prefix); sb.append(" Wifi Scan: ");
2688 formatTimeMs(sb, wifiScanTime / 1000);
2689 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2690 whichBatteryRealtime)); sb.append(")");
2691 pw.println(sb.toString());
2692 }
2693
Dianne Hackborn617f8772009-03-31 15:04:46 -07002694 if (u.hasUserActivity()) {
2695 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002696 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002697 int val = u.getUserActivityCount(i, which);
2698 if (val != 0) {
2699 if (!hasData) {
2700 sb.setLength(0);
2701 sb.append(" User activity: ");
2702 hasData = true;
2703 } else {
2704 sb.append(", ");
2705 }
2706 sb.append(val);
2707 sb.append(" ");
2708 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2709 }
2710 }
2711 if (hasData) {
2712 pw.println(sb.toString());
2713 }
2714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715
2716 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2717 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002718 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2719 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2721 : wakelocks.entrySet()) {
2722 Uid.Wakelock wl = ent.getValue();
2723 String linePrefix = ": ";
2724 sb.setLength(0);
2725 sb.append(prefix);
2726 sb.append(" Wake lock ");
2727 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002728 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002730 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002732 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 "window", which, linePrefix);
2734 if (!linePrefix.equals(": ")) {
2735 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002736 // Only print out wake locks that were held
2737 pw.println(sb.toString());
2738 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002739 count++;
2740 }
2741 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002742 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002743 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002744 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002745 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002746 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002747 }
2748 if (count > 1) {
2749 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2750 sb.setLength(0);
2751 sb.append(prefix);
2752 sb.append(" TOTAL wake: ");
2753 boolean needComma = false;
2754 if (totalFull != 0) {
2755 needComma = true;
2756 formatTimeMs(sb, totalFull);
2757 sb.append("full");
2758 }
2759 if (totalPartial != 0) {
2760 if (needComma) {
2761 sb.append(", ");
2762 }
2763 needComma = true;
2764 formatTimeMs(sb, totalPartial);
2765 sb.append("partial");
2766 }
2767 if (totalWindow != 0) {
2768 if (needComma) {
2769 sb.append(", ");
2770 }
2771 needComma = true;
2772 formatTimeMs(sb, totalWindow);
2773 sb.append("window");
2774 }
2775 sb.append(" realtime");
2776 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 }
2779 }
2780
2781 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2782 if (sensors.size() > 0) {
2783 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2784 : sensors.entrySet()) {
2785 Uid.Sensor se = ent.getValue();
2786 int sensorNumber = ent.getKey();
2787 sb.setLength(0);
2788 sb.append(prefix);
2789 sb.append(" Sensor ");
2790 int handle = se.getHandle();
2791 if (handle == Uid.Sensor.GPS) {
2792 sb.append("GPS");
2793 } else {
2794 sb.append(handle);
2795 }
2796 sb.append(": ");
2797
2798 Timer timer = se.getSensorTime();
2799 if (timer != null) {
2800 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002801 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002802 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002803 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 //timer.logState();
2805 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002806 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 sb.append("realtime (");
2808 sb.append(count);
2809 sb.append(" times)");
2810 } else {
2811 sb.append("(not used)");
2812 }
2813 } else {
2814 sb.append("(not used)");
2815 }
2816
2817 pw.println(sb.toString());
2818 uidActivity = true;
2819 }
2820 }
2821
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002822 Timer vibTimer = u.getVibratorOnTimer();
2823 if (vibTimer != null) {
2824 // Convert from microseconds to milliseconds with rounding
2825 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002826 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002827 int count = vibTimer.getCountLocked(which);
2828 //timer.logState();
2829 if (totalTime != 0) {
2830 sb.setLength(0);
2831 sb.append(prefix);
2832 sb.append(" Vibrator: ");
2833 formatTimeMs(sb, totalTime);
2834 sb.append("realtime (");
2835 sb.append(count);
2836 sb.append(" times)");
2837 pw.println(sb.toString());
2838 uidActivity = true;
2839 }
2840 }
2841
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002842 Timer fgTimer = u.getForegroundActivityTimer();
2843 if (fgTimer != null) {
2844 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002845 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002846 int count = fgTimer.getCountLocked(which);
2847 if (totalTime != 0) {
2848 sb.setLength(0);
2849 sb.append(prefix);
2850 sb.append(" Foreground activities: ");
2851 formatTimeMs(sb, totalTime);
2852 sb.append("realtime (");
2853 sb.append(count);
2854 sb.append(" times)");
2855 pw.println(sb.toString());
2856 uidActivity = true;
2857 }
2858 }
2859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2861 if (processStats.size() > 0) {
2862 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2863 : processStats.entrySet()) {
2864 Uid.Proc ps = ent.getValue();
2865 long userTime;
2866 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002867 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002869 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870
2871 userTime = ps.getUserTime(which);
2872 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002873 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002875 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002876 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002878 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002879 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002880 sb.setLength(0);
2881 sb.append(prefix); sb.append(" Proc ");
2882 sb.append(ent.getKey()); sb.append(":\n");
2883 sb.append(prefix); sb.append(" CPU: ");
2884 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002885 formatTime(sb, systemTime); sb.append("krn ; ");
2886 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002887 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002888 sb.append("\n"); sb.append(prefix); sb.append(" ");
2889 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002890 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002891 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002892 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002893 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002894 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002895 pw.print(prefix); pw.print(" * Killed for ");
2896 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2897 pw.print("wake lock");
2898 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2899 pw.print("cpu");
2900 } else {
2901 pw.print("unknown");
2902 }
2903 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002904 TimeUtils.formatDuration(ew.usedTime, pw);
2905 pw.print(" over ");
2906 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002907 if (ew.overTime != 0) {
2908 pw.print(" (");
2909 pw.print((ew.usedTime*100)/ew.overTime);
2910 pw.println("%)");
2911 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002912 }
2913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 uidActivity = true;
2915 }
2916 }
2917 }
2918
2919 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2920 if (packageStats.size() > 0) {
2921 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2922 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002923 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 boolean apkActivity = false;
2925 Uid.Pkg ps = ent.getValue();
2926 int wakeups = ps.getWakeups(which);
2927 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002928 pw.print(prefix); pw.print(" ");
2929 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 apkActivity = true;
2931 }
2932 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2933 if (serviceStats.size() > 0) {
2934 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2935 : serviceStats.entrySet()) {
2936 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2937 long startTime = ss.getStartTime(batteryUptime, which);
2938 int starts = ss.getStarts(which);
2939 int launches = ss.getLaunches(which);
2940 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002941 sb.setLength(0);
2942 sb.append(prefix); sb.append(" Service ");
2943 sb.append(sent.getKey()); sb.append(":\n");
2944 sb.append(prefix); sb.append(" Created for: ");
2945 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002946 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002947 sb.append(prefix); sb.append(" Starts: ");
2948 sb.append(starts);
2949 sb.append(", launches: "); sb.append(launches);
2950 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 apkActivity = true;
2952 }
2953 }
2954 }
2955 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002956 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 }
2958 uidActivity = true;
2959 }
2960 }
2961 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002962 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 }
2964 }
2965 }
2966
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002967 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002968 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002969 int diff = oldval ^ newval;
2970 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002971 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002972 for (int i=0; i<descriptions.length; i++) {
2973 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002974 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002975 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002976 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002977 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002978 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002979 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2980 didWake = true;
2981 pw.print("=");
2982 if (longNames) {
2983 UserHandle.formatUid(pw, wakelockTag.uid);
2984 pw.print(":\"");
2985 pw.print(wakelockTag.string);
2986 pw.print("\"");
2987 } else {
2988 pw.print(wakelockTag.poolIdx);
2989 }
2990 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002991 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002992 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002993 pw.print("=");
2994 int val = (newval&bd.mask)>>bd.shift;
2995 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002996 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002997 } else {
2998 pw.print(val);
2999 }
3000 }
3001 }
3002 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003003 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07003004 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003005 if (longNames) {
3006 UserHandle.formatUid(pw, wakelockTag.uid);
3007 pw.print(":\"");
3008 pw.print(wakelockTag.string);
3009 pw.print("\"");
3010 } else {
3011 pw.print(wakelockTag.poolIdx);
3012 }
3013 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003014 }
3015
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003016 public void prepareForDumpLocked() {
3017 }
3018
3019 public static class HistoryPrinter {
3020 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003021 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003022 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003023 int oldStatus = -1;
3024 int oldHealth = -1;
3025 int oldPlug = -1;
3026 int oldTemp = -1;
3027 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003028 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003029 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003030
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003031 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003032 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003033 if (!checkin) {
3034 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003035 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003036 pw.print(" (");
3037 pw.print(rec.numReadInts);
3038 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003039 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003040 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3041 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003042 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003043 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003044 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003045 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003046 }
3047 lastTime = rec.time;
3048 }
3049 if (rec.cmd == HistoryItem.CMD_START) {
3050 if (checkin) {
3051 pw.print(":");
3052 }
3053 pw.println("START");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003054 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3055 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003056 if (checkin) {
3057 pw.print(":");
3058 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003059 if (rec.cmd == HistoryItem.CMD_RESET) {
3060 pw.print("RESET:");
3061 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003062 pw.print("TIME:");
3063 if (checkin) {
3064 pw.println(rec.currentTime);
3065 } else {
3066 pw.print(" ");
3067 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
3068 rec.currentTime).toString());
3069 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003070 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
3071 if (checkin) {
3072 pw.print(":");
3073 }
3074 pw.println("*OVERFLOW*");
3075 } else {
3076 if (!checkin) {
3077 if (rec.batteryLevel < 10) pw.print("00");
3078 else if (rec.batteryLevel < 100) pw.print("0");
3079 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003080 if (verbose) {
3081 pw.print(" ");
3082 if (rec.states < 0) ;
3083 else if (rec.states < 0x10) pw.print("0000000");
3084 else if (rec.states < 0x100) pw.print("000000");
3085 else if (rec.states < 0x1000) pw.print("00000");
3086 else if (rec.states < 0x10000) pw.print("0000");
3087 else if (rec.states < 0x100000) pw.print("000");
3088 else if (rec.states < 0x1000000) pw.print("00");
3089 else if (rec.states < 0x10000000) pw.print("0");
3090 pw.print(Integer.toHexString(rec.states));
3091 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003092 } else {
3093 if (oldLevel != rec.batteryLevel) {
3094 oldLevel = rec.batteryLevel;
3095 pw.print(",Bl="); pw.print(rec.batteryLevel);
3096 }
3097 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003098 if (oldStatus != rec.batteryStatus) {
3099 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003100 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003101 switch (oldStatus) {
3102 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003103 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003104 break;
3105 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003106 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003107 break;
3108 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003109 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003110 break;
3111 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003112 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003113 break;
3114 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003115 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003116 break;
3117 default:
3118 pw.print(oldStatus);
3119 break;
3120 }
3121 }
3122 if (oldHealth != rec.batteryHealth) {
3123 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003124 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003125 switch (oldHealth) {
3126 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003127 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003128 break;
3129 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003130 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003131 break;
3132 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003133 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003134 break;
3135 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003136 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003137 break;
3138 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003139 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003140 break;
3141 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003142 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003143 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003144 case BatteryManager.BATTERY_HEALTH_COLD:
3145 pw.print(checkin ? "c" : "cold");
3146 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003147 default:
3148 pw.print(oldHealth);
3149 break;
3150 }
3151 }
3152 if (oldPlug != rec.batteryPlugType) {
3153 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003154 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003155 switch (oldPlug) {
3156 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003157 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003158 break;
3159 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003160 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003161 break;
3162 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003163 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003164 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003165 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003166 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003167 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003168 default:
3169 pw.print(oldPlug);
3170 break;
3171 }
3172 }
3173 if (oldTemp != rec.batteryTemperature) {
3174 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003175 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003176 pw.print(oldTemp);
3177 }
3178 if (oldVolt != rec.batteryVoltage) {
3179 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003180 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003181 pw.print(oldVolt);
3182 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003183 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003184 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003185 printBitDescriptions(pw, oldState2, rec.states2, null,
3186 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003187 if (rec.wakeReasonTag != null) {
3188 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003189 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003190 pw.print(rec.wakeReasonTag.poolIdx);
3191 } else {
3192 pw.print(" wake_reason=");
3193 pw.print(rec.wakeReasonTag.uid);
3194 pw.print(":\"");
3195 pw.print(rec.wakeReasonTag.string);
3196 pw.print("\"");
3197 }
3198 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003199 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003200 pw.print(checkin ? "," : " ");
3201 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3202 pw.print("+");
3203 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3204 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003205 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003206 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3207 : HISTORY_EVENT_NAMES;
3208 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3209 | HistoryItem.EVENT_FLAG_FINISH);
3210 if (idx >= 0 && idx < eventNames.length) {
3211 pw.print(eventNames[idx]);
3212 } else {
3213 pw.print(checkin ? "Ev" : "event");
3214 pw.print(idx);
3215 }
3216 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003217 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003218 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003219 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003220 UserHandle.formatUid(pw, rec.eventTag.uid);
3221 pw.print(":\"");
3222 pw.print(rec.eventTag.string);
3223 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003224 }
3225 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003226 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003227 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003228 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003229 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003230 }
3231 }
3232
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003233 private void printSizeValue(PrintWriter pw, long size) {
3234 float result = size;
3235 String suffix = "";
3236 if (result >= 10*1024) {
3237 suffix = "KB";
3238 result = result / 1024;
3239 }
3240 if (result >= 10*1024) {
3241 suffix = "MB";
3242 result = result / 1024;
3243 }
3244 if (result >= 10*1024) {
3245 suffix = "GB";
3246 result = result / 1024;
3247 }
3248 if (result >= 10*1024) {
3249 suffix = "TB";
3250 result = result / 1024;
3251 }
3252 if (result >= 10*1024) {
3253 suffix = "PB";
3254 result = result / 1024;
3255 }
3256 pw.print((int)result);
3257 pw.print(suffix);
3258 }
3259
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003260 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3261 int count, boolean checkin) {
3262 if (count <= 0) {
3263 return false;
3264 }
3265 if (!checkin) {
3266 pw.println(header);
3267 }
3268 String[] lineArgs = new String[1];
3269 for (int i=0; i<count; i++) {
3270 if (checkin) {
3271 lineArgs[0] = Long.toString(steps[i]);
3272 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3273 } else {
3274 pw.print(" #"); pw.print(i); pw.print(": ");
3275 TimeUtils.formatDuration(steps[i], pw);
3276 pw.println();
3277 }
3278 }
3279 return true;
3280 }
3281
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003282 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3283 public static final int DUMP_CHARGED_ONLY = 1<<1;
3284 public static final int DUMP_HISTORY_ONLY = 1<<2;
3285 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003286 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003287
Dianne Hackborn37de0982014-05-09 09:32:18 -07003288 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
3289 final HistoryPrinter hprinter = new HistoryPrinter();
3290 final HistoryItem rec = new HistoryItem();
3291 long lastTime = -1;
3292 long baseTime = -1;
3293 boolean printed = false;
3294 HistoryEventTracker tracker = null;
3295 while (getNextHistoryLocked(rec)) {
3296 lastTime = rec.time;
3297 if (baseTime < 0) {
3298 baseTime = lastTime;
3299 }
3300 if (rec.time >= histStart) {
3301 if (histStart >= 0 && !printed) {
3302 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3303 || rec.cmd == HistoryItem.CMD_RESET) {
3304 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003305 hprinter.printNextItem(pw, rec, baseTime, checkin,
3306 (flags&DUMP_VERBOSE) != 0);
3307 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003308 } else if (rec.currentTime != 0) {
3309 printed = true;
3310 byte cmd = rec.cmd;
3311 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003312 hprinter.printNextItem(pw, rec, baseTime, checkin,
3313 (flags&DUMP_VERBOSE) != 0);
3314 rec.cmd = cmd;
3315 }
3316 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003317 if (rec.cmd != HistoryItem.CMD_UPDATE) {
3318 hprinter.printNextItem(pw, rec, baseTime, checkin,
3319 (flags&DUMP_VERBOSE) != 0);
3320 rec.cmd = HistoryItem.CMD_UPDATE;
3321 }
3322 int oldEventCode = rec.eventCode;
3323 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003324 rec.eventTag = new HistoryTag();
3325 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
3326 HashMap<String, SparseIntArray> active
3327 = tracker.getStateForEvent(i);
3328 if (active == null) {
3329 continue;
3330 }
3331 for (HashMap.Entry<String, SparseIntArray> ent
3332 : active.entrySet()) {
3333 SparseIntArray uids = ent.getValue();
3334 for (int j=0; j<uids.size(); j++) {
3335 rec.eventCode = i;
3336 rec.eventTag.string = ent.getKey();
3337 rec.eventTag.uid = uids.keyAt(j);
3338 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003339 hprinter.printNextItem(pw, rec, baseTime, checkin,
3340 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003341 rec.wakeReasonTag = null;
3342 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003343 }
3344 }
3345 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003346 rec.eventCode = oldEventCode;
3347 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003348 tracker = null;
3349 }
3350 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003351 hprinter.printNextItem(pw, rec, baseTime, checkin,
3352 (flags&DUMP_VERBOSE) != 0);
3353 } else if (rec.eventCode != HistoryItem.EVENT_NONE) {
3354 if (tracker == null) {
3355 tracker = new HistoryEventTracker();
3356 }
3357 tracker.updateState(rec.eventCode, rec.eventTag.string,
3358 rec.eventTag.uid, rec.eventTag.poolIdx);
3359 }
3360 }
3361 if (histStart >= 0) {
3362 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
3363 }
3364 }
3365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 /**
3367 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3368 *
3369 * @param pw a Printer to receive the dump output.
3370 */
3371 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003372 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003373 prepareForDumpLocked();
3374
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003375 final boolean filtering =
3376 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003377
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003378 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003379 final long historyTotalSize = getHistoryTotalSize();
3380 final long historyUsedSize = getHistoryUsedSize();
3381 if (startIteratingHistoryLocked()) {
3382 try {
3383 pw.print("Battery History (");
3384 pw.print((100*historyUsedSize)/historyTotalSize);
3385 pw.print("% used, ");
3386 printSizeValue(pw, historyUsedSize);
3387 pw.print(" used of ");
3388 printSizeValue(pw, historyTotalSize);
3389 pw.print(", ");
3390 pw.print(getHistoryStringPoolSize());
3391 pw.print(" strings using ");
3392 printSizeValue(pw, getHistoryStringPoolBytes());
3393 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003394 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003395 pw.println();
3396 } finally {
3397 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003398 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003399 }
3400
3401 if (startIteratingOldHistoryLocked()) {
3402 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003403 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003404 pw.println("Old battery History:");
3405 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003406 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003407 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003408 if (baseTime < 0) {
3409 baseTime = rec.time;
3410 }
3411 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003412 }
3413 pw.println();
3414 } finally {
3415 finishIteratingOldHistoryLocked();
3416 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003417 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003418 }
3419
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003420 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003421 return;
3422 }
3423
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003424 if (!filtering) {
3425 SparseArray<? extends Uid> uidStats = getUidStats();
3426 final int NU = uidStats.size();
3427 boolean didPid = false;
3428 long nowRealtime = SystemClock.elapsedRealtime();
3429 for (int i=0; i<NU; i++) {
3430 Uid uid = uidStats.valueAt(i);
3431 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3432 if (pids != null) {
3433 for (int j=0; j<pids.size(); j++) {
3434 Uid.Pid pid = pids.valueAt(j);
3435 if (!didPid) {
3436 pw.println("Per-PID Stats:");
3437 didPid = true;
3438 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003439 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3440 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003441 pw.print(" PID "); pw.print(pids.keyAt(j));
3442 pw.print(" wake time: ");
3443 TimeUtils.formatDuration(time, pw);
3444 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003445 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003446 }
3447 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003448 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003449 pw.println();
3450 }
3451 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3452 getNumDischargeStepDurations(), false)) {
3453 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3454 if (timeRemaining >= 0) {
3455 pw.print(" Estimated discharge time remaining: ");
3456 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3457 pw.println();
3458 }
3459 pw.println();
3460 }
3461 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3462 getNumChargeStepDurations(), false)) {
3463 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3464 if (timeRemaining >= 0) {
3465 pw.print(" Estimated charge time remaining: ");
3466 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3467 pw.println();
3468 }
3469 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003470 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003471 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003472
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003473 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003474 pw.println("Statistics since last charge:");
3475 pw.println(" System starts: " + getStartCount()
3476 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003477 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003478 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003479 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003480 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3481 pw.println("Statistics since last unplugged:");
3482 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485
3486 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003487 public void dumpCheckinLocked(Context context, PrintWriter pw,
3488 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003489 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003491 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3492
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003493 final boolean filtering =
3494 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3495
3496 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003497 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003498 try {
3499 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3500 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3501 pw.print(HISTORY_STRING_POOL); pw.print(',');
3502 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003503 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003504 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003505 pw.print(",\"");
3506 String str = getHistoryTagPoolString(i);
3507 str = str.replace("\\", "\\\\");
3508 str = str.replace("\"", "\\\"");
3509 pw.print(str);
3510 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003511 pw.println();
3512 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003513 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003514 } finally {
3515 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003516 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003517 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003518 }
3519
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003520 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003521 return;
3522 }
3523
Dianne Hackborne4a59512010-12-07 11:08:07 -08003524 if (apps != null) {
3525 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3526 for (int i=0; i<apps.size(); i++) {
3527 ApplicationInfo ai = apps.get(i);
3528 ArrayList<String> pkgs = uids.get(ai.uid);
3529 if (pkgs == null) {
3530 pkgs = new ArrayList<String>();
3531 uids.put(ai.uid, pkgs);
3532 }
3533 pkgs.add(ai.packageName);
3534 }
3535 SparseArray<? extends Uid> uidStats = getUidStats();
3536 final int NU = uidStats.size();
3537 String[] lineArgs = new String[2];
3538 for (int i=0; i<NU; i++) {
3539 int uid = uidStats.keyAt(i);
3540 ArrayList<String> pkgs = uids.get(uid);
3541 if (pkgs != null) {
3542 for (int j=0; j<pkgs.size(); j++) {
3543 lineArgs[0] = Integer.toString(uid);
3544 lineArgs[1] = pkgs.get(j);
3545 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3546 (Object[])lineArgs);
3547 }
3548 }
3549 }
3550 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003551 if (!filtering) {
3552 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
3553 getNumDischargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003554 String[] lineArgs = new String[1];
3555 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3556 if (timeRemaining >= 0) {
3557 lineArgs[0] = Long.toString(timeRemaining);
3558 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
3559 (Object[])lineArgs);
3560 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003561 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
3562 getNumChargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003563 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3564 if (timeRemaining >= 0) {
3565 lineArgs[0] = Long.toString(timeRemaining);
3566 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
3567 (Object[])lineArgs);
3568 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003569 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003570 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003571 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003572 }
3573 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003574 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 }
3576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577}