blob: 5fd697a6b4a58b9cac1d9e2dbeeeba8eb3bce5b3 [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 Hackborn0068d3dc2014-08-06 19:20:25 -070036import android.view.Display;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080037import com.android.internal.os.BatterySipper;
38import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40/**
41 * A class providing access to battery usage statistics, including information on
42 * wakelocks, processes, packages, and services. All times are represented in microseconds
43 * except where indicated otherwise.
44 * @hide
45 */
46public abstract class BatteryStats implements Parcelable {
47
48 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070049
50 /** @hide */
51 public static final String SERVICE_NAME = "batterystats";
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 /**
54 * A constant indicating a partial wake lock timer.
55 */
56 public static final int WAKE_TYPE_PARTIAL = 0;
57
58 /**
59 * A constant indicating a full wake lock timer.
60 */
61 public static final int WAKE_TYPE_FULL = 1;
62
63 /**
64 * A constant indicating a window wake lock timer.
65 */
66 public static final int WAKE_TYPE_WINDOW = 2;
67
68 /**
69 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 */
71 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070072
73 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070074 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070075 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070076 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070077
78 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070079 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070080 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070082
83 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070084 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070085 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070086 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Robert Greenwalt5347bd42009-05-13 15:10:16 -070088 /**
89 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070090 */
91 public static final int WIFI_MULTICAST_ENABLED = 7;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070094 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070095 */
96 public static final int VIDEO_TURNED_ON = 8;
97
98 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -080099 * A constant indicating a vibrator on timer
100 */
101 public static final int VIBRATOR_ON = 9;
102
103 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700104 * A constant indicating a foreground activity timer
105 */
106 public static final int FOREGROUND_ACTIVITY = 10;
107
108 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700109 * A constant indicating a wifi batched scan is active
110 */
111 public static final int WIFI_BATCHED_SCAN = 11;
112
113 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700114 * A constant indicating a process state timer
115 */
116 public static final int PROCESS_STATE = 12;
117
118 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700119 * A constant indicating a sync timer
120 */
121 public static final int SYNC = 13;
122
123 /**
124 * A constant indicating a job timer
125 */
126 public static final int JOB = 14;
127
128 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800129 * A constant indicating an audio turn on timer
130 */
131 public static final int AUDIO_TURNED_ON = 15;
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Include all of the data in the stats, including previously saved data.
135 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700136 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * Include only the current run in the stats.
140 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700141 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 /**
144 * Include only the run since the last time the device was unplugged in the stats.
145 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700146 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700147
148 // NOTE: Update this list if you add/change any stats above.
149 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700150 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700151 private static final String[] STAT_NAMES = { "l", "c", "u" };
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Bump the version on this if the checkin format changes.
155 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700156 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Evan Millar22ac0432009-03-31 11:33:18 -0700157
158 private static final long BYTES_PER_KB = 1024;
159 private static final long BYTES_PER_MB = 1048576; // 1024^2
160 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700162 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800163 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700165 private static final String PROCESS_DATA = "pr";
166 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800167 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700168 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700169 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700170 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700171 private static final String SYNC_DATA = "sy";
172 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700173 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700174 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700175 private static final String NETWORK_DATA = "nt";
176 private static final String USER_ACTIVITY_DATA = "ua";
177 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800178 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700179 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700180 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700181 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800182 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800183 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700184 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700185 private static final String SCREEN_BRIGHTNESS_DATA = "br";
186 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700187 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700188 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
189 private static final String DATA_CONNECTION_TIME_DATA = "dct";
190 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800191 private static final String WIFI_STATE_TIME_DATA = "wst";
192 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700193 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
194 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
195 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
196 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800197 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
198 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800199 private static final String POWER_USE_SUMMARY_DATA = "pws";
200 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700201 private static final String DISCHARGE_STEP_DATA = "dsd";
202 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700203 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
204 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700206 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 private final Formatter mFormatter = new Formatter(mFormatBuilder);
208
209 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700210 * State for keeping track of counting information.
211 */
212 public static abstract class Counter {
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 Hackborn617f8772009-03-31 15:04:46 -0700219 */
Evan Millarc64edde2009-04-18 12:26:32 -0700220 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700221
222 /**
223 * Temporary for debugging.
224 */
225 public abstract void logState(Printer pw, String prefix);
226 }
227
228 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700229 * State for keeping track of long counting information.
230 */
231 public static abstract class LongCounter {
232
233 /**
234 * Returns the count associated with this Counter 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
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700238 */
239 public abstract long getCountLocked(int which);
240
241 /**
242 * Temporary for debugging.
243 */
244 public abstract void logState(Printer pw, String prefix);
245 }
246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * State for keeping track of timing information.
249 */
250 public static abstract class Timer {
251
252 /**
253 * Returns the count associated with this Timer for the
254 * selected type of statistics.
255 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700256 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 */
Evan Millarc64edde2009-04-18 12:26:32 -0700258 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 /**
261 * Returns the total time in microseconds associated with this Timer for the
262 * selected type of statistics.
263 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800264 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700265 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * @return a time in microseconds
267 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800268 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * Temporary for debugging.
272 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700273 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
276 /**
277 * The statistics associated with a particular uid.
278 */
279 public static abstract class Uid {
280
281 /**
282 * Returns a mapping containing wakelock statistics.
283 *
284 * @return a Map from Strings to Uid.Wakelock objects.
285 */
286 public abstract Map<String, ? extends Wakelock> getWakelockStats();
287
288 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700289 * Returns a mapping containing sync statistics.
290 *
291 * @return a Map from Strings to Timer objects.
292 */
293 public abstract Map<String, ? extends Timer> getSyncStats();
294
295 /**
296 * Returns a mapping containing scheduled job statistics.
297 *
298 * @return a Map from Strings to Timer objects.
299 */
300 public abstract Map<String, ? extends Timer> getJobStats();
301
302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 * The statistics associated with a particular wake lock.
304 */
305 public static abstract class Wakelock {
306 public abstract Timer getWakeTime(int type);
307 }
308
309 /**
310 * Returns a mapping containing sensor statistics.
311 *
312 * @return a Map from Integer sensor ids to Uid.Sensor objects.
313 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700314 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700317 * Returns a mapping containing active process data.
318 */
319 public abstract SparseArray<? extends Pid> getPidStats();
320
321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 * Returns a mapping containing process statistics.
323 *
324 * @return a Map from Strings to Uid.Proc objects.
325 */
326 public abstract Map<String, ? extends Proc> getProcessStats();
327
328 /**
329 * Returns a mapping containing package statistics.
330 *
331 * @return a Map from Strings to Uid.Pkg objects.
332 */
333 public abstract Map<String, ? extends Pkg> getPackageStats();
334
335 /**
336 * {@hide}
337 */
338 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700339
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800340 public abstract void noteWifiRunningLocked(long elapsedRealtime);
341 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
342 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
343 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
344 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
345 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
346 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
347 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
348 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
349 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800350 public abstract void noteActivityResumedLocked(long elapsedRealtime);
351 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800352 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
353 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
354 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
355 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
356 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
357 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
358 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700359 public abstract Timer getForegroundActivityTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700360
361 // Time this uid has any processes in foreground state.
362 public static final int PROCESS_STATE_FOREGROUND = 0;
363 // Time this uid has any process in active state (not cached).
364 public static final int PROCESS_STATE_ACTIVE = 1;
365 // Time this uid has any processes running at all.
366 public static final int PROCESS_STATE_RUNNING = 2;
367 // Total number of process states we track.
368 public static final int NUM_PROCESS_STATE = 3;
369
370 static final String[] PROCESS_STATE_NAMES = {
371 "Foreground", "Active", "Running"
372 };
373
374 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
375
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800376 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Robert Greenwalta029ea12013-09-25 16:38:12 -0700378 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
379
Dianne Hackborn617f8772009-03-31 15:04:46 -0700380 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700381 * Note that these must match the constants in android.os.PowerManager.
382 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
383 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700384 */
385 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700386 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700387 };
388
Jeff Browndf693de2012-07-27 12:03:38 -0700389 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700390
Dianne Hackborn617f8772009-03-31 15:04:46 -0700391 public abstract void noteUserActivityLocked(int type);
392 public abstract boolean hasUserActivity();
393 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700394
395 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800396 public abstract long getNetworkActivityBytes(int type, int which);
397 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800398 public abstract long getMobileRadioActiveTime(int which);
399 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800402 /*
403 * FIXME: it's not correct to use this magic value because it
404 * could clash with a sensor handle (which are defined by
405 * the sensor HAL, and therefore out of our control
406 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 // Magic sensor number for the GPS.
408 public static final int GPS = -10000;
409
410 public abstract int getHandle();
411
412 public abstract Timer getSensorTime();
413 }
414
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700415 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800416 public int mWakeNesting;
417 public long mWakeSumMs;
418 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700419 }
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 /**
422 * The statistics associated with a particular process.
423 */
424 public static abstract class Proc {
425
Dianne Hackborn287952c2010-09-22 22:34:31 -0700426 public static class ExcessivePower {
427 public static final int TYPE_WAKE = 1;
428 public static final int TYPE_CPU = 2;
429
430 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700431 public long overTime;
432 public long usedTime;
433 }
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800436 * Returns true if this process is still active in the battery stats.
437 */
438 public abstract boolean isActive();
439
440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * Returns the total time (in 1/100 sec) spent executing in user code.
442 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700443 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 */
445 public abstract long getUserTime(int which);
446
447 /**
448 * Returns the total time (in 1/100 sec) spent executing in system code.
449 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700450 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 */
452 public abstract long getSystemTime(int which);
453
454 /**
455 * Returns the number of times the process has been started.
456 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700457 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
459 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700460
461 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800462 * Returns the number of times the process has crashed.
463 *
464 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
465 */
466 public abstract int getNumCrashes(int which);
467
468 /**
469 * Returns the number of times the process has ANRed.
470 *
471 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
472 */
473 public abstract int getNumAnrs(int which);
474
475 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700476 * Returns the cpu time spent in microseconds while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700477 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700478 * @return foreground cpu time in microseconds
479 */
480 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700481
482 /**
483 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
484 * @param speedStep the index of the CPU speed. This is not the actual speed of the
485 * CPU.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700486 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanie43530a2009-08-21 13:11:37 -0700487 * @see BatteryStats#getCpuSpeedSteps()
488 */
489 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700490
Dianne Hackborn287952c2010-09-22 22:34:31 -0700491 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700492
Dianne Hackborn287952c2010-09-22 22:34:31 -0700493 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
496 /**
497 * The statistics associated with a particular package.
498 */
499 public static abstract class Pkg {
500
501 /**
502 * Returns the number of times this package has done something that could wake up the
503 * device from sleep.
504 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700505 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507 public abstract int getWakeups(int which);
508
509 /**
510 * Returns a mapping containing service statistics.
511 */
512 public abstract Map<String, ? extends Serv> getServiceStats();
513
514 /**
515 * The statistics associated with a particular service.
516 */
517 public abstract class Serv {
518
519 /**
520 * Returns the amount of time spent started.
521 *
522 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700523 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * @return
525 */
526 public abstract long getStartTime(long batteryUptime, int which);
527
528 /**
529 * Returns the total number of times startService() has been called.
530 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700531 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 */
533 public abstract int getStarts(int which);
534
535 /**
536 * Returns the total number times the service has been launched.
537 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700538 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 */
540 public abstract int getLaunches(int which);
541 }
542 }
543 }
544
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800545 public final static class HistoryTag {
546 public String string;
547 public int uid;
548
549 public int poolIdx;
550
551 public void setTo(HistoryTag o) {
552 string = o.string;
553 uid = o.uid;
554 poolIdx = o.poolIdx;
555 }
556
557 public void setTo(String _string, int _uid) {
558 string = _string;
559 uid = _uid;
560 poolIdx = -1;
561 }
562
563 public void writeToParcel(Parcel dest, int flags) {
564 dest.writeString(string);
565 dest.writeInt(uid);
566 }
567
568 public void readFromParcel(Parcel src) {
569 string = src.readString();
570 uid = src.readInt();
571 poolIdx = -1;
572 }
573
574 @Override
575 public boolean equals(Object o) {
576 if (this == o) return true;
577 if (o == null || getClass() != o.getClass()) return false;
578
579 HistoryTag that = (HistoryTag) o;
580
581 if (uid != that.uid) return false;
582 if (!string.equals(that.string)) return false;
583
584 return true;
585 }
586
587 @Override
588 public int hashCode() {
589 int result = string.hashCode();
590 result = 31 * result + uid;
591 return result;
592 }
593 }
594
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700595 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700596 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700597
598 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700599 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800600
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800601 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800602 public static final byte CMD_NULL = -1;
603 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800604 public static final byte CMD_CURRENT_TIME = 5;
605 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700606 public static final byte CMD_RESET = 7;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800607
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700608 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700609
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800610 /**
611 * Return whether the command code is a delta data update.
612 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800613 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800614 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800615 }
616
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700617 public byte batteryLevel;
618 public byte batteryStatus;
619 public byte batteryHealth;
620 public byte batteryPlugType;
621
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900622 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700623 public char batteryVoltage;
624
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700625 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700626 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800627 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700628 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -0700629 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
630 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700631 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800632 public static final int STATE_PHONE_STATE_SHIFT = 6;
633 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700634 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800635 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800636 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800637
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700638 // These states always appear directly in the first int token
639 // of a delta change; they should be ones that change relatively
640 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700641 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
642 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800643 public static final int STATE_GPS_ON_FLAG = 1<<29;
644 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800645 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800646 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800647 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700648 // These are on the lower bits used for the command; if they change
649 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700650 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700651 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700652 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700653 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
654 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
655 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700656 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700657
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700658 public static final int MOST_INTERESTING_STATES =
659 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
Dianne Hackborn3251b902014-06-20 14:40:53 -0700660 | STATE_PHONE_IN_CALL_FLAG | STATE_BLUETOOTH_ON_FLAG;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700661
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700662 public int states;
663
Dianne Hackborn3251b902014-06-20 14:40:53 -0700664 // Constants from WIFI_SUPPL_STATE_*
665 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
666 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
667 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
668 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
669 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
670 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
671
672 public static final int STATE2_LOW_POWER_FLAG = 1<<31;
673 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
674 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
675 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700676 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn3251b902014-06-20 14:40:53 -0700677
678 public static final int MOST_INTERESTING_STATES2 =
679 STATE2_LOW_POWER_FLAG | STATE2_WIFI_ON_FLAG;
680
Dianne Hackborn40c87252014-03-19 16:55:40 -0700681 public int states2;
682
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800683 // The wake lock that was acquired at this point.
684 public HistoryTag wakelockTag;
685
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800686 // Kernel wakeup reason at this point.
687 public HistoryTag wakeReasonTag;
688
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800689 public static final int EVENT_FLAG_START = 0x8000;
690 public static final int EVENT_FLAG_FINISH = 0x4000;
691
692 // No event in this item.
693 public static final int EVENT_NONE = 0x0000;
694 // Event is about a process that is running.
695 public static final int EVENT_PROC = 0x0001;
696 // Event is about an application package that is in the foreground.
697 public static final int EVENT_FOREGROUND = 0x0002;
698 // Event is about an application package that is at the top of the screen.
699 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700700 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800701 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700702 // Events for all additional wake locks aquired/release within a wake block.
703 // These are not generated by default.
704 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700705 // Event is about an application executing a scheduled job.
706 public static final int EVENT_JOB = 0x0006;
707 // Events for users running.
708 public static final int EVENT_USER_RUNNING = 0x0007;
709 // Events for foreground user.
710 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800711 // Events for connectivity changed.
712 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800713 // Number of event types.
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800714 public static final int EVENT_COUNT = 0x000a;
Dianne Hackborn37de0982014-05-09 09:32:18 -0700715 // Mask to extract out only the type part of the event.
716 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800717
718 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
719 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
720 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
721 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
722 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
723 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800724 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
725 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700726 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
727 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700728 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
729 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
730 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
731 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
732 public static final int EVENT_USER_FOREGROUND_START =
733 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
734 public static final int EVENT_USER_FOREGROUND_FINISH =
735 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800736
737 // For CMD_EVENT.
738 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800739 public HistoryTag eventTag;
740
Dianne Hackborn9a755432014-05-15 17:05:22 -0700741 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800742 public long currentTime;
743
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800744 // Meta-data when reading.
745 public int numReadInts;
746
747 // Pre-allocated objects.
748 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800749 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800750 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800751
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700752 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700753 }
754
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700755 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700756 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800757 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700758 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700759 }
760
761 public int describeContents() {
762 return 0;
763 }
764
765 public void writeToParcel(Parcel dest, int flags) {
766 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700767 int bat = (((int)cmd)&0xff)
768 | ((((int)batteryLevel)<<8)&0xff00)
769 | ((((int)batteryStatus)<<16)&0xf0000)
770 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800771 | ((((int)batteryPlugType)<<24)&0xf000000)
772 | (wakelockTag != null ? 0x10000000 : 0)
773 | (wakeReasonTag != null ? 0x20000000 : 0)
774 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700775 dest.writeInt(bat);
776 bat = (((int)batteryTemperature)&0xffff)
777 | ((((int)batteryVoltage)<<16)&0xffff0000);
778 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700779 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700780 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800781 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800782 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800783 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800784 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800785 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800786 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800787 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800788 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800789 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800790 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700791 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800792 dest.writeLong(currentTime);
793 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700794 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700795
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800796 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800797 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700798 int bat = src.readInt();
799 cmd = (byte)(bat&0xff);
800 batteryLevel = (byte)((bat>>8)&0xff);
801 batteryStatus = (byte)((bat>>16)&0xf);
802 batteryHealth = (byte)((bat>>20)&0xf);
803 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800804 int bat2 = src.readInt();
805 batteryTemperature = (short)(bat2&0xffff);
806 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700807 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700808 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800809 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800810 wakelockTag = localWakelockTag;
811 wakelockTag.readFromParcel(src);
812 } else {
813 wakelockTag = null;
814 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800815 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800816 wakeReasonTag = localWakeReasonTag;
817 wakeReasonTag.readFromParcel(src);
818 } else {
819 wakeReasonTag = null;
820 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800821 if ((bat&0x40000000) != 0) {
822 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800823 eventTag = localEventTag;
824 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800825 } else {
826 eventCode = EVENT_NONE;
827 eventTag = null;
828 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700829 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800830 currentTime = src.readLong();
831 } else {
832 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700833 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800834 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700835 }
836
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700837 public void clear() {
838 time = 0;
839 cmd = CMD_NULL;
840 batteryLevel = 0;
841 batteryStatus = 0;
842 batteryHealth = 0;
843 batteryPlugType = 0;
844 batteryTemperature = 0;
845 batteryVoltage = 0;
846 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700847 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800848 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800849 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800850 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800851 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700852 }
853
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700854 public void setTo(HistoryItem o) {
855 time = o.time;
856 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800857 setToCommon(o);
858 }
859
860 public void setTo(long time, byte cmd, HistoryItem o) {
861 this.time = time;
862 this.cmd = cmd;
863 setToCommon(o);
864 }
865
866 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700867 batteryLevel = o.batteryLevel;
868 batteryStatus = o.batteryStatus;
869 batteryHealth = o.batteryHealth;
870 batteryPlugType = o.batteryPlugType;
871 batteryTemperature = o.batteryTemperature;
872 batteryVoltage = o.batteryVoltage;
873 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700874 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800875 if (o.wakelockTag != null) {
876 wakelockTag = localWakelockTag;
877 wakelockTag.setTo(o.wakelockTag);
878 } else {
879 wakelockTag = null;
880 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800881 if (o.wakeReasonTag != null) {
882 wakeReasonTag = localWakeReasonTag;
883 wakeReasonTag.setTo(o.wakeReasonTag);
884 } else {
885 wakeReasonTag = null;
886 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800887 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800888 if (o.eventTag != null) {
889 eventTag = localEventTag;
890 eventTag.setTo(o.eventTag);
891 } else {
892 eventTag = null;
893 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800894 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700895 }
896
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800897 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700898 return batteryLevel == o.batteryLevel
899 && batteryStatus == o.batteryStatus
900 && batteryHealth == o.batteryHealth
901 && batteryPlugType == o.batteryPlugType
902 && batteryTemperature == o.batteryTemperature
903 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800904 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700905 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800906 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700907 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800908
909 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800910 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800911 return false;
912 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800913 if (wakelockTag != o.wakelockTag) {
914 if (wakelockTag == null || o.wakelockTag == null) {
915 return false;
916 }
917 if (!wakelockTag.equals(o.wakelockTag)) {
918 return false;
919 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800920 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800921 if (wakeReasonTag != o.wakeReasonTag) {
922 if (wakeReasonTag == null || o.wakeReasonTag == null) {
923 return false;
924 }
925 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
926 return false;
927 }
928 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800929 if (eventTag != o.eventTag) {
930 if (eventTag == null || o.eventTag == null) {
931 return false;
932 }
933 if (!eventTag.equals(o.eventTag)) {
934 return false;
935 }
936 }
937 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800938 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700939 }
Dianne Hackborn37de0982014-05-09 09:32:18 -0700940
941 public final static class HistoryEventTracker {
942 private final HashMap<String, SparseIntArray>[] mActiveEvents
943 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
944
945 public boolean updateState(int code, String name, int uid, int poolIdx) {
946 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
947 int idx = code&HistoryItem.EVENT_TYPE_MASK;
948 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
949 if (active == null) {
950 active = new HashMap<String, SparseIntArray>();
951 mActiveEvents[idx] = active;
952 }
953 SparseIntArray uids = active.get(name);
954 if (uids == null) {
955 uids = new SparseIntArray();
956 active.put(name, uids);
957 }
958 if (uids.indexOfKey(uid) >= 0) {
959 // Already set, nothing to do!
960 return false;
961 }
962 uids.put(uid, poolIdx);
963 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
964 int idx = code&HistoryItem.EVENT_TYPE_MASK;
965 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
966 if (active == null) {
967 // not currently active, nothing to do.
968 return false;
969 }
970 SparseIntArray uids = active.get(name);
971 if (uids == null) {
972 // not currently active, nothing to do.
973 return false;
974 }
975 idx = uids.indexOfKey(uid);
976 if (idx < 0) {
977 // not currently active, nothing to do.
978 return false;
979 }
980 uids.removeAt(idx);
981 if (uids.size() <= 0) {
982 active.remove(name);
983 }
984 }
985 return true;
986 }
987
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700988 public void removeEvents(int code) {
989 int idx = code&HistoryItem.EVENT_TYPE_MASK;
990 mActiveEvents[idx] = null;
991 }
992
Dianne Hackborn37de0982014-05-09 09:32:18 -0700993 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
994 return mActiveEvents[code];
995 }
996 }
997
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700998 public static final class BitDescription {
999 public final int mask;
1000 public final int shift;
1001 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001002 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001003 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001004 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001005
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001006 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001007 this.mask = mask;
1008 this.shift = -1;
1009 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001010 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001011 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001012 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001013 }
1014
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001015 public BitDescription(int mask, int shift, String name, String shortName,
1016 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001017 this.mask = mask;
1018 this.shift = shift;
1019 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001020 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001021 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001022 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001023 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001024 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001025
Dianne Hackbornfc064132014-06-02 12:42:12 -07001026 /**
1027 * Don't allow any more batching in to the current history event. This
1028 * is called when printing partial histories, so to ensure that the next
1029 * history event will go in to a new batch after what was printed in the
1030 * last partial history.
1031 */
1032 public abstract void commitCurrentHistoryBatchLocked();
1033
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001034 public abstract int getHistoryTotalSize();
1035
1036 public abstract int getHistoryUsedSize();
1037
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001038 public abstract boolean startIteratingHistoryLocked();
1039
Dianne Hackborn099bc622014-01-22 13:39:16 -08001040 public abstract int getHistoryStringPoolSize();
1041
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001042 public abstract int getHistoryStringPoolBytes();
1043
1044 public abstract String getHistoryTagPoolString(int index);
1045
1046 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001047
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001048 public abstract boolean getNextHistoryLocked(HistoryItem out);
1049
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001050 public abstract void finishIteratingHistoryLocked();
1051
1052 public abstract boolean startIteratingOldHistoryLocked();
1053
1054 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1055
1056 public abstract void finishIteratingOldHistoryLocked();
1057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001059 * Return the base time offset for the battery history.
1060 */
1061 public abstract long getHistoryBaseTime();
1062
1063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 * Returns the number of times the device has been started.
1065 */
1066 public abstract int getStartCount();
1067
1068 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001069 * 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 -08001070 * running on battery.
1071 *
1072 * {@hide}
1073 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001074 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001076 /**
1077 * Returns the number of times the screen was turned on.
1078 *
1079 * {@hide}
1080 */
1081 public abstract int getScreenOnCount(int which);
1082
Jeff Browne95c3cd2014-05-02 16:59:26 -07001083 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1084
Dianne Hackborn617f8772009-03-31 15:04:46 -07001085 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1086 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1087 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1088 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1089 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1090
1091 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1092 "dark", "dim", "medium", "light", "bright"
1093 };
1094
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001095 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1096 "0", "1", "2", "3", "4"
1097 };
1098
Dianne Hackborn617f8772009-03-31 15:04:46 -07001099 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001100
Dianne Hackborn617f8772009-03-31 15:04:46 -07001101 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001102 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001103 * the given brightness
1104 *
1105 * {@hide}
1106 */
1107 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001108 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001111 * Returns the time in microseconds that low power mode has been enabled while the device was
1112 * running on battery.
1113 *
1114 * {@hide}
1115 */
1116 public abstract long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which);
1117
1118 /**
1119 * Returns the number of times that low power mode was enabled.
1120 *
1121 * {@hide}
1122 */
1123 public abstract int getLowPowerModeEnabledCount(int which);
1124
1125 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001126 * Returns the number of times that connectivity state changed.
1127 *
1128 * {@hide}
1129 */
1130 public abstract int getNumConnectivityChange(int which);
1131
1132 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001133 * 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 -08001134 * running on battery.
1135 *
1136 * {@hide}
1137 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001138 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001139
1140 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001141 * Returns the number of times a phone call was activated.
1142 *
1143 * {@hide}
1144 */
1145 public abstract int getPhoneOnCount(int which);
1146
1147 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001148 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001149 * the given signal strength.
1150 *
1151 * {@hide}
1152 */
1153 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001154 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001155
Dianne Hackborn617f8772009-03-31 15:04:46 -07001156 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001157 * Returns the time in microseconds that the phone has been trying to
1158 * acquire a signal.
1159 *
1160 * {@hide}
1161 */
1162 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001164
1165 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001166 * Returns the number of times the phone has entered the given signal strength.
1167 *
1168 * {@hide}
1169 */
1170 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1171
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001172 /**
1173 * Returns the time in microseconds that the mobile network has been active
1174 * (in a high power state).
1175 *
1176 * {@hide}
1177 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001178 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001179
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001180 /**
1181 * Returns the number of times that the mobile network has transitioned to the
1182 * active state.
1183 *
1184 * {@hide}
1185 */
1186 public abstract int getMobileRadioActiveCount(int which);
1187
1188 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001189 * Returns the time in microseconds that is the difference between the mobile radio
1190 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1191 * from the radio.
1192 *
1193 * {@hide}
1194 */
1195 public abstract long getMobileRadioActiveAdjustedTime(int which);
1196
1197 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001198 * Returns the time in microseconds that the mobile network has been active
1199 * (in a high power state) but not being able to blame on an app.
1200 *
1201 * {@hide}
1202 */
1203 public abstract long getMobileRadioActiveUnknownTime(int which);
1204
1205 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001206 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001207 *
1208 * {@hide}
1209 */
1210 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001211
Dianne Hackborn627bba72009-03-24 22:32:56 -07001212 public static final int DATA_CONNECTION_NONE = 0;
1213 public static final int DATA_CONNECTION_GPRS = 1;
1214 public static final int DATA_CONNECTION_EDGE = 2;
1215 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001216 public static final int DATA_CONNECTION_CDMA = 4;
1217 public static final int DATA_CONNECTION_EVDO_0 = 5;
1218 public static final int DATA_CONNECTION_EVDO_A = 6;
1219 public static final int DATA_CONNECTION_1xRTT = 7;
1220 public static final int DATA_CONNECTION_HSDPA = 8;
1221 public static final int DATA_CONNECTION_HSUPA = 9;
1222 public static final int DATA_CONNECTION_HSPA = 10;
1223 public static final int DATA_CONNECTION_IDEN = 11;
1224 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001225 public static final int DATA_CONNECTION_LTE = 13;
1226 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001227 public static final int DATA_CONNECTION_HSPAP = 15;
1228 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001229
Dianne Hackborn627bba72009-03-24 22:32:56 -07001230 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001231 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001232 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001233 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001234 };
1235
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001236 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001237
1238 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001239 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001240 * the given data connection.
1241 *
1242 * {@hide}
1243 */
1244 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001245 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001248 * Returns the number of times the phone has entered the given data
1249 * connection type.
1250 *
1251 * {@hide}
1252 */
1253 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001254
Dianne Hackborn3251b902014-06-20 14:40:53 -07001255 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1256 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1257 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1258 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1259 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1260 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1261 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1262 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1263 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1264 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1265 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1266 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1267 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1268
1269 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1270
1271 static final String[] WIFI_SUPPL_STATE_NAMES = {
1272 "invalid", "disconn", "disabled", "inactive", "scanning",
1273 "authenticating", "associating", "associated", "4-way-handshake",
1274 "group-handshake", "completed", "dormant", "uninit"
1275 };
1276
1277 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1278 "inv", "dsc", "dis", "inact", "scan",
1279 "auth", "ascing", "asced", "4-way",
1280 "group", "compl", "dorm", "uninit"
1281 };
1282
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001283 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1284 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001285 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001286 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1287 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001288 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001289 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1290 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1291 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001292 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001293 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001294 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001295 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1296 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1297 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001298 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1299 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1300 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1301 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1302 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1303 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1304 new String[] {"in", "out", "emergency", "off"},
1305 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001306 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1307 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1308 SignalStrength.SIGNAL_STRENGTH_NAMES,
1309 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001310 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1311 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1312 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001313 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001314
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001315 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1316 = new BitDescription[] {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001317 new BitDescription(HistoryItem.STATE2_LOW_POWER_FLAG, "low_power", "lp"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001318 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1319 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1320 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001321 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001322 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1323 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1324 new String[] { "0", "1", "2", "3", "4" },
1325 new String[] { "0", "1", "2", "3", "4" }),
1326 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1327 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1328 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001329 };
1330
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001331 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001332 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001333 };
1334
1335 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001336 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001337 };
1338
Dianne Hackborn617f8772009-03-31 15:04:46 -07001339 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001340 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001341 * running on battery.
1342 *
1343 * {@hide}
1344 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001345 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001346
1347 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001348 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001349 * been in the running state while the device was running on battery.
1350 *
1351 * {@hide}
1352 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001353 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001354
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001355 public static final int WIFI_STATE_OFF = 0;
1356 public static final int WIFI_STATE_OFF_SCANNING = 1;
1357 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1358 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1359 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1360 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1361 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1362 public static final int WIFI_STATE_SOFT_AP = 7;
1363
1364 static final String[] WIFI_STATE_NAMES = {
1365 "off", "scanning", "no_net", "disconn",
1366 "sta", "p2p", "sta_p2p", "soft_ap"
1367 };
1368
1369 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1370
1371 /**
1372 * Returns the time in microseconds that WiFi has been running in the given state.
1373 *
1374 * {@hide}
1375 */
1376 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001377 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001378
1379 /**
1380 * Returns the number of times that WiFi has entered the given state.
1381 *
1382 * {@hide}
1383 */
1384 public abstract int getWifiStateCount(int wifiState, int which);
1385
The Android Open Source Project10592532009-03-18 17:39:46 -07001386 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001387 * Returns the time in microseconds that the wifi supplicant has been
1388 * in a given state.
1389 *
1390 * {@hide}
1391 */
1392 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1393
1394 /**
1395 * Returns the number of times that the wifi supplicant has transitioned
1396 * to a given state.
1397 *
1398 * {@hide}
1399 */
1400 public abstract int getWifiSupplStateCount(int state, int which);
1401
1402 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1403
1404 /**
1405 * Returns the time in microseconds that WIFI has been running with
1406 * the given signal strength.
1407 *
1408 * {@hide}
1409 */
1410 public abstract long getWifiSignalStrengthTime(int strengthBin,
1411 long elapsedRealtimeUs, int which);
1412
1413 /**
1414 * Returns the number of times WIFI has entered the given signal strength.
1415 *
1416 * {@hide}
1417 */
1418 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1419
1420 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001421 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001422 * running on battery.
1423 *
1424 * {@hide}
1425 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001426 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001427
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001428 public abstract int getBluetoothPingCount();
1429
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001430 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1431 public static final int BLUETOOTH_STATE_LOW = 1;
1432 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1433 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001434
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001435 static final String[] BLUETOOTH_STATE_NAMES = {
1436 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001437 };
1438
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001439 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001440
1441 /**
1442 * Returns the time in microseconds that Bluetooth has been running in the
1443 * given active state.
1444 *
1445 * {@hide}
1446 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001447 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001448 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001449
1450 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001451 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001452 *
1453 * {@hide}
1454 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001455 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001456
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001457 /**
1458 * Returns the time in microseconds that the flashlight has been on while the device was
1459 * running on battery.
1460 *
1461 * {@hide}
1462 */
1463 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1464
1465 /**
1466 * Returns the number of times that the flashlight has been turned on while the device was
1467 * running on battery.
1468 *
1469 * {@hide}
1470 */
1471 public abstract long getFlashlightOnCount(int which);
1472
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001473 public static final int NETWORK_MOBILE_RX_DATA = 0;
1474 public static final int NETWORK_MOBILE_TX_DATA = 1;
1475 public static final int NETWORK_WIFI_RX_DATA = 2;
1476 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001477
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001478 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001479
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001480 public abstract long getNetworkActivityBytes(int type, int which);
1481 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001482
The Android Open Source Project10592532009-03-18 17:39:46 -07001483 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001484 * Return the wall clock time when battery stats data collection started.
1485 */
1486 public abstract long getStartClockTime();
1487
1488 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07001489 * Return platform version tag that we were running in when the battery stats started.
1490 */
1491 public abstract String getStartPlatformVersion();
1492
1493 /**
1494 * Return platform version tag that we were running in when the battery stats ended.
1495 */
1496 public abstract String getEndPlatformVersion();
1497
1498 /**
1499 * Return the internal version code of the parcelled format.
1500 */
1501 public abstract int getParcelVersion();
1502
1503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 * Return whether we are currently running on battery.
1505 */
1506 public abstract boolean getIsOnBattery();
1507
1508 /**
1509 * Returns a SparseArray containing the statistics for each uid.
1510 */
1511 public abstract SparseArray<? extends Uid> getUidStats();
1512
1513 /**
1514 * Returns the current battery uptime in microseconds.
1515 *
1516 * @param curTime the amount of elapsed realtime in microseconds.
1517 */
1518 public abstract long getBatteryUptime(long curTime);
1519
1520 /**
1521 * Returns the current battery realtime in microseconds.
1522 *
1523 * @param curTime the amount of elapsed realtime in microseconds.
1524 */
1525 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001526
1527 /**
Evan Millar633a1742009-04-02 16:36:33 -07001528 * Returns the battery percentage level at the last time the device was unplugged from power, or
1529 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001530 */
Evan Millar633a1742009-04-02 16:36:33 -07001531 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001532
1533 /**
Evan Millar633a1742009-04-02 16:36:33 -07001534 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1535 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001536 */
Evan Millar633a1742009-04-02 16:36:33 -07001537 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
1539 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001540 * Get the amount the battery has discharged since the stats were
1541 * last reset after charging, as a lower-end approximation.
1542 */
1543 public abstract int getLowDischargeAmountSinceCharge();
1544
1545 /**
1546 * Get the amount the battery has discharged since the stats were
1547 * last reset after charging, as an upper-end approximation.
1548 */
1549 public abstract int getHighDischargeAmountSinceCharge();
1550
1551 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001552 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1553 */
1554 public abstract int getDischargeAmount(int which);
1555
1556 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001557 * Get the amount the battery has discharged while the screen was on,
1558 * since the last time power was unplugged.
1559 */
1560 public abstract int getDischargeAmountScreenOn();
1561
1562 /**
1563 * Get the amount the battery has discharged while the screen was on,
1564 * since the last time the device was charged.
1565 */
1566 public abstract int getDischargeAmountScreenOnSinceCharge();
1567
1568 /**
1569 * Get the amount the battery has discharged while the screen was off,
1570 * since the last time power was unplugged.
1571 */
1572 public abstract int getDischargeAmountScreenOff();
1573
1574 /**
1575 * Get the amount the battery has discharged while the screen was off,
1576 * since the last time the device was charged.
1577 */
1578 public abstract int getDischargeAmountScreenOffSinceCharge();
1579
1580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 * Returns the total, last, or current battery uptime in microseconds.
1582 *
1583 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001584 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 */
1586 public abstract long computeBatteryUptime(long curTime, int which);
1587
1588 /**
1589 * Returns the total, last, or current battery realtime in microseconds.
1590 *
1591 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001592 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 */
1594 public abstract long computeBatteryRealtime(long curTime, int which);
1595
1596 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001597 * Returns the total, last, or current battery screen off uptime in microseconds.
1598 *
1599 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001600 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001601 */
1602 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1603
1604 /**
1605 * Returns the total, last, or current battery screen off realtime in microseconds.
1606 *
1607 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001608 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001609 */
1610 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1611
1612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 * Returns the total, last, or current uptime in microseconds.
1614 *
1615 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001616 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 */
1618 public abstract long computeUptime(long curTime, int which);
1619
1620 /**
1621 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001624 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 */
1626 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001627
1628 /**
1629 * Compute an approximation for how much run time (in microseconds) is remaining on
1630 * the battery. Returns -1 if no time can be computed: either there is not
1631 * enough current data to make a decision, or the battery is currently
1632 * charging.
1633 *
1634 * @param curTime The current elepsed realtime in microseconds.
1635 */
1636 public abstract long computeBatteryTimeRemaining(long curTime);
1637
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001638 // The part of a step duration that is the actual time.
1639 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
1640
1641 // Bits in a step duration that are the new battery level we are at.
1642 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
1643 public static final long STEP_LEVEL_LEVEL_SHIFT = 40;
1644
1645 // Bits in a step duration that are the initial mode we were in at that step.
1646 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
1647 public static final long STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
1648
1649 // Bits in a step duration that indicate which modes changed during that step.
1650 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
1651 public static final long STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
1652
1653 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
1654 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
1655
1656 // Step duration mode: power save is on.
1657 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
1658
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001659 /**
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001660 * Return the historical number of discharge steps we currently have.
1661 */
1662 public abstract int getNumDischargeStepDurations();
1663
1664 /**
1665 * Return the array of discharge step durations; the number of valid
1666 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1667 * These values are in milliseconds.
1668 */
1669 public abstract long[] getDischargeStepDurationsArray();
1670
1671 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001672 * Compute an approximation for how much time (in microseconds) remains until the battery
1673 * is fully charged. Returns -1 if no time can be computed: either there is not
1674 * enough current data to make a decision, or the battery is currently
1675 * discharging.
1676 *
1677 * @param curTime The current elepsed realtime in microseconds.
1678 */
1679 public abstract long computeChargeTimeRemaining(long curTime);
1680
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07001681 /**
1682 * Return the historical number of charge steps we currently have.
1683 */
1684 public abstract int getNumChargeStepDurations();
1685
1686 /**
1687 * Return the array of charge step durations; the number of valid
1688 * items in it is returned by {@link #getNumChargeStepDurations()}.
1689 * These values are in milliseconds.
1690 */
1691 public abstract long[] getChargeStepDurationsArray();
1692
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001693 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001694
Evan Millarc64edde2009-04-18 12:26:32 -07001695 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696
Amith Yamasanie43530a2009-08-21 13:11:37 -07001697 /** Returns the number of different speeds that the CPU can run at */
1698 public abstract int getCpuSpeedSteps();
1699
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001700 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1701
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001702 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 long days = seconds / (60 * 60 * 24);
1704 if (days != 0) {
1705 out.append(days);
1706 out.append("d ");
1707 }
1708 long used = days * 60 * 60 * 24;
1709
1710 long hours = (seconds - used) / (60 * 60);
1711 if (hours != 0 || used != 0) {
1712 out.append(hours);
1713 out.append("h ");
1714 }
1715 used += hours * 60 * 60;
1716
1717 long mins = (seconds-used) / 60;
1718 if (mins != 0 || used != 0) {
1719 out.append(mins);
1720 out.append("m ");
1721 }
1722 used += mins * 60;
1723
1724 if (seconds != 0 || used != 0) {
1725 out.append(seconds-used);
1726 out.append("s ");
1727 }
1728 }
1729
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001730 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001732 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 sb.append((time - (sec * 100)) * 10);
1734 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001737 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001739 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 sb.append(time - (sec * 1000));
1741 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001744 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001745 long sec = time / 1000;
1746 formatTimeRaw(sb, sec);
1747 sb.append(time - (sec * 1000));
1748 sb.append("ms");
1749 }
1750
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001751 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001753 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
1755 float perc = ((float)num) / ((float)den) * 100;
1756 mFormatBuilder.setLength(0);
1757 mFormatter.format("%.1f%%", perc);
1758 return mFormatBuilder.toString();
1759 }
1760
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001761 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001762 mFormatBuilder.setLength(0);
1763
1764 if (bytes < BYTES_PER_KB) {
1765 return bytes + "B";
1766 } else if (bytes < BYTES_PER_MB) {
1767 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1768 return mFormatBuilder.toString();
1769 } else if (bytes < BYTES_PER_GB){
1770 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1771 return mFormatBuilder.toString();
1772 } else {
1773 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1774 return mFormatBuilder.toString();
1775 }
1776 }
1777
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001778 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001779 if (timer != null) {
1780 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001781 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001782 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1783 return totalTimeMillis;
1784 }
1785 return 0;
1786 }
1787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 /**
1789 *
1790 * @param sb a StringBuilder object.
1791 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001792 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001794 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 * @param linePrefix a String to be prepended to each line of output.
1796 * @return the line prefix
1797 */
1798 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001799 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800
1801 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001802 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803
Evan Millarc64edde2009-04-18 12:26:32 -07001804 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (totalTimeMillis != 0) {
1806 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001807 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001808 if (name != null) {
1809 sb.append(name);
1810 sb.append(' ');
1811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 sb.append('(');
1813 sb.append(count);
1814 sb.append(" times)");
1815 return ", ";
1816 }
1817 }
1818 return linePrefix;
1819 }
1820
1821 /**
1822 * Checkin version of wakelock printer. Prints simple comma-separated list.
1823 *
1824 * @param sb a StringBuilder object.
1825 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001826 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001828 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 * @param linePrefix a String to be prepended to each line of output.
1830 * @return the line prefix
1831 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001832 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1833 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 long totalTimeMicros = 0;
1835 int count = 0;
1836 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001837 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001838 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 sb.append(linePrefix);
1841 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1842 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001843 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 sb.append(count);
1845 return ",";
1846 }
1847
1848 /**
1849 * Dump a comma-separated line of values for terse checkin mode.
1850 *
1851 * @param pw the PageWriter to dump log to
1852 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1853 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1854 * @param args type-dependent data arguments
1855 */
1856 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1857 Object... args ) {
1858 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1859 pw.print(uid); pw.print(',');
1860 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001861 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862
1863 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001864 pw.print(',');
1865 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001867 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001869
1870 /**
1871 * Temporary for settings.
1872 */
1873 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
1874 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
1875 }
1876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 /**
1878 * Checkin server version of dump to produce more compact, computer-readable log.
1879 *
1880 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07001882 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
1883 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1885 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1886 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1888 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001889 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1890 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1891 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 final long totalRealtime = computeRealtime(rawRealtime, which);
1893 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001894 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07001895 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001896 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001897 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001898 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1899 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1900 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1901 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 StringBuilder sb = new StringBuilder(128);
1904
Evan Millar22ac0432009-03-31 11:33:18 -07001905 SparseArray<? extends Uid> uidStats = getUidStats();
1906 final int NU = uidStats.size();
1907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 // Dump "battery" stat
1911 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001912 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001913 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001914 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001915 getStartClockTime(),
1916 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001918 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001919 long fullWakeLockTimeTotal = 0;
1920 long partialWakeLockTimeTotal = 0;
1921
1922 for (int iu = 0; iu < NU; iu++) {
1923 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001924
Evan Millar22ac0432009-03-31 11:33:18 -07001925 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1926 if (wakelocks.size() > 0) {
1927 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1928 : wakelocks.entrySet()) {
1929 Uid.Wakelock wl = ent.getValue();
1930
1931 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1932 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001933 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1934 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001935 }
1936
1937 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1938 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001939 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001940 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001941 }
1942 }
1943 }
1944 }
1945
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001946 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1947 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1948 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1949 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1950 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1951 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1952 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1953 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1954
1955 // Dump network stats
1956 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1957 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1958 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 // Dump misc stats
1961 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001962 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001963 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001964 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07001965 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
1966 0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which) / 1000,
1967 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001968 lowPowerModeEnabledTime / 1000, connChanges);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001969
1970 // Dump screen brightness stats
1971 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1972 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001973 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001974 }
1975 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001976
Dianne Hackborn627bba72009-03-24 22:32:56 -07001977 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001978 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1979 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001980 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001981 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001982 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001983 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001984 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001985 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001986 args[i] = getPhoneSignalStrengthCount(i, which);
1987 }
1988 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001989
Dianne Hackborn627bba72009-03-24 22:32:56 -07001990 // Dump network type stats
1991 args = new Object[NUM_DATA_CONNECTION_TYPES];
1992 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001993 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001994 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001995 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1996 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1997 args[i] = getPhoneDataConnectionCount(i, which);
1998 }
1999 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002000
2001 // Dump wifi state stats
2002 args = new Object[NUM_WIFI_STATES];
2003 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002004 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002005 }
2006 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2007 for (int i=0; i<NUM_WIFI_STATES; i++) {
2008 args[i] = getWifiStateCount(i, which);
2009 }
2010 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2011
Dianne Hackborn3251b902014-06-20 14:40:53 -07002012 // Dump wifi suppl state stats
2013 args = new Object[NUM_WIFI_SUPPL_STATES];
2014 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2015 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2016 }
2017 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2018 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2019 args[i] = getWifiSupplStateCount(i, which);
2020 }
2021 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2022
2023 // Dump wifi signal strength stats
2024 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2025 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2026 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2027 }
2028 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2029 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2030 args[i] = getWifiSignalStrengthCount(i, which);
2031 }
2032 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2033
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002034 // Dump bluetooth state stats
2035 args = new Object[NUM_BLUETOOTH_STATES];
2036 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002037 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002038 }
2039 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
2040 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2041 args[i] = getBluetoothStateCount(i, which);
2042 }
2043 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
2044
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002045 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002046 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002047 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002050 if (which == STATS_SINCE_UNPLUGGED) {
2051 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2052 getDischargeStartLevel()-getDischargeCurrentLevel(),
2053 getDischargeStartLevel()-getDischargeCurrentLevel(),
2054 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2055 } else {
2056 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2057 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002058 getDischargeAmountScreenOnSinceCharge(),
2059 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002060 }
2061
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002062 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002063 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002064 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002065 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002066 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002067 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002068 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002069 sb.toString());
2070 }
Evan Millarc64edde2009-04-18 12:26:32 -07002071 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002072 Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002073 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002074 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2075 // Not doing the regular wake lock formatting to remain compatible
2076 // with the old checkin format.
2077 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2078 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002079 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002080 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002081 }
2082 }
Evan Millarc64edde2009-04-18 12:26:32 -07002083 }
2084
Dianne Hackbornd953c532014-08-16 18:17:38 -07002085 BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002086 helper.create(this);
2087 helper.refreshStats(which, UserHandle.USER_ALL);
2088 List<BatterySipper> sippers = helper.getUsageList();
2089 if (sippers != null && sippers.size() > 0) {
2090 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2091 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002092 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002093 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2094 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2095 for (int i=0; i<sippers.size(); i++) {
2096 BatterySipper bs = sippers.get(i);
2097 int uid = 0;
2098 String label;
2099 switch (bs.drainType) {
2100 case IDLE:
2101 label="idle";
2102 break;
2103 case CELL:
2104 label="cell";
2105 break;
2106 case PHONE:
2107 label="phone";
2108 break;
2109 case WIFI:
2110 label="wifi";
2111 break;
2112 case BLUETOOTH:
2113 label="blue";
2114 break;
2115 case SCREEN:
2116 label="scrn";
2117 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002118 case FLASHLIGHT:
2119 label="flashlight";
2120 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002121 case APP:
2122 uid = bs.uidObj.getUid();
2123 label = "uid";
2124 break;
2125 case USER:
2126 uid = UserHandle.getUid(bs.userId, 0);
2127 label = "user";
2128 break;
2129 case UNACCOUNTED:
2130 label = "unacc";
2131 break;
2132 case OVERCOUNTED:
2133 label = "over";
2134 break;
2135 default:
2136 label = "???";
2137 }
2138 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
2139 BatteryStatsHelper.makemAh(bs.value));
2140 }
2141 }
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 for (int iu = 0; iu < NU; iu++) {
2144 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002145 if (reqUid >= 0 && uid != reqUid) {
2146 continue;
2147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 Uid u = uidStats.valueAt(iu);
2149 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002150 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2151 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2152 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2153 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2154 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2155 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002156 long mobileActiveTime = u.getMobileRadioActiveTime(which);
2157 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002158 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2159 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002160 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2161 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2162 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002163
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002164 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2165 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002166 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002167 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2168 wifiBytesRx, wifiBytesTx,
2169 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002170 wifiPacketsRx, wifiPacketsTx,
2171 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002172 }
2173
Nick Pelly6ccaa542012-06-15 15:22:47 -07002174 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002175 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002176 dumpLine(pw, uid, category, WIFI_DATA,
2177 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179
Dianne Hackborn617f8772009-03-31 15:04:46 -07002180 if (u.hasUserActivity()) {
2181 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2182 boolean hasData = false;
2183 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2184 int val = u.getUserActivityCount(i, which);
2185 args[i] = val;
2186 if (val != 0) hasData = true;
2187 }
2188 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07002189 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002190 }
2191 }
2192
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002193 Map<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 if (wakelocks.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002195 for (Map.Entry<String, ? extends Uid.Wakelock> ent : wakelocks.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 Uid.Wakelock wl = ent.getValue();
2197 String linePrefix = "";
2198 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07002199 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002200 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07002201 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002202 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07002203 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002204 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205
2206 // Only log if we had at lease one wakelock...
2207 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002208 String name = ent.getKey();
2209 if (name.indexOf(',') >= 0) {
2210 name = name.replace(',', '_');
2211 }
2212 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 }
2214 }
2215 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002216
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002217 Map<String, ? extends Timer> syncs = u.getSyncStats();
2218 if (syncs.size() > 0) {
2219 for (Map.Entry<String, ? extends Timer> ent : syncs.entrySet()) {
2220 Timer timer = ent.getValue();
2221 // Convert from microseconds to milliseconds with rounding
2222 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2223 int count = timer.getCountLocked(which);
2224 if (totalTime != 0) {
2225 dumpLine(pw, uid, category, SYNC_DATA, ent.getKey(), totalTime, count);
2226 }
2227 }
2228 }
2229
2230 Map<String, ? extends Timer> jobs = u.getJobStats();
2231 if (jobs.size() > 0) {
2232 for (Map.Entry<String, ? extends Timer> ent : jobs.entrySet()) {
2233 Timer timer = ent.getValue();
2234 // Convert from microseconds to milliseconds with rounding
2235 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2236 int count = timer.getCountLocked(which);
2237 if (totalTime != 0) {
2238 dumpLine(pw, uid, category, JOB_DATA, ent.getKey(), totalTime, count);
2239 }
2240 }
2241 }
2242
Dianne Hackborn61659e52014-07-09 16:13:01 -07002243 SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2244 int NSE = sensors.size();
2245 for (int ise=0; ise<NSE; ise++) {
2246 Uid.Sensor se = sensors.valueAt(ise);
2247 int sensorNumber = sensors.keyAt(ise);
2248 Timer timer = se.getSensorTime();
2249 if (timer != null) {
2250 // Convert from microseconds to milliseconds with rounding
2251 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2252 int count = timer.getCountLocked(which);
2253 if (totalTime != 0) {
2254 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 }
2257 }
2258
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002259 Timer vibTimer = u.getVibratorOnTimer();
2260 if (vibTimer != null) {
2261 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002262 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002263 int count = vibTimer.getCountLocked(which);
2264 if (totalTime != 0) {
2265 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
2266 }
2267 }
2268
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002269 Timer fgTimer = u.getForegroundActivityTimer();
2270 if (fgTimer != null) {
2271 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002272 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002273 int count = fgTimer.getCountLocked(which);
2274 if (totalTime != 0) {
2275 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
2276 }
2277 }
2278
Dianne Hackborn61659e52014-07-09 16:13:01 -07002279 Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
2280 long totalStateTime = 0;
2281 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
2282 totalStateTime += u.getProcessStateTime(ips, rawRealtime, which);
2283 stateTimes[ips] = (totalStateTime + 500) / 1000;
2284 }
2285 if (totalStateTime > 0) {
2286 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2287 }
2288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2290 if (processStats.size() > 0) {
2291 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2292 : processStats.entrySet()) {
2293 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002294
2295 final long userMillis = ps.getUserTime(which) * 10;
2296 final long systemMillis = ps.getSystemTime(which) * 10;
2297 final long foregroundMillis = ps.getForegroundTime(which) * 10;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002298 final int starts = ps.getStarts(which);
2299 final int numCrashes = ps.getNumCrashes(which);
2300 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002301
2302 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002303 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002304 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002305 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 }
2307 }
2308 }
2309
2310 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2311 if (packageStats.size() > 0) {
2312 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2313 : packageStats.entrySet()) {
2314
2315 Uid.Pkg ps = ent.getValue();
2316 int wakeups = ps.getWakeups(which);
2317 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2318 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2319 : serviceStats.entrySet()) {
2320 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2321 long startTime = ss.getStartTime(batteryUptime, which);
2322 int starts = ss.getStarts(which);
2323 int launches = ss.getLaunches(which);
2324 if (startTime != 0 || starts != 0 || launches != 0) {
2325 dumpLine(pw, uid, category, APK_DATA,
2326 wakeups, // wakeup alarms
2327 ent.getKey(), // Apk
2328 sent.getKey(), // service
2329 startTime / 1000, // time spent started, in ms
2330 starts,
2331 launches);
2332 }
2333 }
2334 }
2335 }
2336 }
2337 }
2338
Dianne Hackborn81038902012-11-26 17:04:09 -08002339 static final class TimerEntry {
2340 final String mName;
2341 final int mId;
2342 final BatteryStats.Timer mTimer;
2343 final long mTime;
2344 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2345 mName = name;
2346 mId = id;
2347 mTimer = timer;
2348 mTime = time;
2349 }
2350 }
2351
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002352 private void printmAh(PrintWriter printer, double power) {
2353 printer.print(BatteryStatsHelper.makemAh(power));
2354 }
2355
Dianne Hackbornd953c532014-08-16 18:17:38 -07002356 /**
2357 * Temporary for settings.
2358 */
2359 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
2360 int reqUid) {
2361 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2362 }
2363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002365 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07002366 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2368 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2369 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370
2371 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2372 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2373 final long totalRealtime = computeRealtime(rawRealtime, which);
2374 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002375 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2376 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2377 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002378 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2379 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07002382
2383 SparseArray<? extends Uid> uidStats = getUidStats();
2384 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002386 sb.setLength(0);
2387 sb.append(prefix);
2388 sb.append(" Time on battery: ");
2389 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2390 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2391 sb.append(") realtime, ");
2392 formatTimeMs(sb, whichBatteryUptime / 1000);
2393 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2394 sb.append(") uptime");
2395 pw.println(sb.toString());
2396 sb.setLength(0);
2397 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002398 sb.append(" Time on battery screen off: ");
2399 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2400 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2401 sb.append(") realtime, ");
2402 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2403 sb.append("(");
2404 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2405 sb.append(") uptime");
2406 pw.println(sb.toString());
2407 sb.setLength(0);
2408 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002409 sb.append(" Total run time: ");
2410 formatTimeMs(sb, totalRealtime / 1000);
2411 sb.append("realtime, ");
2412 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002413 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07002414 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002415 if (batteryTimeRemaining >= 0) {
2416 sb.setLength(0);
2417 sb.append(prefix);
2418 sb.append(" Battery time remaining: ");
2419 formatTimeMs(sb, batteryTimeRemaining / 1000);
2420 pw.println(sb.toString());
2421 }
2422 if (chargeTimeRemaining >= 0) {
2423 sb.setLength(0);
2424 sb.append(prefix);
2425 sb.append(" Charge time remaining: ");
2426 formatTimeMs(sb, chargeTimeRemaining / 1000);
2427 pw.println(sb.toString());
2428 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002429 pw.print(" Start clock time: ");
2430 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2431
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002432 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002433 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002434 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002435 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2436 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2437 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2438 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002439 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002440 sb.append(prefix);
2441 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2442 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002443 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002444 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2445 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07002446 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002447 pw.println(sb.toString());
2448 sb.setLength(0);
2449 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002450 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002451 boolean didOne = false;
2452 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002453 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002454 if (time == 0) {
2455 continue;
2456 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002457 sb.append("\n ");
2458 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002459 didOne = true;
2460 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2461 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002462 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002463 sb.append("(");
2464 sb.append(formatRatioLocked(time, screenOnTime));
2465 sb.append(")");
2466 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002467 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002468 pw.println(sb.toString());
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002469 if (lowPowerModeEnabledTime != 0) {
2470 sb.setLength(0);
2471 sb.append(prefix);
2472 sb.append(" Low power mode enabled: ");
2473 formatTimeMs(sb, lowPowerModeEnabledTime / 1000);
2474 sb.append("(");
2475 sb.append(formatRatioLocked(lowPowerModeEnabledTime, whichBatteryRealtime));
2476 sb.append(")");
2477 pw.println(sb.toString());
2478 }
2479 if (phoneOnTime != 0) {
2480 sb.setLength(0);
2481 sb.append(prefix);
2482 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2483 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2484 sb.append(") "); sb.append(getPhoneOnCount(which));
2485 }
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002486 int connChanges = getNumConnectivityChange(which);
2487 if (connChanges != 0) {
2488 pw.print(prefix);
2489 pw.print(" Connectivity changes: "); pw.println(connChanges);
2490 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002491
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002492 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002493 long fullWakeLockTimeTotalMicros = 0;
2494 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002495
Dianne Hackborn81038902012-11-26 17:04:09 -08002496 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2497
Evan Millar22ac0432009-03-31 11:33:18 -07002498 for (int iu = 0; iu < NU; iu++) {
2499 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002500
Evan Millar22ac0432009-03-31 11:33:18 -07002501 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2502 if (wakelocks.size() > 0) {
2503 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2504 : wakelocks.entrySet()) {
2505 Uid.Wakelock wl = ent.getValue();
2506
2507 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2508 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002509 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002510 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002511 }
2512
2513 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2514 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002515 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002516 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002517 if (totalTimeMicros > 0) {
2518 if (reqUid < 0) {
2519 // Only show the ordered list of all wake
2520 // locks if the caller is not asking for data
2521 // about a specific uid.
2522 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2523 partialWakeTimer, totalTimeMicros));
2524 }
2525 partialWakeLockTimeTotalMicros += totalTimeMicros;
2526 }
Evan Millar22ac0432009-03-31 11:33:18 -07002527 }
2528 }
2529 }
2530 }
2531
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002532 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2533 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2534 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2535 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2536 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2537 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2538 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2539 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2540
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002541 if (fullWakeLockTimeTotalMicros != 0) {
2542 sb.setLength(0);
2543 sb.append(prefix);
2544 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2545 (fullWakeLockTimeTotalMicros + 500) / 1000);
2546 pw.println(sb.toString());
2547 }
2548
2549 if (partialWakeLockTimeTotalMicros != 0) {
2550 sb.setLength(0);
2551 sb.append(prefix);
2552 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2553 (partialWakeLockTimeTotalMicros + 500) / 1000);
2554 pw.println(sb.toString());
2555 }
2556
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002557 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002558 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2559 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2560 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2561 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002562 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002563 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07002564 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002565 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002566 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002567 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002568 if (time == 0) {
2569 continue;
2570 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002571 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002572 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002573 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002574 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002575 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002576 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002577 sb.append("(");
2578 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002579 sb.append(") ");
2580 sb.append(getPhoneSignalStrengthCount(i, which));
2581 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002582 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002583 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002584 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002585
2586 sb.setLength(0);
2587 sb.append(prefix);
2588 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002589 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002590 pw.println(sb.toString());
2591
Dianne Hackborn627bba72009-03-24 22:32:56 -07002592 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002593 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002594 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002595 didOne = false;
2596 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002597 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002598 if (time == 0) {
2599 continue;
2600 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002601 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002602 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002603 didOne = true;
2604 sb.append(DATA_CONNECTION_NAMES[i]);
2605 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002606 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002607 sb.append("(");
2608 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002609 sb.append(") ");
2610 sb.append(getPhoneDataConnectionCount(i, which));
2611 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002612 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002613 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002614 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002615
2616 sb.setLength(0);
2617 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002618 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002619 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002620 formatTimeMs(sb, mobileActiveTime / 1000);
2621 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2622 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2623 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002624 pw.println(sb.toString());
2625
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002626 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2627 if (mobileActiveUnknownTime != 0) {
2628 sb.setLength(0);
2629 sb.append(prefix);
2630 sb.append(" Mobile radio active unknown time: ");
2631 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2632 sb.append("(");
2633 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2634 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2635 sb.append("x");
2636 pw.println(sb.toString());
2637 }
2638
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002639 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2640 if (mobileActiveAdjustedTime != 0) {
2641 sb.setLength(0);
2642 sb.append(prefix);
2643 sb.append(" Mobile radio active adjusted time: ");
2644 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2645 sb.append("(");
2646 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2647 sb.append(")");
2648 pw.println(sb.toString());
2649 }
2650
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002651 pw.print(prefix);
2652 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2653 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2654 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2655 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002656 sb.setLength(0);
2657 sb.append(prefix);
2658 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2659 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2660 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2661 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002662 sb.append(")");
2663 pw.println(sb.toString());
2664
2665 sb.setLength(0);
2666 sb.append(prefix);
2667 sb.append(" Wifi states:");
2668 didOne = false;
2669 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002670 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002671 if (time == 0) {
2672 continue;
2673 }
2674 sb.append("\n ");
2675 didOne = true;
2676 sb.append(WIFI_STATE_NAMES[i]);
2677 sb.append(" ");
2678 formatTimeMs(sb, time/1000);
2679 sb.append("(");
2680 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2681 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07002682 sb.append(getWifiStateCount(i, which));
2683 sb.append("x");
2684 }
2685 if (!didOne) sb.append(" (no activity)");
2686 pw.println(sb.toString());
2687
2688 sb.setLength(0);
2689 sb.append(prefix);
2690 sb.append(" Wifi supplicant states:");
2691 didOne = false;
2692 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2693 final long time = getWifiSupplStateTime(i, rawRealtime, which);
2694 if (time == 0) {
2695 continue;
2696 }
2697 sb.append("\n ");
2698 didOne = true;
2699 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
2700 sb.append(" ");
2701 formatTimeMs(sb, time/1000);
2702 sb.append("(");
2703 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2704 sb.append(") ");
2705 sb.append(getWifiSupplStateCount(i, which));
2706 sb.append("x");
2707 }
2708 if (!didOne) sb.append(" (no activity)");
2709 pw.println(sb.toString());
2710
2711 sb.setLength(0);
2712 sb.append(prefix);
2713 sb.append(" Wifi signal levels:");
2714 didOne = false;
2715 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2716 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
2717 if (time == 0) {
2718 continue;
2719 }
2720 sb.append("\n ");
2721 sb.append(prefix);
2722 didOne = true;
2723 sb.append("level(");
2724 sb.append(i);
2725 sb.append(") ");
2726 formatTimeMs(sb, time/1000);
2727 sb.append("(");
2728 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2729 sb.append(") ");
2730 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002731 sb.append("x");
2732 }
2733 if (!didOne) sb.append(" (no activity)");
2734 pw.println(sb.toString());
2735
2736 sb.setLength(0);
2737 sb.append(prefix);
2738 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002739 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2740 sb.append(")");
2741 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002742
2743 sb.setLength(0);
2744 sb.append(prefix);
2745 sb.append(" Bluetooth states:");
2746 didOne = false;
2747 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002748 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002749 if (time == 0) {
2750 continue;
2751 }
2752 sb.append("\n ");
2753 didOne = true;
2754 sb.append(BLUETOOTH_STATE_NAMES[i]);
2755 sb.append(" ");
2756 formatTimeMs(sb, time/1000);
2757 sb.append("(");
2758 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2759 sb.append(") ");
2760 sb.append(getPhoneDataConnectionCount(i, which));
2761 sb.append("x");
2762 }
2763 if (!didOne) sb.append(" (no activity)");
2764 pw.println(sb.toString());
2765
2766 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002767
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002768 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002769 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002770 pw.print(prefix); pw.println(" Device is currently unplugged");
2771 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2772 pw.println(getDischargeStartLevel());
2773 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2774 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002775 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002776 pw.print(prefix); pw.println(" Device is currently plugged into power");
2777 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2778 pw.println(getDischargeStartLevel());
2779 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2780 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002781 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002782 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2783 pw.println(getDischargeAmountScreenOn());
2784 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2785 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002786 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002787 } else {
2788 pw.print(prefix); pw.println(" Device battery use since last full charge");
2789 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2790 pw.println(getLowDischargeAmountSinceCharge());
2791 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2792 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002793 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2794 pw.println(getDischargeAmountScreenOnSinceCharge());
2795 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2796 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002797 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002798 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002799
Dianne Hackbornd953c532014-08-16 18:17:38 -07002800 BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002801 helper.create(this);
2802 helper.refreshStats(which, UserHandle.USER_ALL);
2803 List<BatterySipper> sippers = helper.getUsageList();
2804 if (sippers != null && sippers.size() > 0) {
2805 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2806 pw.print(prefix); pw.print(" Capacity: ");
2807 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002808 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07002809 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
2810 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
2811 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
2812 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002813 pw.println();
2814 for (int i=0; i<sippers.size(); i++) {
2815 BatterySipper bs = sippers.get(i);
2816 switch (bs.drainType) {
2817 case IDLE:
2818 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2819 pw.println();
2820 break;
2821 case CELL:
2822 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2823 pw.println();
2824 break;
2825 case PHONE:
2826 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2827 pw.println();
2828 break;
2829 case WIFI:
2830 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2831 pw.println();
2832 break;
2833 case BLUETOOTH:
2834 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2835 pw.println();
2836 break;
2837 case SCREEN:
2838 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2839 pw.println();
2840 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002841 case FLASHLIGHT:
2842 pw.print(prefix); pw.print(" Flashlight: "); printmAh(pw, bs.value);
2843 pw.println();
2844 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002845 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002846 pw.print(prefix); pw.print(" Uid ");
2847 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002848 pw.print(": "); printmAh(pw, bs.value); pw.println();
2849 break;
2850 case USER:
2851 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2852 pw.print(": "); printmAh(pw, bs.value); pw.println();
2853 break;
2854 case UNACCOUNTED:
2855 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2856 pw.println();
2857 break;
2858 case OVERCOUNTED:
2859 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2860 pw.println();
2861 break;
2862 }
2863 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002864 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002865 }
2866
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002867 sippers = helper.getMobilemsppList();
2868 if (sippers != null && sippers.size() > 0) {
2869 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002870 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002871 for (int i=0; i<sippers.size(); i++) {
2872 BatterySipper bs = sippers.get(i);
2873 sb.setLength(0);
2874 sb.append(prefix); sb.append(" Uid ");
2875 UserHandle.formatUid(sb, bs.uidObj.getUid());
2876 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2877 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2878 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002879 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002880 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002881 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002882 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002883 sb.setLength(0);
2884 sb.append(prefix);
2885 sb.append(" TOTAL TIME: ");
2886 formatTimeMs(sb, totalTime);
2887 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2888 sb.append(")");
2889 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002890 pw.println();
2891 }
2892
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002893 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2894 @Override
2895 public int compare(TimerEntry lhs, TimerEntry rhs) {
2896 long lhsTime = lhs.mTime;
2897 long rhsTime = rhs.mTime;
2898 if (lhsTime < rhsTime) {
2899 return 1;
2900 }
2901 if (lhsTime > rhsTime) {
2902 return -1;
2903 }
2904 return 0;
2905 }
2906 };
2907
2908 if (reqUid < 0) {
2909 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2910 if (kernelWakelocks.size() > 0) {
2911 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2912 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2913 BatteryStats.Timer timer = ent.getValue();
2914 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2915 if (totalTimeMillis > 0) {
2916 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2917 }
2918 }
2919 if (ktimers.size() > 0) {
2920 Collections.sort(ktimers, timerComparator);
2921 pw.print(prefix); pw.println(" All kernel wake locks:");
2922 for (int i=0; i<ktimers.size(); i++) {
2923 TimerEntry timer = ktimers.get(i);
2924 String linePrefix = ": ";
2925 sb.setLength(0);
2926 sb.append(prefix);
2927 sb.append(" Kernel Wake lock ");
2928 sb.append(timer.mName);
2929 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2930 which, linePrefix);
2931 if (!linePrefix.equals(": ")) {
2932 sb.append(" realtime");
2933 // Only print out wake locks that were held
2934 pw.println(sb.toString());
2935 }
2936 }
2937 pw.println();
2938 }
2939 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002940
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002941 if (timers.size() > 0) {
2942 Collections.sort(timers, timerComparator);
2943 pw.print(prefix); pw.println(" All partial wake locks:");
2944 for (int i=0; i<timers.size(); i++) {
2945 TimerEntry timer = timers.get(i);
2946 sb.setLength(0);
2947 sb.append(" Wake lock ");
2948 UserHandle.formatUid(sb, timer.mId);
2949 sb.append(" ");
2950 sb.append(timer.mName);
2951 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2952 sb.append(" realtime");
2953 pw.println(sb.toString());
2954 }
2955 timers.clear();
2956 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002957 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002958
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002959 Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002960 if (wakeupReasons.size() > 0) {
2961 pw.print(prefix); pw.println(" All wakeup reasons:");
2962 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002963 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2964 Timer timer = ent.getValue();
2965 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
2966 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002967 }
2968 Collections.sort(reasons, timerComparator);
2969 for (int i=0; i<reasons.size(); i++) {
2970 TimerEntry timer = reasons.get(i);
2971 String linePrefix = ": ";
2972 sb.setLength(0);
2973 sb.append(prefix);
2974 sb.append(" Wakeup reason ");
2975 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002976 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2977 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002978 pw.println(sb.toString());
2979 }
2980 pw.println();
2981 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002982 }
Evan Millar22ac0432009-03-31 11:33:18 -07002983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 for (int iu=0; iu<NU; iu++) {
2985 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002986 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002987 continue;
2988 }
2989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002991
2992 pw.print(prefix);
2993 pw.print(" ");
2994 UserHandle.formatUid(pw, uid);
2995 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002997
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002998 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2999 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3000 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3001 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3002 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3003 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003004 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3005 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003006 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3007 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003008 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3009 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3010 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003011
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003012 if (mobileRxBytes > 0 || mobileTxBytes > 0
3013 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003014 pw.print(prefix); pw.print(" Mobile network: ");
3015 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003016 pw.print(formatBytesLocked(mobileTxBytes));
3017 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3018 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003020 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3021 sb.setLength(0);
3022 sb.append(prefix); sb.append(" Mobile radio active: ");
3023 formatTimeMs(sb, uidMobileActiveTime / 1000);
3024 sb.append("(");
3025 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3026 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3027 long packets = mobileRxPackets + mobileTxPackets;
3028 if (packets == 0) {
3029 packets = 1;
3030 }
3031 sb.append(" @ ");
3032 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3033 sb.append(" mspp");
3034 pw.println(sb.toString());
3035 }
3036
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003037 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003038 pw.print(prefix); pw.print(" Wi-Fi network: ");
3039 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003040 pw.print(formatBytesLocked(wifiTxBytes));
3041 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3042 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003043 }
3044
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003045 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
3046 || uidWifiRunningTime != 0) {
3047 sb.setLength(0);
3048 sb.append(prefix); sb.append(" Wifi Running: ");
3049 formatTimeMs(sb, uidWifiRunningTime / 1000);
3050 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3051 whichBatteryRealtime)); sb.append(")\n");
3052 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3053 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3054 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3055 whichBatteryRealtime)); sb.append(")\n");
3056 sb.append(prefix); sb.append(" Wifi Scan: ");
3057 formatTimeMs(sb, wifiScanTime / 1000);
3058 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
3059 whichBatteryRealtime)); sb.append(")");
3060 pw.println(sb.toString());
3061 }
3062
Dianne Hackborn617f8772009-03-31 15:04:46 -07003063 if (u.hasUserActivity()) {
3064 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07003065 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003066 int val = u.getUserActivityCount(i, which);
3067 if (val != 0) {
3068 if (!hasData) {
3069 sb.setLength(0);
3070 sb.append(" User activity: ");
3071 hasData = true;
3072 } else {
3073 sb.append(", ");
3074 }
3075 sb.append(val);
3076 sb.append(" ");
3077 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3078 }
3079 }
3080 if (hasData) {
3081 pw.println(sb.toString());
3082 }
3083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084
3085 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
3086 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003087 long totalFull = 0, totalPartial = 0, totalWindow = 0;
3088 int count = 0;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003089 for (Map.Entry<String, ? extends Uid.Wakelock> ent : wakelocks.entrySet()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 Uid.Wakelock wl = ent.getValue();
3091 String linePrefix = ": ";
3092 sb.setLength(0);
3093 sb.append(prefix);
3094 sb.append(" Wake lock ");
3095 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003096 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003098 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003100 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 "window", which, linePrefix);
Dianne Hackbornacc4a122014-08-18 16:33:44 -07003102 if (true || !linePrefix.equals(": ")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05003104 // Only print out wake locks that were held
3105 pw.println(sb.toString());
3106 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003107 count++;
3108 }
3109 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003110 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003111 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003112 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003113 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003114 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003115 }
3116 if (count > 1) {
3117 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
3118 sb.setLength(0);
3119 sb.append(prefix);
3120 sb.append(" TOTAL wake: ");
3121 boolean needComma = false;
3122 if (totalFull != 0) {
3123 needComma = true;
3124 formatTimeMs(sb, totalFull);
3125 sb.append("full");
3126 }
3127 if (totalPartial != 0) {
3128 if (needComma) {
3129 sb.append(", ");
3130 }
3131 needComma = true;
3132 formatTimeMs(sb, totalPartial);
3133 sb.append("partial");
3134 }
3135 if (totalWindow != 0) {
3136 if (needComma) {
3137 sb.append(", ");
3138 }
3139 needComma = true;
3140 formatTimeMs(sb, totalWindow);
3141 sb.append("window");
3142 }
3143 sb.append(" realtime");
3144 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
3147 }
3148
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003149 Map<String, ? extends Timer> syncs = u.getSyncStats();
3150 if (syncs.size() > 0) {
3151 for (Map.Entry<String, ? extends Timer> ent : syncs.entrySet()) {
3152 Timer timer = ent.getValue();
3153 // Convert from microseconds to milliseconds with rounding
3154 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3155 int count = timer.getCountLocked(which);
3156 sb.setLength(0);
3157 sb.append(prefix);
3158 sb.append(" Sync ");
3159 sb.append(ent.getKey());
3160 sb.append(": ");
3161 if (totalTime != 0) {
3162 formatTimeMs(sb, totalTime);
3163 sb.append("realtime (");
3164 sb.append(count);
3165 sb.append(" times)");
3166 } else {
3167 sb.append("(not used)");
3168 }
3169 pw.println(sb.toString());
3170 uidActivity = true;
3171 }
3172 }
3173
3174 Map<String, ? extends Timer> jobs = u.getJobStats();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003175 if (jobs.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003176 for (Map.Entry<String, ? extends Timer> ent : jobs.entrySet()) {
3177 Timer timer = ent.getValue();
3178 // Convert from microseconds to milliseconds with rounding
3179 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3180 int count = timer.getCountLocked(which);
3181 sb.setLength(0);
3182 sb.append(prefix);
3183 sb.append(" Job ");
3184 sb.append(ent.getKey());
3185 sb.append(": ");
3186 if (totalTime != 0) {
3187 formatTimeMs(sb, totalTime);
3188 sb.append("realtime (");
3189 sb.append(count);
3190 sb.append(" times)");
3191 } else {
3192 sb.append("(not used)");
3193 }
3194 pw.println(sb.toString());
3195 uidActivity = true;
3196 }
3197 }
3198
Dianne Hackborn61659e52014-07-09 16:13:01 -07003199 SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3200 int NSE = sensors.size();
3201 for (int ise=0; ise<NSE; ise++) {
3202 Uid.Sensor se = sensors.valueAt(ise);
3203 int sensorNumber = sensors.keyAt(ise);
3204 sb.setLength(0);
3205 sb.append(prefix);
3206 sb.append(" Sensor ");
3207 int handle = se.getHandle();
3208 if (handle == Uid.Sensor.GPS) {
3209 sb.append("GPS");
3210 } else {
3211 sb.append(handle);
3212 }
3213 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214
Dianne Hackborn61659e52014-07-09 16:13:01 -07003215 Timer timer = se.getSensorTime();
3216 if (timer != null) {
3217 // Convert from microseconds to milliseconds with rounding
3218 long totalTime = (timer.getTotalTimeLocked(
3219 rawRealtime, which) + 500) / 1000;
3220 int count = timer.getCountLocked(which);
3221 //timer.logState();
3222 if (totalTime != 0) {
3223 formatTimeMs(sb, totalTime);
3224 sb.append("realtime (");
3225 sb.append(count);
3226 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 } else {
3228 sb.append("(not used)");
3229 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003230 } else {
3231 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003233
3234 pw.println(sb.toString());
3235 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 }
3237
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003238 Timer vibTimer = u.getVibratorOnTimer();
3239 if (vibTimer != null) {
3240 // Convert from microseconds to milliseconds with rounding
3241 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003242 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003243 int count = vibTimer.getCountLocked(which);
3244 //timer.logState();
3245 if (totalTime != 0) {
3246 sb.setLength(0);
3247 sb.append(prefix);
3248 sb.append(" Vibrator: ");
3249 formatTimeMs(sb, totalTime);
3250 sb.append("realtime (");
3251 sb.append(count);
3252 sb.append(" times)");
3253 pw.println(sb.toString());
3254 uidActivity = true;
3255 }
3256 }
3257
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003258 Timer fgTimer = u.getForegroundActivityTimer();
3259 if (fgTimer != null) {
3260 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003261 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003262 int count = fgTimer.getCountLocked(which);
3263 if (totalTime != 0) {
3264 sb.setLength(0);
3265 sb.append(prefix);
3266 sb.append(" Foreground activities: ");
3267 formatTimeMs(sb, totalTime);
3268 sb.append("realtime (");
3269 sb.append(count);
3270 sb.append(" times)");
3271 pw.println(sb.toString());
3272 uidActivity = true;
3273 }
3274 }
3275
Dianne Hackborn61659e52014-07-09 16:13:01 -07003276 long totalStateTime = 0;
3277 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
3278 long time = u.getProcessStateTime(ips, rawRealtime, which);
3279 if (time > 0) {
3280 totalStateTime += time;
3281 sb.setLength(0);
3282 sb.append(prefix);
3283 sb.append(" ");
3284 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
3285 sb.append(" for: ");
3286 formatTimeMs(sb, (totalStateTime + 500) / 1000);
3287 pw.println(sb.toString());
3288 uidActivity = true;
3289 }
3290 }
3291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
3293 if (processStats.size() > 0) {
3294 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
3295 : processStats.entrySet()) {
3296 Uid.Proc ps = ent.getValue();
3297 long userTime;
3298 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003299 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003301 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302
3303 userTime = ps.getUserTime(which);
3304 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003305 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 starts = ps.getStarts(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003307 final int numCrashes = ps.getNumCrashes(which);
3308 final int numAnrs = ps.getNumAnrs(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003309 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07003310 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003312 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003313 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003314 sb.setLength(0);
3315 sb.append(prefix); sb.append(" Proc ");
3316 sb.append(ent.getKey()); sb.append(":\n");
3317 sb.append(prefix); sb.append(" CPU: ");
3318 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003319 formatTime(sb, systemTime); sb.append("krn ; ");
3320 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003321 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07003322 sb.append("\n"); sb.append(prefix); sb.append(" ");
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003323 boolean hasOne = false;
3324 if (starts != 0) {
3325 hasOne = true;
3326 sb.append(starts); sb.append(" starts");
3327 }
3328 if (numCrashes != 0) {
3329 if (hasOne) {
3330 sb.append(", ");
3331 }
3332 hasOne = true;
3333 sb.append(numCrashes); sb.append(" crashes");
3334 }
3335 if (numAnrs != 0) {
3336 if (hasOne) {
3337 sb.append(", ");
3338 }
3339 sb.append(numAnrs); sb.append(" anrs");
3340 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003341 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003342 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003343 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07003344 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003345 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07003346 pw.print(prefix); pw.print(" * Killed for ");
3347 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
3348 pw.print("wake lock");
3349 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
3350 pw.print("cpu");
3351 } else {
3352 pw.print("unknown");
3353 }
3354 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003355 TimeUtils.formatDuration(ew.usedTime, pw);
3356 pw.print(" over ");
3357 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003358 if (ew.overTime != 0) {
3359 pw.print(" (");
3360 pw.print((ew.usedTime*100)/ew.overTime);
3361 pw.println("%)");
3362 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003363 }
3364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 uidActivity = true;
3366 }
3367 }
3368 }
3369
3370 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
3371 if (packageStats.size() > 0) {
3372 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
3373 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003374 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 boolean apkActivity = false;
3376 Uid.Pkg ps = ent.getValue();
3377 int wakeups = ps.getWakeups(which);
3378 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003379 pw.print(prefix); pw.print(" ");
3380 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 apkActivity = true;
3382 }
3383 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3384 if (serviceStats.size() > 0) {
3385 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
3386 : serviceStats.entrySet()) {
3387 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
3388 long startTime = ss.getStartTime(batteryUptime, which);
3389 int starts = ss.getStarts(which);
3390 int launches = ss.getLaunches(which);
3391 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003392 sb.setLength(0);
3393 sb.append(prefix); sb.append(" Service ");
3394 sb.append(sent.getKey()); sb.append(":\n");
3395 sb.append(prefix); sb.append(" Created for: ");
3396 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003397 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003398 sb.append(prefix); sb.append(" Starts: ");
3399 sb.append(starts);
3400 sb.append(", launches: "); sb.append(launches);
3401 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 apkActivity = true;
3403 }
3404 }
3405 }
3406 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003407 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 }
3409 uidActivity = true;
3410 }
3411 }
3412 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003413 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 }
3415 }
3416 }
3417
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003418 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003419 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003420 int diff = oldval ^ newval;
3421 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003422 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003423 for (int i=0; i<descriptions.length; i++) {
3424 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003425 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003426 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003427 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003428 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003429 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003430 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
3431 didWake = true;
3432 pw.print("=");
3433 if (longNames) {
3434 UserHandle.formatUid(pw, wakelockTag.uid);
3435 pw.print(":\"");
3436 pw.print(wakelockTag.string);
3437 pw.print("\"");
3438 } else {
3439 pw.print(wakelockTag.poolIdx);
3440 }
3441 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003442 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003443 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003444 pw.print("=");
3445 int val = (newval&bd.mask)>>bd.shift;
3446 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003447 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003448 } else {
3449 pw.print(val);
3450 }
3451 }
3452 }
3453 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003454 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07003455 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003456 if (longNames) {
3457 UserHandle.formatUid(pw, wakelockTag.uid);
3458 pw.print(":\"");
3459 pw.print(wakelockTag.string);
3460 pw.print("\"");
3461 } else {
3462 pw.print(wakelockTag.poolIdx);
3463 }
3464 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003465 }
3466
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003467 public void prepareForDumpLocked() {
3468 }
3469
3470 public static class HistoryPrinter {
3471 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003472 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003473 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003474 int oldStatus = -1;
3475 int oldHealth = -1;
3476 int oldPlug = -1;
3477 int oldTemp = -1;
3478 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003479 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003480
Dianne Hackborn3251b902014-06-20 14:40:53 -07003481 void reset() {
3482 oldState = oldState2 = 0;
3483 oldLevel = -1;
3484 oldStatus = -1;
3485 oldHealth = -1;
3486 oldPlug = -1;
3487 oldTemp = -1;
3488 oldVolt = -1;
3489 }
3490
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003491 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003492 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003493 if (!checkin) {
3494 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003495 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003496 pw.print(" (");
3497 pw.print(rec.numReadInts);
3498 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003499 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003500 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3501 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003502 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003503 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003504 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003505 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003506 }
3507 lastTime = rec.time;
3508 }
3509 if (rec.cmd == HistoryItem.CMD_START) {
3510 if (checkin) {
3511 pw.print(":");
3512 }
3513 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003514 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003515 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3516 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003517 if (checkin) {
3518 pw.print(":");
3519 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003520 if (rec.cmd == HistoryItem.CMD_RESET) {
3521 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003522 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003523 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003524 pw.print("TIME:");
3525 if (checkin) {
3526 pw.println(rec.currentTime);
3527 } else {
3528 pw.print(" ");
3529 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
3530 rec.currentTime).toString());
3531 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003532 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
3533 if (checkin) {
3534 pw.print(":");
3535 }
3536 pw.println("*OVERFLOW*");
3537 } else {
3538 if (!checkin) {
3539 if (rec.batteryLevel < 10) pw.print("00");
3540 else if (rec.batteryLevel < 100) pw.print("0");
3541 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003542 if (verbose) {
3543 pw.print(" ");
3544 if (rec.states < 0) ;
3545 else if (rec.states < 0x10) pw.print("0000000");
3546 else if (rec.states < 0x100) pw.print("000000");
3547 else if (rec.states < 0x1000) pw.print("00000");
3548 else if (rec.states < 0x10000) pw.print("0000");
3549 else if (rec.states < 0x100000) pw.print("000");
3550 else if (rec.states < 0x1000000) pw.print("00");
3551 else if (rec.states < 0x10000000) pw.print("0");
3552 pw.print(Integer.toHexString(rec.states));
3553 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003554 } else {
3555 if (oldLevel != rec.batteryLevel) {
3556 oldLevel = rec.batteryLevel;
3557 pw.print(",Bl="); pw.print(rec.batteryLevel);
3558 }
3559 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003560 if (oldStatus != rec.batteryStatus) {
3561 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003562 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003563 switch (oldStatus) {
3564 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003565 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003566 break;
3567 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003568 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003569 break;
3570 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003571 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003572 break;
3573 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003574 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003575 break;
3576 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003577 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003578 break;
3579 default:
3580 pw.print(oldStatus);
3581 break;
3582 }
3583 }
3584 if (oldHealth != rec.batteryHealth) {
3585 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003586 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003587 switch (oldHealth) {
3588 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003589 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003590 break;
3591 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003592 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003593 break;
3594 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003595 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003596 break;
3597 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003598 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003599 break;
3600 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003601 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003602 break;
3603 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003604 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003605 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003606 case BatteryManager.BATTERY_HEALTH_COLD:
3607 pw.print(checkin ? "c" : "cold");
3608 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003609 default:
3610 pw.print(oldHealth);
3611 break;
3612 }
3613 }
3614 if (oldPlug != rec.batteryPlugType) {
3615 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003616 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003617 switch (oldPlug) {
3618 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003619 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003620 break;
3621 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003622 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003623 break;
3624 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003625 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003626 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003627 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003628 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003629 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003630 default:
3631 pw.print(oldPlug);
3632 break;
3633 }
3634 }
3635 if (oldTemp != rec.batteryTemperature) {
3636 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003637 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003638 pw.print(oldTemp);
3639 }
3640 if (oldVolt != rec.batteryVoltage) {
3641 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003642 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003643 pw.print(oldVolt);
3644 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003645 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003646 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003647 printBitDescriptions(pw, oldState2, rec.states2, null,
3648 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003649 if (rec.wakeReasonTag != null) {
3650 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07003651 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003652 pw.print(rec.wakeReasonTag.poolIdx);
3653 } else {
3654 pw.print(" wake_reason=");
3655 pw.print(rec.wakeReasonTag.uid);
3656 pw.print(":\"");
3657 pw.print(rec.wakeReasonTag.string);
3658 pw.print("\"");
3659 }
3660 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003661 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003662 pw.print(checkin ? "," : " ");
3663 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3664 pw.print("+");
3665 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3666 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003667 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003668 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3669 : HISTORY_EVENT_NAMES;
3670 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3671 | HistoryItem.EVENT_FLAG_FINISH);
3672 if (idx >= 0 && idx < eventNames.length) {
3673 pw.print(eventNames[idx]);
3674 } else {
3675 pw.print(checkin ? "Ev" : "event");
3676 pw.print(idx);
3677 }
3678 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003679 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003680 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003681 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003682 UserHandle.formatUid(pw, rec.eventTag.uid);
3683 pw.print(":\"");
3684 pw.print(rec.eventTag.string);
3685 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003686 }
3687 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003688 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003689 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003690 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003691 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003692 }
3693 }
3694
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003695 private void printSizeValue(PrintWriter pw, long size) {
3696 float result = size;
3697 String suffix = "";
3698 if (result >= 10*1024) {
3699 suffix = "KB";
3700 result = result / 1024;
3701 }
3702 if (result >= 10*1024) {
3703 suffix = "MB";
3704 result = result / 1024;
3705 }
3706 if (result >= 10*1024) {
3707 suffix = "GB";
3708 result = result / 1024;
3709 }
3710 if (result >= 10*1024) {
3711 suffix = "TB";
3712 result = result / 1024;
3713 }
3714 if (result >= 10*1024) {
3715 suffix = "PB";
3716 result = result / 1024;
3717 }
3718 pw.print((int)result);
3719 pw.print(suffix);
3720 }
3721
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08003722 private static boolean dumpTimeEstimate(PrintWriter pw, String label, long[] steps,
3723 int count, long modesOfInterest, long modeValues) {
3724 if (count <= 0) {
3725 return false;
3726 }
3727 long total = 0;
3728 int numOfInterest = 0;
3729 for (int i=0; i<count; i++) {
3730 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
3731 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
3732 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
3733 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
3734 // If the modes of interest didn't change during this step period...
3735 if ((modMode&modesOfInterest) == 0) {
3736 // And the mode values during this period match those we are measuring...
3737 if ((initMode&modesOfInterest) == modeValues) {
3738 // Then this can be used to estimate the total time!
3739 numOfInterest++;
3740 total += steps[i] & STEP_LEVEL_TIME_MASK;
3741 }
3742 }
3743 }
3744 if (numOfInterest <= 0) {
3745 return false;
3746 }
3747
3748 // The estimated time is the average time we spend in each level, multipled
3749 // by 100 -- the total number of battery levels
3750 long estimatedTime = (total / numOfInterest) * 100;
3751
3752 pw.print(label);
3753 StringBuilder sb = new StringBuilder(64);
3754 formatTimeMs(sb, estimatedTime);
3755 pw.print(sb);
3756 pw.println();
3757
3758 return true;
3759 }
3760
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003761 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3762 int count, boolean checkin) {
3763 if (count <= 0) {
3764 return false;
3765 }
3766 if (!checkin) {
3767 pw.println(header);
3768 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003769 String[] lineArgs = new String[4];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003770 for (int i=0; i<count; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003771 long duration = steps[i] & STEP_LEVEL_TIME_MASK;
3772 int level = (int)((steps[i] & STEP_LEVEL_LEVEL_MASK)
3773 >> STEP_LEVEL_LEVEL_SHIFT);
3774 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
3775 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
3776 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
3777 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003778 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003779 lineArgs[0] = Long.toString(duration);
3780 lineArgs[1] = Integer.toString(level);
3781 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
3782 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
3783 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
3784 case Display.STATE_ON: lineArgs[2] = "s+"; break;
3785 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
3786 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
3787 default: lineArgs[1] = "?"; break;
3788 }
3789 } else {
3790 lineArgs[2] = "";
3791 }
3792 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
3793 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
3794 } else {
3795 lineArgs[3] = "";
3796 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003797 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3798 } else {
3799 pw.print(" #"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003800 TimeUtils.formatDuration(duration, pw);
3801 pw.print(" to "); pw.print(level);
3802 boolean haveModes = false;
3803 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
3804 pw.print(" (");
3805 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
3806 case Display.STATE_OFF: pw.print("screen-off"); break;
3807 case Display.STATE_ON: pw.print("screen-on"); break;
3808 case Display.STATE_DOZE: pw.print("screen-doze"); break;
3809 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
3810 default: lineArgs[1] = "screen-?"; break;
3811 }
3812 haveModes = true;
3813 }
3814 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
3815 pw.print(haveModes ? ", " : " (");
3816 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
3817 ? "power-save-on" : "power-save-off");
3818 haveModes = true;
3819 }
3820 if (haveModes) {
3821 pw.print(")");
3822 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003823 pw.println();
3824 }
3825 }
3826 return true;
3827 }
3828
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003829 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3830 public static final int DUMP_CHARGED_ONLY = 1<<1;
3831 public static final int DUMP_HISTORY_ONLY = 1<<2;
3832 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003833 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornd953c532014-08-16 18:17:38 -07003834 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<5;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003835
Dianne Hackborn37de0982014-05-09 09:32:18 -07003836 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
3837 final HistoryPrinter hprinter = new HistoryPrinter();
3838 final HistoryItem rec = new HistoryItem();
3839 long lastTime = -1;
3840 long baseTime = -1;
3841 boolean printed = false;
3842 HistoryEventTracker tracker = null;
3843 while (getNextHistoryLocked(rec)) {
3844 lastTime = rec.time;
3845 if (baseTime < 0) {
3846 baseTime = lastTime;
3847 }
3848 if (rec.time >= histStart) {
3849 if (histStart >= 0 && !printed) {
3850 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07003851 || rec.cmd == HistoryItem.CMD_RESET
3852 || rec.cmd == HistoryItem.CMD_START) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003853 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003854 hprinter.printNextItem(pw, rec, baseTime, checkin,
3855 (flags&DUMP_VERBOSE) != 0);
3856 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003857 } else if (rec.currentTime != 0) {
3858 printed = true;
3859 byte cmd = rec.cmd;
3860 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003861 hprinter.printNextItem(pw, rec, baseTime, checkin,
3862 (flags&DUMP_VERBOSE) != 0);
3863 rec.cmd = cmd;
3864 }
3865 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003866 if (rec.cmd != HistoryItem.CMD_UPDATE) {
3867 hprinter.printNextItem(pw, rec, baseTime, checkin,
3868 (flags&DUMP_VERBOSE) != 0);
3869 rec.cmd = HistoryItem.CMD_UPDATE;
3870 }
3871 int oldEventCode = rec.eventCode;
3872 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003873 rec.eventTag = new HistoryTag();
3874 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
3875 HashMap<String, SparseIntArray> active
3876 = tracker.getStateForEvent(i);
3877 if (active == null) {
3878 continue;
3879 }
3880 for (HashMap.Entry<String, SparseIntArray> ent
3881 : active.entrySet()) {
3882 SparseIntArray uids = ent.getValue();
3883 for (int j=0; j<uids.size(); j++) {
3884 rec.eventCode = i;
3885 rec.eventTag.string = ent.getKey();
3886 rec.eventTag.uid = uids.keyAt(j);
3887 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003888 hprinter.printNextItem(pw, rec, baseTime, checkin,
3889 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003890 rec.wakeReasonTag = null;
3891 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003892 }
3893 }
3894 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003895 rec.eventCode = oldEventCode;
3896 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003897 tracker = null;
3898 }
3899 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07003900 hprinter.printNextItem(pw, rec, baseTime, checkin,
3901 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003902 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
3903 // This is an attempt to aggregate the previous state and generate
3904 // fake events to reflect that state at the point where we start
3905 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07003906 if (tracker == null) {
3907 tracker = new HistoryEventTracker();
3908 }
3909 tracker.updateState(rec.eventCode, rec.eventTag.string,
3910 rec.eventTag.uid, rec.eventTag.poolIdx);
3911 }
3912 }
3913 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003914 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07003915 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
3916 }
3917 }
3918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 /**
3920 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3921 *
3922 * @param pw a Printer to receive the dump output.
3923 */
3924 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003925 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003926 prepareForDumpLocked();
3927
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003928 final boolean filtering =
3929 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003930
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003931 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003932 final long historyTotalSize = getHistoryTotalSize();
3933 final long historyUsedSize = getHistoryUsedSize();
3934 if (startIteratingHistoryLocked()) {
3935 try {
3936 pw.print("Battery History (");
3937 pw.print((100*historyUsedSize)/historyTotalSize);
3938 pw.print("% used, ");
3939 printSizeValue(pw, historyUsedSize);
3940 pw.print(" used of ");
3941 printSizeValue(pw, historyTotalSize);
3942 pw.print(", ");
3943 pw.print(getHistoryStringPoolSize());
3944 pw.print(" strings using ");
3945 printSizeValue(pw, getHistoryStringPoolBytes());
3946 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07003947 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003948 pw.println();
3949 } finally {
3950 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003951 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003952 }
3953
3954 if (startIteratingOldHistoryLocked()) {
3955 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07003956 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003957 pw.println("Old battery History:");
3958 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003959 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003960 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07003961 if (baseTime < 0) {
3962 baseTime = rec.time;
3963 }
3964 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003965 }
3966 pw.println();
3967 } finally {
3968 finishIteratingOldHistoryLocked();
3969 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003970 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003971 }
3972
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003973 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003974 return;
3975 }
3976
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003977 if (!filtering) {
3978 SparseArray<? extends Uid> uidStats = getUidStats();
3979 final int NU = uidStats.size();
3980 boolean didPid = false;
3981 long nowRealtime = SystemClock.elapsedRealtime();
3982 for (int i=0; i<NU; i++) {
3983 Uid uid = uidStats.valueAt(i);
3984 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3985 if (pids != null) {
3986 for (int j=0; j<pids.size(); j++) {
3987 Uid.Pid pid = pids.valueAt(j);
3988 if (!didPid) {
3989 pw.println("Per-PID Stats:");
3990 didPid = true;
3991 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003992 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3993 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003994 pw.print(" PID "); pw.print(pids.keyAt(j));
3995 pw.print(" wake time: ");
3996 TimeUtils.formatDuration(time, pw);
3997 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003998 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003999 }
4000 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004001 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004002 pw.println();
4003 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004004 }
4005
4006 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004007 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
4008 getNumDischargeStepDurations(), false)) {
4009 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4010 if (timeRemaining >= 0) {
4011 pw.print(" Estimated discharge time remaining: ");
4012 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4013 pw.println();
4014 }
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004015 dumpTimeEstimate(pw, " Estimated screen off time: ",
4016 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4017 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4018 (Display.STATE_OFF-1));
4019 dumpTimeEstimate(pw, " Estimated screen off power save time: ",
4020 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4021 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4022 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE);
4023 dumpTimeEstimate(pw, " Estimated screen on time: ",
4024 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4025 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4026 (Display.STATE_ON-1));
4027 dumpTimeEstimate(pw, " Estimated screen on power save time: ",
4028 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4029 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4030 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE);
4031 dumpTimeEstimate(pw, " Estimated screen doze time: ",
4032 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4033 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4034 (Display.STATE_DOZE-1));
4035 dumpTimeEstimate(pw, " Estimated screen doze power save time: ",
4036 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4037 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4038 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE);
4039 dumpTimeEstimate(pw, " Estimated screen doze suspend time: ",
4040 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4041 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4042 (Display.STATE_DOZE_SUSPEND-1));
4043 dumpTimeEstimate(pw, " Estimated screen doze suspend power save time: ",
4044 getDischargeStepDurationsArray(), getNumDischargeStepDurations(),
4045 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
4046 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004047 pw.println();
4048 }
4049 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
4050 getNumChargeStepDurations(), false)) {
4051 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4052 if (timeRemaining >= 0) {
4053 pw.print(" Estimated charge time remaining: ");
4054 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4055 pw.println();
4056 }
4057 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004058 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07004059 pw.println("Statistics since last charge:");
4060 pw.println(" System starts: " + getStartCount()
4061 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07004062 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
4063 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004064 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07004065 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004066 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
4067 pw.println("Statistics since last unplugged:");
Dianne Hackbornd953c532014-08-16 18:17:38 -07004068 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid,
4069 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 }
4072
4073 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004074 public void dumpCheckinLocked(Context context, PrintWriter pw,
4075 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004076 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004077
4078 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004079 "11", getParcelVersion(), getStartPlatformVersion(), getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004080
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004081 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
4082
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004083 final boolean filtering =
4084 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
4085
4086 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07004087 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004088 try {
4089 for (int i=0; i<getHistoryStringPoolSize(); i++) {
4090 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4091 pw.print(HISTORY_STRING_POOL); pw.print(',');
4092 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004093 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004094 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004095 pw.print(",\"");
4096 String str = getHistoryTagPoolString(i);
4097 str = str.replace("\\", "\\\\");
4098 str = str.replace("\"", "\\\"");
4099 pw.print(str);
4100 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004101 pw.println();
4102 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004103 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004104 } finally {
4105 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08004106 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004107 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004108 }
4109
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004110 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004111 return;
4112 }
4113
Dianne Hackborne4a59512010-12-07 11:08:07 -08004114 if (apps != null) {
4115 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
4116 for (int i=0; i<apps.size(); i++) {
4117 ApplicationInfo ai = apps.get(i);
4118 ArrayList<String> pkgs = uids.get(ai.uid);
4119 if (pkgs == null) {
4120 pkgs = new ArrayList<String>();
4121 uids.put(ai.uid, pkgs);
4122 }
4123 pkgs.add(ai.packageName);
4124 }
4125 SparseArray<? extends Uid> uidStats = getUidStats();
4126 final int NU = uidStats.size();
4127 String[] lineArgs = new String[2];
4128 for (int i=0; i<NU; i++) {
4129 int uid = uidStats.keyAt(i);
4130 ArrayList<String> pkgs = uids.get(uid);
4131 if (pkgs != null) {
4132 for (int j=0; j<pkgs.size(); j++) {
4133 lineArgs[0] = Integer.toString(uid);
4134 lineArgs[1] = pkgs.get(j);
4135 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
4136 (Object[])lineArgs);
4137 }
4138 }
4139 }
4140 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004141 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004142 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
4143 getNumDischargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004144 String[] lineArgs = new String[1];
4145 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4146 if (timeRemaining >= 0) {
4147 lineArgs[0] = Long.toString(timeRemaining);
4148 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
4149 (Object[])lineArgs);
4150 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004151 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
4152 getNumChargeStepDurations(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004153 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4154 if (timeRemaining >= 0) {
4155 lineArgs[0] = Long.toString(timeRemaining);
4156 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
4157 (Object[])lineArgs);
4158 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07004159 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
4160 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004161 }
4162 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004163 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1,
4164 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167}