blob: 5f515ebb10e6e33a5d90985c6de6f157f77e7723 [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;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Printer;
34import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070035import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070036import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070037import android.view.Display;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080038import com.android.internal.os.BatterySipper;
39import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41/**
42 * A class providing access to battery usage statistics, including information on
43 * wakelocks, processes, packages, and services. All times are represented in microseconds
44 * except where indicated otherwise.
45 * @hide
46 */
47public abstract class BatteryStats implements Parcelable {
48
49 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070050
51 /** @hide */
52 public static final String SERVICE_NAME = "batterystats";
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 /**
55 * A constant indicating a partial wake lock timer.
56 */
57 public static final int WAKE_TYPE_PARTIAL = 0;
58
59 /**
60 * A constant indicating a full wake lock timer.
61 */
62 public static final int WAKE_TYPE_FULL = 1;
63
64 /**
65 * A constant indicating a window wake lock timer.
66 */
67 public static final int WAKE_TYPE_WINDOW = 2;
68
69 /**
70 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070073
74 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070075 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070076 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070077 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070078
79 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070080 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070081 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070082 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070083
84 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070085 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070087 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Dianne Hackborn62793e42015-03-09 11:15:41 -070089 /**
90 * A constant indicating a wifi multicast timer
91 */
92 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070095 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070096 */
97 public static final int VIDEO_TURNED_ON = 8;
98
99 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800100 * A constant indicating a vibrator on timer
101 */
102 public static final int VIBRATOR_ON = 9;
103
104 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700105 * A constant indicating a foreground activity timer
106 */
107 public static final int FOREGROUND_ACTIVITY = 10;
108
109 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700110 * A constant indicating a wifi batched scan is active
111 */
112 public static final int WIFI_BATCHED_SCAN = 11;
113
114 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700115 * A constant indicating a process state timer
116 */
117 public static final int PROCESS_STATE = 12;
118
119 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700120 * A constant indicating a sync timer
121 */
122 public static final int SYNC = 13;
123
124 /**
125 * A constant indicating a job timer
126 */
127 public static final int JOB = 14;
128
129 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800130 * A constant indicating an audio turn on timer
131 */
132 public static final int AUDIO_TURNED_ON = 15;
133
134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * Include all of the data in the stats, including previously saved data.
136 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700137 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * Include only the current run in the stats.
141 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700142 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144 /**
145 * Include only the run since the last time the device was unplugged in the stats.
146 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700147 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700148
149 // NOTE: Update this list if you add/change any stats above.
150 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700151 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700152 private static final String[] STAT_NAMES = { "l", "c", "u" };
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700155 * Current version of checkin data format.
156 */
157 static final String CHECKIN_VERSION = "14";
158
159 /**
160 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700162 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700163
Evan Millar22ac0432009-03-31 11:33:18 -0700164 private static final long BYTES_PER_KB = 1024;
165 private static final long BYTES_PER_MB = 1048576; // 1024^2
166 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700168 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800169 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700171 private static final String PROCESS_DATA = "pr";
172 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800173 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700174 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700175 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700176 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700177 private static final String SYNC_DATA = "sy";
178 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700179 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700180 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700181 private static final String NETWORK_DATA = "nt";
182 private static final String USER_ACTIVITY_DATA = "ua";
183 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800184 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700185 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700186 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700187 private static final String WIFI_DATA = "wfl";
Adam Lesinskie283d332015-04-16 12:29:25 -0700188 private static final String GLOBAL_BLUETOOTH_DATA = "gble";
Evan Millare84de8d2009-04-02 22:16:12 -0700189 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800190 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800191 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700192 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700193 private static final String SCREEN_BRIGHTNESS_DATA = "br";
194 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700195 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700196 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
197 private static final String DATA_CONNECTION_TIME_DATA = "dct";
198 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800199 private static final String WIFI_STATE_TIME_DATA = "wst";
200 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700201 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
202 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
203 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
204 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800205 private static final String POWER_USE_SUMMARY_DATA = "pws";
206 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700207 private static final String DISCHARGE_STEP_DATA = "dsd";
208 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700209 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
210 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700212 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 private final Formatter mFormatter = new Formatter(mFormatBuilder);
214
215 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700216 * State for keeping track of counting information.
217 */
218 public static abstract class Counter {
219
220 /**
221 * Returns the count associated with this Counter for the
222 * selected type of statistics.
223 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700224 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700225 */
Evan Millarc64edde2009-04-18 12:26:32 -0700226 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700227
228 /**
229 * Temporary for debugging.
230 */
231 public abstract void logState(Printer pw, String prefix);
232 }
233
234 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700235 * State for keeping track of long counting information.
236 */
237 public static abstract class LongCounter {
238
239 /**
240 * Returns the count associated with this Counter for the
241 * selected type of statistics.
242 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700243 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700244 */
245 public abstract long getCountLocked(int which);
246
247 /**
248 * Temporary for debugging.
249 */
250 public abstract void logState(Printer pw, String prefix);
251 }
252
253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * State for keeping track of timing information.
255 */
256 public static abstract class Timer {
257
258 /**
259 * Returns the count associated with this Timer for the
260 * selected type of statistics.
261 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700262 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
Evan Millarc64edde2009-04-18 12:26:32 -0700264 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
266 /**
267 * Returns the total time in microseconds associated with this Timer for the
268 * selected type of statistics.
269 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800270 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700271 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * @return a time in microseconds
273 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800274 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700277 * Returns the total time in microseconds associated with this Timer since the
278 * 'mark' was last set.
279 *
280 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
281 * @return a time in microseconds
282 */
283 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
284
285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * Temporary for debugging.
287 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700288 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
290
291 /**
292 * The statistics associated with a particular uid.
293 */
294 public static abstract class Uid {
295
296 /**
297 * Returns a mapping containing wakelock statistics.
298 *
299 * @return a Map from Strings to Uid.Wakelock objects.
300 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700301 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700304 * Returns a mapping containing sync statistics.
305 *
306 * @return a Map from Strings to Timer objects.
307 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700308 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700309
310 /**
311 * Returns a mapping containing scheduled job statistics.
312 *
313 * @return a Map from Strings to Timer objects.
314 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700315 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700316
317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * The statistics associated with a particular wake lock.
319 */
320 public static abstract class Wakelock {
321 public abstract Timer getWakeTime(int type);
322 }
323
324 /**
325 * Returns a mapping containing sensor statistics.
326 *
327 * @return a Map from Integer sensor ids to Uid.Sensor objects.
328 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700329 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700332 * Returns a mapping containing active process data.
333 */
334 public abstract SparseArray<? extends Pid> getPidStats();
335
336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * Returns a mapping containing process statistics.
338 *
339 * @return a Map from Strings to Uid.Proc objects.
340 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700341 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
343 /**
344 * Returns a mapping containing package statistics.
345 *
346 * @return a Map from Strings to Uid.Pkg objects.
347 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700348 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700349
350 /**
351 * Returns the time in milliseconds that this app kept the WiFi controller in the
352 * specified state <code>type</code>.
353 * @param type one of {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, or
354 * {@link #CONTROLLER_TX_TIME}.
355 * @param which one of {@link #STATS_CURRENT}, {@link #STATS_SINCE_CHARGED}, or
356 * {@link #STATS_SINCE_UNPLUGGED}.
357 */
358 public abstract long getWifiControllerActivity(int type, int which);
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * {@hide}
362 */
363 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700364
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800365 public abstract void noteWifiRunningLocked(long elapsedRealtime);
366 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
367 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
368 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
369 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
370 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
371 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
372 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
373 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
374 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800375 public abstract void noteActivityResumedLocked(long elapsedRealtime);
376 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800377 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
378 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
379 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700380 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800381 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700382 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800383 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
384 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
385 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700386 public abstract Timer getForegroundActivityTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700387
388 // Time this uid has any processes in foreground state.
389 public static final int PROCESS_STATE_FOREGROUND = 0;
390 // Time this uid has any process in active state (not cached).
391 public static final int PROCESS_STATE_ACTIVE = 1;
392 // Time this uid has any processes running at all.
393 public static final int PROCESS_STATE_RUNNING = 2;
394 // Total number of process states we track.
395 public static final int NUM_PROCESS_STATE = 3;
396
397 static final String[] PROCESS_STATE_NAMES = {
398 "Foreground", "Active", "Running"
399 };
400
401 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
402
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800403 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404
Robert Greenwalta029ea12013-09-25 16:38:12 -0700405 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
406
Dianne Hackborn617f8772009-03-31 15:04:46 -0700407 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700408 * Note that these must match the constants in android.os.PowerManager.
409 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
410 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700411 */
412 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700413 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700414 };
415
Jeff Browndf693de2012-07-27 12:03:38 -0700416 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700417
Dianne Hackborn617f8772009-03-31 15:04:46 -0700418 public abstract void noteUserActivityLocked(int type);
419 public abstract boolean hasUserActivity();
420 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700421
422 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800423 public abstract long getNetworkActivityBytes(int type, int which);
424 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800425 public abstract long getMobileRadioActiveTime(int which);
426 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700427
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700428 /**
429 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
430 */
431 public abstract long getUserCpuTimeUs(int which);
432
433 /**
434 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
435 */
436 public abstract long getSystemCpuTimeUs(int which);
437
438 /**
439 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed.
440 * @param speedStep the index of the CPU speed. This is not the actual speed of the CPU.
441 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
442 * @see BatteryStats#getCpuSpeedSteps()
443 */
444 public abstract long getTimeAtCpuSpeed(int step, int which);
445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800447 /*
448 * FIXME: it's not correct to use this magic value because it
449 * could clash with a sensor handle (which are defined by
450 * the sensor HAL, and therefore out of our control
451 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 // Magic sensor number for the GPS.
453 public static final int GPS = -10000;
454
455 public abstract int getHandle();
456
457 public abstract Timer getSensorTime();
458 }
459
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700460 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800461 public int mWakeNesting;
462 public long mWakeSumMs;
463 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700464 }
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * The statistics associated with a particular process.
468 */
469 public static abstract class Proc {
470
Dianne Hackborn287952c2010-09-22 22:34:31 -0700471 public static class ExcessivePower {
472 public static final int TYPE_WAKE = 1;
473 public static final int TYPE_CPU = 2;
474
475 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700476 public long overTime;
477 public long usedTime;
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800481 * Returns true if this process is still active in the battery stats.
482 */
483 public abstract boolean isActive();
484
485 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700486 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700488 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 */
490 public abstract long getUserTime(int which);
491
492 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700493 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700495 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 */
497 public abstract long getSystemTime(int which);
498
499 /**
500 * Returns the number of times the process has been started.
501 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700502 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 */
504 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700505
506 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800507 * Returns the number of times the process has crashed.
508 *
509 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
510 */
511 public abstract int getNumCrashes(int which);
512
513 /**
514 * Returns the number of times the process has ANRed.
515 *
516 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
517 */
518 public abstract int getNumAnrs(int which);
519
520 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700521 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700522 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700523 * @return foreground cpu time in microseconds
524 */
525 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700526
Dianne Hackborn287952c2010-09-22 22:34:31 -0700527 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700528
Dianne Hackborn287952c2010-09-22 22:34:31 -0700529 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531
532 /**
533 * The statistics associated with a particular package.
534 */
535 public static abstract class Pkg {
536
537 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700538 * Returns information about all wakeup alarms that have been triggered for this
539 * package. The mapping keys are tag names for the alarms, the counter contains
540 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700542 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544 /**
545 * Returns a mapping containing service statistics.
546 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700547 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
549 /**
550 * The statistics associated with a particular service.
551 */
552 public abstract class Serv {
553
554 /**
555 * Returns the amount of time spent started.
556 *
557 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700558 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 * @return
560 */
561 public abstract long getStartTime(long batteryUptime, int which);
562
563 /**
564 * Returns the total number of times startService() has been called.
565 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700566 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 */
568 public abstract int getStarts(int which);
569
570 /**
571 * Returns the total number times the service has been launched.
572 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700573 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 */
575 public abstract int getLaunches(int which);
576 }
577 }
578 }
579
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800580 public static final class LevelStepTracker {
581 public long mLastStepTime = -1;
582 public int mNumStepDurations;
583 public final long[] mStepDurations;
584
585 public LevelStepTracker(int maxLevelSteps) {
586 mStepDurations = new long[maxLevelSteps];
587 }
588
589 public LevelStepTracker(int numSteps, long[] steps) {
590 mNumStepDurations = numSteps;
591 mStepDurations = new long[numSteps];
592 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
593 }
594
595 public long getDurationAt(int index) {
596 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
597 }
598
599 public int getLevelAt(int index) {
600 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
601 >> STEP_LEVEL_LEVEL_SHIFT);
602 }
603
604 public int getInitModeAt(int index) {
605 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
606 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
607 }
608
609 public int getModModeAt(int index) {
610 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
611 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
612 }
613
614 private void appendHex(long val, int topOffset, StringBuilder out) {
615 boolean hasData = false;
616 while (topOffset >= 0) {
617 int digit = (int)( (val>>topOffset) & 0xf );
618 topOffset -= 4;
619 if (!hasData && digit == 0) {
620 continue;
621 }
622 hasData = true;
623 if (digit >= 0 && digit <= 9) {
624 out.append((char)('0' + digit));
625 } else {
626 out.append((char)('a' + digit - 10));
627 }
628 }
629 }
630
631 public void encodeEntryAt(int index, StringBuilder out) {
632 long item = mStepDurations[index];
633 long duration = item & STEP_LEVEL_TIME_MASK;
634 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
635 >> STEP_LEVEL_LEVEL_SHIFT);
636 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
637 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
638 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
639 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
640 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
641 case Display.STATE_OFF: out.append('f'); break;
642 case Display.STATE_ON: out.append('o'); break;
643 case Display.STATE_DOZE: out.append('d'); break;
644 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
645 }
646 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
647 out.append('p');
648 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700649 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
650 out.append('i');
651 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800652 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
653 case Display.STATE_OFF: out.append('F'); break;
654 case Display.STATE_ON: out.append('O'); break;
655 case Display.STATE_DOZE: out.append('D'); break;
656 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
657 }
658 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
659 out.append('P');
660 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700661 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
662 out.append('I');
663 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800664 out.append('-');
665 appendHex(level, 4, out);
666 out.append('-');
667 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
668 }
669
670 public void decodeEntryAt(int index, String value) {
671 final int N = value.length();
672 int i = 0;
673 char c;
674 long out = 0;
675 while (i < N && (c=value.charAt(i)) != '-') {
676 i++;
677 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800678 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800679 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800680 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800681 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800682 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800683 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800684 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
685 << STEP_LEVEL_INITIAL_MODE_SHIFT);
686 break;
687 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
688 << STEP_LEVEL_INITIAL_MODE_SHIFT);
689 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700690 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
691 << STEP_LEVEL_INITIAL_MODE_SHIFT);
692 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800693 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
694 break;
695 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
696 break;
697 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
698 break;
699 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
700 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
701 break;
702 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
703 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800704 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700705 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
706 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
707 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800708 }
709 }
710 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800711 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800712 while (i < N && (c=value.charAt(i)) != '-') {
713 i++;
714 level <<= 4;
715 if (c >= '0' && c <= '9') {
716 level += c - '0';
717 } else if (c >= 'a' && c <= 'f') {
718 level += c - 'a' + 10;
719 } else if (c >= 'A' && c <= 'F') {
720 level += c - 'A' + 10;
721 }
722 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800723 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800724 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
725 long duration = 0;
726 while (i < N && (c=value.charAt(i)) != '-') {
727 i++;
728 duration <<= 4;
729 if (c >= '0' && c <= '9') {
730 duration += c - '0';
731 } else if (c >= 'a' && c <= 'f') {
732 duration += c - 'a' + 10;
733 } else if (c >= 'A' && c <= 'F') {
734 duration += c - 'A' + 10;
735 }
736 }
737 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
738 }
739
740 public void init() {
741 mLastStepTime = -1;
742 mNumStepDurations = 0;
743 }
744
745 public void clearTime() {
746 mLastStepTime = -1;
747 }
748
749 public long computeTimePerLevel() {
750 final long[] steps = mStepDurations;
751 final int numSteps = mNumStepDurations;
752
753 // For now we'll do a simple average across all steps.
754 if (numSteps <= 0) {
755 return -1;
756 }
757 long total = 0;
758 for (int i=0; i<numSteps; i++) {
759 total += steps[i] & STEP_LEVEL_TIME_MASK;
760 }
761 return total / numSteps;
762 /*
763 long[] buckets = new long[numSteps];
764 int numBuckets = 0;
765 int numToAverage = 4;
766 int i = 0;
767 while (i < numSteps) {
768 long totalTime = 0;
769 int num = 0;
770 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
771 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
772 num++;
773 }
774 buckets[numBuckets] = totalTime / num;
775 numBuckets++;
776 numToAverage *= 2;
777 i += num;
778 }
779 if (numBuckets < 1) {
780 return -1;
781 }
782 long averageTime = buckets[numBuckets-1];
783 for (i=numBuckets-2; i>=0; i--) {
784 averageTime = (averageTime + buckets[i]) / 2;
785 }
786 return averageTime;
787 */
788 }
789
790 public long computeTimeEstimate(long modesOfInterest, long modeValues,
791 int[] outNumOfInterest) {
792 final long[] steps = mStepDurations;
793 final int count = mNumStepDurations;
794 if (count <= 0) {
795 return -1;
796 }
797 long total = 0;
798 int numOfInterest = 0;
799 for (int i=0; i<count; i++) {
800 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
801 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
802 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
803 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
804 // If the modes of interest didn't change during this step period...
805 if ((modMode&modesOfInterest) == 0) {
806 // And the mode values during this period match those we are measuring...
807 if ((initMode&modesOfInterest) == modeValues) {
808 // Then this can be used to estimate the total time!
809 numOfInterest++;
810 total += steps[i] & STEP_LEVEL_TIME_MASK;
811 }
812 }
813 }
814 if (numOfInterest <= 0) {
815 return -1;
816 }
817
818 if (outNumOfInterest != null) {
819 outNumOfInterest[0] = numOfInterest;
820 }
821
822 // The estimated time is the average time we spend in each level, multipled
823 // by 100 -- the total number of battery levels
824 return (total / numOfInterest) * 100;
825 }
826
827 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
828 int stepCount = mNumStepDurations;
829 final long lastStepTime = mLastStepTime;
830 if (lastStepTime >= 0 && numStepLevels > 0) {
831 final long[] steps = mStepDurations;
832 long duration = elapsedRealtime - lastStepTime;
833 for (int i=0; i<numStepLevels; i++) {
834 System.arraycopy(steps, 0, steps, 1, steps.length-1);
835 long thisDuration = duration / (numStepLevels-i);
836 duration -= thisDuration;
837 if (thisDuration > STEP_LEVEL_TIME_MASK) {
838 thisDuration = STEP_LEVEL_TIME_MASK;
839 }
840 steps[0] = thisDuration | modeBits;
841 }
842 stepCount += numStepLevels;
843 if (stepCount > steps.length) {
844 stepCount = steps.length;
845 }
846 }
847 mNumStepDurations = stepCount;
848 mLastStepTime = elapsedRealtime;
849 }
850
851 public void readFromParcel(Parcel in) {
852 final int N = in.readInt();
853 mNumStepDurations = N;
854 for (int i=0; i<N; i++) {
855 mStepDurations[i] = in.readLong();
856 }
857 }
858
859 public void writeToParcel(Parcel out) {
860 final int N = mNumStepDurations;
861 out.writeInt(N);
862 for (int i=0; i<N; i++) {
863 out.writeLong(mStepDurations[i]);
864 }
865 }
866 }
867
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700868 public static final class PackageChange {
869 public String mPackageName;
870 public boolean mUpdate;
871 public int mVersionCode;
872 }
873
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800874 public static final class DailyItem {
875 public long mStartTime;
876 public long mEndTime;
877 public LevelStepTracker mDischargeSteps;
878 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700879 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800880 }
881
882 public abstract DailyItem getDailyItemLocked(int daysAgo);
883
884 public abstract long getCurrentDailyStartTime();
885
886 public abstract long getNextMinDailyDeadline();
887
888 public abstract long getNextMaxDailyDeadline();
889
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800890 public final static class HistoryTag {
891 public String string;
892 public int uid;
893
894 public int poolIdx;
895
896 public void setTo(HistoryTag o) {
897 string = o.string;
898 uid = o.uid;
899 poolIdx = o.poolIdx;
900 }
901
902 public void setTo(String _string, int _uid) {
903 string = _string;
904 uid = _uid;
905 poolIdx = -1;
906 }
907
908 public void writeToParcel(Parcel dest, int flags) {
909 dest.writeString(string);
910 dest.writeInt(uid);
911 }
912
913 public void readFromParcel(Parcel src) {
914 string = src.readString();
915 uid = src.readInt();
916 poolIdx = -1;
917 }
918
919 @Override
920 public boolean equals(Object o) {
921 if (this == o) return true;
922 if (o == null || getClass() != o.getClass()) return false;
923
924 HistoryTag that = (HistoryTag) o;
925
926 if (uid != that.uid) return false;
927 if (!string.equals(that.string)) return false;
928
929 return true;
930 }
931
932 @Override
933 public int hashCode() {
934 int result = string.hashCode();
935 result = 31 * result + uid;
936 return result;
937 }
938 }
939
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800940 /**
941 * Optional detailed information that can go into a history step. This is typically
942 * generated each time the battery level changes.
943 */
944 public final static class HistoryStepDetails {
945 // Time (in 1/100 second) spent in user space and the kernel since the last step.
946 public int userTime;
947 public int systemTime;
948
949 // Top three apps using CPU in the last step, with times in 1/100 second.
950 public int appCpuUid1;
951 public int appCpuUTime1;
952 public int appCpuSTime1;
953 public int appCpuUid2;
954 public int appCpuUTime2;
955 public int appCpuSTime2;
956 public int appCpuUid3;
957 public int appCpuUTime3;
958 public int appCpuSTime3;
959
960 // Information from /proc/stat
961 public int statUserTime;
962 public int statSystemTime;
963 public int statIOWaitTime;
964 public int statIrqTime;
965 public int statSoftIrqTime;
966 public int statIdlTime;
967
968 public HistoryStepDetails() {
969 clear();
970 }
971
972 public void clear() {
973 userTime = systemTime = 0;
974 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
975 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
976 = appCpuUTime3 = appCpuSTime3 = 0;
977 }
978
979 public void writeToParcel(Parcel out) {
980 out.writeInt(userTime);
981 out.writeInt(systemTime);
982 out.writeInt(appCpuUid1);
983 out.writeInt(appCpuUTime1);
984 out.writeInt(appCpuSTime1);
985 out.writeInt(appCpuUid2);
986 out.writeInt(appCpuUTime2);
987 out.writeInt(appCpuSTime2);
988 out.writeInt(appCpuUid3);
989 out.writeInt(appCpuUTime3);
990 out.writeInt(appCpuSTime3);
991 out.writeInt(statUserTime);
992 out.writeInt(statSystemTime);
993 out.writeInt(statIOWaitTime);
994 out.writeInt(statIrqTime);
995 out.writeInt(statSoftIrqTime);
996 out.writeInt(statIdlTime);
997 }
998
999 public void readFromParcel(Parcel in) {
1000 userTime = in.readInt();
1001 systemTime = in.readInt();
1002 appCpuUid1 = in.readInt();
1003 appCpuUTime1 = in.readInt();
1004 appCpuSTime1 = in.readInt();
1005 appCpuUid2 = in.readInt();
1006 appCpuUTime2 = in.readInt();
1007 appCpuSTime2 = in.readInt();
1008 appCpuUid3 = in.readInt();
1009 appCpuUTime3 = in.readInt();
1010 appCpuSTime3 = in.readInt();
1011 statUserTime = in.readInt();
1012 statSystemTime = in.readInt();
1013 statIOWaitTime = in.readInt();
1014 statIrqTime = in.readInt();
1015 statSoftIrqTime = in.readInt();
1016 statIdlTime = in.readInt();
1017 }
1018 }
1019
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001020 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001021 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001022
1023 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001024 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001025
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001026 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001027 public static final byte CMD_NULL = -1;
1028 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001029 public static final byte CMD_CURRENT_TIME = 5;
1030 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001031 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001032 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001033
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001034 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001035
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001036 /**
1037 * Return whether the command code is a delta data update.
1038 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001039 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001040 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001041 }
1042
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001043 public byte batteryLevel;
1044 public byte batteryStatus;
1045 public byte batteryHealth;
1046 public byte batteryPlugType;
1047
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001048 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001049 public char batteryVoltage;
1050
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001051 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001052 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001053 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001054 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001055 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1056 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001057 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001058 public static final int STATE_PHONE_STATE_SHIFT = 6;
1059 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001060 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001061 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001062 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001063
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001064 // These states always appear directly in the first int token
1065 // of a delta change; they should be ones that change relatively
1066 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001067 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1068 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001069 public static final int STATE_GPS_ON_FLAG = 1<<29;
1070 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001071 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001072 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001073 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001074 // These are on the lower bits used for the command; if they change
1075 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001076 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001077 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001078 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001079 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1080 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1081 // empty slot
1082 // empty slot
1083 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001084
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001085 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001086 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1087
1088 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001089
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001090 public int states;
1091
Dianne Hackborn3251b902014-06-20 14:40:53 -07001092 // Constants from WIFI_SUPPL_STATE_*
1093 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1094 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1095 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1096 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1097 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1098 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1099
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001100 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001101 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1102 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1103 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001104 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001105 public static final int STATE2_DEVICE_IDLE_FLAG = 1<<26;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001106 public static final int STATE2_CHARGING_FLAG = 1<<25;
1107 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<24;
1108 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<23;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001109
1110 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001111 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_FLAG
1112 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1113
1114 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001115
Dianne Hackborn40c87252014-03-19 16:55:40 -07001116 public int states2;
1117
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001118 // The wake lock that was acquired at this point.
1119 public HistoryTag wakelockTag;
1120
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001121 // Kernel wakeup reason at this point.
1122 public HistoryTag wakeReasonTag;
1123
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001124 // Non-null when there is more detailed information at this step.
1125 public HistoryStepDetails stepDetails;
1126
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001127 public static final int EVENT_FLAG_START = 0x8000;
1128 public static final int EVENT_FLAG_FINISH = 0x4000;
1129
1130 // No event in this item.
1131 public static final int EVENT_NONE = 0x0000;
1132 // Event is about a process that is running.
1133 public static final int EVENT_PROC = 0x0001;
1134 // Event is about an application package that is in the foreground.
1135 public static final int EVENT_FOREGROUND = 0x0002;
1136 // Event is about an application package that is at the top of the screen.
1137 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001138 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001139 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001140 // Events for all additional wake locks aquired/release within a wake block.
1141 // These are not generated by default.
1142 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001143 // Event is about an application executing a scheduled job.
1144 public static final int EVENT_JOB = 0x0006;
1145 // Events for users running.
1146 public static final int EVENT_USER_RUNNING = 0x0007;
1147 // Events for foreground user.
1148 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001149 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001150 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001151 // Event for significant motion taking us out of idle mode.
1152 public static final int EVENT_SIGNIFICANT_MOTION = 0x000a;
1153 // Event for becoming active taking us out of idle mode.
1154 public static final int EVENT_ACTIVE = 0x000b;
1155 // Event for a package being installed.
1156 public static final int EVENT_PACKAGE_INSTALLED = 0x000c;
1157 // Event for a package being uninstalled.
1158 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000d;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001159 // Event for a package being uninstalled.
1160 public static final int EVENT_ALARM = 0x000e;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001161 // Record that we have decided we need to collect new stats data.
1162 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000f;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001163 // Number of event types.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001164 public static final int EVENT_COUNT = 0x0010;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001165 // Mask to extract out only the type part of the event.
1166 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001167
1168 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1169 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1170 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1171 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1172 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1173 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001174 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1175 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001176 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1177 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001178 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1179 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1180 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1181 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1182 public static final int EVENT_USER_FOREGROUND_START =
1183 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1184 public static final int EVENT_USER_FOREGROUND_FINISH =
1185 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001186 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1187 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001188
1189 // For CMD_EVENT.
1190 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001191 public HistoryTag eventTag;
1192
Dianne Hackborn9a755432014-05-15 17:05:22 -07001193 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001194 public long currentTime;
1195
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001196 // Meta-data when reading.
1197 public int numReadInts;
1198
1199 // Pre-allocated objects.
1200 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001201 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001202 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001203
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001204 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001205 }
1206
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001207 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001208 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001209 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001210 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001211 }
1212
1213 public int describeContents() {
1214 return 0;
1215 }
1216
1217 public void writeToParcel(Parcel dest, int flags) {
1218 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001219 int bat = (((int)cmd)&0xff)
1220 | ((((int)batteryLevel)<<8)&0xff00)
1221 | ((((int)batteryStatus)<<16)&0xf0000)
1222 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001223 | ((((int)batteryPlugType)<<24)&0xf000000)
1224 | (wakelockTag != null ? 0x10000000 : 0)
1225 | (wakeReasonTag != null ? 0x20000000 : 0)
1226 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001227 dest.writeInt(bat);
1228 bat = (((int)batteryTemperature)&0xffff)
1229 | ((((int)batteryVoltage)<<16)&0xffff0000);
1230 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001231 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001232 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001233 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001234 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001235 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001236 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001237 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001238 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001239 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001240 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001241 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001243 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001244 dest.writeLong(currentTime);
1245 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001246 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001247
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001248 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001249 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001250 int bat = src.readInt();
1251 cmd = (byte)(bat&0xff);
1252 batteryLevel = (byte)((bat>>8)&0xff);
1253 batteryStatus = (byte)((bat>>16)&0xf);
1254 batteryHealth = (byte)((bat>>20)&0xf);
1255 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001256 int bat2 = src.readInt();
1257 batteryTemperature = (short)(bat2&0xffff);
1258 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001259 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001260 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001261 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001262 wakelockTag = localWakelockTag;
1263 wakelockTag.readFromParcel(src);
1264 } else {
1265 wakelockTag = null;
1266 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001267 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001268 wakeReasonTag = localWakeReasonTag;
1269 wakeReasonTag.readFromParcel(src);
1270 } else {
1271 wakeReasonTag = null;
1272 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001273 if ((bat&0x40000000) != 0) {
1274 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001275 eventTag = localEventTag;
1276 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001277 } else {
1278 eventCode = EVENT_NONE;
1279 eventTag = null;
1280 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001281 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001282 currentTime = src.readLong();
1283 } else {
1284 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001285 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001286 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001287 }
1288
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001289 public void clear() {
1290 time = 0;
1291 cmd = CMD_NULL;
1292 batteryLevel = 0;
1293 batteryStatus = 0;
1294 batteryHealth = 0;
1295 batteryPlugType = 0;
1296 batteryTemperature = 0;
1297 batteryVoltage = 0;
1298 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001299 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001300 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001301 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001302 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001303 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001304 }
1305
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001306 public void setTo(HistoryItem o) {
1307 time = o.time;
1308 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001309 setToCommon(o);
1310 }
1311
1312 public void setTo(long time, byte cmd, HistoryItem o) {
1313 this.time = time;
1314 this.cmd = cmd;
1315 setToCommon(o);
1316 }
1317
1318 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001319 batteryLevel = o.batteryLevel;
1320 batteryStatus = o.batteryStatus;
1321 batteryHealth = o.batteryHealth;
1322 batteryPlugType = o.batteryPlugType;
1323 batteryTemperature = o.batteryTemperature;
1324 batteryVoltage = o.batteryVoltage;
1325 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001326 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001327 if (o.wakelockTag != null) {
1328 wakelockTag = localWakelockTag;
1329 wakelockTag.setTo(o.wakelockTag);
1330 } else {
1331 wakelockTag = null;
1332 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001333 if (o.wakeReasonTag != null) {
1334 wakeReasonTag = localWakeReasonTag;
1335 wakeReasonTag.setTo(o.wakeReasonTag);
1336 } else {
1337 wakeReasonTag = null;
1338 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001339 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001340 if (o.eventTag != null) {
1341 eventTag = localEventTag;
1342 eventTag.setTo(o.eventTag);
1343 } else {
1344 eventTag = null;
1345 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001346 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001347 }
1348
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001349 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001350 return batteryLevel == o.batteryLevel
1351 && batteryStatus == o.batteryStatus
1352 && batteryHealth == o.batteryHealth
1353 && batteryPlugType == o.batteryPlugType
1354 && batteryTemperature == o.batteryTemperature
1355 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001356 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001357 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001358 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001359 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001360
1361 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001362 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001363 return false;
1364 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001365 if (wakelockTag != o.wakelockTag) {
1366 if (wakelockTag == null || o.wakelockTag == null) {
1367 return false;
1368 }
1369 if (!wakelockTag.equals(o.wakelockTag)) {
1370 return false;
1371 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001372 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001373 if (wakeReasonTag != o.wakeReasonTag) {
1374 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1375 return false;
1376 }
1377 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1378 return false;
1379 }
1380 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001381 if (eventTag != o.eventTag) {
1382 if (eventTag == null || o.eventTag == null) {
1383 return false;
1384 }
1385 if (!eventTag.equals(o.eventTag)) {
1386 return false;
1387 }
1388 }
1389 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001390 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001391 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001392
1393 public final static class HistoryEventTracker {
1394 private final HashMap<String, SparseIntArray>[] mActiveEvents
1395 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1396
1397 public boolean updateState(int code, String name, int uid, int poolIdx) {
1398 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1399 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1400 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1401 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001402 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001403 mActiveEvents[idx] = active;
1404 }
1405 SparseIntArray uids = active.get(name);
1406 if (uids == null) {
1407 uids = new SparseIntArray();
1408 active.put(name, uids);
1409 }
1410 if (uids.indexOfKey(uid) >= 0) {
1411 // Already set, nothing to do!
1412 return false;
1413 }
1414 uids.put(uid, poolIdx);
1415 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1416 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1417 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1418 if (active == null) {
1419 // not currently active, nothing to do.
1420 return false;
1421 }
1422 SparseIntArray uids = active.get(name);
1423 if (uids == null) {
1424 // not currently active, nothing to do.
1425 return false;
1426 }
1427 idx = uids.indexOfKey(uid);
1428 if (idx < 0) {
1429 // not currently active, nothing to do.
1430 return false;
1431 }
1432 uids.removeAt(idx);
1433 if (uids.size() <= 0) {
1434 active.remove(name);
1435 }
1436 }
1437 return true;
1438 }
1439
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001440 public void removeEvents(int code) {
1441 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1442 mActiveEvents[idx] = null;
1443 }
1444
Dianne Hackborn37de0982014-05-09 09:32:18 -07001445 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1446 return mActiveEvents[code];
1447 }
1448 }
1449
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001450 public static final class BitDescription {
1451 public final int mask;
1452 public final int shift;
1453 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001455 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001456 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001457
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001459 this.mask = mask;
1460 this.shift = -1;
1461 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001462 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001463 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001464 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001465 }
1466
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001467 public BitDescription(int mask, int shift, String name, String shortName,
1468 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001469 this.mask = mask;
1470 this.shift = shift;
1471 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001472 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001473 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001474 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001475 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001476 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001477
Dianne Hackbornfc064132014-06-02 12:42:12 -07001478 /**
1479 * Don't allow any more batching in to the current history event. This
1480 * is called when printing partial histories, so to ensure that the next
1481 * history event will go in to a new batch after what was printed in the
1482 * last partial history.
1483 */
1484 public abstract void commitCurrentHistoryBatchLocked();
1485
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001486 public abstract int getHistoryTotalSize();
1487
1488 public abstract int getHistoryUsedSize();
1489
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001490 public abstract boolean startIteratingHistoryLocked();
1491
Dianne Hackborn099bc622014-01-22 13:39:16 -08001492 public abstract int getHistoryStringPoolSize();
1493
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001494 public abstract int getHistoryStringPoolBytes();
1495
1496 public abstract String getHistoryTagPoolString(int index);
1497
1498 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001499
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001500 public abstract boolean getNextHistoryLocked(HistoryItem out);
1501
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001502 public abstract void finishIteratingHistoryLocked();
1503
1504 public abstract boolean startIteratingOldHistoryLocked();
1505
1506 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1507
1508 public abstract void finishIteratingOldHistoryLocked();
1509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001511 * Return the base time offset for the battery history.
1512 */
1513 public abstract long getHistoryBaseTime();
1514
1515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * Returns the number of times the device has been started.
1517 */
1518 public abstract int getStartCount();
1519
1520 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001521 * 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 -08001522 * running on battery.
1523 *
1524 * {@hide}
1525 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001526 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001528 /**
1529 * Returns the number of times the screen was turned on.
1530 *
1531 * {@hide}
1532 */
1533 public abstract int getScreenOnCount(int which);
1534
Jeff Browne95c3cd2014-05-02 16:59:26 -07001535 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1536
Dianne Hackborn617f8772009-03-31 15:04:46 -07001537 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1538 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1539 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1540 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1541 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1542
1543 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1544 "dark", "dim", "medium", "light", "bright"
1545 };
1546
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001547 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1548 "0", "1", "2", "3", "4"
1549 };
1550
Dianne Hackborn617f8772009-03-31 15:04:46 -07001551 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001552
Dianne Hackborn617f8772009-03-31 15:04:46 -07001553 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001554 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001555 * the given brightness
1556 *
1557 * {@hide}
1558 */
1559 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001560 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001563 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001564 * running on battery.
1565 *
1566 * {@hide}
1567 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001568 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001569
1570 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001571 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001572 *
1573 * {@hide}
1574 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001575 public abstract int getPowerSaveModeEnabledCount(int which);
1576
1577 /**
1578 * Returns the time in microseconds that device has been in idle mode while
1579 * running on battery.
1580 *
1581 * {@hide}
1582 */
1583 public abstract long getDeviceIdleModeEnabledTime(long elapsedRealtimeUs, int which);
1584
1585 /**
1586 * Returns the number of times that the devie has gone in to idle mode.
1587 *
1588 * {@hide}
1589 */
1590 public abstract int getDeviceIdleModeEnabledCount(int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001591
1592 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001593 * Returns the time in microseconds that device has been in idling while on
1594 * battery. This is broader than {@link #getDeviceIdleModeEnabledTime} -- it
1595 * counts all of the time that we consider the device to be idle, whether or not
1596 * it is currently in the actual device idle mode.
1597 *
1598 * {@hide}
1599 */
1600 public abstract long getDeviceIdlingTime(long elapsedRealtimeUs, int which);
1601
1602 /**
1603 * Returns the number of times that the devie has started idling.
1604 *
1605 * {@hide}
1606 */
1607 public abstract int getDeviceIdlingCount(int which);
1608
1609 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001610 * Returns the number of times that connectivity state changed.
1611 *
1612 * {@hide}
1613 */
1614 public abstract int getNumConnectivityChange(int which);
1615
1616 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001617 * 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 -08001618 * running on battery.
1619 *
1620 * {@hide}
1621 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001622 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001623
1624 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001625 * Returns the number of times a phone call was activated.
1626 *
1627 * {@hide}
1628 */
1629 public abstract int getPhoneOnCount(int which);
1630
1631 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001632 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001633 * the given signal strength.
1634 *
1635 * {@hide}
1636 */
1637 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001638 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001639
Dianne Hackborn617f8772009-03-31 15:04:46 -07001640 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001641 * Returns the time in microseconds that the phone has been trying to
1642 * acquire a signal.
1643 *
1644 * {@hide}
1645 */
1646 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001647 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001648
1649 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001650 * Returns the number of times the phone has entered the given signal strength.
1651 *
1652 * {@hide}
1653 */
1654 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1655
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001656 /**
1657 * Returns the time in microseconds that the mobile network has been active
1658 * (in a high power state).
1659 *
1660 * {@hide}
1661 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001662 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001663
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001664 /**
1665 * Returns the number of times that the mobile network has transitioned to the
1666 * active state.
1667 *
1668 * {@hide}
1669 */
1670 public abstract int getMobileRadioActiveCount(int which);
1671
1672 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001673 * Returns the time in microseconds that is the difference between the mobile radio
1674 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1675 * from the radio.
1676 *
1677 * {@hide}
1678 */
1679 public abstract long getMobileRadioActiveAdjustedTime(int which);
1680
1681 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001682 * Returns the time in microseconds that the mobile network has been active
1683 * (in a high power state) but not being able to blame on an app.
1684 *
1685 * {@hide}
1686 */
1687 public abstract long getMobileRadioActiveUnknownTime(int which);
1688
1689 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001690 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001691 *
1692 * {@hide}
1693 */
1694 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001695
Dianne Hackborn627bba72009-03-24 22:32:56 -07001696 public static final int DATA_CONNECTION_NONE = 0;
1697 public static final int DATA_CONNECTION_GPRS = 1;
1698 public static final int DATA_CONNECTION_EDGE = 2;
1699 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001700 public static final int DATA_CONNECTION_CDMA = 4;
1701 public static final int DATA_CONNECTION_EVDO_0 = 5;
1702 public static final int DATA_CONNECTION_EVDO_A = 6;
1703 public static final int DATA_CONNECTION_1xRTT = 7;
1704 public static final int DATA_CONNECTION_HSDPA = 8;
1705 public static final int DATA_CONNECTION_HSUPA = 9;
1706 public static final int DATA_CONNECTION_HSPA = 10;
1707 public static final int DATA_CONNECTION_IDEN = 11;
1708 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001709 public static final int DATA_CONNECTION_LTE = 13;
1710 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001711 public static final int DATA_CONNECTION_HSPAP = 15;
1712 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001713
Dianne Hackborn627bba72009-03-24 22:32:56 -07001714 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001715 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001716 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001717 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001718 };
1719
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001720 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001721
1722 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001723 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001724 * the given data connection.
1725 *
1726 * {@hide}
1727 */
1728 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001729 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001732 * Returns the number of times the phone has entered the given data
1733 * connection type.
1734 *
1735 * {@hide}
1736 */
1737 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001738
Dianne Hackborn3251b902014-06-20 14:40:53 -07001739 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1740 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1741 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1742 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1743 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1744 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1745 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1746 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1747 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1748 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1749 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1750 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1751 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1752
1753 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1754
1755 static final String[] WIFI_SUPPL_STATE_NAMES = {
1756 "invalid", "disconn", "disabled", "inactive", "scanning",
1757 "authenticating", "associating", "associated", "4-way-handshake",
1758 "group-handshake", "completed", "dormant", "uninit"
1759 };
1760
1761 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1762 "inv", "dsc", "dis", "inact", "scan",
1763 "auth", "ascing", "asced", "4-way",
1764 "group", "compl", "dorm", "uninit"
1765 };
1766
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001767 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1768 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001769 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001770 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1771 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001772 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001773 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1774 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1775 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001776 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001777 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001778 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001779 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001780 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1781 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001782 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1783 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1784 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1785 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1786 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1787 new String[] {"in", "out", "emergency", "off"},
1788 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001789 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1790 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1791 SignalStrength.SIGNAL_STRENGTH_NAMES,
1792 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001793 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1794 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1795 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001796 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001797
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001798 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1799 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001800 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001801 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001802 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001803 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001804 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001805 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_FLAG, "device_idle", "di"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001806 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1807 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1808 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001809 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1810 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1811 new String[] { "0", "1", "2", "3", "4" },
1812 new String[] { "0", "1", "2", "3", "4" }),
1813 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1814 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1815 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001816 };
1817
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001818 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001819 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001820 "motion", "active", "pkginst", "pkgunin", "alarm", "stats"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001821 };
1822
1823 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001824 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001825 "Esm", "Eac", "Epi", "Epu", "Eal", "Est"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001826 };
1827
Dianne Hackborn617f8772009-03-31 15:04:46 -07001828 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001829 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001830 * running on battery.
1831 *
1832 * {@hide}
1833 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001834 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001835
1836 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001837 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001838 * been in the running state while the device was running on battery.
1839 *
1840 * {@hide}
1841 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001843
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001844 public static final int WIFI_STATE_OFF = 0;
1845 public static final int WIFI_STATE_OFF_SCANNING = 1;
1846 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1847 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1848 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1849 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1850 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1851 public static final int WIFI_STATE_SOFT_AP = 7;
1852
1853 static final String[] WIFI_STATE_NAMES = {
1854 "off", "scanning", "no_net", "disconn",
1855 "sta", "p2p", "sta_p2p", "soft_ap"
1856 };
1857
1858 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1859
1860 /**
1861 * Returns the time in microseconds that WiFi has been running in the given state.
1862 *
1863 * {@hide}
1864 */
1865 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001866 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001867
1868 /**
1869 * Returns the number of times that WiFi has entered the given state.
1870 *
1871 * {@hide}
1872 */
1873 public abstract int getWifiStateCount(int wifiState, int which);
1874
The Android Open Source Project10592532009-03-18 17:39:46 -07001875 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001876 * Returns the time in microseconds that the wifi supplicant has been
1877 * in a given state.
1878 *
1879 * {@hide}
1880 */
1881 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1882
1883 /**
1884 * Returns the number of times that the wifi supplicant has transitioned
1885 * to a given state.
1886 *
1887 * {@hide}
1888 */
1889 public abstract int getWifiSupplStateCount(int state, int which);
1890
1891 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1892
1893 /**
1894 * Returns the time in microseconds that WIFI has been running with
1895 * the given signal strength.
1896 *
1897 * {@hide}
1898 */
1899 public abstract long getWifiSignalStrengthTime(int strengthBin,
1900 long elapsedRealtimeUs, int which);
1901
1902 /**
1903 * Returns the number of times WIFI has entered the given signal strength.
1904 *
1905 * {@hide}
1906 */
1907 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1908
1909 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001910 * Returns the time in microseconds that the flashlight has been on while the device was
1911 * running on battery.
1912 *
1913 * {@hide}
1914 */
1915 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1916
1917 /**
1918 * Returns the number of times that the flashlight has been turned on while the device was
1919 * running on battery.
1920 *
1921 * {@hide}
1922 */
1923 public abstract long getFlashlightOnCount(int which);
1924
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001925 public static final int NETWORK_MOBILE_RX_DATA = 0;
1926 public static final int NETWORK_MOBILE_TX_DATA = 1;
1927 public static final int NETWORK_WIFI_RX_DATA = 2;
1928 public static final int NETWORK_WIFI_TX_DATA = 3;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001929 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001930
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001931 public abstract long getNetworkActivityBytes(int type, int which);
1932 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001933
Adam Lesinski33dac552015-03-09 15:24:48 -07001934 public static final int CONTROLLER_IDLE_TIME = 0;
1935 public static final int CONTROLLER_RX_TIME = 1;
1936 public static final int CONTROLLER_TX_TIME = 2;
Adam Lesinskie08af192015-03-25 16:42:59 -07001937 public static final int CONTROLLER_POWER_DRAIN = 3;
1938 public static final int NUM_CONTROLLER_ACTIVITY_TYPES = CONTROLLER_POWER_DRAIN + 1;
Adam Lesinski33dac552015-03-09 15:24:48 -07001939
Adam Lesinskie08af192015-03-25 16:42:59 -07001940 /**
Adam Lesinski17390762015-04-10 13:17:47 -07001941 * Returns true if the BatteryStats object has detailed bluetooth power reports.
1942 * When true, calling {@link #getBluetoothControllerActivity(int, int)} will yield the
1943 * actual power data.
1944 */
1945 public abstract boolean hasBluetoothActivityReporting();
1946
1947 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001948 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
1949 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
1950 * respective state.
1951 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
1952 * milli-ampere-milliseconds (mAms).
1953 */
Adam Lesinski33dac552015-03-09 15:24:48 -07001954 public abstract long getBluetoothControllerActivity(int type, int which);
Adam Lesinskie08af192015-03-25 16:42:59 -07001955
1956 /**
Adam Lesinski17390762015-04-10 13:17:47 -07001957 * Returns true if the BatteryStats object has detailed WiFi power reports.
1958 * When true, calling {@link #getWifiControllerActivity(int, int)} will yield the
1959 * actual power data.
1960 */
1961 public abstract boolean hasWifiActivityReporting();
1962
1963 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001964 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
1965 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
1966 * respective state.
1967 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
1968 * milli-ampere-milliseconds (mAms).
1969 */
Adam Lesinski33dac552015-03-09 15:24:48 -07001970 public abstract long getWifiControllerActivity(int type, int which);
1971
The Android Open Source Project10592532009-03-18 17:39:46 -07001972 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001973 * Return the wall clock time when battery stats data collection started.
1974 */
1975 public abstract long getStartClockTime();
1976
1977 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07001978 * Return platform version tag that we were running in when the battery stats started.
1979 */
1980 public abstract String getStartPlatformVersion();
1981
1982 /**
1983 * Return platform version tag that we were running in when the battery stats ended.
1984 */
1985 public abstract String getEndPlatformVersion();
1986
1987 /**
1988 * Return the internal version code of the parcelled format.
1989 */
1990 public abstract int getParcelVersion();
1991
1992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 * Return whether we are currently running on battery.
1994 */
1995 public abstract boolean getIsOnBattery();
1996
1997 /**
1998 * Returns a SparseArray containing the statistics for each uid.
1999 */
2000 public abstract SparseArray<? extends Uid> getUidStats();
2001
2002 /**
2003 * Returns the current battery uptime in microseconds.
2004 *
2005 * @param curTime the amount of elapsed realtime in microseconds.
2006 */
2007 public abstract long getBatteryUptime(long curTime);
2008
2009 /**
2010 * Returns the current battery realtime in microseconds.
2011 *
2012 * @param curTime the amount of elapsed realtime in microseconds.
2013 */
2014 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002015
2016 /**
Evan Millar633a1742009-04-02 16:36:33 -07002017 * Returns the battery percentage level at the last time the device was unplugged from power, or
2018 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002019 */
Evan Millar633a1742009-04-02 16:36:33 -07002020 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002021
2022 /**
Evan Millar633a1742009-04-02 16:36:33 -07002023 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2024 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002025 */
Evan Millar633a1742009-04-02 16:36:33 -07002026 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027
2028 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002029 * Get the amount the battery has discharged since the stats were
2030 * last reset after charging, as a lower-end approximation.
2031 */
2032 public abstract int getLowDischargeAmountSinceCharge();
2033
2034 /**
2035 * Get the amount the battery has discharged since the stats were
2036 * last reset after charging, as an upper-end approximation.
2037 */
2038 public abstract int getHighDischargeAmountSinceCharge();
2039
2040 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002041 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2042 */
2043 public abstract int getDischargeAmount(int which);
2044
2045 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002046 * Get the amount the battery has discharged while the screen was on,
2047 * since the last time power was unplugged.
2048 */
2049 public abstract int getDischargeAmountScreenOn();
2050
2051 /**
2052 * Get the amount the battery has discharged while the screen was on,
2053 * since the last time the device was charged.
2054 */
2055 public abstract int getDischargeAmountScreenOnSinceCharge();
2056
2057 /**
2058 * Get the amount the battery has discharged while the screen was off,
2059 * since the last time power was unplugged.
2060 */
2061 public abstract int getDischargeAmountScreenOff();
2062
2063 /**
2064 * Get the amount the battery has discharged while the screen was off,
2065 * since the last time the device was charged.
2066 */
2067 public abstract int getDischargeAmountScreenOffSinceCharge();
2068
2069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 * Returns the total, last, or current battery uptime in microseconds.
2071 *
2072 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002073 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 */
2075 public abstract long computeBatteryUptime(long curTime, int which);
2076
2077 /**
2078 * Returns the total, last, or current battery realtime in microseconds.
2079 *
2080 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002081 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 */
2083 public abstract long computeBatteryRealtime(long curTime, int which);
2084
2085 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002086 * Returns the total, last, or current battery screen off uptime in microseconds.
2087 *
2088 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002089 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002090 */
2091 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2092
2093 /**
2094 * Returns the total, last, or current battery screen off realtime in microseconds.
2095 *
2096 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002097 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002098 */
2099 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2100
2101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 * Returns the total, last, or current uptime in microseconds.
2103 *
2104 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002105 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 */
2107 public abstract long computeUptime(long curTime, int which);
2108
2109 /**
2110 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002113 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 */
2115 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002116
2117 /**
2118 * Compute an approximation for how much run time (in microseconds) is remaining on
2119 * the battery. Returns -1 if no time can be computed: either there is not
2120 * enough current data to make a decision, or the battery is currently
2121 * charging.
2122 *
2123 * @param curTime The current elepsed realtime in microseconds.
2124 */
2125 public abstract long computeBatteryTimeRemaining(long curTime);
2126
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002127 // The part of a step duration that is the actual time.
2128 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2129
2130 // Bits in a step duration that are the new battery level we are at.
2131 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002132 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002133
2134 // Bits in a step duration that are the initial mode we were in at that step.
2135 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002136 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002137
2138 // Bits in a step duration that indicate which modes changed during that step.
2139 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002140 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002141
2142 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2143 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2144
2145 // Step duration mode: power save is on.
2146 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2147
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002148 // Step duration mode: device is currently in idle mode.
2149 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2150
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002151 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2152 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002153 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2154 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002155 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2156 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2157 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2158 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2159 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002160 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2161 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002162 };
2163 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2164 (Display.STATE_OFF-1),
2165 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002166 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002167 (Display.STATE_ON-1),
2168 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2169 (Display.STATE_DOZE-1),
2170 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2171 (Display.STATE_DOZE_SUSPEND-1),
2172 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002173 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002174 };
2175 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2176 "screen off",
2177 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002178 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002179 "screen on",
2180 "screen on power save",
2181 "screen doze",
2182 "screen doze power save",
2183 "screen doze-suspend",
2184 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002185 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002186 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002187
2188 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002189 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002190 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002191 public abstract LevelStepTracker getDischargeLevelStepTracker();
2192
2193 /**
2194 * Return the array of daily discharge step durations.
2195 */
2196 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002197
2198 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002199 * Compute an approximation for how much time (in microseconds) remains until the battery
2200 * is fully charged. Returns -1 if no time can be computed: either there is not
2201 * enough current data to make a decision, or the battery is currently
2202 * discharging.
2203 *
2204 * @param curTime The current elepsed realtime in microseconds.
2205 */
2206 public abstract long computeChargeTimeRemaining(long curTime);
2207
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002208 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002209 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002210 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002211 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002212
2213 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002214 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002215 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002216 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002217
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002218 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2219
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002220 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002221
Evan Millarc64edde2009-04-18 12:26:32 -07002222 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223
Amith Yamasanie43530a2009-08-21 13:11:37 -07002224 /** Returns the number of different speeds that the CPU can run at */
2225 public abstract int getCpuSpeedSteps();
2226
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002227 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2228
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002229 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 long days = seconds / (60 * 60 * 24);
2231 if (days != 0) {
2232 out.append(days);
2233 out.append("d ");
2234 }
2235 long used = days * 60 * 60 * 24;
2236
2237 long hours = (seconds - used) / (60 * 60);
2238 if (hours != 0 || used != 0) {
2239 out.append(hours);
2240 out.append("h ");
2241 }
2242 used += hours * 60 * 60;
2243
2244 long mins = (seconds-used) / 60;
2245 if (mins != 0 || used != 0) {
2246 out.append(mins);
2247 out.append("m ");
2248 }
2249 used += mins * 60;
2250
2251 if (seconds != 0 || used != 0) {
2252 out.append(seconds-used);
2253 out.append("s ");
2254 }
2255 }
2256
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002257 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002259 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 sb.append(time - (sec * 1000));
2261 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002264 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002265 long sec = time / 1000;
2266 formatTimeRaw(sb, sec);
2267 sb.append(time - (sec * 1000));
2268 sb.append("ms");
2269 }
2270
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002271 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
2275 float perc = ((float)num) / ((float)den) * 100;
2276 mFormatBuilder.setLength(0);
2277 mFormatter.format("%.1f%%", perc);
2278 return mFormatBuilder.toString();
2279 }
2280
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002281 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002282 mFormatBuilder.setLength(0);
2283
2284 if (bytes < BYTES_PER_KB) {
2285 return bytes + "B";
2286 } else if (bytes < BYTES_PER_MB) {
2287 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2288 return mFormatBuilder.toString();
2289 } else if (bytes < BYTES_PER_GB){
2290 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2291 return mFormatBuilder.toString();
2292 } else {
2293 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2294 return mFormatBuilder.toString();
2295 }
2296 }
2297
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002298 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002299 if (timer != null) {
2300 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002301 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002302 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2303 return totalTimeMillis;
2304 }
2305 return 0;
2306 }
2307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 /**
2309 *
2310 * @param sb a StringBuilder object.
2311 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002312 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002314 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 * @param linePrefix a String to be prepended to each line of output.
2316 * @return the line prefix
2317 */
2318 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002319 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320
2321 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002322 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323
Evan Millarc64edde2009-04-18 12:26:32 -07002324 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 if (totalTimeMillis != 0) {
2326 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002327 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002328 if (name != null) {
2329 sb.append(name);
2330 sb.append(' ');
2331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 sb.append('(');
2333 sb.append(count);
2334 sb.append(" times)");
2335 return ", ";
2336 }
2337 }
2338 return linePrefix;
2339 }
2340
2341 /**
2342 * Checkin version of wakelock printer. Prints simple comma-separated list.
2343 *
2344 * @param sb a StringBuilder object.
2345 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002346 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002348 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 * @param linePrefix a String to be prepended to each line of output.
2350 * @return the line prefix
2351 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002352 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2353 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 long totalTimeMicros = 0;
2355 int count = 0;
2356 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002357 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002358 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360 sb.append(linePrefix);
2361 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2362 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002363 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 sb.append(count);
2365 return ",";
2366 }
2367
2368 /**
2369 * Dump a comma-separated line of values for terse checkin mode.
2370 *
2371 * @param pw the PageWriter to dump log to
2372 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2373 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2374 * @param args type-dependent data arguments
2375 */
2376 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2377 Object... args ) {
2378 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2379 pw.print(uid); pw.print(',');
2380 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002381 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382
2383 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002384 pw.print(',');
2385 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002387 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002389
2390 /**
2391 * Temporary for settings.
2392 */
2393 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2394 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2395 }
2396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
2398 * Checkin server version of dump to produce more compact, computer-readable log.
2399 *
2400 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002402 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2403 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2405 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2406 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2408 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002409 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2410 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2411 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 final long totalRealtime = computeRealtime(rawRealtime, which);
2413 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002414 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002415 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002416 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
2417 final long deviceIdleModeEnabledTime = getDeviceIdleModeEnabledTime(rawRealtime, which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002418 final long deviceIdlingTime = getDeviceIdlingTime(rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002419 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002420 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002421
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002422 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002424 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002425 final int NU = uidStats.size();
2426
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002427 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 // Dump "battery" stat
2430 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002431 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002432 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002433 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002434 getStartClockTime(),
2435 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002437 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002438 long fullWakeLockTimeTotal = 0;
2439 long partialWakeLockTimeTotal = 0;
2440
2441 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002442 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002443
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002444 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2445 = u.getWakelockStats();
2446 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2447 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002448
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002449 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2450 if (fullWakeTimer != null) {
2451 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2452 which);
2453 }
2454
2455 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2456 if (partialWakeTimer != null) {
2457 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2458 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002459 }
2460 }
2461 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002462
2463 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002464 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2465 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2466 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2467 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2468 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2469 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2470 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2471 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002472 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2473 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
2474 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
2475
Adam Lesinskie283d332015-04-16 12:29:25 -07002476 // Dump Wifi controller stats
2477 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2478 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2479 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2480 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2481 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
2482 final long wifiPowerMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
2483 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA,
2484 wifiOnTime / 1000, wifiRunningTime / 1000,
2485 wifiIdleTimeMs, wifiRxTimeMs, wifiTxTimeMs, wifiPowerMaMs / (1000*60*60));
2486
2487 // Dump Bluetooth controller stats
2488 final long btIdleTimeMs = getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
2489 final long btRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
2490 final long btTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
2491 final long btPowerMaMs = getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which);
2492 dumpLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_DATA,
2493 btIdleTimeMs, btRxTimeMs, btTxTimeMs, btPowerMaMs / (1000*60*60));
2494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 // Dump misc stats
2496 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002497 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002498 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002499 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002500 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002501 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeEnabledTime / 1000,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002502 getDeviceIdleModeEnabledCount(which), deviceIdlingTime / 1000,
2503 getDeviceIdlingCount(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002504
2505 // Dump screen brightness stats
2506 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2507 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002508 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002509 }
2510 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002511
Dianne Hackborn627bba72009-03-24 22:32:56 -07002512 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002513 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2514 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002515 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002516 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002517 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002518 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002519 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002520 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002521 args[i] = getPhoneSignalStrengthCount(i, which);
2522 }
2523 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002524
Dianne Hackborn627bba72009-03-24 22:32:56 -07002525 // Dump network type stats
2526 args = new Object[NUM_DATA_CONNECTION_TYPES];
2527 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002528 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002529 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002530 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2531 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2532 args[i] = getPhoneDataConnectionCount(i, which);
2533 }
2534 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002535
2536 // Dump wifi state stats
2537 args = new Object[NUM_WIFI_STATES];
2538 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002539 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002540 }
2541 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2542 for (int i=0; i<NUM_WIFI_STATES; i++) {
2543 args[i] = getWifiStateCount(i, which);
2544 }
2545 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2546
Dianne Hackborn3251b902014-06-20 14:40:53 -07002547 // Dump wifi suppl state stats
2548 args = new Object[NUM_WIFI_SUPPL_STATES];
2549 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2550 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2551 }
2552 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2553 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2554 args[i] = getWifiSupplStateCount(i, which);
2555 }
2556 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2557
2558 // Dump wifi signal strength stats
2559 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2560 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2561 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2562 }
2563 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2564 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2565 args[i] = getWifiSignalStrengthCount(i, which);
2566 }
2567 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2568
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002569 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002570 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002571 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002574 if (which == STATS_SINCE_UNPLUGGED) {
2575 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2576 getDischargeStartLevel()-getDischargeCurrentLevel(),
2577 getDischargeStartLevel()-getDischargeCurrentLevel(),
2578 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2579 } else {
2580 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2581 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002582 getDischargeAmountScreenOnSinceCharge(),
2583 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002584 }
2585
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002586 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002587 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002588 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002589 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002590 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002591 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002592 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002593 sb.toString());
2594 }
Evan Millarc64edde2009-04-18 12:26:32 -07002595 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002596 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002597 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002598 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2599 // Not doing the regular wake lock formatting to remain compatible
2600 // with the old checkin format.
2601 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2602 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002603 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002604 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002605 }
2606 }
Evan Millarc64edde2009-04-18 12:26:32 -07002607 }
2608
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002609 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002610 helper.create(this);
2611 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002612 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002613 if (sippers != null && sippers.size() > 0) {
2614 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2615 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002616 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002617 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2618 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2619 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002620 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002621 int uid = 0;
2622 String label;
2623 switch (bs.drainType) {
2624 case IDLE:
2625 label="idle";
2626 break;
2627 case CELL:
2628 label="cell";
2629 break;
2630 case PHONE:
2631 label="phone";
2632 break;
2633 case WIFI:
2634 label="wifi";
2635 break;
2636 case BLUETOOTH:
2637 label="blue";
2638 break;
2639 case SCREEN:
2640 label="scrn";
2641 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002642 case FLASHLIGHT:
2643 label="flashlight";
2644 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002645 case APP:
2646 uid = bs.uidObj.getUid();
2647 label = "uid";
2648 break;
2649 case USER:
2650 uid = UserHandle.getUid(bs.userId, 0);
2651 label = "user";
2652 break;
2653 case UNACCOUNTED:
2654 label = "unacc";
2655 break;
2656 case OVERCOUNTED:
2657 label = "over";
2658 break;
2659 default:
2660 label = "???";
2661 }
2662 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07002663 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002664 }
2665 }
2666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 for (int iu = 0; iu < NU; iu++) {
2668 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002669 if (reqUid >= 0 && uid != reqUid) {
2670 continue;
2671 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002672 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002675 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2676 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2677 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2678 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2679 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2680 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2681 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
2682 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
2683 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2684 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002685 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2686 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002687 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002688 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2689 wifiBytesRx, wifiBytesTx,
2690 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002691 wifiPacketsRx, wifiPacketsTx,
2692 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002693 }
2694
Adam Lesinskie283d332015-04-16 12:29:25 -07002695 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2696 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2697 final int wifiScanCount = u.getWifiScanCount(which);
2698 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2699 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2700 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2701 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07002702 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinskie283d332015-04-16 12:29:25 -07002703 || uidWifiRunningTime != 0 || uidWifiIdleTimeMs != 0 || uidWifiRxTimeMs != 0
2704 || uidWifiTxTimeMs != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002705 dumpLine(pw, uid, category, WIFI_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002706 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount,
2707 uidWifiIdleTimeMs, uidWifiRxTimeMs, uidWifiTxTimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07002708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
Dianne Hackborn617f8772009-03-31 15:04:46 -07002710 if (u.hasUserActivity()) {
2711 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2712 boolean hasData = false;
2713 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2714 int val = u.getUserActivityCount(i, which);
2715 args[i] = val;
2716 if (val != 0) hasData = true;
2717 }
2718 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07002719 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002720 }
2721 }
2722
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002723 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
2724 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2725 final Uid.Wakelock wl = wakelocks.valueAt(iw);
2726 String linePrefix = "";
2727 sb.setLength(0);
2728 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
2729 rawRealtime, "f", which, linePrefix);
2730 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
2731 rawRealtime, "p", which, linePrefix);
2732 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
2733 rawRealtime, "w", which, linePrefix);
2734
2735 // Only log if we had at lease one wakelock...
2736 if (sb.length() > 0) {
2737 String name = wakelocks.keyAt(iw);
2738 if (name.indexOf(',') >= 0) {
2739 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002741 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
2743 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002744
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002745 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
2746 for (int isy=syncs.size()-1; isy>=0; isy--) {
2747 final Timer timer = syncs.valueAt(isy);
2748 // Convert from microseconds to milliseconds with rounding
2749 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2750 final int count = timer.getCountLocked(which);
2751 if (totalTime != 0) {
2752 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002753 }
2754 }
2755
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002756 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
2757 for (int ij=jobs.size()-1; ij>=0; ij--) {
2758 final Timer timer = jobs.valueAt(ij);
2759 // Convert from microseconds to milliseconds with rounding
2760 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2761 final int count = timer.getCountLocked(which);
2762 if (totalTime != 0) {
2763 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002764 }
2765 }
2766
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002767 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2768 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002769 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002770 final Uid.Sensor se = sensors.valueAt(ise);
2771 final int sensorNumber = sensors.keyAt(ise);
2772 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002773 if (timer != null) {
2774 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002775 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2776 / 1000;
2777 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002778 if (totalTime != 0) {
2779 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 }
2782 }
2783
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002784 final Timer vibTimer = u.getVibratorOnTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002785 if (vibTimer != null) {
2786 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002787 final long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500)
2788 / 1000;
2789 final int count = vibTimer.getCountLocked(which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002790 if (totalTime != 0) {
2791 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
2792 }
2793 }
2794
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002795 final Timer fgTimer = u.getForegroundActivityTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002796 if (fgTimer != null) {
2797 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002798 final long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500)
2799 / 1000;
2800 final int count = fgTimer.getCountLocked(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002801 if (totalTime != 0) {
2802 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
2803 }
2804 }
2805
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002806 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07002807 long totalStateTime = 0;
2808 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
2809 totalStateTime += u.getProcessStateTime(ips, rawRealtime, which);
2810 stateTimes[ips] = (totalStateTime + 500) / 1000;
2811 }
2812 if (totalStateTime > 0) {
2813 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2814 }
2815
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002816 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
2817 = u.getProcessStats();
2818 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
2819 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002820
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002821 final long userMillis = ps.getUserTime(which);
2822 final long systemMillis = ps.getSystemTime(which);
2823 final long foregroundMillis = ps.getForegroundTime(which);
2824 final int starts = ps.getStarts(which);
2825 final int numCrashes = ps.getNumCrashes(which);
2826 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002827
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002828 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2829 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
2830 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
2831 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
2833 }
2834
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002835 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
2836 = u.getPackageStats();
2837 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
2838 final Uid.Pkg ps = packageStats.valueAt(ipkg);
2839 int wakeups = 0;
2840 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
2841 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
2842 wakeups += alarms.valueAt(iwa).getCountLocked(which);
2843 }
2844 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2845 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
2846 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
2847 final long startTime = ss.getStartTime(batteryUptime, which);
2848 final int starts = ss.getStarts(which);
2849 final int launches = ss.getLaunches(which);
2850 if (startTime != 0 || starts != 0 || launches != 0) {
2851 dumpLine(pw, uid, category, APK_DATA,
2852 wakeups, // wakeup alarms
2853 packageStats.keyAt(ipkg), // Apk
2854 serviceStats.keyAt(isvc), // service
2855 startTime / 1000, // time spent started, in ms
2856 starts,
2857 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
2859 }
2860 }
2861 }
2862 }
2863
Dianne Hackborn81038902012-11-26 17:04:09 -08002864 static final class TimerEntry {
2865 final String mName;
2866 final int mId;
2867 final BatteryStats.Timer mTimer;
2868 final long mTime;
2869 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2870 mName = name;
2871 mId = id;
2872 mTimer = timer;
2873 mTime = time;
2874 }
2875 }
2876
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002877 private void printmAh(PrintWriter printer, double power) {
2878 printer.print(BatteryStatsHelper.makemAh(power));
2879 }
2880
Dianne Hackbornd953c532014-08-16 18:17:38 -07002881 /**
2882 * Temporary for settings.
2883 */
2884 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
2885 int reqUid) {
2886 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2887 }
2888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002890 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07002891 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2893 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2894 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895
2896 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2897 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2898 final long totalRealtime = computeRealtime(rawRealtime, which);
2899 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002900 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2901 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2902 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002903 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2904 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002905
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002906 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07002907
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002908 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002909 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002911 sb.setLength(0);
2912 sb.append(prefix);
2913 sb.append(" Time on battery: ");
2914 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2915 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2916 sb.append(") realtime, ");
2917 formatTimeMs(sb, whichBatteryUptime / 1000);
2918 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2919 sb.append(") uptime");
2920 pw.println(sb.toString());
2921 sb.setLength(0);
2922 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002923 sb.append(" Time on battery screen off: ");
2924 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2925 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2926 sb.append(") realtime, ");
2927 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2928 sb.append("(");
2929 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2930 sb.append(") uptime");
2931 pw.println(sb.toString());
2932 sb.setLength(0);
2933 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002934 sb.append(" Total run time: ");
2935 formatTimeMs(sb, totalRealtime / 1000);
2936 sb.append("realtime, ");
2937 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002938 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07002939 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002940 if (batteryTimeRemaining >= 0) {
2941 sb.setLength(0);
2942 sb.append(prefix);
2943 sb.append(" Battery time remaining: ");
2944 formatTimeMs(sb, batteryTimeRemaining / 1000);
2945 pw.println(sb.toString());
2946 }
2947 if (chargeTimeRemaining >= 0) {
2948 sb.setLength(0);
2949 sb.append(prefix);
2950 sb.append(" Charge time remaining: ");
2951 formatTimeMs(sb, chargeTimeRemaining / 1000);
2952 pw.println(sb.toString());
2953 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002954 pw.print(" Start clock time: ");
2955 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2956
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002957 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002958 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002959 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
2960 final long deviceIdleModeEnabledTime = getDeviceIdleModeEnabledTime(rawRealtime, which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002961 final long deviceIdlingTime = getDeviceIdlingTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002962 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2963 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2964 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002965 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002966 sb.append(prefix);
2967 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2968 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002969 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002970 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2971 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07002972 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002973 pw.println(sb.toString());
2974 sb.setLength(0);
2975 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002976 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002977 boolean didOne = false;
2978 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002979 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002980 if (time == 0) {
2981 continue;
2982 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002983 sb.append("\n ");
2984 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002985 didOne = true;
2986 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2987 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002988 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002989 sb.append("(");
2990 sb.append(formatRatioLocked(time, screenOnTime));
2991 sb.append(")");
2992 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002993 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002994 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002995 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002996 sb.setLength(0);
2997 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002998 sb.append(" Power save mode enabled: ");
2999 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003000 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003001 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003002 sb.append(")");
3003 pw.println(sb.toString());
3004 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003005 if (deviceIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003006 sb.setLength(0);
3007 sb.append(prefix);
3008 sb.append(" Device idling: ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003009 formatTimeMs(sb, deviceIdlingTime / 1000);
3010 sb.append("(");
3011 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3012 sb.append(") "); sb.append(getDeviceIdlingCount(which));
3013 sb.append("x");
3014 pw.println(sb.toString());
3015 }
3016 if (deviceIdleModeEnabledTime != 0) {
3017 sb.setLength(0);
3018 sb.append(prefix);
3019 sb.append(" Idle mode time: ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003020 formatTimeMs(sb, deviceIdleModeEnabledTime / 1000);
3021 sb.append("(");
3022 sb.append(formatRatioLocked(deviceIdleModeEnabledTime, whichBatteryRealtime));
3023 sb.append(") "); sb.append(getDeviceIdleModeEnabledCount(which));
3024 sb.append("x");
3025 pw.println(sb.toString());
3026 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003027 if (phoneOnTime != 0) {
3028 sb.setLength(0);
3029 sb.append(prefix);
3030 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3031 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003032 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003033 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003034 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003035 if (connChanges != 0) {
3036 pw.print(prefix);
3037 pw.print(" Connectivity changes: "); pw.println(connChanges);
3038 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003039
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003040 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003041 long fullWakeLockTimeTotalMicros = 0;
3042 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003043
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003044 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003045
Evan Millar22ac0432009-03-31 11:33:18 -07003046 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003047 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003048
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003049 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3050 = u.getWakelockStats();
3051 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3052 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003053
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003054 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3055 if (fullWakeTimer != null) {
3056 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3057 rawRealtime, which);
3058 }
3059
3060 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3061 if (partialWakeTimer != null) {
3062 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3063 rawRealtime, which);
3064 if (totalTimeMicros > 0) {
3065 if (reqUid < 0) {
3066 // Only show the ordered list of all wake
3067 // locks if the caller is not asking for data
3068 // about a specific uid.
3069 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3070 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003071 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003072 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003073 }
3074 }
3075 }
3076 }
3077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003078 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3079 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3080 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3081 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3082 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3083 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3084 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3085 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003086
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003087 if (fullWakeLockTimeTotalMicros != 0) {
3088 sb.setLength(0);
3089 sb.append(prefix);
3090 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3091 (fullWakeLockTimeTotalMicros + 500) / 1000);
3092 pw.println(sb.toString());
3093 }
3094
3095 if (partialWakeLockTimeTotalMicros != 0) {
3096 sb.setLength(0);
3097 sb.append(prefix);
3098 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3099 (partialWakeLockTimeTotalMicros + 500) / 1000);
3100 pw.println(sb.toString());
3101 }
3102
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003103 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003104 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3105 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3106 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3107 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003108 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003109 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003110 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003111 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003112 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003113 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003114 if (time == 0) {
3115 continue;
3116 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003117 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003118 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003119 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003120 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003121 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003122 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003123 sb.append("(");
3124 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003125 sb.append(") ");
3126 sb.append(getPhoneSignalStrengthCount(i, which));
3127 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003128 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003129 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003130 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003131
3132 sb.setLength(0);
3133 sb.append(prefix);
3134 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003135 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003136 pw.println(sb.toString());
3137
Dianne Hackborn627bba72009-03-24 22:32:56 -07003138 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003139 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003140 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003141 didOne = false;
3142 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003143 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003144 if (time == 0) {
3145 continue;
3146 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003147 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003148 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003149 didOne = true;
3150 sb.append(DATA_CONNECTION_NAMES[i]);
3151 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003152 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003153 sb.append("(");
3154 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003155 sb.append(") ");
3156 sb.append(getPhoneDataConnectionCount(i, which));
3157 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003158 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003159 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003160 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003161
3162 sb.setLength(0);
3163 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003164 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003165 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003166 formatTimeMs(sb, mobileActiveTime / 1000);
3167 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3168 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3169 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003170 pw.println(sb.toString());
3171
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003172 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3173 if (mobileActiveUnknownTime != 0) {
3174 sb.setLength(0);
3175 sb.append(prefix);
3176 sb.append(" Mobile radio active unknown time: ");
3177 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3178 sb.append("(");
3179 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3180 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3181 sb.append("x");
3182 pw.println(sb.toString());
3183 }
3184
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003185 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3186 if (mobileActiveAdjustedTime != 0) {
3187 sb.setLength(0);
3188 sb.append(prefix);
3189 sb.append(" Mobile radio active adjusted time: ");
3190 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3191 sb.append("(");
3192 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3193 sb.append(")");
3194 pw.println(sb.toString());
3195 }
3196
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003197 pw.print(prefix);
3198 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3199 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3200 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3201 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003202 sb.setLength(0);
3203 sb.append(prefix);
3204 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3205 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3206 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3207 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003208 sb.append(")");
3209 pw.println(sb.toString());
3210
3211 sb.setLength(0);
3212 sb.append(prefix);
3213 sb.append(" Wifi states:");
3214 didOne = false;
3215 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003216 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003217 if (time == 0) {
3218 continue;
3219 }
3220 sb.append("\n ");
3221 didOne = true;
3222 sb.append(WIFI_STATE_NAMES[i]);
3223 sb.append(" ");
3224 formatTimeMs(sb, time/1000);
3225 sb.append("(");
3226 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3227 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003228 sb.append(getWifiStateCount(i, which));
3229 sb.append("x");
3230 }
3231 if (!didOne) sb.append(" (no activity)");
3232 pw.println(sb.toString());
3233
3234 sb.setLength(0);
3235 sb.append(prefix);
3236 sb.append(" Wifi supplicant states:");
3237 didOne = false;
3238 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3239 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3240 if (time == 0) {
3241 continue;
3242 }
3243 sb.append("\n ");
3244 didOne = true;
3245 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3246 sb.append(" ");
3247 formatTimeMs(sb, time/1000);
3248 sb.append("(");
3249 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3250 sb.append(") ");
3251 sb.append(getWifiSupplStateCount(i, which));
3252 sb.append("x");
3253 }
3254 if (!didOne) sb.append(" (no activity)");
3255 pw.println(sb.toString());
3256
3257 sb.setLength(0);
3258 sb.append(prefix);
3259 sb.append(" Wifi signal levels:");
3260 didOne = false;
3261 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3262 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3263 if (time == 0) {
3264 continue;
3265 }
3266 sb.append("\n ");
3267 sb.append(prefix);
3268 didOne = true;
3269 sb.append("level(");
3270 sb.append(i);
3271 sb.append(") ");
3272 formatTimeMs(sb, time/1000);
3273 sb.append("(");
3274 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3275 sb.append(") ");
3276 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003277 sb.append("x");
3278 }
3279 if (!didOne) sb.append(" (no activity)");
3280 pw.println(sb.toString());
3281
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003282 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3283 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3284 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Adam Lesinski33dac552015-03-09 15:24:48 -07003285 final long wifiTotalTimeMs = wifiIdleTimeMs + wifiRxTimeMs + wifiTxTimeMs;
3286
3287 sb.setLength(0);
3288 sb.append(prefix);
3289 sb.append(" WiFi Idle time: "); formatTimeMs(sb, wifiIdleTimeMs);
3290 sb.append(" (");
3291 sb.append(formatRatioLocked(wifiIdleTimeMs, wifiTotalTimeMs));
3292 sb.append(")");
3293 pw.println(sb.toString());
3294
3295 sb.setLength(0);
3296 sb.append(prefix);
3297 sb.append(" WiFi Rx time: "); formatTimeMs(sb, wifiRxTimeMs);
3298 sb.append(" (");
3299 sb.append(formatRatioLocked(wifiRxTimeMs, wifiTotalTimeMs));
3300 sb.append(")");
3301 pw.println(sb.toString());
3302
3303 sb.setLength(0);
3304 sb.append(prefix);
3305 sb.append(" WiFi Tx time: "); formatTimeMs(sb, wifiTxTimeMs);
3306 sb.append(" (");
3307 sb.append(formatRatioLocked(wifiTxTimeMs, wifiTotalTimeMs));
3308 sb.append(")");
3309 pw.println(sb.toString());
3310
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003311 sb.setLength(0);
3312 sb.append(prefix);
Adam Lesinskie283d332015-04-16 12:29:25 -07003313 sb.append(" WiFi Power drain: ").append(BatteryStatsHelper.makemAh(
Adam Lesinskie08af192015-03-25 16:42:59 -07003314 getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which) / (double)(1000*60*60)));
3315 sb.append(" mAh");
3316 pw.println(sb.toString());
3317
Adam Lesinski33dac552015-03-09 15:24:48 -07003318 final long bluetoothIdleTimeMs =
3319 getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
3320 final long bluetoothRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
3321 final long bluetoothTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
3322 final long bluetoothTotalTimeMs = bluetoothIdleTimeMs + bluetoothRxTimeMs +
3323 bluetoothTxTimeMs;
3324
3325 sb.setLength(0);
3326 sb.append(prefix);
3327 sb.append(" Bluetooth Idle time: "); formatTimeMs(sb, bluetoothIdleTimeMs);
3328 sb.append(" (");
3329 sb.append(formatRatioLocked(bluetoothIdleTimeMs, bluetoothTotalTimeMs));
3330 sb.append(")");
3331 pw.println(sb.toString());
3332
3333 sb.setLength(0);
3334 sb.append(prefix);
3335 sb.append(" Bluetooth Rx time: "); formatTimeMs(sb, bluetoothRxTimeMs);
3336 sb.append(" (");
3337 sb.append(formatRatioLocked(bluetoothRxTimeMs, bluetoothTotalTimeMs));
3338 sb.append(")");
3339 pw.println(sb.toString());
3340
3341 sb.setLength(0);
3342 sb.append(prefix);
3343 sb.append(" Bluetooth Tx time: "); formatTimeMs(sb, bluetoothTxTimeMs);
3344 sb.append(" (");
3345 sb.append(formatRatioLocked(bluetoothTxTimeMs, bluetoothTotalTimeMs));
3346 sb.append(")");
3347 pw.println(sb.toString());
3348
Adam Lesinskie283d332015-04-16 12:29:25 -07003349 sb.setLength(0);
3350 sb.append(prefix);
3351 sb.append(" Bluetooth Power drain: ").append(BatteryStatsHelper.makemAh(
3352 getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which) /
3353 (double)(1000*60*60)));
3354 sb.append(" mAh");
3355 pw.println(sb.toString());
3356
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003357 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003358
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003359 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003360 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003361 pw.print(prefix); pw.println(" Device is currently unplugged");
3362 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3363 pw.println(getDischargeStartLevel());
3364 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3365 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003366 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003367 pw.print(prefix); pw.println(" Device is currently plugged into power");
3368 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3369 pw.println(getDischargeStartLevel());
3370 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3371 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003372 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003373 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3374 pw.println(getDischargeAmountScreenOn());
3375 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3376 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003377 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003378 } else {
3379 pw.print(prefix); pw.println(" Device battery use since last full charge");
3380 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3381 pw.println(getLowDischargeAmountSinceCharge());
3382 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3383 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003384 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3385 pw.println(getDischargeAmountScreenOnSinceCharge());
3386 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3387 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003388 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003389 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003390
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003391 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003392 helper.create(this);
3393 helper.refreshStats(which, UserHandle.USER_ALL);
3394 List<BatterySipper> sippers = helper.getUsageList();
3395 if (sippers != null && sippers.size() > 0) {
3396 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3397 pw.print(prefix); pw.print(" Capacity: ");
3398 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003399 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003400 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3401 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3402 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3403 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003404 pw.println();
3405 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003406 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003407 switch (bs.drainType) {
3408 case IDLE:
Adam Lesinskie08af192015-03-25 16:42:59 -07003409 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003410 pw.println();
3411 break;
3412 case CELL:
Adam Lesinskie08af192015-03-25 16:42:59 -07003413 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003414 pw.println();
3415 break;
3416 case PHONE:
Adam Lesinskie08af192015-03-25 16:42:59 -07003417 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003418 pw.println();
3419 break;
3420 case WIFI:
Adam Lesinskie08af192015-03-25 16:42:59 -07003421 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003422 pw.println();
3423 break;
3424 case BLUETOOTH:
Adam Lesinskie08af192015-03-25 16:42:59 -07003425 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003426 pw.println();
3427 break;
3428 case SCREEN:
Adam Lesinskie08af192015-03-25 16:42:59 -07003429 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003430 pw.println();
3431 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003432 case FLASHLIGHT:
Adam Lesinskie08af192015-03-25 16:42:59 -07003433 pw.print(prefix); pw.print(" Flashlight: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003434 pw.println();
3435 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003436 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003437 pw.print(prefix); pw.print(" Uid ");
3438 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinskie08af192015-03-25 16:42:59 -07003439 pw.print(": "); printmAh(pw, bs.totalPowerMah); pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003440 break;
3441 case USER:
3442 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
Adam Lesinskie08af192015-03-25 16:42:59 -07003443 pw.print(": "); printmAh(pw, bs.totalPowerMah); pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003444 break;
3445 case UNACCOUNTED:
Adam Lesinskie08af192015-03-25 16:42:59 -07003446 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003447 pw.println();
3448 break;
3449 case OVERCOUNTED:
Adam Lesinskie08af192015-03-25 16:42:59 -07003450 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.totalPowerMah);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003451 pw.println();
3452 break;
3453 }
3454 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003455 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003456 }
3457
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003458 sippers = helper.getMobilemsppList();
3459 if (sippers != null && sippers.size() > 0) {
3460 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003461 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003462 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003463 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003464 sb.setLength(0);
3465 sb.append(prefix); sb.append(" Uid ");
3466 UserHandle.formatUid(sb, bs.uidObj.getUid());
3467 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3468 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3469 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003470 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003471 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003472 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003473 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003474 sb.setLength(0);
3475 sb.append(prefix);
3476 sb.append(" TOTAL TIME: ");
3477 formatTimeMs(sb, totalTime);
3478 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3479 sb.append(")");
3480 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003481 pw.println();
3482 }
3483
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003484 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3485 @Override
3486 public int compare(TimerEntry lhs, TimerEntry rhs) {
3487 long lhsTime = lhs.mTime;
3488 long rhsTime = rhs.mTime;
3489 if (lhsTime < rhsTime) {
3490 return 1;
3491 }
3492 if (lhsTime > rhsTime) {
3493 return -1;
3494 }
3495 return 0;
3496 }
3497 };
3498
3499 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003500 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3501 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003502 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003503 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3504 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3505 : kernelWakelocks.entrySet()) {
3506 final BatteryStats.Timer timer = ent.getValue();
3507 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003508 if (totalTimeMillis > 0) {
3509 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3510 }
3511 }
3512 if (ktimers.size() > 0) {
3513 Collections.sort(ktimers, timerComparator);
3514 pw.print(prefix); pw.println(" All kernel wake locks:");
3515 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003516 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003517 String linePrefix = ": ";
3518 sb.setLength(0);
3519 sb.append(prefix);
3520 sb.append(" Kernel Wake lock ");
3521 sb.append(timer.mName);
3522 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3523 which, linePrefix);
3524 if (!linePrefix.equals(": ")) {
3525 sb.append(" realtime");
3526 // Only print out wake locks that were held
3527 pw.println(sb.toString());
3528 }
3529 }
3530 pw.println();
3531 }
3532 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003533
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003534 if (timers.size() > 0) {
3535 Collections.sort(timers, timerComparator);
3536 pw.print(prefix); pw.println(" All partial wake locks:");
3537 for (int i=0; i<timers.size(); i++) {
3538 TimerEntry timer = timers.get(i);
3539 sb.setLength(0);
3540 sb.append(" Wake lock ");
3541 UserHandle.formatUid(sb, timer.mId);
3542 sb.append(" ");
3543 sb.append(timer.mName);
3544 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3545 sb.append(" realtime");
3546 pw.println(sb.toString());
3547 }
3548 timers.clear();
3549 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003550 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003551
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003552 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003553 if (wakeupReasons.size() > 0) {
3554 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003555 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003556 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003557 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003558 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3559 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003560 }
3561 Collections.sort(reasons, timerComparator);
3562 for (int i=0; i<reasons.size(); i++) {
3563 TimerEntry timer = reasons.get(i);
3564 String linePrefix = ": ";
3565 sb.setLength(0);
3566 sb.append(prefix);
3567 sb.append(" Wakeup reason ");
3568 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003569 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3570 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003571 pw.println(sb.toString());
3572 }
3573 pw.println();
3574 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003575 }
Evan Millar22ac0432009-03-31 11:33:18 -07003576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 for (int iu=0; iu<NU; iu++) {
3578 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003579 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003580 continue;
3581 }
3582
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003583 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003584
3585 pw.print(prefix);
3586 pw.print(" ");
3587 UserHandle.formatUid(pw, uid);
3588 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003590
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003591 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3592 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3593 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3594 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3595 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3596 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3597 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3598 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
3599 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3600 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
3601 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3602 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3603 final int wifiScanCount = u.getWifiScanCount(which);
3604 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003605
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003606 if (mobileRxBytes > 0 || mobileTxBytes > 0
3607 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003608 pw.print(prefix); pw.print(" Mobile network: ");
3609 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003610 pw.print(formatBytesLocked(mobileTxBytes));
3611 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3612 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003614 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3615 sb.setLength(0);
3616 sb.append(prefix); sb.append(" Mobile radio active: ");
3617 formatTimeMs(sb, uidMobileActiveTime / 1000);
3618 sb.append("(");
3619 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3620 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3621 long packets = mobileRxPackets + mobileTxPackets;
3622 if (packets == 0) {
3623 packets = 1;
3624 }
3625 sb.append(" @ ");
3626 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3627 sb.append(" mspp");
3628 pw.println(sb.toString());
3629 }
3630
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003631 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003632 pw.print(prefix); pw.print(" Wi-Fi network: ");
3633 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003634 pw.print(formatBytesLocked(wifiTxBytes));
3635 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3636 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003637 }
3638
Dianne Hackborn62793e42015-03-09 11:15:41 -07003639 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003640 || uidWifiRunningTime != 0) {
3641 sb.setLength(0);
3642 sb.append(prefix); sb.append(" Wifi Running: ");
3643 formatTimeMs(sb, uidWifiRunningTime / 1000);
3644 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3645 whichBatteryRealtime)); sb.append(")\n");
3646 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3647 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3648 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3649 whichBatteryRealtime)); sb.append(")\n");
3650 sb.append(prefix); sb.append(" Wifi Scan: ");
3651 formatTimeMs(sb, wifiScanTime / 1000);
3652 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07003653 whichBatteryRealtime)); sb.append(") ");
3654 sb.append(wifiScanCount);
3655 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003656 pw.println(sb.toString());
3657 }
3658
Dianne Hackborn617f8772009-03-31 15:04:46 -07003659 if (u.hasUserActivity()) {
3660 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07003661 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003662 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003663 if (val != 0) {
3664 if (!hasData) {
3665 sb.setLength(0);
3666 sb.append(" User activity: ");
3667 hasData = true;
3668 } else {
3669 sb.append(", ");
3670 }
3671 sb.append(val);
3672 sb.append(" ");
3673 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3674 }
3675 }
3676 if (hasData) {
3677 pw.println(sb.toString());
3678 }
3679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003681 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3682 = u.getWakelockStats();
3683 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
3684 int countWakelock = 0;
3685 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3686 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3687 String linePrefix = ": ";
3688 sb.setLength(0);
3689 sb.append(prefix);
3690 sb.append(" Wake lock ");
3691 sb.append(wakelocks.keyAt(iw));
3692 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
3693 "full", which, linePrefix);
3694 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
3695 "partial", which, linePrefix);
3696 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
3697 "window", which, linePrefix);
3698 if (true || !linePrefix.equals(": ")) {
3699 sb.append(" realtime");
3700 // Only print out wake locks that were held
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003701 pw.println(sb.toString());
3702 uidActivity = true;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003703 countWakelock++;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003704 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003705 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
3706 rawRealtime, which);
3707 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
3708 rawRealtime, which);
3709 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
3710 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003711 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003712 if (countWakelock > 1) {
3713 if (totalFullWakelock != 0 || totalPartialWakelock != 0
3714 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003715 sb.setLength(0);
3716 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003717 sb.append(" TOTAL wake: ");
3718 boolean needComma = false;
3719 if (totalFullWakelock != 0) {
3720 needComma = true;
3721 formatTimeMs(sb, totalFullWakelock);
3722 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003723 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003724 if (totalPartialWakelock != 0) {
3725 if (needComma) {
3726 sb.append(", ");
3727 }
3728 needComma = true;
3729 formatTimeMs(sb, totalPartialWakelock);
3730 sb.append("partial");
3731 }
3732 if (totalWindowWakelock != 0) {
3733 if (needComma) {
3734 sb.append(", ");
3735 }
3736 needComma = true;
3737 formatTimeMs(sb, totalWindowWakelock);
3738 sb.append("window");
3739 }
3740 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003741 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003742 }
3743 }
3744
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003745 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3746 for (int isy=syncs.size()-1; isy>=0; isy--) {
3747 final Timer timer = syncs.valueAt(isy);
3748 // Convert from microseconds to milliseconds with rounding
3749 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3750 final int count = timer.getCountLocked(which);
3751 sb.setLength(0);
3752 sb.append(prefix);
3753 sb.append(" Sync ");
3754 sb.append(syncs.keyAt(isy));
3755 sb.append(": ");
3756 if (totalTime != 0) {
3757 formatTimeMs(sb, totalTime);
3758 sb.append("realtime (");
3759 sb.append(count);
3760 sb.append(" times)");
3761 } else {
3762 sb.append("(not used)");
3763 }
3764 pw.println(sb.toString());
3765 uidActivity = true;
3766 }
3767
3768 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3769 for (int ij=jobs.size()-1; ij>=0; ij--) {
3770 final Timer timer = jobs.valueAt(ij);
3771 // Convert from microseconds to milliseconds with rounding
3772 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3773 final int count = timer.getCountLocked(which);
3774 sb.setLength(0);
3775 sb.append(prefix);
3776 sb.append(" Job ");
3777 sb.append(jobs.keyAt(ij));
3778 sb.append(": ");
3779 if (totalTime != 0) {
3780 formatTimeMs(sb, totalTime);
3781 sb.append("realtime (");
3782 sb.append(count);
3783 sb.append(" times)");
3784 } else {
3785 sb.append("(not used)");
3786 }
3787 pw.println(sb.toString());
3788 uidActivity = true;
3789 }
3790
3791 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3792 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003793 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003794 final Uid.Sensor se = sensors.valueAt(ise);
3795 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003796 sb.setLength(0);
3797 sb.append(prefix);
3798 sb.append(" Sensor ");
3799 int handle = se.getHandle();
3800 if (handle == Uid.Sensor.GPS) {
3801 sb.append("GPS");
3802 } else {
3803 sb.append(handle);
3804 }
3805 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003807 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003808 if (timer != null) {
3809 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003810 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07003811 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003812 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003813 //timer.logState();
3814 if (totalTime != 0) {
3815 formatTimeMs(sb, totalTime);
3816 sb.append("realtime (");
3817 sb.append(count);
3818 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 } else {
3820 sb.append("(not used)");
3821 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003822 } else {
3823 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003825
3826 pw.println(sb.toString());
3827 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 }
3829
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003830 final Timer vibTimer = u.getVibratorOnTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003831 if (vibTimer != null) {
3832 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003833 final long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003834 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003835 final int count = vibTimer.getCountLocked(which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003836 //timer.logState();
3837 if (totalTime != 0) {
3838 sb.setLength(0);
3839 sb.append(prefix);
3840 sb.append(" Vibrator: ");
3841 formatTimeMs(sb, totalTime);
3842 sb.append("realtime (");
3843 sb.append(count);
3844 sb.append(" times)");
3845 pw.println(sb.toString());
3846 uidActivity = true;
3847 }
3848 }
3849
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003850 final Timer fgTimer = u.getForegroundActivityTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003851 if (fgTimer != null) {
3852 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003853 final long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3854 / 1000;
3855 final int count = fgTimer.getCountLocked(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003856 if (totalTime != 0) {
3857 sb.setLength(0);
3858 sb.append(prefix);
3859 sb.append(" Foreground activities: ");
3860 formatTimeMs(sb, totalTime);
3861 sb.append("realtime (");
3862 sb.append(count);
3863 sb.append(" times)");
3864 pw.println(sb.toString());
3865 uidActivity = true;
3866 }
3867 }
3868
Dianne Hackborn61659e52014-07-09 16:13:01 -07003869 long totalStateTime = 0;
3870 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
3871 long time = u.getProcessStateTime(ips, rawRealtime, which);
3872 if (time > 0) {
3873 totalStateTime += time;
3874 sb.setLength(0);
3875 sb.append(prefix);
3876 sb.append(" ");
3877 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
3878 sb.append(" for: ");
3879 formatTimeMs(sb, (totalStateTime + 500) / 1000);
3880 pw.println(sb.toString());
3881 uidActivity = true;
3882 }
3883 }
3884
Adam Lesinski06af1fa2015-05-05 17:35:35 -07003885 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3886 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3887 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
3888 sb.setLength(0);
3889 sb.append(prefix);
3890 sb.append(" Total cpu time: ");
3891 formatTimeMs(sb, (userCpuTimeUs + systemCpuTimeUs) / 1000);
3892 pw.println(sb.toString());
3893 }
3894
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003895 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3896 = u.getProcessStats();
3897 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3898 final Uid.Proc ps = processStats.valueAt(ipr);
3899 long userTime;
3900 long systemTime;
3901 long foregroundTime;
3902 int starts;
3903 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003905 userTime = ps.getUserTime(which);
3906 systemTime = ps.getSystemTime(which);
3907 foregroundTime = ps.getForegroundTime(which);
3908 starts = ps.getStarts(which);
3909 final int numCrashes = ps.getNumCrashes(which);
3910 final int numAnrs = ps.getNumAnrs(which);
3911 numExcessive = which == STATS_SINCE_CHARGED
3912 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003914 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
3915 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
3916 sb.setLength(0);
3917 sb.append(prefix); sb.append(" Proc ");
3918 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
3919 sb.append(prefix); sb.append(" CPU: ");
3920 formatTimeMs(sb, userTime); sb.append("usr + ");
3921 formatTimeMs(sb, systemTime); sb.append("krn ; ");
3922 formatTimeMs(sb, foregroundTime); sb.append("fg");
3923 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
3924 sb.append("\n"); sb.append(prefix); sb.append(" ");
3925 boolean hasOne = false;
3926 if (starts != 0) {
3927 hasOne = true;
3928 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003929 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003930 if (numCrashes != 0) {
3931 if (hasOne) {
3932 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003933 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003934 hasOne = true;
3935 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003936 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003937 if (numAnrs != 0) {
3938 if (hasOne) {
3939 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003941 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
3943 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003944 pw.println(sb.toString());
3945 for (int e=0; e<numExcessive; e++) {
3946 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
3947 if (ew != null) {
3948 pw.print(prefix); pw.print(" * Killed for ");
3949 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
3950 pw.print("wake lock");
3951 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
3952 pw.print("cpu");
3953 } else {
3954 pw.print("unknown");
3955 }
3956 pw.print(" use: ");
3957 TimeUtils.formatDuration(ew.usedTime, pw);
3958 pw.print(" over ");
3959 TimeUtils.formatDuration(ew.overTime, pw);
3960 if (ew.overTime != 0) {
3961 pw.print(" (");
3962 pw.print((ew.usedTime*100)/ew.overTime);
3963 pw.println("%)");
3964 }
3965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967 uidActivity = true;
3968 }
3969 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003970
3971 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3972 = u.getPackageStats();
3973 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3974 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
3975 pw.println(":");
3976 boolean apkActivity = false;
3977 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3978 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3979 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3980 pw.print(prefix); pw.print(" Wakeup alarm ");
3981 pw.print(alarms.keyAt(iwa)); pw.print(": ");
3982 pw.print(alarms.valueAt(iwa).getCountLocked(which));
3983 pw.println(" times");
3984 apkActivity = true;
3985 }
3986 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3987 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3988 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3989 final long startTime = ss.getStartTime(batteryUptime, which);
3990 final int starts = ss.getStarts(which);
3991 final int launches = ss.getLaunches(which);
3992 if (startTime != 0 || starts != 0 || launches != 0) {
3993 sb.setLength(0);
3994 sb.append(prefix); sb.append(" Service ");
3995 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
3996 sb.append(prefix); sb.append(" Created for: ");
3997 formatTimeMs(sb, startTime / 1000);
3998 sb.append("uptime\n");
3999 sb.append(prefix); sb.append(" Starts: ");
4000 sb.append(starts);
4001 sb.append(", launches: "); sb.append(launches);
4002 pw.println(sb.toString());
4003 apkActivity = true;
4004 }
4005 }
4006 if (!apkActivity) {
4007 pw.print(prefix); pw.println(" (nothing executed)");
4008 }
4009 uidActivity = true;
4010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004012 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 }
4015 }
4016
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004017 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004018 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004019 int diff = oldval ^ newval;
4020 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004021 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004022 for (int i=0; i<descriptions.length; i++) {
4023 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004024 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004025 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004026 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004027 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004028 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004029 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4030 didWake = true;
4031 pw.print("=");
4032 if (longNames) {
4033 UserHandle.formatUid(pw, wakelockTag.uid);
4034 pw.print(":\"");
4035 pw.print(wakelockTag.string);
4036 pw.print("\"");
4037 } else {
4038 pw.print(wakelockTag.poolIdx);
4039 }
4040 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004041 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004042 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004043 pw.print("=");
4044 int val = (newval&bd.mask)>>bd.shift;
4045 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004046 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004047 } else {
4048 pw.print(val);
4049 }
4050 }
4051 }
4052 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004053 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004054 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004055 if (longNames) {
4056 UserHandle.formatUid(pw, wakelockTag.uid);
4057 pw.print(":\"");
4058 pw.print(wakelockTag.string);
4059 pw.print("\"");
4060 } else {
4061 pw.print(wakelockTag.poolIdx);
4062 }
4063 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004064 }
4065
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004066 public void prepareForDumpLocked() {
4067 }
4068
4069 public static class HistoryPrinter {
4070 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004071 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004072 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004073 int oldStatus = -1;
4074 int oldHealth = -1;
4075 int oldPlug = -1;
4076 int oldTemp = -1;
4077 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004078 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004079
Dianne Hackborn3251b902014-06-20 14:40:53 -07004080 void reset() {
4081 oldState = oldState2 = 0;
4082 oldLevel = -1;
4083 oldStatus = -1;
4084 oldHealth = -1;
4085 oldPlug = -1;
4086 oldTemp = -1;
4087 oldVolt = -1;
4088 }
4089
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004090 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004091 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004092 if (!checkin) {
4093 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004094 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004095 pw.print(" (");
4096 pw.print(rec.numReadInts);
4097 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004098 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004099 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4100 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004101 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004102 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004103 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004104 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004105 }
4106 lastTime = rec.time;
4107 }
4108 if (rec.cmd == HistoryItem.CMD_START) {
4109 if (checkin) {
4110 pw.print(":");
4111 }
4112 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004113 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004114 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4115 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004116 if (checkin) {
4117 pw.print(":");
4118 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004119 if (rec.cmd == HistoryItem.CMD_RESET) {
4120 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004121 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004122 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004123 pw.print("TIME:");
4124 if (checkin) {
4125 pw.println(rec.currentTime);
4126 } else {
4127 pw.print(" ");
4128 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4129 rec.currentTime).toString());
4130 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004131 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4132 if (checkin) {
4133 pw.print(":");
4134 }
4135 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004136 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4137 if (checkin) {
4138 pw.print(":");
4139 }
4140 pw.println("*OVERFLOW*");
4141 } else {
4142 if (!checkin) {
4143 if (rec.batteryLevel < 10) pw.print("00");
4144 else if (rec.batteryLevel < 100) pw.print("0");
4145 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004146 if (verbose) {
4147 pw.print(" ");
4148 if (rec.states < 0) ;
4149 else if (rec.states < 0x10) pw.print("0000000");
4150 else if (rec.states < 0x100) pw.print("000000");
4151 else if (rec.states < 0x1000) pw.print("00000");
4152 else if (rec.states < 0x10000) pw.print("0000");
4153 else if (rec.states < 0x100000) pw.print("000");
4154 else if (rec.states < 0x1000000) pw.print("00");
4155 else if (rec.states < 0x10000000) pw.print("0");
4156 pw.print(Integer.toHexString(rec.states));
4157 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004158 } else {
4159 if (oldLevel != rec.batteryLevel) {
4160 oldLevel = rec.batteryLevel;
4161 pw.print(",Bl="); pw.print(rec.batteryLevel);
4162 }
4163 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004164 if (oldStatus != rec.batteryStatus) {
4165 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004166 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004167 switch (oldStatus) {
4168 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004169 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004170 break;
4171 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004172 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004173 break;
4174 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004175 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004176 break;
4177 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004178 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004179 break;
4180 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004181 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004182 break;
4183 default:
4184 pw.print(oldStatus);
4185 break;
4186 }
4187 }
4188 if (oldHealth != rec.batteryHealth) {
4189 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004190 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004191 switch (oldHealth) {
4192 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004193 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004194 break;
4195 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004196 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004197 break;
4198 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004199 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004200 break;
4201 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004202 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004203 break;
4204 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004205 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004206 break;
4207 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004208 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004209 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004210 case BatteryManager.BATTERY_HEALTH_COLD:
4211 pw.print(checkin ? "c" : "cold");
4212 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004213 default:
4214 pw.print(oldHealth);
4215 break;
4216 }
4217 }
4218 if (oldPlug != rec.batteryPlugType) {
4219 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004220 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004221 switch (oldPlug) {
4222 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004223 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004224 break;
4225 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004226 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004227 break;
4228 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004229 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004230 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004231 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004232 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004233 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004234 default:
4235 pw.print(oldPlug);
4236 break;
4237 }
4238 }
4239 if (oldTemp != rec.batteryTemperature) {
4240 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004241 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004242 pw.print(oldTemp);
4243 }
4244 if (oldVolt != rec.batteryVoltage) {
4245 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004246 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004247 pw.print(oldVolt);
4248 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004249 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004250 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004251 printBitDescriptions(pw, oldState2, rec.states2, null,
4252 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004253 if (rec.wakeReasonTag != null) {
4254 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004255 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004256 pw.print(rec.wakeReasonTag.poolIdx);
4257 } else {
4258 pw.print(" wake_reason=");
4259 pw.print(rec.wakeReasonTag.uid);
4260 pw.print(":\"");
4261 pw.print(rec.wakeReasonTag.string);
4262 pw.print("\"");
4263 }
4264 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004265 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004266 pw.print(checkin ? "," : " ");
4267 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4268 pw.print("+");
4269 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4270 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004271 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004272 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4273 : HISTORY_EVENT_NAMES;
4274 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4275 | HistoryItem.EVENT_FLAG_FINISH);
4276 if (idx >= 0 && idx < eventNames.length) {
4277 pw.print(eventNames[idx]);
4278 } else {
4279 pw.print(checkin ? "Ev" : "event");
4280 pw.print(idx);
4281 }
4282 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004283 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004284 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004285 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004286 UserHandle.formatUid(pw, rec.eventTag.uid);
4287 pw.print(":\"");
4288 pw.print(rec.eventTag.string);
4289 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004290 }
4291 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004292 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004293 if (rec.stepDetails != null) {
4294 if (!checkin) {
4295 pw.print(" Details: cpu=");
4296 pw.print(rec.stepDetails.userTime);
4297 pw.print("u+");
4298 pw.print(rec.stepDetails.systemTime);
4299 pw.print("s");
4300 if (rec.stepDetails.appCpuUid1 >= 0) {
4301 pw.print(" (");
4302 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4303 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4304 if (rec.stepDetails.appCpuUid2 >= 0) {
4305 pw.print(", ");
4306 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4307 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4308 }
4309 if (rec.stepDetails.appCpuUid3 >= 0) {
4310 pw.print(", ");
4311 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4312 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4313 }
4314 pw.print(')');
4315 }
4316 pw.println();
4317 pw.print(" /proc/stat=");
4318 pw.print(rec.stepDetails.statUserTime);
4319 pw.print(" usr, ");
4320 pw.print(rec.stepDetails.statSystemTime);
4321 pw.print(" sys, ");
4322 pw.print(rec.stepDetails.statIOWaitTime);
4323 pw.print(" io, ");
4324 pw.print(rec.stepDetails.statIrqTime);
4325 pw.print(" irq, ");
4326 pw.print(rec.stepDetails.statSoftIrqTime);
4327 pw.print(" sirq, ");
4328 pw.print(rec.stepDetails.statIdlTime);
4329 pw.print(" idle");
4330 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4331 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4332 + rec.stepDetails.statSoftIrqTime;
4333 int total = totalRun + rec.stepDetails.statIdlTime;
4334 if (total > 0) {
4335 pw.print(" (");
4336 float perc = ((float)totalRun) / ((float)total) * 100;
4337 pw.print(String.format("%.1f%%", perc));
4338 pw.print(" of ");
4339 StringBuilder sb = new StringBuilder(64);
4340 formatTimeMsNoSpace(sb, total*10);
4341 pw.print(sb);
4342 pw.print(")");
4343 }
4344 pw.println();
4345 } else {
4346 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4347 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4348 pw.print(rec.stepDetails.userTime);
4349 pw.print(":");
4350 pw.print(rec.stepDetails.systemTime);
4351 if (rec.stepDetails.appCpuUid1 >= 0) {
4352 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4353 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4354 if (rec.stepDetails.appCpuUid2 >= 0) {
4355 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4356 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4357 }
4358 if (rec.stepDetails.appCpuUid3 >= 0) {
4359 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4360 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4361 }
4362 }
4363 pw.println();
4364 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4365 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4366 pw.print(rec.stepDetails.statUserTime);
4367 pw.print(',');
4368 pw.print(rec.stepDetails.statSystemTime);
4369 pw.print(',');
4370 pw.print(rec.stepDetails.statIOWaitTime);
4371 pw.print(',');
4372 pw.print(rec.stepDetails.statIrqTime);
4373 pw.print(',');
4374 pw.print(rec.stepDetails.statSoftIrqTime);
4375 pw.print(',');
4376 pw.print(rec.stepDetails.statIdlTime);
4377 pw.println();
4378 }
4379 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004380 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004381 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004382 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004383 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004384
4385 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4386 UserHandle.formatUid(pw, uid);
4387 pw.print("=");
4388 pw.print(utime);
4389 pw.print("u+");
4390 pw.print(stime);
4391 pw.print("s");
4392 }
4393
4394 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4395 pw.print('/');
4396 pw.print(uid);
4397 pw.print(":");
4398 pw.print(utime);
4399 pw.print(":");
4400 pw.print(stime);
4401 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004402 }
4403
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004404 private void printSizeValue(PrintWriter pw, long size) {
4405 float result = size;
4406 String suffix = "";
4407 if (result >= 10*1024) {
4408 suffix = "KB";
4409 result = result / 1024;
4410 }
4411 if (result >= 10*1024) {
4412 suffix = "MB";
4413 result = result / 1024;
4414 }
4415 if (result >= 10*1024) {
4416 suffix = "GB";
4417 result = result / 1024;
4418 }
4419 if (result >= 10*1024) {
4420 suffix = "TB";
4421 result = result / 1024;
4422 }
4423 if (result >= 10*1024) {
4424 suffix = "PB";
4425 result = result / 1024;
4426 }
4427 pw.print((int)result);
4428 pw.print(suffix);
4429 }
4430
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004431 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4432 String label3, long estimatedTime) {
4433 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004434 return false;
4435 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004436 pw.print(label1);
4437 pw.print(label2);
4438 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004439 StringBuilder sb = new StringBuilder(64);
4440 formatTimeMs(sb, estimatedTime);
4441 pw.print(sb);
4442 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004443 return true;
4444 }
4445
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004446 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4447 LevelStepTracker steps, boolean checkin) {
4448 if (steps == null) {
4449 return false;
4450 }
4451 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004452 if (count <= 0) {
4453 return false;
4454 }
4455 if (!checkin) {
4456 pw.println(header);
4457 }
Kweku Adams030980a2015-04-01 16:07:48 -07004458 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004459 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004460 long duration = steps.getDurationAt(i);
4461 int level = steps.getLevelAt(i);
4462 long initMode = steps.getInitModeAt(i);
4463 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004464 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004465 lineArgs[0] = Long.toString(duration);
4466 lineArgs[1] = Integer.toString(level);
4467 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4468 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4469 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4470 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4471 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4472 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004473 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004474 }
4475 } else {
4476 lineArgs[2] = "";
4477 }
4478 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4479 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4480 } else {
4481 lineArgs[3] = "";
4482 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004483 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004484 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004485 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004486 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004487 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004488 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4489 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004490 pw.print(prefix);
4491 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004492 TimeUtils.formatDuration(duration, pw);
4493 pw.print(" to "); pw.print(level);
4494 boolean haveModes = false;
4495 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4496 pw.print(" (");
4497 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4498 case Display.STATE_OFF: pw.print("screen-off"); break;
4499 case Display.STATE_ON: pw.print("screen-on"); break;
4500 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4501 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004502 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004503 }
4504 haveModes = true;
4505 }
4506 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4507 pw.print(haveModes ? ", " : " (");
4508 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4509 ? "power-save-on" : "power-save-off");
4510 haveModes = true;
4511 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004512 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4513 pw.print(haveModes ? ", " : " (");
4514 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4515 ? "device-idle-on" : "device-idle-off");
4516 haveModes = true;
4517 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004518 if (haveModes) {
4519 pw.print(")");
4520 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004521 pw.println();
4522 }
4523 }
4524 return true;
4525 }
4526
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004527 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004528 public static final int DUMP_DAILY_ONLY = 1<<2;
4529 public static final int DUMP_HISTORY_ONLY = 1<<3;
4530 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4531 public static final int DUMP_VERBOSE = 1<<5;
4532 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004533
Dianne Hackborn37de0982014-05-09 09:32:18 -07004534 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4535 final HistoryPrinter hprinter = new HistoryPrinter();
4536 final HistoryItem rec = new HistoryItem();
4537 long lastTime = -1;
4538 long baseTime = -1;
4539 boolean printed = false;
4540 HistoryEventTracker tracker = null;
4541 while (getNextHistoryLocked(rec)) {
4542 lastTime = rec.time;
4543 if (baseTime < 0) {
4544 baseTime = lastTime;
4545 }
4546 if (rec.time >= histStart) {
4547 if (histStart >= 0 && !printed) {
4548 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004549 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004550 || rec.cmd == HistoryItem.CMD_START
4551 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004552 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004553 hprinter.printNextItem(pw, rec, baseTime, checkin,
4554 (flags&DUMP_VERBOSE) != 0);
4555 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004556 } else if (rec.currentTime != 0) {
4557 printed = true;
4558 byte cmd = rec.cmd;
4559 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004560 hprinter.printNextItem(pw, rec, baseTime, checkin,
4561 (flags&DUMP_VERBOSE) != 0);
4562 rec.cmd = cmd;
4563 }
4564 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004565 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4566 hprinter.printNextItem(pw, rec, baseTime, checkin,
4567 (flags&DUMP_VERBOSE) != 0);
4568 rec.cmd = HistoryItem.CMD_UPDATE;
4569 }
4570 int oldEventCode = rec.eventCode;
4571 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004572 rec.eventTag = new HistoryTag();
4573 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4574 HashMap<String, SparseIntArray> active
4575 = tracker.getStateForEvent(i);
4576 if (active == null) {
4577 continue;
4578 }
4579 for (HashMap.Entry<String, SparseIntArray> ent
4580 : active.entrySet()) {
4581 SparseIntArray uids = ent.getValue();
4582 for (int j=0; j<uids.size(); j++) {
4583 rec.eventCode = i;
4584 rec.eventTag.string = ent.getKey();
4585 rec.eventTag.uid = uids.keyAt(j);
4586 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07004587 hprinter.printNextItem(pw, rec, baseTime, checkin,
4588 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004589 rec.wakeReasonTag = null;
4590 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004591 }
4592 }
4593 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004594 rec.eventCode = oldEventCode;
4595 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004596 tracker = null;
4597 }
4598 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004599 hprinter.printNextItem(pw, rec, baseTime, checkin,
4600 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004601 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
4602 // This is an attempt to aggregate the previous state and generate
4603 // fake events to reflect that state at the point where we start
4604 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07004605 if (tracker == null) {
4606 tracker = new HistoryEventTracker();
4607 }
4608 tracker.updateState(rec.eventCode, rec.eventTag.string,
4609 rec.eventTag.uid, rec.eventTag.poolIdx);
4610 }
4611 }
4612 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004613 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004614 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
4615 }
4616 }
4617
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004618 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
4619 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
4620 if (steps == null) {
4621 return;
4622 }
4623 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
4624 if (timeRemaining >= 0) {
4625 pw.print(prefix); pw.print(label); pw.print(" total time: ");
4626 tmpSb.setLength(0);
4627 formatTimeMs(tmpSb, timeRemaining);
4628 pw.print(tmpSb);
4629 pw.print(" (from "); pw.print(tmpOutInt[0]);
4630 pw.println(" steps)");
4631 }
4632 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4633 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4634 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
4635 if (estimatedTime > 0) {
4636 pw.print(prefix); pw.print(label); pw.print(" ");
4637 pw.print(STEP_LEVEL_MODE_LABELS[i]);
4638 pw.print(" time: ");
4639 tmpSb.setLength(0);
4640 formatTimeMs(tmpSb, estimatedTime);
4641 pw.print(tmpSb);
4642 pw.print(" (from "); pw.print(tmpOutInt[0]);
4643 pw.println(" steps)");
4644 }
4645 }
4646 }
4647
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004648 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
4649 ArrayList<PackageChange> changes) {
4650 if (changes == null) {
4651 return;
4652 }
4653 pw.print(prefix); pw.println("Package changes:");
4654 for (int i=0; i<changes.size(); i++) {
4655 PackageChange pc = changes.get(i);
4656 if (pc.mUpdate) {
4657 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
4658 pw.print(" vers="); pw.println(pc.mVersionCode);
4659 } else {
4660 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
4661 }
4662 }
4663 }
4664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 /**
4666 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
4667 *
4668 * @param pw a Printer to receive the dump output.
4669 */
4670 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004671 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004672 prepareForDumpLocked();
4673
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004674 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004675 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004676
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004677 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004678 final long historyTotalSize = getHistoryTotalSize();
4679 final long historyUsedSize = getHistoryUsedSize();
4680 if (startIteratingHistoryLocked()) {
4681 try {
4682 pw.print("Battery History (");
4683 pw.print((100*historyUsedSize)/historyTotalSize);
4684 pw.print("% used, ");
4685 printSizeValue(pw, historyUsedSize);
4686 pw.print(" used of ");
4687 printSizeValue(pw, historyTotalSize);
4688 pw.print(", ");
4689 pw.print(getHistoryStringPoolSize());
4690 pw.print(" strings using ");
4691 printSizeValue(pw, getHistoryStringPoolBytes());
4692 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07004693 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004694 pw.println();
4695 } finally {
4696 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004697 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004698 }
4699
4700 if (startIteratingOldHistoryLocked()) {
4701 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004702 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004703 pw.println("Old battery History:");
4704 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004705 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004706 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004707 if (baseTime < 0) {
4708 baseTime = rec.time;
4709 }
4710 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004711 }
4712 pw.println();
4713 } finally {
4714 finishIteratingOldHistoryLocked();
4715 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004716 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004717 }
4718
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004719 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004720 return;
4721 }
4722
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004723 if (!filtering) {
4724 SparseArray<? extends Uid> uidStats = getUidStats();
4725 final int NU = uidStats.size();
4726 boolean didPid = false;
4727 long nowRealtime = SystemClock.elapsedRealtime();
4728 for (int i=0; i<NU; i++) {
4729 Uid uid = uidStats.valueAt(i);
4730 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
4731 if (pids != null) {
4732 for (int j=0; j<pids.size(); j++) {
4733 Uid.Pid pid = pids.valueAt(j);
4734 if (!didPid) {
4735 pw.println("Per-PID Stats:");
4736 didPid = true;
4737 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004738 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
4739 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004740 pw.print(" PID "); pw.print(pids.keyAt(j));
4741 pw.print(" wake time: ");
4742 TimeUtils.formatDuration(time, pw);
4743 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07004744 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07004745 }
4746 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004747 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004748 pw.println();
4749 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004750 }
4751
4752 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004753 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
4754 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004755 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4756 if (timeRemaining >= 0) {
4757 pw.print(" Estimated discharge time remaining: ");
4758 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4759 pw.println();
4760 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004761 final LevelStepTracker steps = getDischargeLevelStepTracker();
4762 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4763 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
4764 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4765 STEP_LEVEL_MODE_VALUES[i], null));
4766 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004767 pw.println();
4768 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004769 if (dumpDurationSteps(pw, " ", "Charge step durations:",
4770 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004771 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4772 if (timeRemaining >= 0) {
4773 pw.print(" Estimated charge time remaining: ");
4774 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4775 pw.println();
4776 }
4777 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004778 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004779 }
4780 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
4781 pw.println("Daily stats:");
4782 pw.print(" Current start time: ");
4783 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4784 getCurrentDailyStartTime()).toString());
4785 pw.print(" Next min deadline: ");
4786 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4787 getNextMinDailyDeadline()).toString());
4788 pw.print(" Next max deadline: ");
4789 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4790 getNextMaxDailyDeadline()).toString());
4791 StringBuilder sb = new StringBuilder(64);
4792 int[] outInt = new int[1];
4793 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
4794 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004795 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
4796 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004797 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004798 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
4799 dsteps, false)) {
4800 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
4801 sb, outInt);
4802 }
4803 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
4804 csteps, false)) {
4805 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
4806 sb, outInt);
4807 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004808 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004809 } else {
4810 pw.println(" Current daily steps:");
4811 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
4812 sb, outInt);
4813 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
4814 sb, outInt);
4815 }
4816 }
4817 DailyItem dit;
4818 int curIndex = 0;
4819 while ((dit=getDailyItemLocked(curIndex)) != null) {
4820 curIndex++;
4821 if ((flags&DUMP_DAILY_ONLY) != 0) {
4822 pw.println();
4823 }
4824 pw.print(" Daily from ");
4825 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
4826 pw.print(" to ");
4827 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
4828 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004829 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004830 if (dumpDurationSteps(pw, " ",
4831 " Discharge step durations:", dit.mDischargeSteps, false)) {
4832 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
4833 sb, outInt);
4834 }
4835 if (dumpDurationSteps(pw, " ",
4836 " Charge step durations:", dit.mChargeSteps, false)) {
4837 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
4838 sb, outInt);
4839 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004840 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004841 } else {
4842 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
4843 sb, outInt);
4844 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
4845 sb, outInt);
4846 }
4847 }
4848 pw.println();
4849 }
4850 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07004851 pw.println("Statistics since last charge:");
4852 pw.println(" System starts: " + getStartCount()
4853 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07004854 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
4855 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004856 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07004857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 }
4859
4860 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004861 public void dumpCheckinLocked(Context context, PrintWriter pw,
4862 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004863 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004864
4865 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004866 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
4867 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004868
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004869 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
4870
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004871 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004872 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004873
4874 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07004875 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004876 try {
4877 for (int i=0; i<getHistoryStringPoolSize(); i++) {
4878 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4879 pw.print(HISTORY_STRING_POOL); pw.print(',');
4880 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004881 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004882 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004883 pw.print(",\"");
4884 String str = getHistoryTagPoolString(i);
4885 str = str.replace("\\", "\\\\");
4886 str = str.replace("\"", "\\\"");
4887 pw.print(str);
4888 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004889 pw.println();
4890 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004891 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004892 } finally {
4893 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08004894 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004895 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07004896 }
4897
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004898 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004899 return;
4900 }
4901
Dianne Hackborne4a59512010-12-07 11:08:07 -08004902 if (apps != null) {
4903 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
4904 for (int i=0; i<apps.size(); i++) {
4905 ApplicationInfo ai = apps.get(i);
4906 ArrayList<String> pkgs = uids.get(ai.uid);
4907 if (pkgs == null) {
4908 pkgs = new ArrayList<String>();
4909 uids.put(ai.uid, pkgs);
4910 }
4911 pkgs.add(ai.packageName);
4912 }
4913 SparseArray<? extends Uid> uidStats = getUidStats();
4914 final int NU = uidStats.size();
4915 String[] lineArgs = new String[2];
4916 for (int i=0; i<NU; i++) {
4917 int uid = uidStats.keyAt(i);
4918 ArrayList<String> pkgs = uids.get(uid);
4919 if (pkgs != null) {
4920 for (int j=0; j<pkgs.size(); j++) {
4921 lineArgs[0] = Integer.toString(uid);
4922 lineArgs[1] = pkgs.get(j);
4923 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
4924 (Object[])lineArgs);
4925 }
4926 }
4927 }
4928 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004929 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004930 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004931 String[] lineArgs = new String[1];
4932 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4933 if (timeRemaining >= 0) {
4934 lineArgs[0] = Long.toString(timeRemaining);
4935 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
4936 (Object[])lineArgs);
4937 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004938 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004939 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4940 if (timeRemaining >= 0) {
4941 lineArgs[0] = Long.toString(timeRemaining);
4942 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
4943 (Object[])lineArgs);
4944 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07004945 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
4946 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949}