blob: af45fa06cb2ca6d3bbfa57a71f650c0d07feebb7 [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 Hackborn9a755432014-05-15 17:05:22 -0700534
535 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700536 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800537
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800538 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800539 public static final byte CMD_NULL = -1;
540 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800541 public static final byte CMD_CURRENT_TIME = 5;
542 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700543 public static final byte CMD_RESET = 7;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800544
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700545 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700546
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800547 /**
548 * Return whether the command code is a delta data update.
549 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800550 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800551 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800552 }
553
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700554 public byte batteryLevel;
555 public byte batteryStatus;
556 public byte batteryHealth;
557 public byte batteryPlugType;
558
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900559 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700560 public char batteryVoltage;
561
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700562 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700563 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800564 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700565 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800566 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
567 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700568 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800569 public static final int STATE_PHONE_STATE_SHIFT = 6;
570 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700571 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800572 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800573 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800574
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700575 // These states always appear directly in the first int token
576 // of a delta change; they should be ones that change relatively
577 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700578 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
579 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800580 public static final int STATE_GPS_ON_FLAG = 1<<29;
581 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800582 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800583 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800584 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800585 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700586 // These are on the lower bits used for the command; if they change
587 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700588 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700589 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700590 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700591 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
592 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
593 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
594 public static final int STATE_WIFI_ON_FLAG = 1<<17;
595 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700596
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700597 public static final int MOST_INTERESTING_STATES =
598 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
599 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
600
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700601 public int states;
602
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700603 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700604 public static final int STATE2_LOW_POWER_FLAG = 1<<1;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700605 public int states2;
606
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800607 // The wake lock that was acquired at this point.
608 public HistoryTag wakelockTag;
609
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800610 // Kernel wakeup reason at this point.
611 public HistoryTag wakeReasonTag;
612
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800613 public static final int EVENT_FLAG_START = 0x8000;
614 public static final int EVENT_FLAG_FINISH = 0x4000;
615
616 // No event in this item.
617 public static final int EVENT_NONE = 0x0000;
618 // Event is about a process that is running.
619 public static final int EVENT_PROC = 0x0001;
620 // Event is about an application package that is in the foreground.
621 public static final int EVENT_FOREGROUND = 0x0002;
622 // Event is about an application package that is at the top of the screen.
623 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800624 // Event is about an application package that is at the top of the screen.
625 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700626 // Events for all additional wake locks aquired/release within a wake block.
627 // These are not generated by default.
628 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800629 // Number of event types.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700630 public static final int EVENT_COUNT = 0x0006;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700631 // Mask to extract out only the type part of the event.
632 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800633
634 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
635 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
636 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
637 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
638 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
639 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800640 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
641 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700642 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
643 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800644
645 // For CMD_EVENT.
646 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800647 public HistoryTag eventTag;
648
Dianne Hackborn9a755432014-05-15 17:05:22 -0700649 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800650 public long currentTime;
651
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800652 // Meta-data when reading.
653 public int numReadInts;
654
655 // Pre-allocated objects.
656 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800657 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800658 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800659
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700660 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700661 }
662
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700663 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700664 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800665 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700666 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700667 }
668
669 public int describeContents() {
670 return 0;
671 }
672
673 public void writeToParcel(Parcel dest, int flags) {
674 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700675 int bat = (((int)cmd)&0xff)
676 | ((((int)batteryLevel)<<8)&0xff00)
677 | ((((int)batteryStatus)<<16)&0xf0000)
678 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800679 | ((((int)batteryPlugType)<<24)&0xf000000)
680 | (wakelockTag != null ? 0x10000000 : 0)
681 | (wakeReasonTag != null ? 0x20000000 : 0)
682 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700683 dest.writeInt(bat);
684 bat = (((int)batteryTemperature)&0xffff)
685 | ((((int)batteryVoltage)<<16)&0xffff0000);
686 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700687 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700688 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800689 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800690 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800691 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800692 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800693 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800694 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800695 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800696 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800697 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800698 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700699 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800700 dest.writeLong(currentTime);
701 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700702 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700703
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800704 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800705 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700706 int bat = src.readInt();
707 cmd = (byte)(bat&0xff);
708 batteryLevel = (byte)((bat>>8)&0xff);
709 batteryStatus = (byte)((bat>>16)&0xf);
710 batteryHealth = (byte)((bat>>20)&0xf);
711 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800712 int bat2 = src.readInt();
713 batteryTemperature = (short)(bat2&0xffff);
714 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700715 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700716 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800717 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800718 wakelockTag = localWakelockTag;
719 wakelockTag.readFromParcel(src);
720 } else {
721 wakelockTag = null;
722 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800723 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800724 wakeReasonTag = localWakeReasonTag;
725 wakeReasonTag.readFromParcel(src);
726 } else {
727 wakeReasonTag = null;
728 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800729 if ((bat&0x40000000) != 0) {
730 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800731 eventTag = localEventTag;
732 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800733 } else {
734 eventCode = EVENT_NONE;
735 eventTag = null;
736 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700737 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800738 currentTime = src.readLong();
739 } else {
740 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700741 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800742 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700743 }
744
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700745 public void clear() {
746 time = 0;
747 cmd = CMD_NULL;
748 batteryLevel = 0;
749 batteryStatus = 0;
750 batteryHealth = 0;
751 batteryPlugType = 0;
752 batteryTemperature = 0;
753 batteryVoltage = 0;
754 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700755 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800756 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800757 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800758 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800759 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700760 }
761
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700762 public void setTo(HistoryItem o) {
763 time = o.time;
764 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800765 setToCommon(o);
766 }
767
768 public void setTo(long time, byte cmd, HistoryItem o) {
769 this.time = time;
770 this.cmd = cmd;
771 setToCommon(o);
772 }
773
774 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700775 batteryLevel = o.batteryLevel;
776 batteryStatus = o.batteryStatus;
777 batteryHealth = o.batteryHealth;
778 batteryPlugType = o.batteryPlugType;
779 batteryTemperature = o.batteryTemperature;
780 batteryVoltage = o.batteryVoltage;
781 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700782 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800783 if (o.wakelockTag != null) {
784 wakelockTag = localWakelockTag;
785 wakelockTag.setTo(o.wakelockTag);
786 } else {
787 wakelockTag = null;
788 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800789 if (o.wakeReasonTag != null) {
790 wakeReasonTag = localWakeReasonTag;
791 wakeReasonTag.setTo(o.wakeReasonTag);
792 } else {
793 wakeReasonTag = null;
794 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800795 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800796 if (o.eventTag != null) {
797 eventTag = localEventTag;
798 eventTag.setTo(o.eventTag);
799 } else {
800 eventTag = null;
801 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800802 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700803 }
804
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800805 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700806 return batteryLevel == o.batteryLevel
807 && batteryStatus == o.batteryStatus
808 && batteryHealth == o.batteryHealth
809 && batteryPlugType == o.batteryPlugType
810 && batteryTemperature == o.batteryTemperature
811 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800812 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700813 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800814 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700815 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800816
817 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800818 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800819 return false;
820 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800821 if (wakelockTag != o.wakelockTag) {
822 if (wakelockTag == null || o.wakelockTag == null) {
823 return false;
824 }
825 if (!wakelockTag.equals(o.wakelockTag)) {
826 return false;
827 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800828 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800829 if (wakeReasonTag != o.wakeReasonTag) {
830 if (wakeReasonTag == null || o.wakeReasonTag == null) {
831 return false;
832 }
833 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
834 return false;
835 }
836 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800837 if (eventTag != o.eventTag) {
838 if (eventTag == null || o.eventTag == null) {
839 return false;
840 }
841 if (!eventTag.equals(o.eventTag)) {
842 return false;
843 }
844 }
845 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800846 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700847 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700848
849 public final static class HistoryEventTracker {
850 private final HashMap<String, SparseIntArray>[] mActiveEvents
851 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
852
853 public boolean updateState(int code, String name, int uid, int poolIdx) {
854 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
855 int idx = code&HistoryItem.EVENT_TYPE_MASK;
856 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
857 if (active == null) {
858 active = new HashMap<String, SparseIntArray>();
859 mActiveEvents[idx] = active;
860 }
861 SparseIntArray uids = active.get(name);
862 if (uids == null) {
863 uids = new SparseIntArray();
864 active.put(name, uids);
865 }
866 if (uids.indexOfKey(uid) >= 0) {
867 // Already set, nothing to do!
868 return false;
869 }
870 uids.put(uid, poolIdx);
871 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
872 int idx = code&HistoryItem.EVENT_TYPE_MASK;
873 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
874 if (active == null) {
875 // not currently active, nothing to do.
876 return false;
877 }
878 SparseIntArray uids = active.get(name);
879 if (uids == null) {
880 // not currently active, nothing to do.
881 return false;
882 }
883 idx = uids.indexOfKey(uid);
884 if (idx < 0) {
885 // not currently active, nothing to do.
886 return false;
887 }
888 uids.removeAt(idx);
889 if (uids.size() <= 0) {
890 active.remove(name);
891 }
892 }
893 return true;
894 }
895
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700896 public void removeEvents(int code) {
897 int idx = code&HistoryItem.EVENT_TYPE_MASK;
898 mActiveEvents[idx] = null;
899 }
900
Dianne Hackborn37de0982014-05-09 09:32:18 -0700901 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
902 return mActiveEvents[code];
903 }
904 }
905
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700906 public static final class BitDescription {
907 public final int mask;
908 public final int shift;
909 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800910 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700911 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800912 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700913
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800914 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700915 this.mask = mask;
916 this.shift = -1;
917 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800918 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700919 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800920 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700921 }
922
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800923 public BitDescription(int mask, int shift, String name, String shortName,
924 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700925 this.mask = mask;
926 this.shift = shift;
927 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800928 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700929 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800930 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700931 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700932 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700933
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800934 public abstract int getHistoryTotalSize();
935
936 public abstract int getHistoryUsedSize();
937
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700938 public abstract boolean startIteratingHistoryLocked();
939
Dianne Hackborn099bc622014-01-22 13:39:16 -0800940 public abstract int getHistoryStringPoolSize();
941
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800942 public abstract int getHistoryStringPoolBytes();
943
944 public abstract String getHistoryTagPoolString(int index);
945
946 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800947
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700948 public abstract boolean getNextHistoryLocked(HistoryItem out);
949
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700950 public abstract void finishIteratingHistoryLocked();
951
952 public abstract boolean startIteratingOldHistoryLocked();
953
954 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
955
956 public abstract void finishIteratingOldHistoryLocked();
957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700959 * Return the base time offset for the battery history.
960 */
961 public abstract long getHistoryBaseTime();
962
963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * Returns the number of times the device has been started.
965 */
966 public abstract int getStartCount();
967
968 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700969 * 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 -0800970 * running on battery.
971 *
972 * {@hide}
973 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800974 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800976 /**
977 * Returns the number of times the screen was turned on.
978 *
979 * {@hide}
980 */
981 public abstract int getScreenOnCount(int which);
982
Jeff Browne95c3cd2014-05-02 16:59:26 -0700983 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
984
Dianne Hackborn617f8772009-03-31 15:04:46 -0700985 public static final int SCREEN_BRIGHTNESS_DARK = 0;
986 public static final int SCREEN_BRIGHTNESS_DIM = 1;
987 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
988 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
989 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
990
991 static final String[] SCREEN_BRIGHTNESS_NAMES = {
992 "dark", "dim", "medium", "light", "bright"
993 };
994
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800995 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
996 "0", "1", "2", "3", "4"
997 };
998
Dianne Hackborn617f8772009-03-31 15:04:46 -0700999 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
1000
1001 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001002 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001003 * the given brightness
1004 *
1005 * {@hide}
1006 */
1007 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001008 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 /**
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001011 * Returns the time in microseconds that low power mode has been enabled while the device was
1012 * running on battery.
1013 *
1014 * {@hide}
1015 */
1016 public abstract long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which);
1017
1018 /**
1019 * Returns the number of times that low power mode was enabled.
1020 *
1021 * {@hide}
1022 */
1023 public abstract int getLowPowerModeEnabledCount(int which);
1024
1025 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001026 * 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 -08001027 * running on battery.
1028 *
1029 * {@hide}
1030 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001031 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001032
1033 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001034 * Returns the number of times a phone call was activated.
1035 *
1036 * {@hide}
1037 */
1038 public abstract int getPhoneOnCount(int which);
1039
1040 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001041 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001042 * the given signal strength.
1043 *
1044 * {@hide}
1045 */
1046 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001047 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001048
Dianne Hackborn617f8772009-03-31 15:04:46 -07001049 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001050 * Returns the time in microseconds that the phone has been trying to
1051 * acquire a signal.
1052 *
1053 * {@hide}
1054 */
1055 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001056 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001057
1058 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001059 * Returns the number of times the phone has entered the given signal strength.
1060 *
1061 * {@hide}
1062 */
1063 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1064
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001065 /**
1066 * Returns the time in microseconds that the mobile network has been active
1067 * (in a high power state).
1068 *
1069 * {@hide}
1070 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001071 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001072
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001073 /**
1074 * Returns the number of times that the mobile network has transitioned to the
1075 * active state.
1076 *
1077 * {@hide}
1078 */
1079 public abstract int getMobileRadioActiveCount(int which);
1080
1081 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001082 * Returns the time in microseconds that is the difference between the mobile radio
1083 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1084 * from the radio.
1085 *
1086 * {@hide}
1087 */
1088 public abstract long getMobileRadioActiveAdjustedTime(int which);
1089
1090 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001091 * Returns the time in microseconds that the mobile network has been active
1092 * (in a high power state) but not being able to blame on an app.
1093 *
1094 * {@hide}
1095 */
1096 public abstract long getMobileRadioActiveUnknownTime(int which);
1097
1098 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001099 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001100 *
1101 * {@hide}
1102 */
1103 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001104
Dianne Hackborn627bba72009-03-24 22:32:56 -07001105 public static final int DATA_CONNECTION_NONE = 0;
1106 public static final int DATA_CONNECTION_GPRS = 1;
1107 public static final int DATA_CONNECTION_EDGE = 2;
1108 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001109 public static final int DATA_CONNECTION_CDMA = 4;
1110 public static final int DATA_CONNECTION_EVDO_0 = 5;
1111 public static final int DATA_CONNECTION_EVDO_A = 6;
1112 public static final int DATA_CONNECTION_1xRTT = 7;
1113 public static final int DATA_CONNECTION_HSDPA = 8;
1114 public static final int DATA_CONNECTION_HSUPA = 9;
1115 public static final int DATA_CONNECTION_HSPA = 10;
1116 public static final int DATA_CONNECTION_IDEN = 11;
1117 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001118 public static final int DATA_CONNECTION_LTE = 13;
1119 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001120 public static final int DATA_CONNECTION_HSPAP = 15;
1121 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001122
Dianne Hackborn627bba72009-03-24 22:32:56 -07001123 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001124 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001125 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001126 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001127 };
1128
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001129 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001130
1131 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001132 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001133 * the given data connection.
1134 *
1135 * {@hide}
1136 */
1137 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001138 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001141 * Returns the number of times the phone has entered the given data
1142 * connection type.
1143 *
1144 * {@hide}
1145 */
1146 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001147
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001148 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1149 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001150 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001151 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1152 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001153 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001154 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1155 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1156 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001157 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001158 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1159 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001160 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001161 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1162 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1163 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1164 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1165 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1166 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1167 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1168 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1169 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1170 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1171 new String[] {"in", "out", "emergency", "off"},
1172 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001173 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001174 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1175 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1176 "0", "1", "2", "3", "4"
1177 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001178 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1179 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1180 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001181 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001182
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001183 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1184 = new BitDescription[] {
1185 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001186 new BitDescription(HistoryItem.STATE2_LOW_POWER_FLAG, "low_power", "lp"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001187 };
1188
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001189 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001190 "null", "proc", "fg", "top", "sync", "wake_lock_in"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001191 };
1192
1193 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001194 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001195 };
1196
Dianne Hackborn617f8772009-03-31 15:04:46 -07001197 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001198 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001199 * running on battery.
1200 *
1201 * {@hide}
1202 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001203 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001204
1205 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001206 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001207 * been in the running state while the device was running on battery.
1208 *
1209 * {@hide}
1210 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001211 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001212
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001213 public static final int WIFI_STATE_OFF = 0;
1214 public static final int WIFI_STATE_OFF_SCANNING = 1;
1215 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1216 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1217 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1218 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1219 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1220 public static final int WIFI_STATE_SOFT_AP = 7;
1221
1222 static final String[] WIFI_STATE_NAMES = {
1223 "off", "scanning", "no_net", "disconn",
1224 "sta", "p2p", "sta_p2p", "soft_ap"
1225 };
1226
1227 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1228
1229 /**
1230 * Returns the time in microseconds that WiFi has been running in the given state.
1231 *
1232 * {@hide}
1233 */
1234 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001235 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001236
1237 /**
1238 * Returns the number of times that WiFi has entered the given state.
1239 *
1240 * {@hide}
1241 */
1242 public abstract int getWifiStateCount(int wifiState, int which);
1243
The Android Open Source Project10592532009-03-18 17:39:46 -07001244 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001245 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001246 * running on battery.
1247 *
1248 * {@hide}
1249 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001250 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001251
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001252 public abstract int getBluetoothPingCount();
1253
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001254 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1255 public static final int BLUETOOTH_STATE_LOW = 1;
1256 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1257 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001258
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001259 static final String[] BLUETOOTH_STATE_NAMES = {
1260 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001261 };
1262
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001263 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001264
1265 /**
1266 * Returns the time in microseconds that Bluetooth has been running in the
1267 * given active state.
1268 *
1269 * {@hide}
1270 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001271 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001272 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001273
1274 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001275 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001276 *
1277 * {@hide}
1278 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001279 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001280
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001281 public static final int NETWORK_MOBILE_RX_DATA = 0;
1282 public static final int NETWORK_MOBILE_TX_DATA = 1;
1283 public static final int NETWORK_WIFI_RX_DATA = 2;
1284 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001285
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001286 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001287
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001288 public abstract long getNetworkActivityBytes(int type, int which);
1289 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001290
The Android Open Source Project10592532009-03-18 17:39:46 -07001291 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001292 * Return the wall clock time when battery stats data collection started.
1293 */
1294 public abstract long getStartClockTime();
1295
1296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 * Return whether we are currently running on battery.
1298 */
1299 public abstract boolean getIsOnBattery();
1300
1301 /**
1302 * Returns a SparseArray containing the statistics for each uid.
1303 */
1304 public abstract SparseArray<? extends Uid> getUidStats();
1305
1306 /**
1307 * Returns the current battery uptime in microseconds.
1308 *
1309 * @param curTime the amount of elapsed realtime in microseconds.
1310 */
1311 public abstract long getBatteryUptime(long curTime);
1312
1313 /**
1314 * Returns the current battery realtime in microseconds.
1315 *
1316 * @param curTime the amount of elapsed realtime in microseconds.
1317 */
1318 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001319
1320 /**
Evan Millar633a1742009-04-02 16:36:33 -07001321 * Returns the battery percentage level at the last time the device was unplugged from power, or
1322 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001323 */
Evan Millar633a1742009-04-02 16:36:33 -07001324 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001325
1326 /**
Evan Millar633a1742009-04-02 16:36:33 -07001327 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1328 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001329 */
Evan Millar633a1742009-04-02 16:36:33 -07001330 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001333 * Get the amount the battery has discharged since the stats were
1334 * last reset after charging, as a lower-end approximation.
1335 */
1336 public abstract int getLowDischargeAmountSinceCharge();
1337
1338 /**
1339 * Get the amount the battery has discharged since the stats were
1340 * last reset after charging, as an upper-end approximation.
1341 */
1342 public abstract int getHighDischargeAmountSinceCharge();
1343
1344 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001345 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1346 */
1347 public abstract int getDischargeAmount(int which);
1348
1349 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001350 * Get the amount the battery has discharged while the screen was on,
1351 * since the last time power was unplugged.
1352 */
1353 public abstract int getDischargeAmountScreenOn();
1354
1355 /**
1356 * Get the amount the battery has discharged while the screen was on,
1357 * since the last time the device was charged.
1358 */
1359 public abstract int getDischargeAmountScreenOnSinceCharge();
1360
1361 /**
1362 * Get the amount the battery has discharged while the screen was off,
1363 * since the last time power was unplugged.
1364 */
1365 public abstract int getDischargeAmountScreenOff();
1366
1367 /**
1368 * Get the amount the battery has discharged while the screen was off,
1369 * since the last time the device was charged.
1370 */
1371 public abstract int getDischargeAmountScreenOffSinceCharge();
1372
1373 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 * Returns the total, last, or current battery uptime in microseconds.
1375 *
1376 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001377 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 */
1379 public abstract long computeBatteryUptime(long curTime, int which);
1380
1381 /**
1382 * Returns the total, last, or current battery realtime in microseconds.
1383 *
1384 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001385 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 */
1387 public abstract long computeBatteryRealtime(long curTime, int which);
1388
1389 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 * Returns the total, last, or current battery screen off uptime in microseconds.
1391 *
1392 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001393 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001394 */
1395 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1396
1397 /**
1398 * Returns the total, last, or current battery screen off realtime in microseconds.
1399 *
1400 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001401 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001402 */
1403 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1404
1405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 * Returns the total, last, or current uptime in microseconds.
1407 *
1408 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001409 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 */
1411 public abstract long computeUptime(long curTime, int which);
1412
1413 /**
1414 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001417 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 */
1419 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001420
1421 /**
1422 * Compute an approximation for how much run time (in microseconds) is remaining on
1423 * the battery. Returns -1 if no time can be computed: either there is not
1424 * enough current data to make a decision, or the battery is currently
1425 * charging.
1426 *
1427 * @param curTime The current elepsed realtime in microseconds.
1428 */
1429 public abstract long computeBatteryTimeRemaining(long curTime);
1430
1431 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001432 * Return the historical number of discharge steps we currently have.
1433 */
1434 public abstract int getNumDischargeStepDurations();
1435
1436 /**
1437 * Return the array of discharge step durations; the number of valid
1438 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1439 * These values are in milliseconds.
1440 */
1441 public abstract long[] getDischargeStepDurationsArray();
1442
1443 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001444 * Compute an approximation for how much time (in microseconds) remains until the battery
1445 * is fully charged. Returns -1 if no time can be computed: either there is not
1446 * enough current data to make a decision, or the battery is currently
1447 * discharging.
1448 *
1449 * @param curTime The current elepsed realtime in microseconds.
1450 */
1451 public abstract long computeChargeTimeRemaining(long curTime);
1452
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001453 /**
1454 * Return the historical number of charge steps we currently have.
1455 */
1456 public abstract int getNumChargeStepDurations();
1457
1458 /**
1459 * Return the array of charge step durations; the number of valid
1460 * items in it is returned by {@link #getNumChargeStepDurations()}.
1461 * These values are in milliseconds.
1462 */
1463 public abstract long[] getChargeStepDurationsArray();
1464
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001465 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1466
Evan Millarc64edde2009-04-18 12:26:32 -07001467 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468
Amith Yamasanie43530a2009-08-21 13:11:37 -07001469 /** Returns the number of different speeds that the CPU can run at */
1470 public abstract int getCpuSpeedSteps();
1471
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001472 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1473
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001474 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 long days = seconds / (60 * 60 * 24);
1476 if (days != 0) {
1477 out.append(days);
1478 out.append("d ");
1479 }
1480 long used = days * 60 * 60 * 24;
1481
1482 long hours = (seconds - used) / (60 * 60);
1483 if (hours != 0 || used != 0) {
1484 out.append(hours);
1485 out.append("h ");
1486 }
1487 used += hours * 60 * 60;
1488
1489 long mins = (seconds-used) / 60;
1490 if (mins != 0 || used != 0) {
1491 out.append(mins);
1492 out.append("m ");
1493 }
1494 used += mins * 60;
1495
1496 if (seconds != 0 || used != 0) {
1497 out.append(seconds-used);
1498 out.append("s ");
1499 }
1500 }
1501
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001502 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001504 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 sb.append((time - (sec * 100)) * 10);
1506 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
1508
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001509 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001511 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 sb.append(time - (sec * 1000));
1513 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001516 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001517 long sec = time / 1000;
1518 formatTimeRaw(sb, sec);
1519 sb.append(time - (sec * 1000));
1520 sb.append("ms");
1521 }
1522
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001523 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001525 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 }
1527 float perc = ((float)num) / ((float)den) * 100;
1528 mFormatBuilder.setLength(0);
1529 mFormatter.format("%.1f%%", perc);
1530 return mFormatBuilder.toString();
1531 }
1532
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001533 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001534 mFormatBuilder.setLength(0);
1535
1536 if (bytes < BYTES_PER_KB) {
1537 return bytes + "B";
1538 } else if (bytes < BYTES_PER_MB) {
1539 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1540 return mFormatBuilder.toString();
1541 } else if (bytes < BYTES_PER_GB){
1542 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1543 return mFormatBuilder.toString();
1544 } else {
1545 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1546 return mFormatBuilder.toString();
1547 }
1548 }
1549
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001550 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001551 if (timer != null) {
1552 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001553 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001554 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1555 return totalTimeMillis;
1556 }
1557 return 0;
1558 }
1559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 /**
1561 *
1562 * @param sb a StringBuilder object.
1563 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001566 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 * @param linePrefix a String to be prepended to each line of output.
1568 * @return the line prefix
1569 */
1570 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001571 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572
1573 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001574 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575
Evan Millarc64edde2009-04-18 12:26:32 -07001576 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 if (totalTimeMillis != 0) {
1578 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001579 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001580 if (name != null) {
1581 sb.append(name);
1582 sb.append(' ');
1583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 sb.append('(');
1585 sb.append(count);
1586 sb.append(" times)");
1587 return ", ";
1588 }
1589 }
1590 return linePrefix;
1591 }
1592
1593 /**
1594 * Checkin version of wakelock printer. Prints simple comma-separated list.
1595 *
1596 * @param sb a StringBuilder object.
1597 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001598 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001600 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 * @param linePrefix a String to be prepended to each line of output.
1602 * @return the line prefix
1603 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001604 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1605 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 long totalTimeMicros = 0;
1607 int count = 0;
1608 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001609 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001610 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 sb.append(linePrefix);
1613 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1614 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001615 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 sb.append(count);
1617 return ",";
1618 }
1619
1620 /**
1621 * Dump a comma-separated line of values for terse checkin mode.
1622 *
1623 * @param pw the PageWriter to dump log to
1624 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1625 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1626 * @param args type-dependent data arguments
1627 */
1628 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1629 Object... args ) {
1630 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1631 pw.print(uid); pw.print(',');
1632 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001633 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634
1635 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001636 pw.print(',');
1637 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001639 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641
1642 /**
1643 * Checkin server version of dump to produce more compact, computer-readable log.
1644 *
1645 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001647 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1649 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1650 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1652 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001653 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1654 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1655 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 final long totalRealtime = computeRealtime(rawRealtime, which);
1657 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001658 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07001659 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001660 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001661 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1662 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1663 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1664 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 StringBuilder sb = new StringBuilder(128);
1667
Evan Millar22ac0432009-03-31 11:33:18 -07001668 SparseArray<? extends Uid> uidStats = getUidStats();
1669 final int NU = uidStats.size();
1670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 // Dump "battery" stat
1674 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001675 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001676 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001677 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 getStartClockTime(),
1679 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001681 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001682 long fullWakeLockTimeTotal = 0;
1683 long partialWakeLockTimeTotal = 0;
1684
1685 for (int iu = 0; iu < NU; iu++) {
1686 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001687
Evan Millar22ac0432009-03-31 11:33:18 -07001688 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1689 if (wakelocks.size() > 0) {
1690 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1691 : wakelocks.entrySet()) {
1692 Uid.Wakelock wl = ent.getValue();
1693
1694 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1695 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001696 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1697 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001698 }
1699
1700 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1701 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001702 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001703 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001704 }
1705 }
1706 }
1707 }
1708
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001709 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1710 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1711 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1712 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1713 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1714 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1715 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1716 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1717
1718 // Dump network stats
1719 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1720 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1721 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 // Dump misc stats
1724 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001725 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001726 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001727 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001728 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Jeff Browne95c3cd2014-05-02 16:59:26 -07001729 0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which),
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001730 getMobileRadioActiveAdjustedTime(which), interactiveTime / 1000,
1731 lowPowerModeEnabledTime / 1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001732
1733 // Dump screen brightness stats
1734 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1735 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001736 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001737 }
1738 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001739
Dianne Hackborn627bba72009-03-24 22:32:56 -07001740 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001741 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1742 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001743 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001744 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001745 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001746 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001747 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001748 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001749 args[i] = getPhoneSignalStrengthCount(i, which);
1750 }
1751 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001752
Dianne Hackborn627bba72009-03-24 22:32:56 -07001753 // Dump network type stats
1754 args = new Object[NUM_DATA_CONNECTION_TYPES];
1755 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001756 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001757 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001758 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1759 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1760 args[i] = getPhoneDataConnectionCount(i, which);
1761 }
1762 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001763
1764 // Dump wifi state stats
1765 args = new Object[NUM_WIFI_STATES];
1766 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001767 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001768 }
1769 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1770 for (int i=0; i<NUM_WIFI_STATES; i++) {
1771 args[i] = getWifiStateCount(i, which);
1772 }
1773 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1774
1775 // Dump bluetooth state stats
1776 args = new Object[NUM_BLUETOOTH_STATES];
1777 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001778 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001779 }
1780 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1781 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1782 args[i] = getBluetoothStateCount(i, which);
1783 }
1784 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1785
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001786 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001787 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001788 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001791 if (which == STATS_SINCE_UNPLUGGED) {
1792 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1793 getDischargeStartLevel()-getDischargeCurrentLevel(),
1794 getDischargeStartLevel()-getDischargeCurrentLevel(),
1795 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1796 } else {
1797 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1798 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1799 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1800 }
1801
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001802 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001803 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001804 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001805 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001806 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001807 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001808 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001809 sb.toString());
1810 }
Evan Millarc64edde2009-04-18 12:26:32 -07001811 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001812 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1813 if (wakeupReasons.size() > 0) {
1814 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1815 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1816 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1817 }
1818 }
Evan Millarc64edde2009-04-18 12:26:32 -07001819 }
1820
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001821 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001822 helper.create(this);
1823 helper.refreshStats(which, UserHandle.USER_ALL);
1824 List<BatterySipper> sippers = helper.getUsageList();
1825 if (sippers != null && sippers.size() > 0) {
1826 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1827 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001828 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001829 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1830 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1831 for (int i=0; i<sippers.size(); i++) {
1832 BatterySipper bs = sippers.get(i);
1833 int uid = 0;
1834 String label;
1835 switch (bs.drainType) {
1836 case IDLE:
1837 label="idle";
1838 break;
1839 case CELL:
1840 label="cell";
1841 break;
1842 case PHONE:
1843 label="phone";
1844 break;
1845 case WIFI:
1846 label="wifi";
1847 break;
1848 case BLUETOOTH:
1849 label="blue";
1850 break;
1851 case SCREEN:
1852 label="scrn";
1853 break;
1854 case APP:
1855 uid = bs.uidObj.getUid();
1856 label = "uid";
1857 break;
1858 case USER:
1859 uid = UserHandle.getUid(bs.userId, 0);
1860 label = "user";
1861 break;
1862 case UNACCOUNTED:
1863 label = "unacc";
1864 break;
1865 case OVERCOUNTED:
1866 label = "over";
1867 break;
1868 default:
1869 label = "???";
1870 }
1871 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1872 BatteryStatsHelper.makemAh(bs.value));
1873 }
1874 }
1875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 for (int iu = 0; iu < NU; iu++) {
1877 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001878 if (reqUid >= 0 && uid != reqUid) {
1879 continue;
1880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 Uid u = uidStats.valueAt(iu);
1882 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001883 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1884 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1885 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1886 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1887 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1888 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001889 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1890 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001891 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1892 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001893 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1894 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1895 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001896
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001897 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1898 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001899 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001900 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1901 wifiBytesRx, wifiBytesTx,
1902 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001903 wifiPacketsRx, wifiPacketsTx,
1904 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001905 }
1906
Nick Pelly6ccaa542012-06-15 15:22:47 -07001907 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001908 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001909 dumpLine(pw, uid, category, WIFI_DATA,
1910 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912
Dianne Hackborn617f8772009-03-31 15:04:46 -07001913 if (u.hasUserActivity()) {
1914 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1915 boolean hasData = false;
1916 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1917 int val = u.getUserActivityCount(i, which);
1918 args[i] = val;
1919 if (val != 0) hasData = true;
1920 }
1921 if (hasData) {
1922 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1923 }
1924 }
1925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1927 if (wakelocks.size() > 0) {
1928 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1929 : wakelocks.entrySet()) {
1930 Uid.Wakelock wl = ent.getValue();
1931 String linePrefix = "";
1932 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001933 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001934 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001935 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001936 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001937 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001938 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939
1940 // Only log if we had at lease one wakelock...
1941 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001942 String name = ent.getKey();
1943 if (name.indexOf(',') >= 0) {
1944 name = name.replace(',', '_');
1945 }
1946 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
1948 }
1949 }
1950
1951 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1952 if (sensors.size() > 0) {
1953 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1954 : sensors.entrySet()) {
1955 Uid.Sensor se = ent.getValue();
1956 int sensorNumber = ent.getKey();
1957 Timer timer = se.getSensorTime();
1958 if (timer != null) {
1959 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001960 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001961 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 if (totalTime != 0) {
1963 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1964 }
1965 }
1966 }
1967 }
1968
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001969 Timer vibTimer = u.getVibratorOnTimer();
1970 if (vibTimer != null) {
1971 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001972 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001973 int count = vibTimer.getCountLocked(which);
1974 if (totalTime != 0) {
1975 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1976 }
1977 }
1978
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001979 Timer fgTimer = u.getForegroundActivityTimer();
1980 if (fgTimer != null) {
1981 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001982 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001983 int count = fgTimer.getCountLocked(which);
1984 if (totalTime != 0) {
1985 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1986 }
1987 }
1988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1990 if (processStats.size() > 0) {
1991 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1992 : processStats.entrySet()) {
1993 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001994
1995 final long userMillis = ps.getUserTime(which) * 10;
1996 final long systemMillis = ps.getSystemTime(which) * 10;
1997 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1998 final long starts = ps.getStarts(which);
1999
2000 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2001 || starts != 0) {
2002 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
2003 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
2005 }
2006 }
2007
2008 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2009 if (packageStats.size() > 0) {
2010 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2011 : packageStats.entrySet()) {
2012
2013 Uid.Pkg ps = ent.getValue();
2014 int wakeups = ps.getWakeups(which);
2015 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2016 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2017 : serviceStats.entrySet()) {
2018 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2019 long startTime = ss.getStartTime(batteryUptime, which);
2020 int starts = ss.getStarts(which);
2021 int launches = ss.getLaunches(which);
2022 if (startTime != 0 || starts != 0 || launches != 0) {
2023 dumpLine(pw, uid, category, APK_DATA,
2024 wakeups, // wakeup alarms
2025 ent.getKey(), // Apk
2026 sent.getKey(), // service
2027 startTime / 1000, // time spent started, in ms
2028 starts,
2029 launches);
2030 }
2031 }
2032 }
2033 }
2034 }
2035 }
2036
Dianne Hackborn81038902012-11-26 17:04:09 -08002037 static final class TimerEntry {
2038 final String mName;
2039 final int mId;
2040 final BatteryStats.Timer mTimer;
2041 final long mTime;
2042 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2043 mName = name;
2044 mId = id;
2045 mTimer = timer;
2046 mTime = time;
2047 }
2048 }
2049
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002050 private void printmAh(PrintWriter printer, double power) {
2051 printer.print(BatteryStatsHelper.makemAh(power));
2052 }
2053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002055 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
2056 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2058 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2059 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060
2061 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2062 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2063 final long totalRealtime = computeRealtime(rawRealtime, which);
2064 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002065 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2066 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2067 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002068 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2069 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07002072
2073 SparseArray<? extends Uid> uidStats = getUidStats();
2074 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002076 sb.setLength(0);
2077 sb.append(prefix);
2078 sb.append(" Time on battery: ");
2079 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2080 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2081 sb.append(") realtime, ");
2082 formatTimeMs(sb, whichBatteryUptime / 1000);
2083 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2084 sb.append(") uptime");
2085 pw.println(sb.toString());
2086 sb.setLength(0);
2087 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002088 sb.append(" Time on battery screen off: ");
2089 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2090 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2091 sb.append(") realtime, ");
2092 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2093 sb.append("(");
2094 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2095 sb.append(") uptime");
2096 pw.println(sb.toString());
2097 sb.setLength(0);
2098 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002099 sb.append(" Total run time: ");
2100 formatTimeMs(sb, totalRealtime / 1000);
2101 sb.append("realtime, ");
2102 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002103 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07002104 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002105 if (batteryTimeRemaining >= 0) {
2106 sb.setLength(0);
2107 sb.append(prefix);
2108 sb.append(" Battery time remaining: ");
2109 formatTimeMs(sb, batteryTimeRemaining / 1000);
2110 pw.println(sb.toString());
2111 }
2112 if (chargeTimeRemaining >= 0) {
2113 sb.setLength(0);
2114 sb.append(prefix);
2115 sb.append(" Charge time remaining: ");
2116 formatTimeMs(sb, chargeTimeRemaining / 1000);
2117 pw.println(sb.toString());
2118 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002119 pw.print(" Start clock time: ");
2120 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2121
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002122 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002123 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002124 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002125 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2126 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2127 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2128 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002129 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002130 sb.append(prefix);
2131 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2132 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002133 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002134 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2135 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07002136 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002137 pw.println(sb.toString());
2138 sb.setLength(0);
2139 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002140 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002141 boolean didOne = false;
2142 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002143 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002144 if (time == 0) {
2145 continue;
2146 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002147 sb.append("\n ");
2148 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002149 didOne = true;
2150 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2151 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002152 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002153 sb.append("(");
2154 sb.append(formatRatioLocked(time, screenOnTime));
2155 sb.append(")");
2156 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002157 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002158 pw.println(sb.toString());
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002159 if (lowPowerModeEnabledTime != 0) {
2160 sb.setLength(0);
2161 sb.append(prefix);
2162 sb.append(" Low power mode enabled: ");
2163 formatTimeMs(sb, lowPowerModeEnabledTime / 1000);
2164 sb.append("(");
2165 sb.append(formatRatioLocked(lowPowerModeEnabledTime, whichBatteryRealtime));
2166 sb.append(")");
2167 pw.println(sb.toString());
2168 }
2169 if (phoneOnTime != 0) {
2170 sb.setLength(0);
2171 sb.append(prefix);
2172 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2173 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2174 sb.append(") "); sb.append(getPhoneOnCount(which));
2175 }
2176
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002177 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002178 long fullWakeLockTimeTotalMicros = 0;
2179 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002180
Dianne Hackborn81038902012-11-26 17:04:09 -08002181 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2182
Evan Millar22ac0432009-03-31 11:33:18 -07002183 for (int iu = 0; iu < NU; iu++) {
2184 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002185
Evan Millar22ac0432009-03-31 11:33:18 -07002186 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2187 if (wakelocks.size() > 0) {
2188 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2189 : wakelocks.entrySet()) {
2190 Uid.Wakelock wl = ent.getValue();
2191
2192 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2193 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002194 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002195 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002196 }
2197
2198 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2199 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002200 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002201 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002202 if (totalTimeMicros > 0) {
2203 if (reqUid < 0) {
2204 // Only show the ordered list of all wake
2205 // locks if the caller is not asking for data
2206 // about a specific uid.
2207 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2208 partialWakeTimer, totalTimeMicros));
2209 }
2210 partialWakeLockTimeTotalMicros += totalTimeMicros;
2211 }
Evan Millar22ac0432009-03-31 11:33:18 -07002212 }
2213 }
2214 }
2215 }
2216
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002217 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2218 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2219 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2220 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2221 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2222 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2223 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2224 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2225
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002226 if (fullWakeLockTimeTotalMicros != 0) {
2227 sb.setLength(0);
2228 sb.append(prefix);
2229 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2230 (fullWakeLockTimeTotalMicros + 500) / 1000);
2231 pw.println(sb.toString());
2232 }
2233
2234 if (partialWakeLockTimeTotalMicros != 0) {
2235 sb.setLength(0);
2236 sb.append(prefix);
2237 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2238 (partialWakeLockTimeTotalMicros + 500) / 1000);
2239 pw.println(sb.toString());
2240 }
2241
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002242 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002243 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2244 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2245 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2246 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002247 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002248 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002249 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002250 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002251 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002252 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002253 if (time == 0) {
2254 continue;
2255 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002256 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002257 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002258 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002259 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002260 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002261 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002262 sb.append("(");
2263 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002264 sb.append(") ");
2265 sb.append(getPhoneSignalStrengthCount(i, which));
2266 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002267 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002268 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002269 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002270
2271 sb.setLength(0);
2272 sb.append(prefix);
2273 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002274 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002275 pw.println(sb.toString());
2276
Dianne Hackborn627bba72009-03-24 22:32:56 -07002277 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002278 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002279 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002280 didOne = false;
2281 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002282 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002283 if (time == 0) {
2284 continue;
2285 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002286 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002287 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002288 didOne = true;
2289 sb.append(DATA_CONNECTION_NAMES[i]);
2290 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002291 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002292 sb.append("(");
2293 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002294 sb.append(") ");
2295 sb.append(getPhoneDataConnectionCount(i, which));
2296 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002297 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002298 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002299 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002300
2301 sb.setLength(0);
2302 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002303 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002304 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002305 formatTimeMs(sb, mobileActiveTime / 1000);
2306 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2307 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2308 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002309 pw.println(sb.toString());
2310
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002311 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2312 if (mobileActiveUnknownTime != 0) {
2313 sb.setLength(0);
2314 sb.append(prefix);
2315 sb.append(" Mobile radio active unknown time: ");
2316 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2317 sb.append("(");
2318 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2319 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2320 sb.append("x");
2321 pw.println(sb.toString());
2322 }
2323
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002324 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2325 if (mobileActiveAdjustedTime != 0) {
2326 sb.setLength(0);
2327 sb.append(prefix);
2328 sb.append(" Mobile radio active adjusted time: ");
2329 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2330 sb.append("(");
2331 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2332 sb.append(")");
2333 pw.println(sb.toString());
2334 }
2335
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002336 pw.print(prefix);
2337 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2338 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2339 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2340 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002341 sb.setLength(0);
2342 sb.append(prefix);
2343 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2344 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2345 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2346 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002347 sb.append(")");
2348 pw.println(sb.toString());
2349
2350 sb.setLength(0);
2351 sb.append(prefix);
2352 sb.append(" Wifi states:");
2353 didOne = false;
2354 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002355 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002356 if (time == 0) {
2357 continue;
2358 }
2359 sb.append("\n ");
2360 didOne = true;
2361 sb.append(WIFI_STATE_NAMES[i]);
2362 sb.append(" ");
2363 formatTimeMs(sb, time/1000);
2364 sb.append("(");
2365 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2366 sb.append(") ");
2367 sb.append(getPhoneDataConnectionCount(i, which));
2368 sb.append("x");
2369 }
2370 if (!didOne) sb.append(" (no activity)");
2371 pw.println(sb.toString());
2372
2373 sb.setLength(0);
2374 sb.append(prefix);
2375 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002376 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2377 sb.append(")");
2378 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002379
2380 sb.setLength(0);
2381 sb.append(prefix);
2382 sb.append(" Bluetooth states:");
2383 didOne = false;
2384 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002385 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002386 if (time == 0) {
2387 continue;
2388 }
2389 sb.append("\n ");
2390 didOne = true;
2391 sb.append(BLUETOOTH_STATE_NAMES[i]);
2392 sb.append(" ");
2393 formatTimeMs(sb, time/1000);
2394 sb.append("(");
2395 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2396 sb.append(") ");
2397 sb.append(getPhoneDataConnectionCount(i, which));
2398 sb.append("x");
2399 }
2400 if (!didOne) sb.append(" (no activity)");
2401 pw.println(sb.toString());
2402
2403 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002404
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002405 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002406 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002407 pw.print(prefix); pw.println(" Device is currently unplugged");
2408 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2409 pw.println(getDischargeStartLevel());
2410 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2411 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002412 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002413 pw.print(prefix); pw.println(" Device is currently plugged into power");
2414 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2415 pw.println(getDischargeStartLevel());
2416 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2417 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002418 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002419 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2420 pw.println(getDischargeAmountScreenOn());
2421 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2422 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002423 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002424 } else {
2425 pw.print(prefix); pw.println(" Device battery use since last full charge");
2426 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2427 pw.println(getLowDischargeAmountSinceCharge());
2428 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2429 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002430 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2431 pw.println(getDischargeAmountScreenOnSinceCharge());
2432 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2433 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002434 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002435 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002436
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002437 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002438 helper.create(this);
2439 helper.refreshStats(which, UserHandle.USER_ALL);
2440 List<BatterySipper> sippers = helper.getUsageList();
2441 if (sippers != null && sippers.size() > 0) {
2442 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2443 pw.print(prefix); pw.print(" Capacity: ");
2444 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002445 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002446 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2447 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2448 pw.println();
2449 for (int i=0; i<sippers.size(); i++) {
2450 BatterySipper bs = sippers.get(i);
2451 switch (bs.drainType) {
2452 case IDLE:
2453 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2454 pw.println();
2455 break;
2456 case CELL:
2457 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2458 pw.println();
2459 break;
2460 case PHONE:
2461 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2462 pw.println();
2463 break;
2464 case WIFI:
2465 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2466 pw.println();
2467 break;
2468 case BLUETOOTH:
2469 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2470 pw.println();
2471 break;
2472 case SCREEN:
2473 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2474 pw.println();
2475 break;
2476 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002477 pw.print(prefix); pw.print(" Uid ");
2478 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002479 pw.print(": "); printmAh(pw, bs.value); pw.println();
2480 break;
2481 case USER:
2482 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2483 pw.print(": "); printmAh(pw, bs.value); pw.println();
2484 break;
2485 case UNACCOUNTED:
2486 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2487 pw.println();
2488 break;
2489 case OVERCOUNTED:
2490 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2491 pw.println();
2492 break;
2493 }
2494 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002495 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002496 }
2497
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002498 sippers = helper.getMobilemsppList();
2499 if (sippers != null && sippers.size() > 0) {
2500 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002501 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002502 for (int i=0; i<sippers.size(); i++) {
2503 BatterySipper bs = sippers.get(i);
2504 sb.setLength(0);
2505 sb.append(prefix); sb.append(" Uid ");
2506 UserHandle.formatUid(sb, bs.uidObj.getUid());
2507 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2508 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2509 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002510 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002511 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002512 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002513 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002514 sb.setLength(0);
2515 sb.append(prefix);
2516 sb.append(" TOTAL TIME: ");
2517 formatTimeMs(sb, totalTime);
2518 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2519 sb.append(")");
2520 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002521 pw.println();
2522 }
2523
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002524 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2525 @Override
2526 public int compare(TimerEntry lhs, TimerEntry rhs) {
2527 long lhsTime = lhs.mTime;
2528 long rhsTime = rhs.mTime;
2529 if (lhsTime < rhsTime) {
2530 return 1;
2531 }
2532 if (lhsTime > rhsTime) {
2533 return -1;
2534 }
2535 return 0;
2536 }
2537 };
2538
2539 if (reqUid < 0) {
2540 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2541 if (kernelWakelocks.size() > 0) {
2542 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2543 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2544 BatteryStats.Timer timer = ent.getValue();
2545 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2546 if (totalTimeMillis > 0) {
2547 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2548 }
2549 }
2550 if (ktimers.size() > 0) {
2551 Collections.sort(ktimers, timerComparator);
2552 pw.print(prefix); pw.println(" All kernel wake locks:");
2553 for (int i=0; i<ktimers.size(); i++) {
2554 TimerEntry timer = ktimers.get(i);
2555 String linePrefix = ": ";
2556 sb.setLength(0);
2557 sb.append(prefix);
2558 sb.append(" Kernel Wake lock ");
2559 sb.append(timer.mName);
2560 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2561 which, linePrefix);
2562 if (!linePrefix.equals(": ")) {
2563 sb.append(" realtime");
2564 // Only print out wake locks that were held
2565 pw.println(sb.toString());
2566 }
2567 }
2568 pw.println();
2569 }
2570 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002571
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002572 if (timers.size() > 0) {
2573 Collections.sort(timers, timerComparator);
2574 pw.print(prefix); pw.println(" All partial wake locks:");
2575 for (int i=0; i<timers.size(); i++) {
2576 TimerEntry timer = timers.get(i);
2577 sb.setLength(0);
2578 sb.append(" Wake lock ");
2579 UserHandle.formatUid(sb, timer.mId);
2580 sb.append(" ");
2581 sb.append(timer.mName);
2582 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2583 sb.append(" realtime");
2584 pw.println(sb.toString());
2585 }
2586 timers.clear();
2587 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002588 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002589
2590 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2591 if (wakeupReasons.size() > 0) {
2592 pw.print(prefix); pw.println(" All wakeup reasons:");
2593 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2594 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2595 BatteryStats.LongCounter counter = ent.getValue();
2596 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2597 ent.getValue().getCountLocked(which)));
2598 }
2599 Collections.sort(reasons, timerComparator);
2600 for (int i=0; i<reasons.size(); i++) {
2601 TimerEntry timer = reasons.get(i);
2602 String linePrefix = ": ";
2603 sb.setLength(0);
2604 sb.append(prefix);
2605 sb.append(" Wakeup reason ");
2606 sb.append(timer.mName);
2607 sb.append(": ");
2608 formatTimeMs(sb, timer.mTime);
2609 sb.append("realtime");
2610 pw.println(sb.toString());
2611 }
2612 pw.println();
2613 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002614 }
Evan Millar22ac0432009-03-31 11:33:18 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 for (int iu=0; iu<NU; iu++) {
2617 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002618 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002619 continue;
2620 }
2621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002623
2624 pw.print(prefix);
2625 pw.print(" ");
2626 UserHandle.formatUid(pw, uid);
2627 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002629
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002630 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2631 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2632 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2633 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2634 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2635 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002636 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2637 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002638 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2639 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002640 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2641 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2642 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002643
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002644 if (mobileRxBytes > 0 || mobileTxBytes > 0
2645 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002646 pw.print(prefix); pw.print(" Mobile network: ");
2647 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002648 pw.print(formatBytesLocked(mobileTxBytes));
2649 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2650 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002652 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2653 sb.setLength(0);
2654 sb.append(prefix); sb.append(" Mobile radio active: ");
2655 formatTimeMs(sb, uidMobileActiveTime / 1000);
2656 sb.append("(");
2657 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2658 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2659 long packets = mobileRxPackets + mobileTxPackets;
2660 if (packets == 0) {
2661 packets = 1;
2662 }
2663 sb.append(" @ ");
2664 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2665 sb.append(" mspp");
2666 pw.println(sb.toString());
2667 }
2668
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002669 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002670 pw.print(prefix); pw.print(" Wi-Fi network: ");
2671 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002672 pw.print(formatBytesLocked(wifiTxBytes));
2673 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2674 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002675 }
2676
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002677 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2678 || uidWifiRunningTime != 0) {
2679 sb.setLength(0);
2680 sb.append(prefix); sb.append(" Wifi Running: ");
2681 formatTimeMs(sb, uidWifiRunningTime / 1000);
2682 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2683 whichBatteryRealtime)); sb.append(")\n");
2684 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2685 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2686 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2687 whichBatteryRealtime)); sb.append(")\n");
2688 sb.append(prefix); sb.append(" Wifi Scan: ");
2689 formatTimeMs(sb, wifiScanTime / 1000);
2690 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2691 whichBatteryRealtime)); sb.append(")");
2692 pw.println(sb.toString());
2693 }
2694
Dianne Hackborn617f8772009-03-31 15:04:46 -07002695 if (u.hasUserActivity()) {
2696 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002697 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002698 int val = u.getUserActivityCount(i, which);
2699 if (val != 0) {
2700 if (!hasData) {
2701 sb.setLength(0);
2702 sb.append(" User activity: ");
2703 hasData = true;
2704 } else {
2705 sb.append(", ");
2706 }
2707 sb.append(val);
2708 sb.append(" ");
2709 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2710 }
2711 }
2712 if (hasData) {
2713 pw.println(sb.toString());
2714 }
2715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
2717 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2718 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002719 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2720 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2722 : wakelocks.entrySet()) {
2723 Uid.Wakelock wl = ent.getValue();
2724 String linePrefix = ": ";
2725 sb.setLength(0);
2726 sb.append(prefix);
2727 sb.append(" Wake lock ");
2728 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002729 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002731 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002733 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 "window", which, linePrefix);
2735 if (!linePrefix.equals(": ")) {
2736 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002737 // Only print out wake locks that were held
2738 pw.println(sb.toString());
2739 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002740 count++;
2741 }
2742 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002743 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002744 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002745 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002746 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002747 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002748 }
2749 if (count > 1) {
2750 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2751 sb.setLength(0);
2752 sb.append(prefix);
2753 sb.append(" TOTAL wake: ");
2754 boolean needComma = false;
2755 if (totalFull != 0) {
2756 needComma = true;
2757 formatTimeMs(sb, totalFull);
2758 sb.append("full");
2759 }
2760 if (totalPartial != 0) {
2761 if (needComma) {
2762 sb.append(", ");
2763 }
2764 needComma = true;
2765 formatTimeMs(sb, totalPartial);
2766 sb.append("partial");
2767 }
2768 if (totalWindow != 0) {
2769 if (needComma) {
2770 sb.append(", ");
2771 }
2772 needComma = true;
2773 formatTimeMs(sb, totalWindow);
2774 sb.append("window");
2775 }
2776 sb.append(" realtime");
2777 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
2780 }
2781
2782 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2783 if (sensors.size() > 0) {
2784 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2785 : sensors.entrySet()) {
2786 Uid.Sensor se = ent.getValue();
2787 int sensorNumber = ent.getKey();
2788 sb.setLength(0);
2789 sb.append(prefix);
2790 sb.append(" Sensor ");
2791 int handle = se.getHandle();
2792 if (handle == Uid.Sensor.GPS) {
2793 sb.append("GPS");
2794 } else {
2795 sb.append(handle);
2796 }
2797 sb.append(": ");
2798
2799 Timer timer = se.getSensorTime();
2800 if (timer != null) {
2801 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002802 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002803 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002804 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 //timer.logState();
2806 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002807 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 sb.append("realtime (");
2809 sb.append(count);
2810 sb.append(" times)");
2811 } else {
2812 sb.append("(not used)");
2813 }
2814 } else {
2815 sb.append("(not used)");
2816 }
2817
2818 pw.println(sb.toString());
2819 uidActivity = true;
2820 }
2821 }
2822
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002823 Timer vibTimer = u.getVibratorOnTimer();
2824 if (vibTimer != null) {
2825 // Convert from microseconds to milliseconds with rounding
2826 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002827 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002828 int count = vibTimer.getCountLocked(which);
2829 //timer.logState();
2830 if (totalTime != 0) {
2831 sb.setLength(0);
2832 sb.append(prefix);
2833 sb.append(" Vibrator: ");
2834 formatTimeMs(sb, totalTime);
2835 sb.append("realtime (");
2836 sb.append(count);
2837 sb.append(" times)");
2838 pw.println(sb.toString());
2839 uidActivity = true;
2840 }
2841 }
2842
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002843 Timer fgTimer = u.getForegroundActivityTimer();
2844 if (fgTimer != null) {
2845 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002846 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002847 int count = fgTimer.getCountLocked(which);
2848 if (totalTime != 0) {
2849 sb.setLength(0);
2850 sb.append(prefix);
2851 sb.append(" Foreground activities: ");
2852 formatTimeMs(sb, totalTime);
2853 sb.append("realtime (");
2854 sb.append(count);
2855 sb.append(" times)");
2856 pw.println(sb.toString());
2857 uidActivity = true;
2858 }
2859 }
2860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2862 if (processStats.size() > 0) {
2863 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2864 : processStats.entrySet()) {
2865 Uid.Proc ps = ent.getValue();
2866 long userTime;
2867 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002868 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002870 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
2872 userTime = ps.getUserTime(which);
2873 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002874 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002876 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002877 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002879 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002880 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002881 sb.setLength(0);
2882 sb.append(prefix); sb.append(" Proc ");
2883 sb.append(ent.getKey()); sb.append(":\n");
2884 sb.append(prefix); sb.append(" CPU: ");
2885 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002886 formatTime(sb, systemTime); sb.append("krn ; ");
2887 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002888 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002889 sb.append("\n"); sb.append(prefix); sb.append(" ");
2890 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002891 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002892 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002893 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002894 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002895 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002896 pw.print(prefix); pw.print(" * Killed for ");
2897 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2898 pw.print("wake lock");
2899 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2900 pw.print("cpu");
2901 } else {
2902 pw.print("unknown");
2903 }
2904 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002905 TimeUtils.formatDuration(ew.usedTime, pw);
2906 pw.print(" over ");
2907 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002908 if (ew.overTime != 0) {
2909 pw.print(" (");
2910 pw.print((ew.usedTime*100)/ew.overTime);
2911 pw.println("%)");
2912 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002913 }
2914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 uidActivity = true;
2916 }
2917 }
2918 }
2919
2920 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2921 if (packageStats.size() > 0) {
2922 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2923 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002924 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 boolean apkActivity = false;
2926 Uid.Pkg ps = ent.getValue();
2927 int wakeups = ps.getWakeups(which);
2928 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002929 pw.print(prefix); pw.print(" ");
2930 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 apkActivity = true;
2932 }
2933 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2934 if (serviceStats.size() > 0) {
2935 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2936 : serviceStats.entrySet()) {
2937 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2938 long startTime = ss.getStartTime(batteryUptime, which);
2939 int starts = ss.getStarts(which);
2940 int launches = ss.getLaunches(which);
2941 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002942 sb.setLength(0);
2943 sb.append(prefix); sb.append(" Service ");
2944 sb.append(sent.getKey()); sb.append(":\n");
2945 sb.append(prefix); sb.append(" Created for: ");
2946 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002947 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002948 sb.append(prefix); sb.append(" Starts: ");
2949 sb.append(starts);
2950 sb.append(", launches: "); sb.append(launches);
2951 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 apkActivity = true;
2953 }
2954 }
2955 }
2956 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002957 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959 uidActivity = true;
2960 }
2961 }
2962 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002963 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965 }
2966 }
2967
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002968 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002969 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002970 int diff = oldval ^ newval;
2971 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002972 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002973 for (int i=0; i<descriptions.length; i++) {
2974 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002975 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002976 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002977 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002978 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002979 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002980 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2981 didWake = true;
2982 pw.print("=");
2983 if (longNames) {
2984 UserHandle.formatUid(pw, wakelockTag.uid);
2985 pw.print(":\"");
2986 pw.print(wakelockTag.string);
2987 pw.print("\"");
2988 } else {
2989 pw.print(wakelockTag.poolIdx);
2990 }
2991 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002992 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002993 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002994 pw.print("=");
2995 int val = (newval&bd.mask)>>bd.shift;
2996 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002997 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002998 } else {
2999 pw.print(val);
3000 }
3001 }
3002 }
3003 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003004 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07003005 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003006 if (longNames) {
3007 UserHandle.formatUid(pw, wakelockTag.uid);
3008 pw.print(":\"");
3009 pw.print(wakelockTag.string);
3010 pw.print("\"");
3011 } else {
3012 pw.print(wakelockTag.poolIdx);
3013 }
3014 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003015 }
3016
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003017 public void prepareForDumpLocked() {
3018 }
3019
3020 public static class HistoryPrinter {
3021 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003022 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003023 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003024 int oldStatus = -1;
3025 int oldHealth = -1;
3026 int oldPlug = -1;
3027 int oldTemp = -1;
3028 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003029 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003030 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003031
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003032 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003033 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003034 if (!checkin) {
3035 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003036 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003037 pw.print(" (");
3038 pw.print(rec.numReadInts);
3039 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003040 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003041 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3042 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003043 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003044 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003045 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003046 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003047 }
3048 lastTime = rec.time;
3049 }
3050 if (rec.cmd == HistoryItem.CMD_START) {
3051 if (checkin) {
3052 pw.print(":");
3053 }
3054 pw.println("START");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003055 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3056 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003057 if (checkin) {
3058 pw.print(":");
3059 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003060 if (rec.cmd == HistoryItem.CMD_RESET) {
3061 pw.print("RESET:");
3062 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003063 pw.print("TIME:");
3064 if (checkin) {
3065 pw.println(rec.currentTime);
3066 } else {
3067 pw.print(" ");
3068 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
3069 rec.currentTime).toString());
3070 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003071 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
3072 if (checkin) {
3073 pw.print(":");
3074 }
3075 pw.println("*OVERFLOW*");
3076 } else {
3077 if (!checkin) {
3078 if (rec.batteryLevel < 10) pw.print("00");
3079 else if (rec.batteryLevel < 100) pw.print("0");
3080 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003081 if (verbose) {
3082 pw.print(" ");
3083 if (rec.states < 0) ;
3084 else if (rec.states < 0x10) pw.print("0000000");
3085 else if (rec.states < 0x100) pw.print("000000");
3086 else if (rec.states < 0x1000) pw.print("00000");
3087 else if (rec.states < 0x10000) pw.print("0000");
3088 else if (rec.states < 0x100000) pw.print("000");
3089 else if (rec.states < 0x1000000) pw.print("00");
3090 else if (rec.states < 0x10000000) pw.print("0");
3091 pw.print(Integer.toHexString(rec.states));
3092 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003093 } else {
3094 if (oldLevel != rec.batteryLevel) {
3095 oldLevel = rec.batteryLevel;
3096 pw.print(",Bl="); pw.print(rec.batteryLevel);
3097 }
3098 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003099 if (oldStatus != rec.batteryStatus) {
3100 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003101 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003102 switch (oldStatus) {
3103 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003104 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003105 break;
3106 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003107 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003108 break;
3109 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003110 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003111 break;
3112 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003113 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003114 break;
3115 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003116 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003117 break;
3118 default:
3119 pw.print(oldStatus);
3120 break;
3121 }
3122 }
3123 if (oldHealth != rec.batteryHealth) {
3124 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003125 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003126 switch (oldHealth) {
3127 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003128 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003129 break;
3130 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003131 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003132 break;
3133 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003134 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003135 break;
3136 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003137 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003138 break;
3139 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003140 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003141 break;
3142 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003143 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003144 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003145 case BatteryManager.BATTERY_HEALTH_COLD:
3146 pw.print(checkin ? "c" : "cold");
3147 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003148 default:
3149 pw.print(oldHealth);
3150 break;
3151 }
3152 }
3153 if (oldPlug != rec.batteryPlugType) {
3154 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003155 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003156 switch (oldPlug) {
3157 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003158 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003159 break;
3160 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003161 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003162 break;
3163 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003164 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003165 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003166 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003167 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003168 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003169 default:
3170 pw.print(oldPlug);
3171 break;
3172 }
3173 }
3174 if (oldTemp != rec.batteryTemperature) {
3175 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003176 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003177 pw.print(oldTemp);
3178 }
3179 if (oldVolt != rec.batteryVoltage) {
3180 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003181 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003182 pw.print(oldVolt);
3183 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003184 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003185 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003186 printBitDescriptions(pw, oldState2, rec.states2, null,
3187 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003188 if (rec.wakeReasonTag != null) {
3189 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003190 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003191 pw.print(rec.wakeReasonTag.poolIdx);
3192 } else {
3193 pw.print(" wake_reason=");
3194 pw.print(rec.wakeReasonTag.uid);
3195 pw.print(":\"");
3196 pw.print(rec.wakeReasonTag.string);
3197 pw.print("\"");
3198 }
3199 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003200 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003201 pw.print(checkin ? "," : " ");
3202 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3203 pw.print("+");
3204 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3205 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003206 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003207 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3208 : HISTORY_EVENT_NAMES;
3209 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3210 | HistoryItem.EVENT_FLAG_FINISH);
3211 if (idx >= 0 && idx < eventNames.length) {
3212 pw.print(eventNames[idx]);
3213 } else {
3214 pw.print(checkin ? "Ev" : "event");
3215 pw.print(idx);
3216 }
3217 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003218 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003219 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003220 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003221 UserHandle.formatUid(pw, rec.eventTag.uid);
3222 pw.print(":\"");
3223 pw.print(rec.eventTag.string);
3224 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003225 }
3226 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003227 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003228 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003229 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003230 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003231 }
3232 }
3233
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003234 private void printSizeValue(PrintWriter pw, long size) {
3235 float result = size;
3236 String suffix = "";
3237 if (result >= 10*1024) {
3238 suffix = "KB";
3239 result = result / 1024;
3240 }
3241 if (result >= 10*1024) {
3242 suffix = "MB";
3243 result = result / 1024;
3244 }
3245 if (result >= 10*1024) {
3246 suffix = "GB";
3247 result = result / 1024;
3248 }
3249 if (result >= 10*1024) {
3250 suffix = "TB";
3251 result = result / 1024;
3252 }
3253 if (result >= 10*1024) {
3254 suffix = "PB";
3255 result = result / 1024;
3256 }
3257 pw.print((int)result);
3258 pw.print(suffix);
3259 }
3260
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003261 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3262 int count, boolean checkin) {
3263 if (count <= 0) {
3264 return false;
3265 }
3266 if (!checkin) {
3267 pw.println(header);
3268 }
3269 String[] lineArgs = new String[1];
3270 for (int i=0; i<count; i++) {
3271 if (checkin) {
3272 lineArgs[0] = Long.toString(steps[i]);
3273 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3274 } else {
3275 pw.print(" #"); pw.print(i); pw.print(": ");
3276 TimeUtils.formatDuration(steps[i], pw);
3277 pw.println();
3278 }
3279 }
3280 return true;
3281 }
3282
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003283 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3284 public static final int DUMP_CHARGED_ONLY = 1<<1;
3285 public static final int DUMP_HISTORY_ONLY = 1<<2;
3286 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003287 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003288
Dianne Hackborn37de0982014-05-09 09:32:18 -07003289 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
3290 final HistoryPrinter hprinter = new HistoryPrinter();
3291 final HistoryItem rec = new HistoryItem();
3292 long lastTime = -1;
3293 long baseTime = -1;
3294 boolean printed = false;
3295 HistoryEventTracker tracker = null;
3296 while (getNextHistoryLocked(rec)) {
3297 lastTime = rec.time;
3298 if (baseTime < 0) {
3299 baseTime = lastTime;
3300 }
3301 if (rec.time >= histStart) {
3302 if (histStart >= 0 && !printed) {
3303 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3304 || rec.cmd == HistoryItem.CMD_RESET) {
3305 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003306 hprinter.printNextItem(pw, rec, baseTime, checkin,
3307 (flags&DUMP_VERBOSE) != 0);
3308 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003309 } else if (rec.currentTime != 0) {
3310 printed = true;
3311 byte cmd = rec.cmd;
3312 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003313 hprinter.printNextItem(pw, rec, baseTime, checkin,
3314 (flags&DUMP_VERBOSE) != 0);
3315 rec.cmd = cmd;
3316 }
3317 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003318 if (rec.cmd != HistoryItem.CMD_UPDATE) {
3319 hprinter.printNextItem(pw, rec, baseTime, checkin,
3320 (flags&DUMP_VERBOSE) != 0);
3321 rec.cmd = HistoryItem.CMD_UPDATE;
3322 }
3323 int oldEventCode = rec.eventCode;
3324 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003325 rec.eventTag = new HistoryTag();
3326 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
3327 HashMap<String, SparseIntArray> active
3328 = tracker.getStateForEvent(i);
3329 if (active == null) {
3330 continue;
3331 }
3332 for (HashMap.Entry<String, SparseIntArray> ent
3333 : active.entrySet()) {
3334 SparseIntArray uids = ent.getValue();
3335 for (int j=0; j<uids.size(); j++) {
3336 rec.eventCode = i;
3337 rec.eventTag.string = ent.getKey();
3338 rec.eventTag.uid = uids.keyAt(j);
3339 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003340 hprinter.printNextItem(pw, rec, baseTime, checkin,
3341 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003342 rec.wakeReasonTag = null;
3343 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003344 }
3345 }
3346 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003347 rec.eventCode = oldEventCode;
3348 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003349 tracker = null;
3350 }
3351 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003352 hprinter.printNextItem(pw, rec, baseTime, checkin,
3353 (flags&DUMP_VERBOSE) != 0);
3354 } else if (rec.eventCode != HistoryItem.EVENT_NONE) {
3355 if (tracker == null) {
3356 tracker = new HistoryEventTracker();
3357 }
3358 tracker.updateState(rec.eventCode, rec.eventTag.string,
3359 rec.eventTag.uid, rec.eventTag.poolIdx);
3360 }
3361 }
3362 if (histStart >= 0) {
3363 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
3364 }
3365 }
3366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 /**
3368 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3369 *
3370 * @param pw a Printer to receive the dump output.
3371 */
3372 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003373 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003374 prepareForDumpLocked();
3375
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003376 final boolean filtering =
3377 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003378
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003379 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003380 final long historyTotalSize = getHistoryTotalSize();
3381 final long historyUsedSize = getHistoryUsedSize();
3382 if (startIteratingHistoryLocked()) {
3383 try {
3384 pw.print("Battery History (");
3385 pw.print((100*historyUsedSize)/historyTotalSize);
3386 pw.print("% used, ");
3387 printSizeValue(pw, historyUsedSize);
3388 pw.print(" used of ");
3389 printSizeValue(pw, historyTotalSize);
3390 pw.print(", ");
3391 pw.print(getHistoryStringPoolSize());
3392 pw.print(" strings using ");
3393 printSizeValue(pw, getHistoryStringPoolBytes());
3394 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003395 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003396 pw.println();
3397 } finally {
3398 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003399 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003400 }
3401
3402 if (startIteratingOldHistoryLocked()) {
3403 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003404 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003405 pw.println("Old battery History:");
3406 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003407 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003408 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003409 if (baseTime < 0) {
3410 baseTime = rec.time;
3411 }
3412 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003413 }
3414 pw.println();
3415 } finally {
3416 finishIteratingOldHistoryLocked();
3417 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003418 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003419 }
3420
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003421 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003422 return;
3423 }
3424
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003425 if (!filtering) {
3426 SparseArray<? extends Uid> uidStats = getUidStats();
3427 final int NU = uidStats.size();
3428 boolean didPid = false;
3429 long nowRealtime = SystemClock.elapsedRealtime();
3430 for (int i=0; i<NU; i++) {
3431 Uid uid = uidStats.valueAt(i);
3432 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3433 if (pids != null) {
3434 for (int j=0; j<pids.size(); j++) {
3435 Uid.Pid pid = pids.valueAt(j);
3436 if (!didPid) {
3437 pw.println("Per-PID Stats:");
3438 didPid = true;
3439 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003440 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3441 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003442 pw.print(" PID "); pw.print(pids.keyAt(j));
3443 pw.print(" wake time: ");
3444 TimeUtils.formatDuration(time, pw);
3445 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003446 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003447 }
3448 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003449 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003450 pw.println();
3451 }
3452 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3453 getNumDischargeStepDurations(), false)) {
3454 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3455 if (timeRemaining >= 0) {
3456 pw.print(" Estimated discharge time remaining: ");
3457 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3458 pw.println();
3459 }
3460 pw.println();
3461 }
3462 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3463 getNumChargeStepDurations(), false)) {
3464 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3465 if (timeRemaining >= 0) {
3466 pw.print(" Estimated charge time remaining: ");
3467 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3468 pw.println();
3469 }
3470 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003471 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003472 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003473
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003474 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003475 pw.println("Statistics since last charge:");
3476 pw.println(" System starts: " + getStartCount()
3477 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003478 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003479 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003480 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003481 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3482 pw.println("Statistics since last unplugged:");
3483 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 }
3486
3487 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003488 public void dumpCheckinLocked(Context context, PrintWriter pw,
3489 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003490 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003492 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3493
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003494 final boolean filtering =
3495 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3496
3497 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003498 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003499 try {
3500 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3501 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3502 pw.print(HISTORY_STRING_POOL); pw.print(',');
3503 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003504 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003505 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003506 pw.print(",\"");
3507 String str = getHistoryTagPoolString(i);
3508 str = str.replace("\\", "\\\\");
3509 str = str.replace("\"", "\\\"");
3510 pw.print(str);
3511 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003512 pw.println();
3513 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003514 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003515 } finally {
3516 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003517 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003518 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003519 }
3520
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003521 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003522 return;
3523 }
3524
Dianne Hackborne4a59512010-12-07 11:08:07 -08003525 if (apps != null) {
3526 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3527 for (int i=0; i<apps.size(); i++) {
3528 ApplicationInfo ai = apps.get(i);
3529 ArrayList<String> pkgs = uids.get(ai.uid);
3530 if (pkgs == null) {
3531 pkgs = new ArrayList<String>();
3532 uids.put(ai.uid, pkgs);
3533 }
3534 pkgs.add(ai.packageName);
3535 }
3536 SparseArray<? extends Uid> uidStats = getUidStats();
3537 final int NU = uidStats.size();
3538 String[] lineArgs = new String[2];
3539 for (int i=0; i<NU; i++) {
3540 int uid = uidStats.keyAt(i);
3541 ArrayList<String> pkgs = uids.get(uid);
3542 if (pkgs != null) {
3543 for (int j=0; j<pkgs.size(); j++) {
3544 lineArgs[0] = Integer.toString(uid);
3545 lineArgs[1] = pkgs.get(j);
3546 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3547 (Object[])lineArgs);
3548 }
3549 }
3550 }
3551 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003552 if (!filtering) {
3553 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
3554 getNumDischargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003555 String[] lineArgs = new String[1];
3556 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3557 if (timeRemaining >= 0) {
3558 lineArgs[0] = Long.toString(timeRemaining);
3559 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
3560 (Object[])lineArgs);
3561 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003562 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
3563 getNumChargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003564 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3565 if (timeRemaining >= 0) {
3566 lineArgs[0] = Long.toString(timeRemaining);
3567 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
3568 (Object[])lineArgs);
3569 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003570 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003571 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003572 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003573 }
3574 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003575 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 }
3577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578}