blob: 21d60c566013c080211e189a8746bf62480431a9 [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";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700174 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
175 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
176 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
177 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800178 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
179 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800180 private static final String POWER_USE_SUMMARY_DATA = "pws";
181 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700182 private static final String DISCHARGE_STEP_DATA = "dsd";
183 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700184 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
185 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700187 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 private final Formatter mFormatter = new Formatter(mFormatBuilder);
189
190 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700191 * State for keeping track of counting information.
192 */
193 public static abstract class Counter {
194
195 /**
196 * Returns the count associated with this Counter for the
197 * selected type of statistics.
198 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700199 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700200 */
Evan Millarc64edde2009-04-18 12:26:32 -0700201 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700202
203 /**
204 * Temporary for debugging.
205 */
206 public abstract void logState(Printer pw, String prefix);
207 }
208
209 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700210 * State for keeping track of long counting information.
211 */
212 public static abstract class LongCounter {
213
214 /**
215 * Returns the count associated with this Counter for the
216 * selected type of statistics.
217 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700218 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700219 */
220 public abstract long getCountLocked(int which);
221
222 /**
223 * Temporary for debugging.
224 */
225 public abstract void logState(Printer pw, String prefix);
226 }
227
228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * State for keeping track of timing information.
230 */
231 public static abstract class Timer {
232
233 /**
234 * Returns the count associated with this Timer for the
235 * selected type of statistics.
236 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700237 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
Evan Millarc64edde2009-04-18 12:26:32 -0700239 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
241 /**
242 * Returns the total time in microseconds associated with this Timer for the
243 * selected type of statistics.
244 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800245 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700246 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * @return a time in microseconds
248 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800249 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * Temporary for debugging.
253 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700254 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256
257 /**
258 * The statistics associated with a particular uid.
259 */
260 public static abstract class Uid {
261
262 /**
263 * Returns a mapping containing wakelock statistics.
264 *
265 * @return a Map from Strings to Uid.Wakelock objects.
266 */
267 public abstract Map<String, ? extends Wakelock> getWakelockStats();
268
269 /**
270 * The statistics associated with a particular wake lock.
271 */
272 public static abstract class Wakelock {
273 public abstract Timer getWakeTime(int type);
274 }
275
276 /**
277 * Returns a mapping containing sensor statistics.
278 *
279 * @return a Map from Integer sensor ids to Uid.Sensor objects.
280 */
281 public abstract Map<Integer, ? extends Sensor> getSensorStats();
282
283 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700284 * Returns a mapping containing active process data.
285 */
286 public abstract SparseArray<? extends Pid> getPidStats();
287
288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * Returns a mapping containing process statistics.
290 *
291 * @return a Map from Strings to Uid.Proc objects.
292 */
293 public abstract Map<String, ? extends Proc> getProcessStats();
294
295 /**
296 * Returns a mapping containing package statistics.
297 *
298 * @return a Map from Strings to Uid.Pkg objects.
299 */
300 public abstract Map<String, ? extends Pkg> getPackageStats();
301
302 /**
303 * {@hide}
304 */
305 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700306
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800307 public abstract void noteWifiRunningLocked(long elapsedRealtime);
308 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
309 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
310 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
311 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
312 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
313 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
314 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
315 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
316 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
317 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
318 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
319 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
320 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
321 public abstract void noteActivityResumedLocked(long elapsedRealtime);
322 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800323 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
324 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
325 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
326 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
327 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
328 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
329 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700330 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800331 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
Robert Greenwalta029ea12013-09-25 16:38:12 -0700333 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
334
Dianne Hackborn617f8772009-03-31 15:04:46 -0700335 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700336 * Note that these must match the constants in android.os.PowerManager.
337 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
338 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700339 */
340 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700341 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700342 };
343
Jeff Browndf693de2012-07-27 12:03:38 -0700344 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700345
Dianne Hackborn617f8772009-03-31 15:04:46 -0700346 public abstract void noteUserActivityLocked(int type);
347 public abstract boolean hasUserActivity();
348 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700349
350 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800351 public abstract long getNetworkActivityBytes(int type, int which);
352 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800353 public abstract long getMobileRadioActiveTime(int which);
354 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800357 /*
358 * FIXME: it's not correct to use this magic value because it
359 * could clash with a sensor handle (which are defined by
360 * the sensor HAL, and therefore out of our control
361 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 // Magic sensor number for the GPS.
363 public static final int GPS = -10000;
364
365 public abstract int getHandle();
366
367 public abstract Timer getSensorTime();
368 }
369
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700370 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800371 public int mWakeNesting;
372 public long mWakeSumMs;
373 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700374 }
375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * The statistics associated with a particular process.
378 */
379 public static abstract class Proc {
380
Dianne Hackborn287952c2010-09-22 22:34:31 -0700381 public static class ExcessivePower {
382 public static final int TYPE_WAKE = 1;
383 public static final int TYPE_CPU = 2;
384
385 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700386 public long overTime;
387 public long usedTime;
388 }
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800391 * Returns true if this process is still active in the battery stats.
392 */
393 public abstract boolean isActive();
394
395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * Returns the total time (in 1/100 sec) spent executing in user code.
397 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700398 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 */
400 public abstract long getUserTime(int which);
401
402 /**
403 * Returns the total time (in 1/100 sec) spent executing in system code.
404 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700405 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 */
407 public abstract long getSystemTime(int which);
408
409 /**
410 * Returns the number of times the process has been started.
411 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 */
414 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700415
416 /**
417 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700418 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700419 * @return foreground cpu time in microseconds
420 */
421 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700422
423 /**
424 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
425 * @param speedStep the index of the CPU speed. This is not the actual speed of the
426 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700427 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700428 * @see BatteryStats#getCpuSpeedSteps()
429 */
430 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700431
Dianne Hackborn287952c2010-09-22 22:34:31 -0700432 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700433
Dianne Hackborn287952c2010-09-22 22:34:31 -0700434 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
437 /**
438 * The statistics associated with a particular package.
439 */
440 public static abstract class Pkg {
441
442 /**
443 * Returns the number of times this package has done something that could wake up the
444 * device from sleep.
445 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700446 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
448 public abstract int getWakeups(int which);
449
450 /**
451 * Returns a mapping containing service statistics.
452 */
453 public abstract Map<String, ? extends Serv> getServiceStats();
454
455 /**
456 * The statistics associated with a particular service.
457 */
458 public abstract class Serv {
459
460 /**
461 * Returns the amount of time spent started.
462 *
463 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700464 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * @return
466 */
467 public abstract long getStartTime(long batteryUptime, int which);
468
469 /**
470 * Returns the total number of times startService() has been called.
471 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700472 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public abstract int getStarts(int which);
475
476 /**
477 * Returns the total number times the service has been launched.
478 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700479 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
481 public abstract int getLaunches(int which);
482 }
483 }
484 }
485
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800486 public final static class HistoryTag {
487 public String string;
488 public int uid;
489
490 public int poolIdx;
491
492 public void setTo(HistoryTag o) {
493 string = o.string;
494 uid = o.uid;
495 poolIdx = o.poolIdx;
496 }
497
498 public void setTo(String _string, int _uid) {
499 string = _string;
500 uid = _uid;
501 poolIdx = -1;
502 }
503
504 public void writeToParcel(Parcel dest, int flags) {
505 dest.writeString(string);
506 dest.writeInt(uid);
507 }
508
509 public void readFromParcel(Parcel src) {
510 string = src.readString();
511 uid = src.readInt();
512 poolIdx = -1;
513 }
514
515 @Override
516 public boolean equals(Object o) {
517 if (this == o) return true;
518 if (o == null || getClass() != o.getClass()) return false;
519
520 HistoryTag that = (HistoryTag) o;
521
522 if (uid != that.uid) return false;
523 if (!string.equals(that.string)) return false;
524
525 return true;
526 }
527
528 @Override
529 public int hashCode() {
530 int result = string.hashCode();
531 result = 31 * result + uid;
532 return result;
533 }
534 }
535
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700536 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700537 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700538
539 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700540 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800541
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800542 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800543 public static final byte CMD_NULL = -1;
544 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800545 public static final byte CMD_CURRENT_TIME = 5;
546 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700547 public static final byte CMD_RESET = 7;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800548
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700549 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700550
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800551 /**
552 * Return whether the command code is a delta data update.
553 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800554 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800555 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800556 }
557
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700558 public byte batteryLevel;
559 public byte batteryStatus;
560 public byte batteryHealth;
561 public byte batteryPlugType;
562
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900563 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700564 public char batteryVoltage;
565
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700566 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700567 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800568 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700569 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -0700570 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
571 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700572 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800573 public static final int STATE_PHONE_STATE_SHIFT = 6;
574 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700575 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800576 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800577 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800578
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700579 // These states always appear directly in the first int token
580 // of a delta change; they should be ones that change relatively
581 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700582 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
583 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800584 public static final int STATE_GPS_ON_FLAG = 1<<29;
585 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800586 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800587 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800588 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700589 // These are on the lower bits used for the command; if they change
590 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700591 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700592 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700593 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700594 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
595 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
596 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700597 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700598
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700599 public static final int MOST_INTERESTING_STATES =
600 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
Dianne Hackborn3251b902014-06-20 14:40:53 -0700601 | STATE_PHONE_IN_CALL_FLAG | STATE_BLUETOOTH_ON_FLAG;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700602
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700603 public int states;
604
Dianne Hackborn3251b902014-06-20 14:40:53 -0700605 // Constants from WIFI_SUPPL_STATE_*
606 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
607 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
608 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
609 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
610 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
611 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
612
613 public static final int STATE2_LOW_POWER_FLAG = 1<<31;
614 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
615 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
616 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700617 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn3251b902014-06-20 14:40:53 -0700618
619 public static final int MOST_INTERESTING_STATES2 =
620 STATE2_LOW_POWER_FLAG | STATE2_WIFI_ON_FLAG;
621
Dianne Hackborn40c87252014-03-19 16:55:40 -0700622 public int states2;
623
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800624 // The wake lock that was acquired at this point.
625 public HistoryTag wakelockTag;
626
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800627 // Kernel wakeup reason at this point.
628 public HistoryTag wakeReasonTag;
629
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800630 public static final int EVENT_FLAG_START = 0x8000;
631 public static final int EVENT_FLAG_FINISH = 0x4000;
632
633 // No event in this item.
634 public static final int EVENT_NONE = 0x0000;
635 // Event is about a process that is running.
636 public static final int EVENT_PROC = 0x0001;
637 // Event is about an application package that is in the foreground.
638 public static final int EVENT_FOREGROUND = 0x0002;
639 // Event is about an application package that is at the top of the screen.
640 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800641 // Event is about an application package that is at the top of the screen.
642 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700643 // Events for all additional wake locks aquired/release within a wake block.
644 // These are not generated by default.
645 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800646 // Number of event types.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700647 public static final int EVENT_COUNT = 0x0006;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700648 // Mask to extract out only the type part of the event.
649 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800650
651 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
652 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
653 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
654 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
655 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
656 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800657 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
658 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700659 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
660 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800661
662 // For CMD_EVENT.
663 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800664 public HistoryTag eventTag;
665
Dianne Hackborn9a755432014-05-15 17:05:22 -0700666 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800667 public long currentTime;
668
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800669 // Meta-data when reading.
670 public int numReadInts;
671
672 // Pre-allocated objects.
673 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800674 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800675 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800676
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700677 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700678 }
679
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700680 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700681 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800682 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700683 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700684 }
685
686 public int describeContents() {
687 return 0;
688 }
689
690 public void writeToParcel(Parcel dest, int flags) {
691 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700692 int bat = (((int)cmd)&0xff)
693 | ((((int)batteryLevel)<<8)&0xff00)
694 | ((((int)batteryStatus)<<16)&0xf0000)
695 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800696 | ((((int)batteryPlugType)<<24)&0xf000000)
697 | (wakelockTag != null ? 0x10000000 : 0)
698 | (wakeReasonTag != null ? 0x20000000 : 0)
699 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700700 dest.writeInt(bat);
701 bat = (((int)batteryTemperature)&0xffff)
702 | ((((int)batteryVoltage)<<16)&0xffff0000);
703 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700704 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700705 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800706 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800707 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800708 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800709 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800710 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800711 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800712 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800713 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800714 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800715 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700716 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800717 dest.writeLong(currentTime);
718 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700719 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700720
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800721 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800722 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700723 int bat = src.readInt();
724 cmd = (byte)(bat&0xff);
725 batteryLevel = (byte)((bat>>8)&0xff);
726 batteryStatus = (byte)((bat>>16)&0xf);
727 batteryHealth = (byte)((bat>>20)&0xf);
728 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800729 int bat2 = src.readInt();
730 batteryTemperature = (short)(bat2&0xffff);
731 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700732 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700733 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800734 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800735 wakelockTag = localWakelockTag;
736 wakelockTag.readFromParcel(src);
737 } else {
738 wakelockTag = null;
739 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800740 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800741 wakeReasonTag = localWakeReasonTag;
742 wakeReasonTag.readFromParcel(src);
743 } else {
744 wakeReasonTag = null;
745 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800746 if ((bat&0x40000000) != 0) {
747 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800748 eventTag = localEventTag;
749 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800750 } else {
751 eventCode = EVENT_NONE;
752 eventTag = null;
753 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700754 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800755 currentTime = src.readLong();
756 } else {
757 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700758 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800759 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700760 }
761
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700762 public void clear() {
763 time = 0;
764 cmd = CMD_NULL;
765 batteryLevel = 0;
766 batteryStatus = 0;
767 batteryHealth = 0;
768 batteryPlugType = 0;
769 batteryTemperature = 0;
770 batteryVoltage = 0;
771 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700772 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800773 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800774 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800775 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800776 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700777 }
778
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700779 public void setTo(HistoryItem o) {
780 time = o.time;
781 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800782 setToCommon(o);
783 }
784
785 public void setTo(long time, byte cmd, HistoryItem o) {
786 this.time = time;
787 this.cmd = cmd;
788 setToCommon(o);
789 }
790
791 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700792 batteryLevel = o.batteryLevel;
793 batteryStatus = o.batteryStatus;
794 batteryHealth = o.batteryHealth;
795 batteryPlugType = o.batteryPlugType;
796 batteryTemperature = o.batteryTemperature;
797 batteryVoltage = o.batteryVoltage;
798 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700799 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800800 if (o.wakelockTag != null) {
801 wakelockTag = localWakelockTag;
802 wakelockTag.setTo(o.wakelockTag);
803 } else {
804 wakelockTag = null;
805 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800806 if (o.wakeReasonTag != null) {
807 wakeReasonTag = localWakeReasonTag;
808 wakeReasonTag.setTo(o.wakeReasonTag);
809 } else {
810 wakeReasonTag = null;
811 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800812 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800813 if (o.eventTag != null) {
814 eventTag = localEventTag;
815 eventTag.setTo(o.eventTag);
816 } else {
817 eventTag = null;
818 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800819 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700820 }
821
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800822 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700823 return batteryLevel == o.batteryLevel
824 && batteryStatus == o.batteryStatus
825 && batteryHealth == o.batteryHealth
826 && batteryPlugType == o.batteryPlugType
827 && batteryTemperature == o.batteryTemperature
828 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800829 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700830 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800831 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700832 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800833
834 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800835 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800836 return false;
837 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800838 if (wakelockTag != o.wakelockTag) {
839 if (wakelockTag == null || o.wakelockTag == null) {
840 return false;
841 }
842 if (!wakelockTag.equals(o.wakelockTag)) {
843 return false;
844 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800845 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800846 if (wakeReasonTag != o.wakeReasonTag) {
847 if (wakeReasonTag == null || o.wakeReasonTag == null) {
848 return false;
849 }
850 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
851 return false;
852 }
853 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800854 if (eventTag != o.eventTag) {
855 if (eventTag == null || o.eventTag == null) {
856 return false;
857 }
858 if (!eventTag.equals(o.eventTag)) {
859 return false;
860 }
861 }
862 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800863 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700864 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700865
866 public final static class HistoryEventTracker {
867 private final HashMap<String, SparseIntArray>[] mActiveEvents
868 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
869
870 public boolean updateState(int code, String name, int uid, int poolIdx) {
871 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
872 int idx = code&HistoryItem.EVENT_TYPE_MASK;
873 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
874 if (active == null) {
875 active = new HashMap<String, SparseIntArray>();
876 mActiveEvents[idx] = active;
877 }
878 SparseIntArray uids = active.get(name);
879 if (uids == null) {
880 uids = new SparseIntArray();
881 active.put(name, uids);
882 }
883 if (uids.indexOfKey(uid) >= 0) {
884 // Already set, nothing to do!
885 return false;
886 }
887 uids.put(uid, poolIdx);
888 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
889 int idx = code&HistoryItem.EVENT_TYPE_MASK;
890 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
891 if (active == null) {
892 // not currently active, nothing to do.
893 return false;
894 }
895 SparseIntArray uids = active.get(name);
896 if (uids == null) {
897 // not currently active, nothing to do.
898 return false;
899 }
900 idx = uids.indexOfKey(uid);
901 if (idx < 0) {
902 // not currently active, nothing to do.
903 return false;
904 }
905 uids.removeAt(idx);
906 if (uids.size() <= 0) {
907 active.remove(name);
908 }
909 }
910 return true;
911 }
912
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700913 public void removeEvents(int code) {
914 int idx = code&HistoryItem.EVENT_TYPE_MASK;
915 mActiveEvents[idx] = null;
916 }
917
Dianne Hackborn37de0982014-05-09 09:32:18 -0700918 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
919 return mActiveEvents[code];
920 }
921 }
922
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700923 public static final class BitDescription {
924 public final int mask;
925 public final int shift;
926 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800927 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700928 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800929 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700930
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800931 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700932 this.mask = mask;
933 this.shift = -1;
934 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800935 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700936 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800937 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700938 }
939
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800940 public BitDescription(int mask, int shift, String name, String shortName,
941 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700942 this.mask = mask;
943 this.shift = shift;
944 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800945 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700946 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800947 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700948 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700949 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700950
Dianne Hackbornfc064132014-06-02 12:42:12 -0700951 /**
952 * Don't allow any more batching in to the current history event. This
953 * is called when printing partial histories, so to ensure that the next
954 * history event will go in to a new batch after what was printed in the
955 * last partial history.
956 */
957 public abstract void commitCurrentHistoryBatchLocked();
958
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800959 public abstract int getHistoryTotalSize();
960
961 public abstract int getHistoryUsedSize();
962
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700963 public abstract boolean startIteratingHistoryLocked();
964
Dianne Hackborn099bc622014-01-22 13:39:16 -0800965 public abstract int getHistoryStringPoolSize();
966
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800967 public abstract int getHistoryStringPoolBytes();
968
969 public abstract String getHistoryTagPoolString(int index);
970
971 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800972
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700973 public abstract boolean getNextHistoryLocked(HistoryItem out);
974
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700975 public abstract void finishIteratingHistoryLocked();
976
977 public abstract boolean startIteratingOldHistoryLocked();
978
979 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
980
981 public abstract void finishIteratingOldHistoryLocked();
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700984 * Return the base time offset for the battery history.
985 */
986 public abstract long getHistoryBaseTime();
987
988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * Returns the number of times the device has been started.
990 */
991 public abstract int getStartCount();
992
993 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700994 * 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 -0800995 * running on battery.
996 *
997 * {@hide}
998 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800999 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001001 /**
1002 * Returns the number of times the screen was turned on.
1003 *
1004 * {@hide}
1005 */
1006 public abstract int getScreenOnCount(int which);
1007
Jeff Browne95c3cd2014-05-02 16:59:26 -07001008 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1009
Dianne Hackborn617f8772009-03-31 15:04:46 -07001010 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1011 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1012 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1013 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1014 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1015
1016 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1017 "dark", "dim", "medium", "light", "bright"
1018 };
1019
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001020 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1021 "0", "1", "2", "3", "4"
1022 };
1023
Dianne Hackborn617f8772009-03-31 15:04:46 -07001024 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001025
Dianne Hackborn617f8772009-03-31 15:04:46 -07001026 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001027 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001028 * the given brightness
1029 *
1030 * {@hide}
1031 */
1032 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001033 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001036 * Returns the time in microseconds that low power mode has been enabled while the device was
1037 * running on battery.
1038 *
1039 * {@hide}
1040 */
1041 public abstract long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which);
1042
1043 /**
1044 * Returns the number of times that low power mode was enabled.
1045 *
1046 * {@hide}
1047 */
1048 public abstract int getLowPowerModeEnabledCount(int which);
1049
1050 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001051 * 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 -08001052 * running on battery.
1053 *
1054 * {@hide}
1055 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001056 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001057
1058 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001059 * Returns the number of times a phone call was activated.
1060 *
1061 * {@hide}
1062 */
1063 public abstract int getPhoneOnCount(int which);
1064
1065 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001066 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001067 * the given signal strength.
1068 *
1069 * {@hide}
1070 */
1071 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001072 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001073
Dianne Hackborn617f8772009-03-31 15:04:46 -07001074 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001075 * Returns the time in microseconds that the phone has been trying to
1076 * acquire a signal.
1077 *
1078 * {@hide}
1079 */
1080 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001081 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001082
1083 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001084 * Returns the number of times the phone has entered the given signal strength.
1085 *
1086 * {@hide}
1087 */
1088 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1089
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001090 /**
1091 * Returns the time in microseconds that the mobile network has been active
1092 * (in a high power state).
1093 *
1094 * {@hide}
1095 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001096 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001097
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001098 /**
1099 * Returns the number of times that the mobile network has transitioned to the
1100 * active state.
1101 *
1102 * {@hide}
1103 */
1104 public abstract int getMobileRadioActiveCount(int which);
1105
1106 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001107 * Returns the time in microseconds that is the difference between the mobile radio
1108 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1109 * from the radio.
1110 *
1111 * {@hide}
1112 */
1113 public abstract long getMobileRadioActiveAdjustedTime(int which);
1114
1115 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001116 * Returns the time in microseconds that the mobile network has been active
1117 * (in a high power state) but not being able to blame on an app.
1118 *
1119 * {@hide}
1120 */
1121 public abstract long getMobileRadioActiveUnknownTime(int which);
1122
1123 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001124 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001125 *
1126 * {@hide}
1127 */
1128 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001129
Dianne Hackborn627bba72009-03-24 22:32:56 -07001130 public static final int DATA_CONNECTION_NONE = 0;
1131 public static final int DATA_CONNECTION_GPRS = 1;
1132 public static final int DATA_CONNECTION_EDGE = 2;
1133 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001134 public static final int DATA_CONNECTION_CDMA = 4;
1135 public static final int DATA_CONNECTION_EVDO_0 = 5;
1136 public static final int DATA_CONNECTION_EVDO_A = 6;
1137 public static final int DATA_CONNECTION_1xRTT = 7;
1138 public static final int DATA_CONNECTION_HSDPA = 8;
1139 public static final int DATA_CONNECTION_HSUPA = 9;
1140 public static final int DATA_CONNECTION_HSPA = 10;
1141 public static final int DATA_CONNECTION_IDEN = 11;
1142 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001143 public static final int DATA_CONNECTION_LTE = 13;
1144 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001145 public static final int DATA_CONNECTION_HSPAP = 15;
1146 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001147
Dianne Hackborn627bba72009-03-24 22:32:56 -07001148 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001149 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001150 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001151 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001152 };
1153
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001154 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001155
1156 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001157 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001158 * the given data connection.
1159 *
1160 * {@hide}
1161 */
1162 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001166 * Returns the number of times the phone has entered the given data
1167 * connection type.
1168 *
1169 * {@hide}
1170 */
1171 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001172
Dianne Hackborn3251b902014-06-20 14:40:53 -07001173 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1174 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1175 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1176 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1177 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1178 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1179 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1180 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1181 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1182 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1183 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1184 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1185 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1186
1187 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1188
1189 static final String[] WIFI_SUPPL_STATE_NAMES = {
1190 "invalid", "disconn", "disabled", "inactive", "scanning",
1191 "authenticating", "associating", "associated", "4-way-handshake",
1192 "group-handshake", "completed", "dormant", "uninit"
1193 };
1194
1195 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1196 "inv", "dsc", "dis", "inact", "scan",
1197 "auth", "ascing", "asced", "4-way",
1198 "group", "compl", "dorm", "uninit"
1199 };
1200
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001201 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1202 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001203 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001204 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1205 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001206 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001207 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1208 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1209 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001210 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001211 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001212 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001213 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1214 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1215 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001216 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1217 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1218 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1219 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1220 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1221 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1222 new String[] {"in", "out", "emergency", "off"},
1223 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001224 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1225 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1226 SignalStrength.SIGNAL_STRENGTH_NAMES,
1227 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001228 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1229 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1230 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001231 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001232
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001233 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1234 = new BitDescription[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001235 new BitDescription(HistoryItem.STATE2_LOW_POWER_FLAG, "low_power", "lp"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001236 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1237 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1238 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001239 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001240 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1241 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1242 new String[] { "0", "1", "2", "3", "4" },
1243 new String[] { "0", "1", "2", "3", "4" }),
1244 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1245 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1246 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001247 };
1248
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001249 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001250 "null", "proc", "fg", "top", "sync", "wake_lock_in"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001251 };
1252
1253 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001254 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001255 };
1256
Dianne Hackborn617f8772009-03-31 15:04:46 -07001257 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001258 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001259 * running on battery.
1260 *
1261 * {@hide}
1262 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001263 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001264
1265 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001266 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001267 * been in the running state while the device was running on battery.
1268 *
1269 * {@hide}
1270 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001271 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001272
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001273 public static final int WIFI_STATE_OFF = 0;
1274 public static final int WIFI_STATE_OFF_SCANNING = 1;
1275 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1276 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1277 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1278 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1279 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1280 public static final int WIFI_STATE_SOFT_AP = 7;
1281
1282 static final String[] WIFI_STATE_NAMES = {
1283 "off", "scanning", "no_net", "disconn",
1284 "sta", "p2p", "sta_p2p", "soft_ap"
1285 };
1286
1287 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1288
1289 /**
1290 * Returns the time in microseconds that WiFi has been running in the given state.
1291 *
1292 * {@hide}
1293 */
1294 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001296
1297 /**
1298 * Returns the number of times that WiFi has entered the given state.
1299 *
1300 * {@hide}
1301 */
1302 public abstract int getWifiStateCount(int wifiState, int which);
1303
The Android Open Source Project10592532009-03-18 17:39:46 -07001304 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001305 * Returns the time in microseconds that the wifi supplicant has been
1306 * in a given state.
1307 *
1308 * {@hide}
1309 */
1310 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1311
1312 /**
1313 * Returns the number of times that the wifi supplicant has transitioned
1314 * to a given state.
1315 *
1316 * {@hide}
1317 */
1318 public abstract int getWifiSupplStateCount(int state, int which);
1319
1320 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1321
1322 /**
1323 * Returns the time in microseconds that WIFI has been running with
1324 * the given signal strength.
1325 *
1326 * {@hide}
1327 */
1328 public abstract long getWifiSignalStrengthTime(int strengthBin,
1329 long elapsedRealtimeUs, int which);
1330
1331 /**
1332 * Returns the number of times WIFI has entered the given signal strength.
1333 *
1334 * {@hide}
1335 */
1336 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1337
1338 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001339 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001340 * running on battery.
1341 *
1342 * {@hide}
1343 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001344 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001345
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001346 public abstract int getBluetoothPingCount();
1347
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001348 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1349 public static final int BLUETOOTH_STATE_LOW = 1;
1350 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1351 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001352
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001353 static final String[] BLUETOOTH_STATE_NAMES = {
1354 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001355 };
1356
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001357 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001358
1359 /**
1360 * Returns the time in microseconds that Bluetooth has been running in the
1361 * given active state.
1362 *
1363 * {@hide}
1364 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001365 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001366 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001367
1368 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001369 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001370 *
1371 * {@hide}
1372 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001373 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001374
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001375 /**
1376 * Returns the time in microseconds that the flashlight has been on while the device was
1377 * running on battery.
1378 *
1379 * {@hide}
1380 */
1381 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1382
1383 /**
1384 * Returns the number of times that the flashlight has been turned on while the device was
1385 * running on battery.
1386 *
1387 * {@hide}
1388 */
1389 public abstract long getFlashlightOnCount(int which);
1390
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001391 public static final int NETWORK_MOBILE_RX_DATA = 0;
1392 public static final int NETWORK_MOBILE_TX_DATA = 1;
1393 public static final int NETWORK_WIFI_RX_DATA = 2;
1394 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001395
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001396 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001397
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001398 public abstract long getNetworkActivityBytes(int type, int which);
1399 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001400
The Android Open Source Project10592532009-03-18 17:39:46 -07001401 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001402 * Return the wall clock time when battery stats data collection started.
1403 */
1404 public abstract long getStartClockTime();
1405
1406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 * Return whether we are currently running on battery.
1408 */
1409 public abstract boolean getIsOnBattery();
1410
1411 /**
1412 * Returns a SparseArray containing the statistics for each uid.
1413 */
1414 public abstract SparseArray<? extends Uid> getUidStats();
1415
1416 /**
1417 * Returns the current battery uptime in microseconds.
1418 *
1419 * @param curTime the amount of elapsed realtime in microseconds.
1420 */
1421 public abstract long getBatteryUptime(long curTime);
1422
1423 /**
1424 * Returns the current battery realtime in microseconds.
1425 *
1426 * @param curTime the amount of elapsed realtime in microseconds.
1427 */
1428 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001429
1430 /**
Evan Millar633a1742009-04-02 16:36:33 -07001431 * Returns the battery percentage level at the last time the device was unplugged from power, or
1432 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001433 */
Evan Millar633a1742009-04-02 16:36:33 -07001434 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001435
1436 /**
Evan Millar633a1742009-04-02 16:36:33 -07001437 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1438 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001439 */
Evan Millar633a1742009-04-02 16:36:33 -07001440 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441
1442 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001443 * Get the amount the battery has discharged since the stats were
1444 * last reset after charging, as a lower-end approximation.
1445 */
1446 public abstract int getLowDischargeAmountSinceCharge();
1447
1448 /**
1449 * Get the amount the battery has discharged since the stats were
1450 * last reset after charging, as an upper-end approximation.
1451 */
1452 public abstract int getHighDischargeAmountSinceCharge();
1453
1454 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001455 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1456 */
1457 public abstract int getDischargeAmount(int which);
1458
1459 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001460 * Get the amount the battery has discharged while the screen was on,
1461 * since the last time power was unplugged.
1462 */
1463 public abstract int getDischargeAmountScreenOn();
1464
1465 /**
1466 * Get the amount the battery has discharged while the screen was on,
1467 * since the last time the device was charged.
1468 */
1469 public abstract int getDischargeAmountScreenOnSinceCharge();
1470
1471 /**
1472 * Get the amount the battery has discharged while the screen was off,
1473 * since the last time power was unplugged.
1474 */
1475 public abstract int getDischargeAmountScreenOff();
1476
1477 /**
1478 * Get the amount the battery has discharged while the screen was off,
1479 * since the last time the device was charged.
1480 */
1481 public abstract int getDischargeAmountScreenOffSinceCharge();
1482
1483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 * Returns the total, last, or current battery uptime in microseconds.
1485 *
1486 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001487 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 */
1489 public abstract long computeBatteryUptime(long curTime, int which);
1490
1491 /**
1492 * Returns the total, last, or current battery realtime in microseconds.
1493 *
1494 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001495 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 */
1497 public abstract long computeBatteryRealtime(long curTime, int which);
1498
1499 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001500 * Returns the total, last, or current battery screen off uptime in microseconds.
1501 *
1502 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001503 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001504 */
1505 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1506
1507 /**
1508 * Returns the total, last, or current battery screen off realtime in microseconds.
1509 *
1510 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001511 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001512 */
1513 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1514
1515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * Returns the total, last, or current uptime in microseconds.
1517 *
1518 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001519 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 */
1521 public abstract long computeUptime(long curTime, int which);
1522
1523 /**
1524 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001527 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 */
1529 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001530
1531 /**
1532 * Compute an approximation for how much run time (in microseconds) is remaining on
1533 * the battery. Returns -1 if no time can be computed: either there is not
1534 * enough current data to make a decision, or the battery is currently
1535 * charging.
1536 *
1537 * @param curTime The current elepsed realtime in microseconds.
1538 */
1539 public abstract long computeBatteryTimeRemaining(long curTime);
1540
1541 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001542 * Return the historical number of discharge steps we currently have.
1543 */
1544 public abstract int getNumDischargeStepDurations();
1545
1546 /**
1547 * Return the array of discharge step durations; the number of valid
1548 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1549 * These values are in milliseconds.
1550 */
1551 public abstract long[] getDischargeStepDurationsArray();
1552
1553 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001554 * Compute an approximation for how much time (in microseconds) remains until the battery
1555 * is fully charged. Returns -1 if no time can be computed: either there is not
1556 * enough current data to make a decision, or the battery is currently
1557 * discharging.
1558 *
1559 * @param curTime The current elepsed realtime in microseconds.
1560 */
1561 public abstract long computeChargeTimeRemaining(long curTime);
1562
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001563 /**
1564 * Return the historical number of charge steps we currently have.
1565 */
1566 public abstract int getNumChargeStepDurations();
1567
1568 /**
1569 * Return the array of charge step durations; the number of valid
1570 * items in it is returned by {@link #getNumChargeStepDurations()}.
1571 * These values are in milliseconds.
1572 */
1573 public abstract long[] getChargeStepDurationsArray();
1574
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001575 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1576
Evan Millarc64edde2009-04-18 12:26:32 -07001577 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
Amith Yamasanie43530a2009-08-21 13:11:37 -07001579 /** Returns the number of different speeds that the CPU can run at */
1580 public abstract int getCpuSpeedSteps();
1581
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001582 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1583
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001584 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 long days = seconds / (60 * 60 * 24);
1586 if (days != 0) {
1587 out.append(days);
1588 out.append("d ");
1589 }
1590 long used = days * 60 * 60 * 24;
1591
1592 long hours = (seconds - used) / (60 * 60);
1593 if (hours != 0 || used != 0) {
1594 out.append(hours);
1595 out.append("h ");
1596 }
1597 used += hours * 60 * 60;
1598
1599 long mins = (seconds-used) / 60;
1600 if (mins != 0 || used != 0) {
1601 out.append(mins);
1602 out.append("m ");
1603 }
1604 used += mins * 60;
1605
1606 if (seconds != 0 || used != 0) {
1607 out.append(seconds-used);
1608 out.append("s ");
1609 }
1610 }
1611
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001614 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 sb.append((time - (sec * 100)) * 10);
1616 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001619 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001621 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 sb.append(time - (sec * 1000));
1623 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001626 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001627 long sec = time / 1000;
1628 formatTimeRaw(sb, sec);
1629 sb.append(time - (sec * 1000));
1630 sb.append("ms");
1631 }
1632
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001633 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001635 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
1637 float perc = ((float)num) / ((float)den) * 100;
1638 mFormatBuilder.setLength(0);
1639 mFormatter.format("%.1f%%", perc);
1640 return mFormatBuilder.toString();
1641 }
1642
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001643 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001644 mFormatBuilder.setLength(0);
1645
1646 if (bytes < BYTES_PER_KB) {
1647 return bytes + "B";
1648 } else if (bytes < BYTES_PER_MB) {
1649 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1650 return mFormatBuilder.toString();
1651 } else if (bytes < BYTES_PER_GB){
1652 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1653 return mFormatBuilder.toString();
1654 } else {
1655 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1656 return mFormatBuilder.toString();
1657 }
1658 }
1659
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001660 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001661 if (timer != null) {
1662 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001663 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001664 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1665 return totalTimeMillis;
1666 }
1667 return 0;
1668 }
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 /**
1671 *
1672 * @param sb a StringBuilder object.
1673 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001674 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001676 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 * @param linePrefix a String to be prepended to each line of output.
1678 * @return the line prefix
1679 */
1680 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001681 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682
1683 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001684 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685
Evan Millarc64edde2009-04-18 12:26:32 -07001686 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (totalTimeMillis != 0) {
1688 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001689 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001690 if (name != null) {
1691 sb.append(name);
1692 sb.append(' ');
1693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 sb.append('(');
1695 sb.append(count);
1696 sb.append(" times)");
1697 return ", ";
1698 }
1699 }
1700 return linePrefix;
1701 }
1702
1703 /**
1704 * Checkin version of wakelock printer. Prints simple comma-separated list.
1705 *
1706 * @param sb a StringBuilder object.
1707 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001708 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001710 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 * @param linePrefix a String to be prepended to each line of output.
1712 * @return the line prefix
1713 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001714 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1715 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long totalTimeMicros = 0;
1717 int count = 0;
1718 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001719 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001720 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722 sb.append(linePrefix);
1723 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1724 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001725 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 sb.append(count);
1727 return ",";
1728 }
1729
1730 /**
1731 * Dump a comma-separated line of values for terse checkin mode.
1732 *
1733 * @param pw the PageWriter to dump log to
1734 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1735 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1736 * @param args type-dependent data arguments
1737 */
1738 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1739 Object... args ) {
1740 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1741 pw.print(uid); pw.print(',');
1742 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001743 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744
1745 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001746 pw.print(',');
1747 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001749 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751
1752 /**
1753 * Checkin server version of dump to produce more compact, computer-readable log.
1754 *
1755 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001757 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1759 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1760 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1762 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001763 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1764 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1765 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 final long totalRealtime = computeRealtime(rawRealtime, which);
1767 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001768 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07001769 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001770 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001771 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1772 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1773 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1774 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 StringBuilder sb = new StringBuilder(128);
1777
Evan Millar22ac0432009-03-31 11:33:18 -07001778 SparseArray<? extends Uid> uidStats = getUidStats();
1779 final int NU = uidStats.size();
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 // Dump "battery" stat
1784 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001785 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001786 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001787 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001788 getStartClockTime(),
1789 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001791 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001792 long fullWakeLockTimeTotal = 0;
1793 long partialWakeLockTimeTotal = 0;
1794
1795 for (int iu = 0; iu < NU; iu++) {
1796 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001797
Evan Millar22ac0432009-03-31 11:33:18 -07001798 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1799 if (wakelocks.size() > 0) {
1800 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1801 : wakelocks.entrySet()) {
1802 Uid.Wakelock wl = ent.getValue();
1803
1804 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1805 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001806 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1807 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001808 }
1809
1810 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1811 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001812 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001813 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001814 }
1815 }
1816 }
1817 }
1818
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001819 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1820 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1821 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1822 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1823 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1824 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1825 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1826 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1827
1828 // Dump network stats
1829 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1830 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1831 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 // Dump misc stats
1834 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001835 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001836 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001837 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001838 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Jeff Browne95c3cd2014-05-02 16:59:26 -07001839 0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which),
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001840 getMobileRadioActiveAdjustedTime(which), interactiveTime / 1000,
1841 lowPowerModeEnabledTime / 1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001842
1843 // Dump screen brightness stats
1844 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1845 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001846 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001847 }
1848 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001849
Dianne Hackborn627bba72009-03-24 22:32:56 -07001850 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001851 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1852 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001853 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001854 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001855 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001856 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001857 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001858 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001859 args[i] = getPhoneSignalStrengthCount(i, which);
1860 }
1861 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001862
Dianne Hackborn627bba72009-03-24 22:32:56 -07001863 // Dump network type stats
1864 args = new Object[NUM_DATA_CONNECTION_TYPES];
1865 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001866 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001867 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001868 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1869 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1870 args[i] = getPhoneDataConnectionCount(i, which);
1871 }
1872 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001873
1874 // Dump wifi state stats
1875 args = new Object[NUM_WIFI_STATES];
1876 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001877 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001878 }
1879 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1880 for (int i=0; i<NUM_WIFI_STATES; i++) {
1881 args[i] = getWifiStateCount(i, which);
1882 }
1883 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1884
Dianne Hackborn3251b902014-06-20 14:40:53 -07001885 // Dump wifi suppl state stats
1886 args = new Object[NUM_WIFI_SUPPL_STATES];
1887 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
1888 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
1889 }
1890 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
1891 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
1892 args[i] = getWifiSupplStateCount(i, which);
1893 }
1894 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
1895
1896 // Dump wifi signal strength stats
1897 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
1898 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
1899 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
1900 }
1901 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
1902 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
1903 args[i] = getWifiSignalStrengthCount(i, which);
1904 }
1905 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
1906
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001907 // Dump bluetooth state stats
1908 args = new Object[NUM_BLUETOOTH_STATES];
1909 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001910 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001911 }
1912 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1913 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1914 args[i] = getBluetoothStateCount(i, which);
1915 }
1916 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1917
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001918 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001919 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001920 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001923 if (which == STATS_SINCE_UNPLUGGED) {
1924 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1925 getDischargeStartLevel()-getDischargeCurrentLevel(),
1926 getDischargeStartLevel()-getDischargeCurrentLevel(),
1927 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1928 } else {
1929 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1930 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1931 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1932 }
1933
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001934 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001935 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001936 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001937 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001938 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001939 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001940 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001941 sb.toString());
1942 }
Evan Millarc64edde2009-04-18 12:26:32 -07001943 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001944 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1945 if (wakeupReasons.size() > 0) {
1946 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1947 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1948 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1949 }
1950 }
Evan Millarc64edde2009-04-18 12:26:32 -07001951 }
1952
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001953 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001954 helper.create(this);
1955 helper.refreshStats(which, UserHandle.USER_ALL);
1956 List<BatterySipper> sippers = helper.getUsageList();
1957 if (sippers != null && sippers.size() > 0) {
1958 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1959 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001960 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001961 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1962 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1963 for (int i=0; i<sippers.size(); i++) {
1964 BatterySipper bs = sippers.get(i);
1965 int uid = 0;
1966 String label;
1967 switch (bs.drainType) {
1968 case IDLE:
1969 label="idle";
1970 break;
1971 case CELL:
1972 label="cell";
1973 break;
1974 case PHONE:
1975 label="phone";
1976 break;
1977 case WIFI:
1978 label="wifi";
1979 break;
1980 case BLUETOOTH:
1981 label="blue";
1982 break;
1983 case SCREEN:
1984 label="scrn";
1985 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001986 case FLASHLIGHT:
1987 label="flashlight";
1988 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001989 case APP:
1990 uid = bs.uidObj.getUid();
1991 label = "uid";
1992 break;
1993 case USER:
1994 uid = UserHandle.getUid(bs.userId, 0);
1995 label = "user";
1996 break;
1997 case UNACCOUNTED:
1998 label = "unacc";
1999 break;
2000 case OVERCOUNTED:
2001 label = "over";
2002 break;
2003 default:
2004 label = "???";
2005 }
2006 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
2007 BatteryStatsHelper.makemAh(bs.value));
2008 }
2009 }
2010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 for (int iu = 0; iu < NU; iu++) {
2012 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002013 if (reqUid >= 0 && uid != reqUid) {
2014 continue;
2015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 Uid u = uidStats.valueAt(iu);
2017 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002018 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2019 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2020 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2021 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2022 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2023 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002024 long mobileActiveTime = u.getMobileRadioActiveTime(which);
2025 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002026 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2027 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002028 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2029 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2030 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002031
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002032 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2033 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002034 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002035 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2036 wifiBytesRx, wifiBytesTx,
2037 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002038 wifiPacketsRx, wifiPacketsTx,
2039 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002040 }
2041
Nick Pelly6ccaa542012-06-15 15:22:47 -07002042 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002043 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002044 dumpLine(pw, uid, category, WIFI_DATA,
2045 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047
Dianne Hackborn617f8772009-03-31 15:04:46 -07002048 if (u.hasUserActivity()) {
2049 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2050 boolean hasData = false;
2051 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2052 int val = u.getUserActivityCount(i, which);
2053 args[i] = val;
2054 if (val != 0) hasData = true;
2055 }
2056 if (hasData) {
2057 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
2058 }
2059 }
2060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2062 if (wakelocks.size() > 0) {
2063 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2064 : wakelocks.entrySet()) {
2065 Uid.Wakelock wl = ent.getValue();
2066 String linePrefix = "";
2067 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07002068 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002069 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07002070 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002071 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07002072 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002073 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074
2075 // Only log if we had at lease one wakelock...
2076 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002077 String name = ent.getKey();
2078 if (name.indexOf(',') >= 0) {
2079 name = name.replace(',', '_');
2080 }
2081 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 }
2084 }
2085
2086 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2087 if (sensors.size() > 0) {
2088 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2089 : sensors.entrySet()) {
2090 Uid.Sensor se = ent.getValue();
2091 int sensorNumber = ent.getKey();
2092 Timer timer = se.getSensorTime();
2093 if (timer != null) {
2094 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002095 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002096 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 if (totalTime != 0) {
2098 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2099 }
2100 }
2101 }
2102 }
2103
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002104 Timer vibTimer = u.getVibratorOnTimer();
2105 if (vibTimer != null) {
2106 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002107 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002108 int count = vibTimer.getCountLocked(which);
2109 if (totalTime != 0) {
2110 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
2111 }
2112 }
2113
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002114 Timer fgTimer = u.getForegroundActivityTimer();
2115 if (fgTimer != null) {
2116 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002117 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002118 int count = fgTimer.getCountLocked(which);
2119 if (totalTime != 0) {
2120 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
2121 }
2122 }
2123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2125 if (processStats.size() > 0) {
2126 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2127 : processStats.entrySet()) {
2128 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002129
2130 final long userMillis = ps.getUserTime(which) * 10;
2131 final long systemMillis = ps.getSystemTime(which) * 10;
2132 final long foregroundMillis = ps.getForegroundTime(which) * 10;
2133 final long starts = ps.getStarts(which);
2134
2135 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2136 || starts != 0) {
2137 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
2138 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140 }
2141 }
2142
2143 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2144 if (packageStats.size() > 0) {
2145 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2146 : packageStats.entrySet()) {
2147
2148 Uid.Pkg ps = ent.getValue();
2149 int wakeups = ps.getWakeups(which);
2150 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2151 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2152 : serviceStats.entrySet()) {
2153 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2154 long startTime = ss.getStartTime(batteryUptime, which);
2155 int starts = ss.getStarts(which);
2156 int launches = ss.getLaunches(which);
2157 if (startTime != 0 || starts != 0 || launches != 0) {
2158 dumpLine(pw, uid, category, APK_DATA,
2159 wakeups, // wakeup alarms
2160 ent.getKey(), // Apk
2161 sent.getKey(), // service
2162 startTime / 1000, // time spent started, in ms
2163 starts,
2164 launches);
2165 }
2166 }
2167 }
2168 }
2169 }
2170 }
2171
Dianne Hackborn81038902012-11-26 17:04:09 -08002172 static final class TimerEntry {
2173 final String mName;
2174 final int mId;
2175 final BatteryStats.Timer mTimer;
2176 final long mTime;
2177 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2178 mName = name;
2179 mId = id;
2180 mTimer = timer;
2181 mTime = time;
2182 }
2183 }
2184
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002185 private void printmAh(PrintWriter printer, double power) {
2186 printer.print(BatteryStatsHelper.makemAh(power));
2187 }
2188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002190 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
2191 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2193 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2194 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195
2196 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2197 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2198 final long totalRealtime = computeRealtime(rawRealtime, which);
2199 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002200 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2201 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2202 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002203 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2204 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07002207
2208 SparseArray<? extends Uid> uidStats = getUidStats();
2209 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002211 sb.setLength(0);
2212 sb.append(prefix);
2213 sb.append(" Time on battery: ");
2214 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2215 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2216 sb.append(") realtime, ");
2217 formatTimeMs(sb, whichBatteryUptime / 1000);
2218 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2219 sb.append(") uptime");
2220 pw.println(sb.toString());
2221 sb.setLength(0);
2222 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002223 sb.append(" Time on battery screen off: ");
2224 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2225 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2226 sb.append(") realtime, ");
2227 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2228 sb.append("(");
2229 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2230 sb.append(") uptime");
2231 pw.println(sb.toString());
2232 sb.setLength(0);
2233 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002234 sb.append(" Total run time: ");
2235 formatTimeMs(sb, totalRealtime / 1000);
2236 sb.append("realtime, ");
2237 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002238 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07002239 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002240 if (batteryTimeRemaining >= 0) {
2241 sb.setLength(0);
2242 sb.append(prefix);
2243 sb.append(" Battery time remaining: ");
2244 formatTimeMs(sb, batteryTimeRemaining / 1000);
2245 pw.println(sb.toString());
2246 }
2247 if (chargeTimeRemaining >= 0) {
2248 sb.setLength(0);
2249 sb.append(prefix);
2250 sb.append(" Charge time remaining: ");
2251 formatTimeMs(sb, chargeTimeRemaining / 1000);
2252 pw.println(sb.toString());
2253 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002254 pw.print(" Start clock time: ");
2255 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2256
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002257 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002258 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002259 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002260 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2261 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2262 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2263 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002264 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002265 sb.append(prefix);
2266 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2267 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002268 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002269 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2270 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07002271 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002272 pw.println(sb.toString());
2273 sb.setLength(0);
2274 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002275 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002276 boolean didOne = false;
2277 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002278 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002279 if (time == 0) {
2280 continue;
2281 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002282 sb.append("\n ");
2283 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002284 didOne = true;
2285 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2286 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002287 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002288 sb.append("(");
2289 sb.append(formatRatioLocked(time, screenOnTime));
2290 sb.append(")");
2291 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002292 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002293 pw.println(sb.toString());
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002294 if (lowPowerModeEnabledTime != 0) {
2295 sb.setLength(0);
2296 sb.append(prefix);
2297 sb.append(" Low power mode enabled: ");
2298 formatTimeMs(sb, lowPowerModeEnabledTime / 1000);
2299 sb.append("(");
2300 sb.append(formatRatioLocked(lowPowerModeEnabledTime, whichBatteryRealtime));
2301 sb.append(")");
2302 pw.println(sb.toString());
2303 }
2304 if (phoneOnTime != 0) {
2305 sb.setLength(0);
2306 sb.append(prefix);
2307 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2308 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2309 sb.append(") "); sb.append(getPhoneOnCount(which));
2310 }
2311
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002312 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002313 long fullWakeLockTimeTotalMicros = 0;
2314 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002315
Dianne Hackborn81038902012-11-26 17:04:09 -08002316 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2317
Evan Millar22ac0432009-03-31 11:33:18 -07002318 for (int iu = 0; iu < NU; iu++) {
2319 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002320
Evan Millar22ac0432009-03-31 11:33:18 -07002321 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2322 if (wakelocks.size() > 0) {
2323 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2324 : wakelocks.entrySet()) {
2325 Uid.Wakelock wl = ent.getValue();
2326
2327 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2328 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002329 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002330 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002331 }
2332
2333 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2334 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002335 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002336 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002337 if (totalTimeMicros > 0) {
2338 if (reqUid < 0) {
2339 // Only show the ordered list of all wake
2340 // locks if the caller is not asking for data
2341 // about a specific uid.
2342 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2343 partialWakeTimer, totalTimeMicros));
2344 }
2345 partialWakeLockTimeTotalMicros += totalTimeMicros;
2346 }
Evan Millar22ac0432009-03-31 11:33:18 -07002347 }
2348 }
2349 }
2350 }
2351
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002352 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2353 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2354 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2355 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2356 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2357 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2358 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2359 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2360
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002361 if (fullWakeLockTimeTotalMicros != 0) {
2362 sb.setLength(0);
2363 sb.append(prefix);
2364 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2365 (fullWakeLockTimeTotalMicros + 500) / 1000);
2366 pw.println(sb.toString());
2367 }
2368
2369 if (partialWakeLockTimeTotalMicros != 0) {
2370 sb.setLength(0);
2371 sb.append(prefix);
2372 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2373 (partialWakeLockTimeTotalMicros + 500) / 1000);
2374 pw.println(sb.toString());
2375 }
2376
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002377 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002378 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2379 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2380 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2381 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002382 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002383 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07002384 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002385 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002386 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002387 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002388 if (time == 0) {
2389 continue;
2390 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002391 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002392 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002393 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002394 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002395 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002396 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002397 sb.append("(");
2398 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002399 sb.append(") ");
2400 sb.append(getPhoneSignalStrengthCount(i, which));
2401 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002402 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002403 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002404 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002405
2406 sb.setLength(0);
2407 sb.append(prefix);
2408 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002409 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002410 pw.println(sb.toString());
2411
Dianne Hackborn627bba72009-03-24 22:32:56 -07002412 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002413 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002414 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002415 didOne = false;
2416 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002417 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002418 if (time == 0) {
2419 continue;
2420 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002421 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002422 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002423 didOne = true;
2424 sb.append(DATA_CONNECTION_NAMES[i]);
2425 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002426 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002427 sb.append("(");
2428 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002429 sb.append(") ");
2430 sb.append(getPhoneDataConnectionCount(i, which));
2431 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002432 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002433 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002434 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002435
2436 sb.setLength(0);
2437 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002438 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002439 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002440 formatTimeMs(sb, mobileActiveTime / 1000);
2441 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2442 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2443 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002444 pw.println(sb.toString());
2445
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002446 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2447 if (mobileActiveUnknownTime != 0) {
2448 sb.setLength(0);
2449 sb.append(prefix);
2450 sb.append(" Mobile radio active unknown time: ");
2451 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2452 sb.append("(");
2453 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2454 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2455 sb.append("x");
2456 pw.println(sb.toString());
2457 }
2458
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002459 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2460 if (mobileActiveAdjustedTime != 0) {
2461 sb.setLength(0);
2462 sb.append(prefix);
2463 sb.append(" Mobile radio active adjusted time: ");
2464 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2465 sb.append("(");
2466 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2467 sb.append(")");
2468 pw.println(sb.toString());
2469 }
2470
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002471 pw.print(prefix);
2472 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2473 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2474 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2475 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002476 sb.setLength(0);
2477 sb.append(prefix);
2478 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2479 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2480 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2481 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002482 sb.append(")");
2483 pw.println(sb.toString());
2484
2485 sb.setLength(0);
2486 sb.append(prefix);
2487 sb.append(" Wifi states:");
2488 didOne = false;
2489 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002490 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002491 if (time == 0) {
2492 continue;
2493 }
2494 sb.append("\n ");
2495 didOne = true;
2496 sb.append(WIFI_STATE_NAMES[i]);
2497 sb.append(" ");
2498 formatTimeMs(sb, time/1000);
2499 sb.append("(");
2500 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2501 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07002502 sb.append(getWifiStateCount(i, which));
2503 sb.append("x");
2504 }
2505 if (!didOne) sb.append(" (no activity)");
2506 pw.println(sb.toString());
2507
2508 sb.setLength(0);
2509 sb.append(prefix);
2510 sb.append(" Wifi supplicant states:");
2511 didOne = false;
2512 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2513 final long time = getWifiSupplStateTime(i, rawRealtime, which);
2514 if (time == 0) {
2515 continue;
2516 }
2517 sb.append("\n ");
2518 didOne = true;
2519 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
2520 sb.append(" ");
2521 formatTimeMs(sb, time/1000);
2522 sb.append("(");
2523 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2524 sb.append(") ");
2525 sb.append(getWifiSupplStateCount(i, which));
2526 sb.append("x");
2527 }
2528 if (!didOne) sb.append(" (no activity)");
2529 pw.println(sb.toString());
2530
2531 sb.setLength(0);
2532 sb.append(prefix);
2533 sb.append(" Wifi signal levels:");
2534 didOne = false;
2535 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2536 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
2537 if (time == 0) {
2538 continue;
2539 }
2540 sb.append("\n ");
2541 sb.append(prefix);
2542 didOne = true;
2543 sb.append("level(");
2544 sb.append(i);
2545 sb.append(") ");
2546 formatTimeMs(sb, time/1000);
2547 sb.append("(");
2548 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2549 sb.append(") ");
2550 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002551 sb.append("x");
2552 }
2553 if (!didOne) sb.append(" (no activity)");
2554 pw.println(sb.toString());
2555
2556 sb.setLength(0);
2557 sb.append(prefix);
2558 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002559 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2560 sb.append(")");
2561 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002562
2563 sb.setLength(0);
2564 sb.append(prefix);
2565 sb.append(" Bluetooth states:");
2566 didOne = false;
2567 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002568 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002569 if (time == 0) {
2570 continue;
2571 }
2572 sb.append("\n ");
2573 didOne = true;
2574 sb.append(BLUETOOTH_STATE_NAMES[i]);
2575 sb.append(" ");
2576 formatTimeMs(sb, time/1000);
2577 sb.append("(");
2578 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2579 sb.append(") ");
2580 sb.append(getPhoneDataConnectionCount(i, which));
2581 sb.append("x");
2582 }
2583 if (!didOne) sb.append(" (no activity)");
2584 pw.println(sb.toString());
2585
2586 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002587
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002588 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002589 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002590 pw.print(prefix); pw.println(" Device is currently unplugged");
2591 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2592 pw.println(getDischargeStartLevel());
2593 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2594 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002595 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002596 pw.print(prefix); pw.println(" Device is currently plugged into power");
2597 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2598 pw.println(getDischargeStartLevel());
2599 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2600 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002601 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002602 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2603 pw.println(getDischargeAmountScreenOn());
2604 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2605 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002606 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002607 } else {
2608 pw.print(prefix); pw.println(" Device battery use since last full charge");
2609 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2610 pw.println(getLowDischargeAmountSinceCharge());
2611 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2612 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002613 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2614 pw.println(getDischargeAmountScreenOnSinceCharge());
2615 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2616 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002617 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002618 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002619
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002620 BatteryStatsHelper helper = new BatteryStatsHelper(context, false);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002621 helper.create(this);
2622 helper.refreshStats(which, UserHandle.USER_ALL);
2623 List<BatterySipper> sippers = helper.getUsageList();
2624 if (sippers != null && sippers.size() > 0) {
2625 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2626 pw.print(prefix); pw.print(" Capacity: ");
2627 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002628 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07002629 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
2630 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
2631 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
2632 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002633 pw.println();
2634 for (int i=0; i<sippers.size(); i++) {
2635 BatterySipper bs = sippers.get(i);
2636 switch (bs.drainType) {
2637 case IDLE:
2638 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2639 pw.println();
2640 break;
2641 case CELL:
2642 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2643 pw.println();
2644 break;
2645 case PHONE:
2646 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2647 pw.println();
2648 break;
2649 case WIFI:
2650 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2651 pw.println();
2652 break;
2653 case BLUETOOTH:
2654 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2655 pw.println();
2656 break;
2657 case SCREEN:
2658 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2659 pw.println();
2660 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002661 case FLASHLIGHT:
2662 pw.print(prefix); pw.print(" Flashlight: "); printmAh(pw, bs.value);
2663 pw.println();
2664 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002665 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002666 pw.print(prefix); pw.print(" Uid ");
2667 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002668 pw.print(": "); printmAh(pw, bs.value); pw.println();
2669 break;
2670 case USER:
2671 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2672 pw.print(": "); printmAh(pw, bs.value); pw.println();
2673 break;
2674 case UNACCOUNTED:
2675 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2676 pw.println();
2677 break;
2678 case OVERCOUNTED:
2679 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2680 pw.println();
2681 break;
2682 }
2683 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002684 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002685 }
2686
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002687 sippers = helper.getMobilemsppList();
2688 if (sippers != null && sippers.size() > 0) {
2689 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002690 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002691 for (int i=0; i<sippers.size(); i++) {
2692 BatterySipper bs = sippers.get(i);
2693 sb.setLength(0);
2694 sb.append(prefix); sb.append(" Uid ");
2695 UserHandle.formatUid(sb, bs.uidObj.getUid());
2696 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2697 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2698 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002699 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002700 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002701 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002702 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002703 sb.setLength(0);
2704 sb.append(prefix);
2705 sb.append(" TOTAL TIME: ");
2706 formatTimeMs(sb, totalTime);
2707 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2708 sb.append(")");
2709 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002710 pw.println();
2711 }
2712
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002713 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2714 @Override
2715 public int compare(TimerEntry lhs, TimerEntry rhs) {
2716 long lhsTime = lhs.mTime;
2717 long rhsTime = rhs.mTime;
2718 if (lhsTime < rhsTime) {
2719 return 1;
2720 }
2721 if (lhsTime > rhsTime) {
2722 return -1;
2723 }
2724 return 0;
2725 }
2726 };
2727
2728 if (reqUid < 0) {
2729 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2730 if (kernelWakelocks.size() > 0) {
2731 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2732 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2733 BatteryStats.Timer timer = ent.getValue();
2734 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2735 if (totalTimeMillis > 0) {
2736 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2737 }
2738 }
2739 if (ktimers.size() > 0) {
2740 Collections.sort(ktimers, timerComparator);
2741 pw.print(prefix); pw.println(" All kernel wake locks:");
2742 for (int i=0; i<ktimers.size(); i++) {
2743 TimerEntry timer = ktimers.get(i);
2744 String linePrefix = ": ";
2745 sb.setLength(0);
2746 sb.append(prefix);
2747 sb.append(" Kernel Wake lock ");
2748 sb.append(timer.mName);
2749 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2750 which, linePrefix);
2751 if (!linePrefix.equals(": ")) {
2752 sb.append(" realtime");
2753 // Only print out wake locks that were held
2754 pw.println(sb.toString());
2755 }
2756 }
2757 pw.println();
2758 }
2759 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002760
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002761 if (timers.size() > 0) {
2762 Collections.sort(timers, timerComparator);
2763 pw.print(prefix); pw.println(" All partial wake locks:");
2764 for (int i=0; i<timers.size(); i++) {
2765 TimerEntry timer = timers.get(i);
2766 sb.setLength(0);
2767 sb.append(" Wake lock ");
2768 UserHandle.formatUid(sb, timer.mId);
2769 sb.append(" ");
2770 sb.append(timer.mName);
2771 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2772 sb.append(" realtime");
2773 pw.println(sb.toString());
2774 }
2775 timers.clear();
2776 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002777 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002778
2779 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2780 if (wakeupReasons.size() > 0) {
2781 pw.print(prefix); pw.println(" All wakeup reasons:");
2782 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2783 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2784 BatteryStats.LongCounter counter = ent.getValue();
2785 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2786 ent.getValue().getCountLocked(which)));
2787 }
2788 Collections.sort(reasons, timerComparator);
2789 for (int i=0; i<reasons.size(); i++) {
2790 TimerEntry timer = reasons.get(i);
2791 String linePrefix = ": ";
2792 sb.setLength(0);
2793 sb.append(prefix);
2794 sb.append(" Wakeup reason ");
2795 sb.append(timer.mName);
2796 sb.append(": ");
2797 formatTimeMs(sb, timer.mTime);
2798 sb.append("realtime");
2799 pw.println(sb.toString());
2800 }
2801 pw.println();
2802 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002803 }
Evan Millar22ac0432009-03-31 11:33:18 -07002804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 for (int iu=0; iu<NU; iu++) {
2806 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002807 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002808 continue;
2809 }
2810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002812
2813 pw.print(prefix);
2814 pw.print(" ");
2815 UserHandle.formatUid(pw, uid);
2816 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002818
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002819 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2820 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2821 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2822 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2823 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2824 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002825 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2826 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002827 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2828 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002829 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2830 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2831 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002832
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002833 if (mobileRxBytes > 0 || mobileTxBytes > 0
2834 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002835 pw.print(prefix); pw.print(" Mobile network: ");
2836 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002837 pw.print(formatBytesLocked(mobileTxBytes));
2838 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2839 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002841 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2842 sb.setLength(0);
2843 sb.append(prefix); sb.append(" Mobile radio active: ");
2844 formatTimeMs(sb, uidMobileActiveTime / 1000);
2845 sb.append("(");
2846 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2847 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2848 long packets = mobileRxPackets + mobileTxPackets;
2849 if (packets == 0) {
2850 packets = 1;
2851 }
2852 sb.append(" @ ");
2853 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2854 sb.append(" mspp");
2855 pw.println(sb.toString());
2856 }
2857
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002858 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002859 pw.print(prefix); pw.print(" Wi-Fi network: ");
2860 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002861 pw.print(formatBytesLocked(wifiTxBytes));
2862 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2863 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002864 }
2865
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002866 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2867 || uidWifiRunningTime != 0) {
2868 sb.setLength(0);
2869 sb.append(prefix); sb.append(" Wifi Running: ");
2870 formatTimeMs(sb, uidWifiRunningTime / 1000);
2871 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2872 whichBatteryRealtime)); sb.append(")\n");
2873 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2874 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2875 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2876 whichBatteryRealtime)); sb.append(")\n");
2877 sb.append(prefix); sb.append(" Wifi Scan: ");
2878 formatTimeMs(sb, wifiScanTime / 1000);
2879 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2880 whichBatteryRealtime)); sb.append(")");
2881 pw.println(sb.toString());
2882 }
2883
Dianne Hackborn617f8772009-03-31 15:04:46 -07002884 if (u.hasUserActivity()) {
2885 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002886 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002887 int val = u.getUserActivityCount(i, which);
2888 if (val != 0) {
2889 if (!hasData) {
2890 sb.setLength(0);
2891 sb.append(" User activity: ");
2892 hasData = true;
2893 } else {
2894 sb.append(", ");
2895 }
2896 sb.append(val);
2897 sb.append(" ");
2898 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2899 }
2900 }
2901 if (hasData) {
2902 pw.println(sb.toString());
2903 }
2904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905
2906 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2907 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002908 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2909 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2911 : wakelocks.entrySet()) {
2912 Uid.Wakelock wl = ent.getValue();
2913 String linePrefix = ": ";
2914 sb.setLength(0);
2915 sb.append(prefix);
2916 sb.append(" Wake lock ");
2917 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002918 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002920 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002922 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 "window", which, linePrefix);
2924 if (!linePrefix.equals(": ")) {
2925 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002926 // Only print out wake locks that were held
2927 pw.println(sb.toString());
2928 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002929 count++;
2930 }
2931 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002932 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002933 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002934 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002935 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002936 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002937 }
2938 if (count > 1) {
2939 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2940 sb.setLength(0);
2941 sb.append(prefix);
2942 sb.append(" TOTAL wake: ");
2943 boolean needComma = false;
2944 if (totalFull != 0) {
2945 needComma = true;
2946 formatTimeMs(sb, totalFull);
2947 sb.append("full");
2948 }
2949 if (totalPartial != 0) {
2950 if (needComma) {
2951 sb.append(", ");
2952 }
2953 needComma = true;
2954 formatTimeMs(sb, totalPartial);
2955 sb.append("partial");
2956 }
2957 if (totalWindow != 0) {
2958 if (needComma) {
2959 sb.append(", ");
2960 }
2961 needComma = true;
2962 formatTimeMs(sb, totalWindow);
2963 sb.append("window");
2964 }
2965 sb.append(" realtime");
2966 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969 }
2970
2971 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2972 if (sensors.size() > 0) {
2973 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2974 : sensors.entrySet()) {
2975 Uid.Sensor se = ent.getValue();
2976 int sensorNumber = ent.getKey();
2977 sb.setLength(0);
2978 sb.append(prefix);
2979 sb.append(" Sensor ");
2980 int handle = se.getHandle();
2981 if (handle == Uid.Sensor.GPS) {
2982 sb.append("GPS");
2983 } else {
2984 sb.append(handle);
2985 }
2986 sb.append(": ");
2987
2988 Timer timer = se.getSensorTime();
2989 if (timer != null) {
2990 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002991 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002992 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002993 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 //timer.logState();
2995 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002996 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 sb.append("realtime (");
2998 sb.append(count);
2999 sb.append(" times)");
3000 } else {
3001 sb.append("(not used)");
3002 }
3003 } else {
3004 sb.append("(not used)");
3005 }
3006
3007 pw.println(sb.toString());
3008 uidActivity = true;
3009 }
3010 }
3011
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003012 Timer vibTimer = u.getVibratorOnTimer();
3013 if (vibTimer != null) {
3014 // Convert from microseconds to milliseconds with rounding
3015 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003016 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003017 int count = vibTimer.getCountLocked(which);
3018 //timer.logState();
3019 if (totalTime != 0) {
3020 sb.setLength(0);
3021 sb.append(prefix);
3022 sb.append(" Vibrator: ");
3023 formatTimeMs(sb, totalTime);
3024 sb.append("realtime (");
3025 sb.append(count);
3026 sb.append(" times)");
3027 pw.println(sb.toString());
3028 uidActivity = true;
3029 }
3030 }
3031
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003032 Timer fgTimer = u.getForegroundActivityTimer();
3033 if (fgTimer != null) {
3034 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003035 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003036 int count = fgTimer.getCountLocked(which);
3037 if (totalTime != 0) {
3038 sb.setLength(0);
3039 sb.append(prefix);
3040 sb.append(" Foreground activities: ");
3041 formatTimeMs(sb, totalTime);
3042 sb.append("realtime (");
3043 sb.append(count);
3044 sb.append(" times)");
3045 pw.println(sb.toString());
3046 uidActivity = true;
3047 }
3048 }
3049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
3051 if (processStats.size() > 0) {
3052 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
3053 : processStats.entrySet()) {
3054 Uid.Proc ps = ent.getValue();
3055 long userTime;
3056 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003057 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003059 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060
3061 userTime = ps.getUserTime(which);
3062 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003063 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003065 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07003066 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003068 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003069 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003070 sb.setLength(0);
3071 sb.append(prefix); sb.append(" Proc ");
3072 sb.append(ent.getKey()); sb.append(":\n");
3073 sb.append(prefix); sb.append(" CPU: ");
3074 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003075 formatTime(sb, systemTime); sb.append("krn ; ");
3076 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003077 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07003078 sb.append("\n"); sb.append(prefix); sb.append(" ");
3079 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003080 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003081 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003082 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07003083 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003084 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07003085 pw.print(prefix); pw.print(" * Killed for ");
3086 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
3087 pw.print("wake lock");
3088 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
3089 pw.print("cpu");
3090 } else {
3091 pw.print("unknown");
3092 }
3093 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003094 TimeUtils.formatDuration(ew.usedTime, pw);
3095 pw.print(" over ");
3096 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003097 if (ew.overTime != 0) {
3098 pw.print(" (");
3099 pw.print((ew.usedTime*100)/ew.overTime);
3100 pw.println("%)");
3101 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003102 }
3103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 uidActivity = true;
3105 }
3106 }
3107 }
3108
3109 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
3110 if (packageStats.size() > 0) {
3111 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
3112 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003113 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 boolean apkActivity = false;
3115 Uid.Pkg ps = ent.getValue();
3116 int wakeups = ps.getWakeups(which);
3117 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003118 pw.print(prefix); pw.print(" ");
3119 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 apkActivity = true;
3121 }
3122 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3123 if (serviceStats.size() > 0) {
3124 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
3125 : serviceStats.entrySet()) {
3126 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
3127 long startTime = ss.getStartTime(batteryUptime, which);
3128 int starts = ss.getStarts(which);
3129 int launches = ss.getLaunches(which);
3130 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003131 sb.setLength(0);
3132 sb.append(prefix); sb.append(" Service ");
3133 sb.append(sent.getKey()); sb.append(":\n");
3134 sb.append(prefix); sb.append(" Created for: ");
3135 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003136 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003137 sb.append(prefix); sb.append(" Starts: ");
3138 sb.append(starts);
3139 sb.append(", launches: "); sb.append(launches);
3140 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 apkActivity = true;
3142 }
3143 }
3144 }
3145 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003146 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148 uidActivity = true;
3149 }
3150 }
3151 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003152 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 }
3154 }
3155 }
3156
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003157 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003158 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003159 int diff = oldval ^ newval;
3160 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003161 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003162 for (int i=0; i<descriptions.length; i++) {
3163 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003164 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003165 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003166 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003167 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003168 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003169 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
3170 didWake = true;
3171 pw.print("=");
3172 if (longNames) {
3173 UserHandle.formatUid(pw, wakelockTag.uid);
3174 pw.print(":\"");
3175 pw.print(wakelockTag.string);
3176 pw.print("\"");
3177 } else {
3178 pw.print(wakelockTag.poolIdx);
3179 }
3180 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003181 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003182 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003183 pw.print("=");
3184 int val = (newval&bd.mask)>>bd.shift;
3185 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003186 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003187 } else {
3188 pw.print(val);
3189 }
3190 }
3191 }
3192 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003193 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07003194 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003195 if (longNames) {
3196 UserHandle.formatUid(pw, wakelockTag.uid);
3197 pw.print(":\"");
3198 pw.print(wakelockTag.string);
3199 pw.print("\"");
3200 } else {
3201 pw.print(wakelockTag.poolIdx);
3202 }
3203 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003204 }
3205
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003206 public void prepareForDumpLocked() {
3207 }
3208
3209 public static class HistoryPrinter {
3210 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003211 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003212 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003213 int oldStatus = -1;
3214 int oldHealth = -1;
3215 int oldPlug = -1;
3216 int oldTemp = -1;
3217 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003218 long lastTime = -1;
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003219 long firstTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003220
Dianne Hackborn3251b902014-06-20 14:40:53 -07003221 void reset() {
3222 oldState = oldState2 = 0;
3223 oldLevel = -1;
3224 oldStatus = -1;
3225 oldHealth = -1;
3226 oldPlug = -1;
3227 oldTemp = -1;
3228 oldVolt = -1;
3229 }
3230
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003231 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003232 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003233 if (!checkin) {
3234 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003235 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003236 pw.print(" (");
3237 pw.print(rec.numReadInts);
3238 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003239 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003240 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3241 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003242 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003243 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003244 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003245 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003246 }
3247 lastTime = rec.time;
3248 }
3249 if (rec.cmd == HistoryItem.CMD_START) {
3250 if (checkin) {
3251 pw.print(":");
3252 }
3253 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003254 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003255 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3256 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003257 if (checkin) {
3258 pw.print(":");
3259 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003260 if (rec.cmd == HistoryItem.CMD_RESET) {
3261 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003262 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003263 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003264 pw.print("TIME:");
3265 if (checkin) {
3266 pw.println(rec.currentTime);
3267 } else {
3268 pw.print(" ");
3269 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
3270 rec.currentTime).toString());
3271 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003272 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
3273 if (checkin) {
3274 pw.print(":");
3275 }
3276 pw.println("*OVERFLOW*");
3277 } else {
3278 if (!checkin) {
3279 if (rec.batteryLevel < 10) pw.print("00");
3280 else if (rec.batteryLevel < 100) pw.print("0");
3281 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003282 if (verbose) {
3283 pw.print(" ");
3284 if (rec.states < 0) ;
3285 else if (rec.states < 0x10) pw.print("0000000");
3286 else if (rec.states < 0x100) pw.print("000000");
3287 else if (rec.states < 0x1000) pw.print("00000");
3288 else if (rec.states < 0x10000) pw.print("0000");
3289 else if (rec.states < 0x100000) pw.print("000");
3290 else if (rec.states < 0x1000000) pw.print("00");
3291 else if (rec.states < 0x10000000) pw.print("0");
3292 pw.print(Integer.toHexString(rec.states));
3293 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003294 } else {
3295 if (oldLevel != rec.batteryLevel) {
3296 oldLevel = rec.batteryLevel;
3297 pw.print(",Bl="); pw.print(rec.batteryLevel);
3298 }
3299 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003300 if (oldStatus != rec.batteryStatus) {
3301 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003302 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003303 switch (oldStatus) {
3304 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003305 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003306 break;
3307 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003308 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003309 break;
3310 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003311 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003312 break;
3313 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003314 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003315 break;
3316 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003317 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003318 break;
3319 default:
3320 pw.print(oldStatus);
3321 break;
3322 }
3323 }
3324 if (oldHealth != rec.batteryHealth) {
3325 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003326 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003327 switch (oldHealth) {
3328 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003329 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003330 break;
3331 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003332 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003333 break;
3334 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003335 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003336 break;
3337 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003338 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003339 break;
3340 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003341 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003342 break;
3343 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003344 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003345 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003346 case BatteryManager.BATTERY_HEALTH_COLD:
3347 pw.print(checkin ? "c" : "cold");
3348 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003349 default:
3350 pw.print(oldHealth);
3351 break;
3352 }
3353 }
3354 if (oldPlug != rec.batteryPlugType) {
3355 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003356 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003357 switch (oldPlug) {
3358 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003359 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003360 break;
3361 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003362 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003363 break;
3364 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003365 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003366 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003367 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003368 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003369 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003370 default:
3371 pw.print(oldPlug);
3372 break;
3373 }
3374 }
3375 if (oldTemp != rec.batteryTemperature) {
3376 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003377 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003378 pw.print(oldTemp);
3379 }
3380 if (oldVolt != rec.batteryVoltage) {
3381 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003382 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003383 pw.print(oldVolt);
3384 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003385 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003386 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003387 printBitDescriptions(pw, oldState2, rec.states2, null,
3388 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003389 if (rec.wakeReasonTag != null) {
3390 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003391 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003392 pw.print(rec.wakeReasonTag.poolIdx);
3393 } else {
3394 pw.print(" wake_reason=");
3395 pw.print(rec.wakeReasonTag.uid);
3396 pw.print(":\"");
3397 pw.print(rec.wakeReasonTag.string);
3398 pw.print("\"");
3399 }
3400 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003401 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003402 pw.print(checkin ? "," : " ");
3403 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3404 pw.print("+");
3405 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3406 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003407 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003408 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3409 : HISTORY_EVENT_NAMES;
3410 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3411 | HistoryItem.EVENT_FLAG_FINISH);
3412 if (idx >= 0 && idx < eventNames.length) {
3413 pw.print(eventNames[idx]);
3414 } else {
3415 pw.print(checkin ? "Ev" : "event");
3416 pw.print(idx);
3417 }
3418 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003419 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003420 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003421 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003422 UserHandle.formatUid(pw, rec.eventTag.uid);
3423 pw.print(":\"");
3424 pw.print(rec.eventTag.string);
3425 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003426 }
3427 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003428 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003429 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003430 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003431 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003432 }
3433 }
3434
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003435 private void printSizeValue(PrintWriter pw, long size) {
3436 float result = size;
3437 String suffix = "";
3438 if (result >= 10*1024) {
3439 suffix = "KB";
3440 result = result / 1024;
3441 }
3442 if (result >= 10*1024) {
3443 suffix = "MB";
3444 result = result / 1024;
3445 }
3446 if (result >= 10*1024) {
3447 suffix = "GB";
3448 result = result / 1024;
3449 }
3450 if (result >= 10*1024) {
3451 suffix = "TB";
3452 result = result / 1024;
3453 }
3454 if (result >= 10*1024) {
3455 suffix = "PB";
3456 result = result / 1024;
3457 }
3458 pw.print((int)result);
3459 pw.print(suffix);
3460 }
3461
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003462 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3463 int count, boolean checkin) {
3464 if (count <= 0) {
3465 return false;
3466 }
3467 if (!checkin) {
3468 pw.println(header);
3469 }
3470 String[] lineArgs = new String[1];
3471 for (int i=0; i<count; i++) {
3472 if (checkin) {
3473 lineArgs[0] = Long.toString(steps[i]);
3474 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3475 } else {
3476 pw.print(" #"); pw.print(i); pw.print(": ");
3477 TimeUtils.formatDuration(steps[i], pw);
3478 pw.println();
3479 }
3480 }
3481 return true;
3482 }
3483
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003484 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3485 public static final int DUMP_CHARGED_ONLY = 1<<1;
3486 public static final int DUMP_HISTORY_ONLY = 1<<2;
3487 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003488 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003489
Dianne Hackborn37de0982014-05-09 09:32:18 -07003490 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
3491 final HistoryPrinter hprinter = new HistoryPrinter();
3492 final HistoryItem rec = new HistoryItem();
3493 long lastTime = -1;
3494 long baseTime = -1;
3495 boolean printed = false;
3496 HistoryEventTracker tracker = null;
3497 while (getNextHistoryLocked(rec)) {
3498 lastTime = rec.time;
3499 if (baseTime < 0) {
3500 baseTime = lastTime;
3501 }
3502 if (rec.time >= histStart) {
3503 if (histStart >= 0 && !printed) {
3504 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07003505 || rec.cmd == HistoryItem.CMD_RESET
3506 || rec.cmd == HistoryItem.CMD_START) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003507 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003508 hprinter.printNextItem(pw, rec, baseTime, checkin,
3509 (flags&DUMP_VERBOSE) != 0);
3510 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003511 } else if (rec.currentTime != 0) {
3512 printed = true;
3513 byte cmd = rec.cmd;
3514 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003515 hprinter.printNextItem(pw, rec, baseTime, checkin,
3516 (flags&DUMP_VERBOSE) != 0);
3517 rec.cmd = cmd;
3518 }
3519 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003520 if (rec.cmd != HistoryItem.CMD_UPDATE) {
3521 hprinter.printNextItem(pw, rec, baseTime, checkin,
3522 (flags&DUMP_VERBOSE) != 0);
3523 rec.cmd = HistoryItem.CMD_UPDATE;
3524 }
3525 int oldEventCode = rec.eventCode;
3526 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003527 rec.eventTag = new HistoryTag();
3528 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
3529 HashMap<String, SparseIntArray> active
3530 = tracker.getStateForEvent(i);
3531 if (active == null) {
3532 continue;
3533 }
3534 for (HashMap.Entry<String, SparseIntArray> ent
3535 : active.entrySet()) {
3536 SparseIntArray uids = ent.getValue();
3537 for (int j=0; j<uids.size(); j++) {
3538 rec.eventCode = i;
3539 rec.eventTag.string = ent.getKey();
3540 rec.eventTag.uid = uids.keyAt(j);
3541 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003542 hprinter.printNextItem(pw, rec, baseTime, checkin,
3543 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003544 rec.wakeReasonTag = null;
3545 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003546 }
3547 }
3548 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003549 rec.eventCode = oldEventCode;
3550 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003551 tracker = null;
3552 }
3553 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003554 hprinter.printNextItem(pw, rec, baseTime, checkin,
3555 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003556 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
3557 // This is an attempt to aggregate the previous state and generate
3558 // fake events to reflect that state at the point where we start
3559 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07003560 if (tracker == null) {
3561 tracker = new HistoryEventTracker();
3562 }
3563 tracker.updateState(rec.eventCode, rec.eventTag.string,
3564 rec.eventTag.uid, rec.eventTag.poolIdx);
3565 }
3566 }
3567 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003568 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003569 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
3570 }
3571 }
3572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 /**
3574 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3575 *
3576 * @param pw a Printer to receive the dump output.
3577 */
3578 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003579 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003580 prepareForDumpLocked();
3581
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003582 final boolean filtering =
3583 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003584
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003585 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003586 final long historyTotalSize = getHistoryTotalSize();
3587 final long historyUsedSize = getHistoryUsedSize();
3588 if (startIteratingHistoryLocked()) {
3589 try {
3590 pw.print("Battery History (");
3591 pw.print((100*historyUsedSize)/historyTotalSize);
3592 pw.print("% used, ");
3593 printSizeValue(pw, historyUsedSize);
3594 pw.print(" used of ");
3595 printSizeValue(pw, historyTotalSize);
3596 pw.print(", ");
3597 pw.print(getHistoryStringPoolSize());
3598 pw.print(" strings using ");
3599 printSizeValue(pw, getHistoryStringPoolBytes());
3600 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003601 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003602 pw.println();
3603 } finally {
3604 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003605 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003606 }
3607
3608 if (startIteratingOldHistoryLocked()) {
3609 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003610 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003611 pw.println("Old battery History:");
3612 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003613 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003614 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003615 if (baseTime < 0) {
3616 baseTime = rec.time;
3617 }
3618 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003619 }
3620 pw.println();
3621 } finally {
3622 finishIteratingOldHistoryLocked();
3623 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003624 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003625 }
3626
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003627 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003628 return;
3629 }
3630
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003631 if (!filtering) {
3632 SparseArray<? extends Uid> uidStats = getUidStats();
3633 final int NU = uidStats.size();
3634 boolean didPid = false;
3635 long nowRealtime = SystemClock.elapsedRealtime();
3636 for (int i=0; i<NU; i++) {
3637 Uid uid = uidStats.valueAt(i);
3638 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3639 if (pids != null) {
3640 for (int j=0; j<pids.size(); j++) {
3641 Uid.Pid pid = pids.valueAt(j);
3642 if (!didPid) {
3643 pw.println("Per-PID Stats:");
3644 didPid = true;
3645 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003646 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3647 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003648 pw.print(" PID "); pw.print(pids.keyAt(j));
3649 pw.print(" wake time: ");
3650 TimeUtils.formatDuration(time, pw);
3651 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003652 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003653 }
3654 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003655 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003656 pw.println();
3657 }
3658 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3659 getNumDischargeStepDurations(), false)) {
3660 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3661 if (timeRemaining >= 0) {
3662 pw.print(" Estimated discharge time remaining: ");
3663 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3664 pw.println();
3665 }
3666 pw.println();
3667 }
3668 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3669 getNumChargeStepDurations(), false)) {
3670 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3671 if (timeRemaining >= 0) {
3672 pw.print(" Estimated charge time remaining: ");
3673 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3674 pw.println();
3675 }
3676 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003677 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003678 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003679
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003680 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003681 pw.println("Statistics since last charge:");
3682 pw.println(" System starts: " + getStartCount()
3683 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003684 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003685 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003686 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003687 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3688 pw.println("Statistics since last unplugged:");
3689 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 }
3692
3693 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003694 public void dumpCheckinLocked(Context context, PrintWriter pw,
3695 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003696 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003698 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3699
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003700 final boolean filtering =
3701 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3702
3703 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003704 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003705 try {
3706 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3707 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3708 pw.print(HISTORY_STRING_POOL); pw.print(',');
3709 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003710 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003711 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003712 pw.print(",\"");
3713 String str = getHistoryTagPoolString(i);
3714 str = str.replace("\\", "\\\\");
3715 str = str.replace("\"", "\\\"");
3716 pw.print(str);
3717 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003718 pw.println();
3719 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003720 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003721 } finally {
3722 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003723 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003724 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003725 }
3726
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003727 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003728 return;
3729 }
3730
Dianne Hackborne4a59512010-12-07 11:08:07 -08003731 if (apps != null) {
3732 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3733 for (int i=0; i<apps.size(); i++) {
3734 ApplicationInfo ai = apps.get(i);
3735 ArrayList<String> pkgs = uids.get(ai.uid);
3736 if (pkgs == null) {
3737 pkgs = new ArrayList<String>();
3738 uids.put(ai.uid, pkgs);
3739 }
3740 pkgs.add(ai.packageName);
3741 }
3742 SparseArray<? extends Uid> uidStats = getUidStats();
3743 final int NU = uidStats.size();
3744 String[] lineArgs = new String[2];
3745 for (int i=0; i<NU; i++) {
3746 int uid = uidStats.keyAt(i);
3747 ArrayList<String> pkgs = uids.get(uid);
3748 if (pkgs != null) {
3749 for (int j=0; j<pkgs.size(); j++) {
3750 lineArgs[0] = Integer.toString(uid);
3751 lineArgs[1] = pkgs.get(j);
3752 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3753 (Object[])lineArgs);
3754 }
3755 }
3756 }
3757 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003758 if (!filtering) {
3759 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
3760 getNumDischargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003761 String[] lineArgs = new String[1];
3762 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3763 if (timeRemaining >= 0) {
3764 lineArgs[0] = Long.toString(timeRemaining);
3765 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
3766 (Object[])lineArgs);
3767 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003768 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
3769 getNumChargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003770 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3771 if (timeRemaining >= 0) {
3772 lineArgs[0] = Long.toString(timeRemaining);
3773 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
3774 (Object[])lineArgs);
3775 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003776 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003777 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003778 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003779 }
3780 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003781 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 }
3783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784}