blob: 918050644ce411423d876fedfaab3bafd5c66891 [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;
Adam Lesinski9425fe22015-06-19 12:02:13 -070068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
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 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700135 * A constant indicating a flashlight turn on timer
136 */
137 public static final int FLASHLIGHT_TURNED_ON = 16;
138
139 /**
140 * A constant indicating a camera turn on timer
141 */
142 public static final int CAMERA_TURNED_ON = 17;
143
144 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700145 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700146 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700147 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700148
149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * Include all of the data in the stats, including previously saved data.
151 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700152 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Include only the current run in the stats.
156 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700157 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 /**
160 * Include only the run since the last time the device was unplugged in the stats.
161 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700162 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700163
164 // NOTE: Update this list if you add/change any stats above.
165 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700166 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700167 private static final String[] STAT_NAMES = { "l", "c", "u" };
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700170 * Current version of checkin data format.
171 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800172 static final String CHECKIN_VERSION = "17";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700173
174 /**
175 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700177 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700178
Evan Millar22ac0432009-03-31 11:33:18 -0700179 private static final long BYTES_PER_KB = 1024;
180 private static final long BYTES_PER_MB = 1048576; // 1024^2
181 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700183 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800184 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700186 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700187 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700188 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800189 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700190 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700191 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700192 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700193 private static final String SYNC_DATA = "sy";
194 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700195 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700196 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700197 private static final String NETWORK_DATA = "nt";
198 private static final String USER_ACTIVITY_DATA = "ua";
199 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800200 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700201 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700202 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700203 private static final String WIFI_DATA = "wfl";
Adam Lesinskie283d332015-04-16 12:29:25 -0700204 private static final String GLOBAL_BLUETOOTH_DATA = "gble";
Evan Millare84de8d2009-04-02 22:16:12 -0700205 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800206 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800207 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700208 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700209 private static final String SCREEN_BRIGHTNESS_DATA = "br";
210 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700211 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700212 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
213 private static final String DATA_CONNECTION_TIME_DATA = "dct";
214 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800215 private static final String WIFI_STATE_TIME_DATA = "wst";
216 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700217 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
218 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
219 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
220 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800221 private static final String POWER_USE_SUMMARY_DATA = "pws";
222 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700223 private static final String DISCHARGE_STEP_DATA = "dsd";
224 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700225 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
226 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700227 private static final String FLASHLIGHT_DATA = "fla";
228 private static final String CAMERA_DATA = "cam";
229 private static final String VIDEO_DATA = "vid";
230 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700232 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 private final Formatter mFormatter = new Formatter(mFormatBuilder);
234
235 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700236 * State for keeping track of counting information.
237 */
238 public static abstract class Counter {
239
240 /**
241 * Returns the count associated with this Counter for the
242 * selected type of statistics.
243 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700244 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700245 */
Evan Millarc64edde2009-04-18 12:26:32 -0700246 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700247
248 /**
249 * Temporary for debugging.
250 */
251 public abstract void logState(Printer pw, String prefix);
252 }
253
254 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700255 * State for keeping track of long counting information.
256 */
257 public static abstract class LongCounter {
258
259 /**
260 * Returns the count associated with this Counter for the
261 * selected type of statistics.
262 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700263 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700264 */
265 public abstract long getCountLocked(int which);
266
267 /**
268 * Temporary for debugging.
269 */
270 public abstract void logState(Printer pw, String prefix);
271 }
272
273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * State for keeping track of timing information.
275 */
276 public static abstract class Timer {
277
278 /**
279 * Returns the count associated with this Timer for the
280 * selected type of statistics.
281 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700282 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
Evan Millarc64edde2009-04-18 12:26:32 -0700284 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
286 /**
287 * Returns the total time in microseconds associated with this Timer for the
288 * selected type of statistics.
289 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800290 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700291 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 * @return a time in microseconds
293 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800294 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700297 * Returns the total time in microseconds associated with this Timer since the
298 * 'mark' was last set.
299 *
300 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
301 * @return a time in microseconds
302 */
303 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
304
305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * Temporary for debugging.
307 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700308 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
310
311 /**
312 * The statistics associated with a particular uid.
313 */
314 public static abstract class Uid {
315
316 /**
317 * Returns a mapping containing wakelock statistics.
318 *
319 * @return a Map from Strings to Uid.Wakelock objects.
320 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700321 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
323 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700324 * Returns a mapping containing sync statistics.
325 *
326 * @return a Map from Strings to Timer objects.
327 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700328 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700329
330 /**
331 * Returns a mapping containing scheduled job statistics.
332 *
333 * @return a Map from Strings to Timer objects.
334 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700335 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700336
337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * The statistics associated with a particular wake lock.
339 */
340 public static abstract class Wakelock {
341 public abstract Timer getWakeTime(int type);
342 }
343
344 /**
345 * Returns a mapping containing sensor statistics.
346 *
347 * @return a Map from Integer sensor ids to Uid.Sensor objects.
348 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700349 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700352 * Returns a mapping containing active process data.
353 */
354 public abstract SparseArray<? extends Pid> getPidStats();
355
356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * Returns a mapping containing process statistics.
358 *
359 * @return a Map from Strings to Uid.Proc objects.
360 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700361 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
363 /**
364 * Returns a mapping containing package statistics.
365 *
366 * @return a Map from Strings to Uid.Pkg objects.
367 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700368 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700369
370 /**
371 * Returns the time in milliseconds that this app kept the WiFi controller in the
372 * specified state <code>type</code>.
373 * @param type one of {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, or
374 * {@link #CONTROLLER_TX_TIME}.
375 * @param which one of {@link #STATS_CURRENT}, {@link #STATS_SINCE_CHARGED}, or
376 * {@link #STATS_SINCE_UNPLUGGED}.
377 */
378 public abstract long getWifiControllerActivity(int type, int which);
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Adam Lesinski50e47602015-12-04 17:04:54 -0800381 * Returns the time in milliseconds that this app kept the Bluetooth controller in the
382 * specified state <code>type</code>.
383 * @param type one of {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, or
384 * {@link #CONTROLLER_TX_TIME}.
385 * @param which one of {@link #STATS_CURRENT}, {@link #STATS_SINCE_CHARGED}, or
386 * {@link #STATS_SINCE_UNPLUGGED}.
387 */
388 public abstract long getBluetoothControllerActivity(int type, int which);
389
390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * {@hide}
392 */
393 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700394
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800395 public abstract void noteWifiRunningLocked(long elapsedRealtime);
396 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
397 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
398 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
399 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
400 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
401 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
402 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
403 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
404 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800405 public abstract void noteActivityResumedLocked(long elapsedRealtime);
406 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800407 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
408 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
409 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700410 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800411 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700412 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800413 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700414 public abstract Timer getAudioTurnedOnTimer();
415 public abstract Timer getVideoTurnedOnTimer();
416 public abstract Timer getFlashlightTurnedOnTimer();
417 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700418 public abstract Timer getForegroundActivityTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700419
Dianne Hackborna8d10942015-11-19 17:55:19 -0800420 // Time this uid has any processes in the top state.
421 public static final int PROCESS_STATE_TOP = 0;
422 // Time this uid has any process with a started out bound foreground service.
423 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
424 // Time this uid has any process that is top while the device is sleeping.
425 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
426 // Time this uid has any process in an active foreground state.
427 public static final int PROCESS_STATE_FOREGROUND = 3;
428 // Time this uid has any process in an active background state.
429 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700430 // Time this uid has any processes running at all.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800431 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700432 // Total number of process states we track.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800433 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700434
435 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800436 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700437 };
438
439 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
440
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800441 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442
Robert Greenwalta029ea12013-09-25 16:38:12 -0700443 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
444
Dianne Hackborn617f8772009-03-31 15:04:46 -0700445 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700446 * Note that these must match the constants in android.os.PowerManager.
447 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
448 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700449 */
450 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700451 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700452 };
453
Jeff Browndf693de2012-07-27 12:03:38 -0700454 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700455
Dianne Hackborn617f8772009-03-31 15:04:46 -0700456 public abstract void noteUserActivityLocked(int type);
457 public abstract boolean hasUserActivity();
458 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700459
460 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800461 public abstract long getNetworkActivityBytes(int type, int which);
462 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800463 public abstract long getMobileRadioActiveTime(int which);
464 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700465
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700466 /**
467 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
468 */
469 public abstract long getUserCpuTimeUs(int which);
470
471 /**
472 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
473 */
474 public abstract long getSystemCpuTimeUs(int which);
475
476 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700477 * Get the total cpu power consumed (in milli-ampere-microseconds).
478 */
479 public abstract long getCpuPowerMaUs(int which);
480
481 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700482 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
483 * given CPU cluster.
484 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700485 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700486 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700487 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
488 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700489 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700490 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800493 /*
494 * FIXME: it's not correct to use this magic value because it
495 * could clash with a sensor handle (which are defined by
496 * the sensor HAL, and therefore out of our control
497 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 // Magic sensor number for the GPS.
499 public static final int GPS = -10000;
500
501 public abstract int getHandle();
502
503 public abstract Timer getSensorTime();
504 }
505
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700506 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800507 public int mWakeNesting;
508 public long mWakeSumMs;
509 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700510 }
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
513 * The statistics associated with a particular process.
514 */
515 public static abstract class Proc {
516
Dianne Hackborn287952c2010-09-22 22:34:31 -0700517 public static class ExcessivePower {
518 public static final int TYPE_WAKE = 1;
519 public static final int TYPE_CPU = 2;
520
521 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700522 public long overTime;
523 public long usedTime;
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800527 * Returns true if this process is still active in the battery stats.
528 */
529 public abstract boolean isActive();
530
531 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700532 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700534 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
536 public abstract long getUserTime(int which);
537
538 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700539 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700541 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 */
543 public abstract long getSystemTime(int which);
544
545 /**
546 * Returns the number of times the process has been started.
547 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700548 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
550 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700551
552 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800553 * Returns the number of times the process has crashed.
554 *
555 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
556 */
557 public abstract int getNumCrashes(int which);
558
559 /**
560 * Returns the number of times the process has ANRed.
561 *
562 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
563 */
564 public abstract int getNumAnrs(int which);
565
566 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700567 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700568 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700569 * @return foreground cpu time in microseconds
570 */
571 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700572
Dianne Hackborn287952c2010-09-22 22:34:31 -0700573 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700574
Dianne Hackborn287952c2010-09-22 22:34:31 -0700575 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577
578 /**
579 * The statistics associated with a particular package.
580 */
581 public static abstract class Pkg {
582
583 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700584 * Returns information about all wakeup alarms that have been triggered for this
585 * package. The mapping keys are tag names for the alarms, the counter contains
586 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700588 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589
590 /**
591 * Returns a mapping containing service statistics.
592 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700593 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
595 /**
596 * The statistics associated with a particular service.
597 */
598 public abstract class Serv {
599
600 /**
601 * Returns the amount of time spent started.
602 *
603 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700604 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 * @return
606 */
607 public abstract long getStartTime(long batteryUptime, int which);
608
609 /**
610 * Returns the total number of times startService() has been called.
611 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700612 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 */
614 public abstract int getStarts(int which);
615
616 /**
617 * Returns the total number times the service has been launched.
618 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700619 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 */
621 public abstract int getLaunches(int which);
622 }
623 }
624 }
625
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800626 public static final class LevelStepTracker {
627 public long mLastStepTime = -1;
628 public int mNumStepDurations;
629 public final long[] mStepDurations;
630
631 public LevelStepTracker(int maxLevelSteps) {
632 mStepDurations = new long[maxLevelSteps];
633 }
634
635 public LevelStepTracker(int numSteps, long[] steps) {
636 mNumStepDurations = numSteps;
637 mStepDurations = new long[numSteps];
638 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
639 }
640
641 public long getDurationAt(int index) {
642 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
643 }
644
645 public int getLevelAt(int index) {
646 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
647 >> STEP_LEVEL_LEVEL_SHIFT);
648 }
649
650 public int getInitModeAt(int index) {
651 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
652 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
653 }
654
655 public int getModModeAt(int index) {
656 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
657 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
658 }
659
660 private void appendHex(long val, int topOffset, StringBuilder out) {
661 boolean hasData = false;
662 while (topOffset >= 0) {
663 int digit = (int)( (val>>topOffset) & 0xf );
664 topOffset -= 4;
665 if (!hasData && digit == 0) {
666 continue;
667 }
668 hasData = true;
669 if (digit >= 0 && digit <= 9) {
670 out.append((char)('0' + digit));
671 } else {
672 out.append((char)('a' + digit - 10));
673 }
674 }
675 }
676
677 public void encodeEntryAt(int index, StringBuilder out) {
678 long item = mStepDurations[index];
679 long duration = item & STEP_LEVEL_TIME_MASK;
680 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
681 >> STEP_LEVEL_LEVEL_SHIFT);
682 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
683 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
684 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
685 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
686 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
687 case Display.STATE_OFF: out.append('f'); break;
688 case Display.STATE_ON: out.append('o'); break;
689 case Display.STATE_DOZE: out.append('d'); break;
690 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
691 }
692 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
693 out.append('p');
694 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700695 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
696 out.append('i');
697 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800698 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
699 case Display.STATE_OFF: out.append('F'); break;
700 case Display.STATE_ON: out.append('O'); break;
701 case Display.STATE_DOZE: out.append('D'); break;
702 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
703 }
704 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
705 out.append('P');
706 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700707 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
708 out.append('I');
709 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800710 out.append('-');
711 appendHex(level, 4, out);
712 out.append('-');
713 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
714 }
715
716 public void decodeEntryAt(int index, String value) {
717 final int N = value.length();
718 int i = 0;
719 char c;
720 long out = 0;
721 while (i < N && (c=value.charAt(i)) != '-') {
722 i++;
723 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800724 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800725 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800726 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800727 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800728 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800729 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800730 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
731 << STEP_LEVEL_INITIAL_MODE_SHIFT);
732 break;
733 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
734 << STEP_LEVEL_INITIAL_MODE_SHIFT);
735 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700736 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
737 << STEP_LEVEL_INITIAL_MODE_SHIFT);
738 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800739 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
740 break;
741 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
742 break;
743 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
744 break;
745 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
746 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
747 break;
748 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
749 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800750 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700751 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
752 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
753 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800754 }
755 }
756 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800757 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800758 while (i < N && (c=value.charAt(i)) != '-') {
759 i++;
760 level <<= 4;
761 if (c >= '0' && c <= '9') {
762 level += c - '0';
763 } else if (c >= 'a' && c <= 'f') {
764 level += c - 'a' + 10;
765 } else if (c >= 'A' && c <= 'F') {
766 level += c - 'A' + 10;
767 }
768 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800769 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800770 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
771 long duration = 0;
772 while (i < N && (c=value.charAt(i)) != '-') {
773 i++;
774 duration <<= 4;
775 if (c >= '0' && c <= '9') {
776 duration += c - '0';
777 } else if (c >= 'a' && c <= 'f') {
778 duration += c - 'a' + 10;
779 } else if (c >= 'A' && c <= 'F') {
780 duration += c - 'A' + 10;
781 }
782 }
783 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
784 }
785
786 public void init() {
787 mLastStepTime = -1;
788 mNumStepDurations = 0;
789 }
790
791 public void clearTime() {
792 mLastStepTime = -1;
793 }
794
795 public long computeTimePerLevel() {
796 final long[] steps = mStepDurations;
797 final int numSteps = mNumStepDurations;
798
799 // For now we'll do a simple average across all steps.
800 if (numSteps <= 0) {
801 return -1;
802 }
803 long total = 0;
804 for (int i=0; i<numSteps; i++) {
805 total += steps[i] & STEP_LEVEL_TIME_MASK;
806 }
807 return total / numSteps;
808 /*
809 long[] buckets = new long[numSteps];
810 int numBuckets = 0;
811 int numToAverage = 4;
812 int i = 0;
813 while (i < numSteps) {
814 long totalTime = 0;
815 int num = 0;
816 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
817 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
818 num++;
819 }
820 buckets[numBuckets] = totalTime / num;
821 numBuckets++;
822 numToAverage *= 2;
823 i += num;
824 }
825 if (numBuckets < 1) {
826 return -1;
827 }
828 long averageTime = buckets[numBuckets-1];
829 for (i=numBuckets-2; i>=0; i--) {
830 averageTime = (averageTime + buckets[i]) / 2;
831 }
832 return averageTime;
833 */
834 }
835
836 public long computeTimeEstimate(long modesOfInterest, long modeValues,
837 int[] outNumOfInterest) {
838 final long[] steps = mStepDurations;
839 final int count = mNumStepDurations;
840 if (count <= 0) {
841 return -1;
842 }
843 long total = 0;
844 int numOfInterest = 0;
845 for (int i=0; i<count; i++) {
846 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
847 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
848 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
849 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
850 // If the modes of interest didn't change during this step period...
851 if ((modMode&modesOfInterest) == 0) {
852 // And the mode values during this period match those we are measuring...
853 if ((initMode&modesOfInterest) == modeValues) {
854 // Then this can be used to estimate the total time!
855 numOfInterest++;
856 total += steps[i] & STEP_LEVEL_TIME_MASK;
857 }
858 }
859 }
860 if (numOfInterest <= 0) {
861 return -1;
862 }
863
864 if (outNumOfInterest != null) {
865 outNumOfInterest[0] = numOfInterest;
866 }
867
868 // The estimated time is the average time we spend in each level, multipled
869 // by 100 -- the total number of battery levels
870 return (total / numOfInterest) * 100;
871 }
872
873 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
874 int stepCount = mNumStepDurations;
875 final long lastStepTime = mLastStepTime;
876 if (lastStepTime >= 0 && numStepLevels > 0) {
877 final long[] steps = mStepDurations;
878 long duration = elapsedRealtime - lastStepTime;
879 for (int i=0; i<numStepLevels; i++) {
880 System.arraycopy(steps, 0, steps, 1, steps.length-1);
881 long thisDuration = duration / (numStepLevels-i);
882 duration -= thisDuration;
883 if (thisDuration > STEP_LEVEL_TIME_MASK) {
884 thisDuration = STEP_LEVEL_TIME_MASK;
885 }
886 steps[0] = thisDuration | modeBits;
887 }
888 stepCount += numStepLevels;
889 if (stepCount > steps.length) {
890 stepCount = steps.length;
891 }
892 }
893 mNumStepDurations = stepCount;
894 mLastStepTime = elapsedRealtime;
895 }
896
897 public void readFromParcel(Parcel in) {
898 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -0700899 if (N > mStepDurations.length) {
900 throw new ParcelFormatException("more step durations than available: " + N);
901 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800902 mNumStepDurations = N;
903 for (int i=0; i<N; i++) {
904 mStepDurations[i] = in.readLong();
905 }
906 }
907
908 public void writeToParcel(Parcel out) {
909 final int N = mNumStepDurations;
910 out.writeInt(N);
911 for (int i=0; i<N; i++) {
912 out.writeLong(mStepDurations[i]);
913 }
914 }
915 }
916
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700917 public static final class PackageChange {
918 public String mPackageName;
919 public boolean mUpdate;
920 public int mVersionCode;
921 }
922
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800923 public static final class DailyItem {
924 public long mStartTime;
925 public long mEndTime;
926 public LevelStepTracker mDischargeSteps;
927 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700928 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800929 }
930
931 public abstract DailyItem getDailyItemLocked(int daysAgo);
932
933 public abstract long getCurrentDailyStartTime();
934
935 public abstract long getNextMinDailyDeadline();
936
937 public abstract long getNextMaxDailyDeadline();
938
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800939 public final static class HistoryTag {
940 public String string;
941 public int uid;
942
943 public int poolIdx;
944
945 public void setTo(HistoryTag o) {
946 string = o.string;
947 uid = o.uid;
948 poolIdx = o.poolIdx;
949 }
950
951 public void setTo(String _string, int _uid) {
952 string = _string;
953 uid = _uid;
954 poolIdx = -1;
955 }
956
957 public void writeToParcel(Parcel dest, int flags) {
958 dest.writeString(string);
959 dest.writeInt(uid);
960 }
961
962 public void readFromParcel(Parcel src) {
963 string = src.readString();
964 uid = src.readInt();
965 poolIdx = -1;
966 }
967
968 @Override
969 public boolean equals(Object o) {
970 if (this == o) return true;
971 if (o == null || getClass() != o.getClass()) return false;
972
973 HistoryTag that = (HistoryTag) o;
974
975 if (uid != that.uid) return false;
976 if (!string.equals(that.string)) return false;
977
978 return true;
979 }
980
981 @Override
982 public int hashCode() {
983 int result = string.hashCode();
984 result = 31 * result + uid;
985 return result;
986 }
987 }
988
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800989 /**
990 * Optional detailed information that can go into a history step. This is typically
991 * generated each time the battery level changes.
992 */
993 public final static class HistoryStepDetails {
994 // Time (in 1/100 second) spent in user space and the kernel since the last step.
995 public int userTime;
996 public int systemTime;
997
998 // Top three apps using CPU in the last step, with times in 1/100 second.
999 public int appCpuUid1;
1000 public int appCpuUTime1;
1001 public int appCpuSTime1;
1002 public int appCpuUid2;
1003 public int appCpuUTime2;
1004 public int appCpuSTime2;
1005 public int appCpuUid3;
1006 public int appCpuUTime3;
1007 public int appCpuSTime3;
1008
1009 // Information from /proc/stat
1010 public int statUserTime;
1011 public int statSystemTime;
1012 public int statIOWaitTime;
1013 public int statIrqTime;
1014 public int statSoftIrqTime;
1015 public int statIdlTime;
1016
1017 public HistoryStepDetails() {
1018 clear();
1019 }
1020
1021 public void clear() {
1022 userTime = systemTime = 0;
1023 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1024 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1025 = appCpuUTime3 = appCpuSTime3 = 0;
1026 }
1027
1028 public void writeToParcel(Parcel out) {
1029 out.writeInt(userTime);
1030 out.writeInt(systemTime);
1031 out.writeInt(appCpuUid1);
1032 out.writeInt(appCpuUTime1);
1033 out.writeInt(appCpuSTime1);
1034 out.writeInt(appCpuUid2);
1035 out.writeInt(appCpuUTime2);
1036 out.writeInt(appCpuSTime2);
1037 out.writeInt(appCpuUid3);
1038 out.writeInt(appCpuUTime3);
1039 out.writeInt(appCpuSTime3);
1040 out.writeInt(statUserTime);
1041 out.writeInt(statSystemTime);
1042 out.writeInt(statIOWaitTime);
1043 out.writeInt(statIrqTime);
1044 out.writeInt(statSoftIrqTime);
1045 out.writeInt(statIdlTime);
1046 }
1047
1048 public void readFromParcel(Parcel in) {
1049 userTime = in.readInt();
1050 systemTime = in.readInt();
1051 appCpuUid1 = in.readInt();
1052 appCpuUTime1 = in.readInt();
1053 appCpuSTime1 = in.readInt();
1054 appCpuUid2 = in.readInt();
1055 appCpuUTime2 = in.readInt();
1056 appCpuSTime2 = in.readInt();
1057 appCpuUid3 = in.readInt();
1058 appCpuUTime3 = in.readInt();
1059 appCpuSTime3 = in.readInt();
1060 statUserTime = in.readInt();
1061 statSystemTime = in.readInt();
1062 statIOWaitTime = in.readInt();
1063 statIrqTime = in.readInt();
1064 statSoftIrqTime = in.readInt();
1065 statIdlTime = in.readInt();
1066 }
1067 }
1068
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001069 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001070 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001071
1072 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001073 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001074
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001075 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001076 public static final byte CMD_NULL = -1;
1077 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001078 public static final byte CMD_CURRENT_TIME = 5;
1079 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001080 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001081 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001082
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001083 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001084
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001085 /**
1086 * Return whether the command code is a delta data update.
1087 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001088 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001089 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001090 }
1091
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001092 public byte batteryLevel;
1093 public byte batteryStatus;
1094 public byte batteryHealth;
1095 public byte batteryPlugType;
1096
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001097 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001098 public char batteryVoltage;
1099
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001100 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001101 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001102 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001103 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001104 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1105 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001106 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001107 public static final int STATE_PHONE_STATE_SHIFT = 6;
1108 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001109 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001110 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001111 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001112
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001113 // These states always appear directly in the first int token
1114 // of a delta change; they should be ones that change relatively
1115 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001116 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1117 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001118 public static final int STATE_GPS_ON_FLAG = 1<<29;
1119 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001120 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001121 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001122 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001123 // These are on the lower bits used for the command; if they change
1124 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001125 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001126 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001127 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001128 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1129 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1130 // empty slot
1131 // empty slot
1132 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001133
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001134 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001135 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1136
1137 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001138
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001139 public int states;
1140
Dianne Hackborn3251b902014-06-20 14:40:53 -07001141 // Constants from WIFI_SUPPL_STATE_*
1142 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1143 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1144 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1145 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1146 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1147 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1148
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001149 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001150 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1151 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1152 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001153 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001154 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1155 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1156 public static final int STATE2_CHARGING_FLAG = 1<<24;
1157 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1158 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1159 public static final int STATE2_CAMERA_FLAG = 1<<21;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001160
1161 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001162 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001163 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1164
1165 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001166
Dianne Hackborn40c87252014-03-19 16:55:40 -07001167 public int states2;
1168
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001169 // The wake lock that was acquired at this point.
1170 public HistoryTag wakelockTag;
1171
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001172 // Kernel wakeup reason at this point.
1173 public HistoryTag wakeReasonTag;
1174
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001175 // Non-null when there is more detailed information at this step.
1176 public HistoryStepDetails stepDetails;
1177
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001178 public static final int EVENT_FLAG_START = 0x8000;
1179 public static final int EVENT_FLAG_FINISH = 0x4000;
1180
1181 // No event in this item.
1182 public static final int EVENT_NONE = 0x0000;
1183 // Event is about a process that is running.
1184 public static final int EVENT_PROC = 0x0001;
1185 // Event is about an application package that is in the foreground.
1186 public static final int EVENT_FOREGROUND = 0x0002;
1187 // Event is about an application package that is at the top of the screen.
1188 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001189 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001190 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001191 // Events for all additional wake locks aquired/release within a wake block.
1192 // These are not generated by default.
1193 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001194 // Event is about an application executing a scheduled job.
1195 public static final int EVENT_JOB = 0x0006;
1196 // Events for users running.
1197 public static final int EVENT_USER_RUNNING = 0x0007;
1198 // Events for foreground user.
1199 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001200 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001201 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001202 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001203 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001204 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001205 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001206 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001207 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001208 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001209 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001210 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001211 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001212 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001213 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001214 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001215 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001216 // Event for a package being on the temporary whitelist.
1217 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001218 // Event for the screen waking up.
1219 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Amith Yamasani67768492015-06-09 12:23:58 -07001220
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001221 // Number of event types.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001222 public static final int EVENT_COUNT = 0x0013;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001223 // Mask to extract out only the type part of the event.
1224 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001225
1226 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1227 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1228 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1229 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1230 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1231 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001232 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1233 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001234 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1235 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001236 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1237 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1238 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1239 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1240 public static final int EVENT_USER_FOREGROUND_START =
1241 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1242 public static final int EVENT_USER_FOREGROUND_FINISH =
1243 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001244 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1245 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001246 public static final int EVENT_TEMP_WHITELIST_START =
1247 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1248 public static final int EVENT_TEMP_WHITELIST_FINISH =
1249 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001250
1251 // For CMD_EVENT.
1252 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001253 public HistoryTag eventTag;
1254
Dianne Hackborn9a755432014-05-15 17:05:22 -07001255 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001256 public long currentTime;
1257
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001258 // Meta-data when reading.
1259 public int numReadInts;
1260
1261 // Pre-allocated objects.
1262 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001263 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001264 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001265
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001266 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001267 }
1268
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001269 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001270 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001271 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001272 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001273 }
1274
1275 public int describeContents() {
1276 return 0;
1277 }
1278
1279 public void writeToParcel(Parcel dest, int flags) {
1280 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001281 int bat = (((int)cmd)&0xff)
1282 | ((((int)batteryLevel)<<8)&0xff00)
1283 | ((((int)batteryStatus)<<16)&0xf0000)
1284 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001285 | ((((int)batteryPlugType)<<24)&0xf000000)
1286 | (wakelockTag != null ? 0x10000000 : 0)
1287 | (wakeReasonTag != null ? 0x20000000 : 0)
1288 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001289 dest.writeInt(bat);
1290 bat = (((int)batteryTemperature)&0xffff)
1291 | ((((int)batteryVoltage)<<16)&0xffff0000);
1292 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001293 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001294 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001295 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001296 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001297 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001298 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001299 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001300 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001301 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001302 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001303 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001304 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001305 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001306 dest.writeLong(currentTime);
1307 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001308 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001309
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001310 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001311 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001312 int bat = src.readInt();
1313 cmd = (byte)(bat&0xff);
1314 batteryLevel = (byte)((bat>>8)&0xff);
1315 batteryStatus = (byte)((bat>>16)&0xf);
1316 batteryHealth = (byte)((bat>>20)&0xf);
1317 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001318 int bat2 = src.readInt();
1319 batteryTemperature = (short)(bat2&0xffff);
1320 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001321 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001322 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001323 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001324 wakelockTag = localWakelockTag;
1325 wakelockTag.readFromParcel(src);
1326 } else {
1327 wakelockTag = null;
1328 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001329 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001330 wakeReasonTag = localWakeReasonTag;
1331 wakeReasonTag.readFromParcel(src);
1332 } else {
1333 wakeReasonTag = null;
1334 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001335 if ((bat&0x40000000) != 0) {
1336 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001337 eventTag = localEventTag;
1338 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001339 } else {
1340 eventCode = EVENT_NONE;
1341 eventTag = null;
1342 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001343 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001344 currentTime = src.readLong();
1345 } else {
1346 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001347 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001348 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001349 }
1350
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001351 public void clear() {
1352 time = 0;
1353 cmd = CMD_NULL;
1354 batteryLevel = 0;
1355 batteryStatus = 0;
1356 batteryHealth = 0;
1357 batteryPlugType = 0;
1358 batteryTemperature = 0;
1359 batteryVoltage = 0;
1360 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001361 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001362 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001363 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001364 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001365 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001366 }
1367
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001368 public void setTo(HistoryItem o) {
1369 time = o.time;
1370 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001371 setToCommon(o);
1372 }
1373
1374 public void setTo(long time, byte cmd, HistoryItem o) {
1375 this.time = time;
1376 this.cmd = cmd;
1377 setToCommon(o);
1378 }
1379
1380 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001381 batteryLevel = o.batteryLevel;
1382 batteryStatus = o.batteryStatus;
1383 batteryHealth = o.batteryHealth;
1384 batteryPlugType = o.batteryPlugType;
1385 batteryTemperature = o.batteryTemperature;
1386 batteryVoltage = o.batteryVoltage;
1387 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001388 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001389 if (o.wakelockTag != null) {
1390 wakelockTag = localWakelockTag;
1391 wakelockTag.setTo(o.wakelockTag);
1392 } else {
1393 wakelockTag = null;
1394 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001395 if (o.wakeReasonTag != null) {
1396 wakeReasonTag = localWakeReasonTag;
1397 wakeReasonTag.setTo(o.wakeReasonTag);
1398 } else {
1399 wakeReasonTag = null;
1400 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001401 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001402 if (o.eventTag != null) {
1403 eventTag = localEventTag;
1404 eventTag.setTo(o.eventTag);
1405 } else {
1406 eventTag = null;
1407 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001408 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001409 }
1410
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001411 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001412 return batteryLevel == o.batteryLevel
1413 && batteryStatus == o.batteryStatus
1414 && batteryHealth == o.batteryHealth
1415 && batteryPlugType == o.batteryPlugType
1416 && batteryTemperature == o.batteryTemperature
1417 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001418 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001419 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001420 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001421 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001422
1423 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001424 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001425 return false;
1426 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001427 if (wakelockTag != o.wakelockTag) {
1428 if (wakelockTag == null || o.wakelockTag == null) {
1429 return false;
1430 }
1431 if (!wakelockTag.equals(o.wakelockTag)) {
1432 return false;
1433 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001434 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001435 if (wakeReasonTag != o.wakeReasonTag) {
1436 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1437 return false;
1438 }
1439 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1440 return false;
1441 }
1442 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001443 if (eventTag != o.eventTag) {
1444 if (eventTag == null || o.eventTag == null) {
1445 return false;
1446 }
1447 if (!eventTag.equals(o.eventTag)) {
1448 return false;
1449 }
1450 }
1451 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001453 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001454
1455 public final static class HistoryEventTracker {
1456 private final HashMap<String, SparseIntArray>[] mActiveEvents
1457 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1458
1459 public boolean updateState(int code, String name, int uid, int poolIdx) {
1460 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1461 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1462 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1463 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001464 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001465 mActiveEvents[idx] = active;
1466 }
1467 SparseIntArray uids = active.get(name);
1468 if (uids == null) {
1469 uids = new SparseIntArray();
1470 active.put(name, uids);
1471 }
1472 if (uids.indexOfKey(uid) >= 0) {
1473 // Already set, nothing to do!
1474 return false;
1475 }
1476 uids.put(uid, poolIdx);
1477 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1478 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1479 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1480 if (active == null) {
1481 // not currently active, nothing to do.
1482 return false;
1483 }
1484 SparseIntArray uids = active.get(name);
1485 if (uids == null) {
1486 // not currently active, nothing to do.
1487 return false;
1488 }
1489 idx = uids.indexOfKey(uid);
1490 if (idx < 0) {
1491 // not currently active, nothing to do.
1492 return false;
1493 }
1494 uids.removeAt(idx);
1495 if (uids.size() <= 0) {
1496 active.remove(name);
1497 }
1498 }
1499 return true;
1500 }
1501
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001502 public void removeEvents(int code) {
1503 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1504 mActiveEvents[idx] = null;
1505 }
1506
Dianne Hackborn37de0982014-05-09 09:32:18 -07001507 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1508 return mActiveEvents[code];
1509 }
1510 }
1511
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001512 public static final class BitDescription {
1513 public final int mask;
1514 public final int shift;
1515 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001517 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001518 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001519
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001520 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001521 this.mask = mask;
1522 this.shift = -1;
1523 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001524 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001525 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001527 }
1528
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001529 public BitDescription(int mask, int shift, String name, String shortName,
1530 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001531 this.mask = mask;
1532 this.shift = shift;
1533 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001534 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001535 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001536 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001537 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001538 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001539
Dianne Hackbornfc064132014-06-02 12:42:12 -07001540 /**
1541 * Don't allow any more batching in to the current history event. This
1542 * is called when printing partial histories, so to ensure that the next
1543 * history event will go in to a new batch after what was printed in the
1544 * last partial history.
1545 */
1546 public abstract void commitCurrentHistoryBatchLocked();
1547
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001548 public abstract int getHistoryTotalSize();
1549
1550 public abstract int getHistoryUsedSize();
1551
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001552 public abstract boolean startIteratingHistoryLocked();
1553
Dianne Hackborn099bc622014-01-22 13:39:16 -08001554 public abstract int getHistoryStringPoolSize();
1555
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001556 public abstract int getHistoryStringPoolBytes();
1557
1558 public abstract String getHistoryTagPoolString(int index);
1559
1560 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001561
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001562 public abstract boolean getNextHistoryLocked(HistoryItem out);
1563
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001564 public abstract void finishIteratingHistoryLocked();
1565
1566 public abstract boolean startIteratingOldHistoryLocked();
1567
1568 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1569
1570 public abstract void finishIteratingOldHistoryLocked();
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001573 * Return the base time offset for the battery history.
1574 */
1575 public abstract long getHistoryBaseTime();
1576
1577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 * Returns the number of times the device has been started.
1579 */
1580 public abstract int getStartCount();
1581
1582 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001583 * 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 -08001584 * running on battery.
1585 *
1586 * {@hide}
1587 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001588 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001590 /**
1591 * Returns the number of times the screen was turned on.
1592 *
1593 * {@hide}
1594 */
1595 public abstract int getScreenOnCount(int which);
1596
Jeff Browne95c3cd2014-05-02 16:59:26 -07001597 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1598
Dianne Hackborn617f8772009-03-31 15:04:46 -07001599 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1600 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1601 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1602 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1603 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1604
1605 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1606 "dark", "dim", "medium", "light", "bright"
1607 };
1608
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001609 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1610 "0", "1", "2", "3", "4"
1611 };
1612
Dianne Hackborn617f8772009-03-31 15:04:46 -07001613 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001614
Dianne Hackborn617f8772009-03-31 15:04:46 -07001615 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001616 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001617 * the given brightness
1618 *
1619 * {@hide}
1620 */
1621 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001622 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001625 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001626 * running on battery.
1627 *
1628 * {@hide}
1629 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001630 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001631
1632 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001633 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001634 *
1635 * {@hide}
1636 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001637 public abstract int getPowerSaveModeEnabledCount(int which);
1638
1639 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001640 * Constant for device idle mode: not active.
1641 */
1642 public static final int DEVICE_IDLE_MODE_OFF = 0;
1643
1644 /**
1645 * Constant for device idle mode: active in lightweight mode.
1646 */
1647 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1648
1649 /**
1650 * Constant for device idle mode: active in full mode.
1651 */
1652 public static final int DEVICE_IDLE_MODE_FULL = 2;
1653
1654 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001655 * Returns the time in microseconds that device has been in idle mode while
1656 * running on battery.
1657 *
1658 * {@hide}
1659 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001660 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001661
1662 /**
1663 * Returns the number of times that the devie has gone in to idle mode.
1664 *
1665 * {@hide}
1666 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001667 public abstract int getDeviceIdleModeCount(int mode, int which);
1668
1669 /**
1670 * Return the longest duration we spent in a particular device idle mode (fully in the
1671 * mode, not in idle maintenance etc).
1672 */
1673 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001674
1675 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001676 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001677 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001678 * counts all of the time that we consider the device to be idle, whether or not
1679 * it is currently in the actual device idle mode.
1680 *
1681 * {@hide}
1682 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001683 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001684
1685 /**
1686 * Returns the number of times that the devie has started idling.
1687 *
1688 * {@hide}
1689 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001690 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001691
1692 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001693 * Returns the number of times that connectivity state changed.
1694 *
1695 * {@hide}
1696 */
1697 public abstract int getNumConnectivityChange(int which);
1698
1699 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001700 * 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 -08001701 * running on battery.
1702 *
1703 * {@hide}
1704 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001705 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001706
1707 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001708 * Returns the number of times a phone call was activated.
1709 *
1710 * {@hide}
1711 */
1712 public abstract int getPhoneOnCount(int which);
1713
1714 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001715 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001716 * the given signal strength.
1717 *
1718 * {@hide}
1719 */
1720 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001721 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001722
Dianne Hackborn617f8772009-03-31 15:04:46 -07001723 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001724 * Returns the time in microseconds that the phone has been trying to
1725 * acquire a signal.
1726 *
1727 * {@hide}
1728 */
1729 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001730 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001731
1732 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001733 * Returns the number of times the phone has entered the given signal strength.
1734 *
1735 * {@hide}
1736 */
1737 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1738
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001739 /**
1740 * Returns the time in microseconds that the mobile network has been active
1741 * (in a high power state).
1742 *
1743 * {@hide}
1744 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001745 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001746
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001747 /**
1748 * Returns the number of times that the mobile network has transitioned to the
1749 * active state.
1750 *
1751 * {@hide}
1752 */
1753 public abstract int getMobileRadioActiveCount(int which);
1754
1755 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001756 * Returns the time in microseconds that is the difference between the mobile radio
1757 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1758 * from the radio.
1759 *
1760 * {@hide}
1761 */
1762 public abstract long getMobileRadioActiveAdjustedTime(int which);
1763
1764 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001765 * Returns the time in microseconds that the mobile network has been active
1766 * (in a high power state) but not being able to blame on an app.
1767 *
1768 * {@hide}
1769 */
1770 public abstract long getMobileRadioActiveUnknownTime(int which);
1771
1772 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001773 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001774 *
1775 * {@hide}
1776 */
1777 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001778
Dianne Hackborn627bba72009-03-24 22:32:56 -07001779 public static final int DATA_CONNECTION_NONE = 0;
1780 public static final int DATA_CONNECTION_GPRS = 1;
1781 public static final int DATA_CONNECTION_EDGE = 2;
1782 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001783 public static final int DATA_CONNECTION_CDMA = 4;
1784 public static final int DATA_CONNECTION_EVDO_0 = 5;
1785 public static final int DATA_CONNECTION_EVDO_A = 6;
1786 public static final int DATA_CONNECTION_1xRTT = 7;
1787 public static final int DATA_CONNECTION_HSDPA = 8;
1788 public static final int DATA_CONNECTION_HSUPA = 9;
1789 public static final int DATA_CONNECTION_HSPA = 10;
1790 public static final int DATA_CONNECTION_IDEN = 11;
1791 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001792 public static final int DATA_CONNECTION_LTE = 13;
1793 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001794 public static final int DATA_CONNECTION_HSPAP = 15;
1795 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001796
Dianne Hackborn627bba72009-03-24 22:32:56 -07001797 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001798 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001799 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001800 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001801 };
1802
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001803 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001804
1805 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001806 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001807 * the given data connection.
1808 *
1809 * {@hide}
1810 */
1811 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001812 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001815 * Returns the number of times the phone has entered the given data
1816 * connection type.
1817 *
1818 * {@hide}
1819 */
1820 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001821
Dianne Hackborn3251b902014-06-20 14:40:53 -07001822 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1823 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1824 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1825 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1826 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1827 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1828 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1829 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1830 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1831 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1832 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1833 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1834 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1835
1836 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1837
1838 static final String[] WIFI_SUPPL_STATE_NAMES = {
1839 "invalid", "disconn", "disabled", "inactive", "scanning",
1840 "authenticating", "associating", "associated", "4-way-handshake",
1841 "group-handshake", "completed", "dormant", "uninit"
1842 };
1843
1844 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1845 "inv", "dsc", "dis", "inact", "scan",
1846 "auth", "ascing", "asced", "4-way",
1847 "group", "compl", "dorm", "uninit"
1848 };
1849
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001850 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1851 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001852 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001853 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1854 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001855 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001856 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1857 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1858 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001859 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001860 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001861 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001862 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001863 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1864 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001865 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1866 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1867 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1868 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1869 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1870 new String[] {"in", "out", "emergency", "off"},
1871 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001872 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1873 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1874 SignalStrength.SIGNAL_STRENGTH_NAMES,
1875 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001876 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1877 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1878 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001879 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001880
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001881 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1882 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001883 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001884 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001885 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001886 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001887 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001888 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
1889 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
1890 new String[] { "off", "light", "full", "???" },
1891 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001892 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1893 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1894 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001895 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1896 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1897 new String[] { "0", "1", "2", "3", "4" },
1898 new String[] { "0", "1", "2", "3", "4" }),
1899 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1900 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1901 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001902 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001903 };
1904
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001905 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001906 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001907 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
1908 "screenwake",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001909 };
1910
1911 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001912 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001913 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
1914 "Esw",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001915 };
1916
Dianne Hackborn617f8772009-03-31 15:04:46 -07001917 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001918 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001919 * running on battery.
1920 *
1921 * {@hide}
1922 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001923 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001924
1925 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001926 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001927 * been in the running state while the device was running on battery.
1928 *
1929 * {@hide}
1930 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001931 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001932
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001933 public static final int WIFI_STATE_OFF = 0;
1934 public static final int WIFI_STATE_OFF_SCANNING = 1;
1935 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1936 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1937 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1938 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1939 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1940 public static final int WIFI_STATE_SOFT_AP = 7;
1941
1942 static final String[] WIFI_STATE_NAMES = {
1943 "off", "scanning", "no_net", "disconn",
1944 "sta", "p2p", "sta_p2p", "soft_ap"
1945 };
1946
1947 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1948
1949 /**
1950 * Returns the time in microseconds that WiFi has been running in the given state.
1951 *
1952 * {@hide}
1953 */
1954 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001955 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001956
1957 /**
1958 * Returns the number of times that WiFi has entered the given state.
1959 *
1960 * {@hide}
1961 */
1962 public abstract int getWifiStateCount(int wifiState, int which);
1963
The Android Open Source Project10592532009-03-18 17:39:46 -07001964 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001965 * Returns the time in microseconds that the wifi supplicant has been
1966 * in a given state.
1967 *
1968 * {@hide}
1969 */
1970 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1971
1972 /**
1973 * Returns the number of times that the wifi supplicant has transitioned
1974 * to a given state.
1975 *
1976 * {@hide}
1977 */
1978 public abstract int getWifiSupplStateCount(int state, int which);
1979
1980 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1981
1982 /**
1983 * Returns the time in microseconds that WIFI has been running with
1984 * the given signal strength.
1985 *
1986 * {@hide}
1987 */
1988 public abstract long getWifiSignalStrengthTime(int strengthBin,
1989 long elapsedRealtimeUs, int which);
1990
1991 /**
1992 * Returns the number of times WIFI has entered the given signal strength.
1993 *
1994 * {@hide}
1995 */
1996 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1997
1998 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001999 * Returns the time in microseconds that the flashlight has been on while the device was
2000 * running on battery.
2001 *
2002 * {@hide}
2003 */
2004 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2005
2006 /**
2007 * Returns the number of times that the flashlight has been turned on while the device was
2008 * running on battery.
2009 *
2010 * {@hide}
2011 */
2012 public abstract long getFlashlightOnCount(int which);
2013
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002014 /**
2015 * Returns the time in microseconds that the camera has been on while the device was
2016 * running on battery.
2017 *
2018 * {@hide}
2019 */
2020 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2021
2022
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002023 public static final int NETWORK_MOBILE_RX_DATA = 0;
2024 public static final int NETWORK_MOBILE_TX_DATA = 1;
2025 public static final int NETWORK_WIFI_RX_DATA = 2;
2026 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002027 public static final int NETWORK_BT_RX_DATA = 4;
2028 public static final int NETWORK_BT_TX_DATA = 5;
2029 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002030
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002031 public abstract long getNetworkActivityBytes(int type, int which);
2032 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002033
Adam Lesinski33dac552015-03-09 15:24:48 -07002034 public static final int CONTROLLER_IDLE_TIME = 0;
2035 public static final int CONTROLLER_RX_TIME = 1;
2036 public static final int CONTROLLER_TX_TIME = 2;
Adam Lesinskie08af192015-03-25 16:42:59 -07002037 public static final int CONTROLLER_POWER_DRAIN = 3;
2038 public static final int NUM_CONTROLLER_ACTIVITY_TYPES = CONTROLLER_POWER_DRAIN + 1;
Adam Lesinski33dac552015-03-09 15:24:48 -07002039
Adam Lesinskie08af192015-03-25 16:42:59 -07002040 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002041 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2042 * When true, calling {@link #getBluetoothControllerActivity(int, int)} will yield the
2043 * actual power data.
2044 */
2045 public abstract boolean hasBluetoothActivityReporting();
2046
2047 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07002048 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
2049 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
2050 * respective state.
2051 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
2052 * milli-ampere-milliseconds (mAms).
2053 */
Adam Lesinski33dac552015-03-09 15:24:48 -07002054 public abstract long getBluetoothControllerActivity(int type, int which);
Adam Lesinskie08af192015-03-25 16:42:59 -07002055
2056 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002057 * Returns true if the BatteryStats object has detailed WiFi power reports.
2058 * When true, calling {@link #getWifiControllerActivity(int, int)} will yield the
2059 * actual power data.
2060 */
2061 public abstract boolean hasWifiActivityReporting();
2062
2063 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07002064 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
2065 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
2066 * respective state.
2067 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
2068 * milli-ampere-milliseconds (mAms).
2069 */
Adam Lesinski33dac552015-03-09 15:24:48 -07002070 public abstract long getWifiControllerActivity(int type, int which);
2071
The Android Open Source Project10592532009-03-18 17:39:46 -07002072 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002073 * Return the wall clock time when battery stats data collection started.
2074 */
2075 public abstract long getStartClockTime();
2076
2077 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002078 * Return platform version tag that we were running in when the battery stats started.
2079 */
2080 public abstract String getStartPlatformVersion();
2081
2082 /**
2083 * Return platform version tag that we were running in when the battery stats ended.
2084 */
2085 public abstract String getEndPlatformVersion();
2086
2087 /**
2088 * Return the internal version code of the parcelled format.
2089 */
2090 public abstract int getParcelVersion();
2091
2092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 * Return whether we are currently running on battery.
2094 */
2095 public abstract boolean getIsOnBattery();
2096
2097 /**
2098 * Returns a SparseArray containing the statistics for each uid.
2099 */
2100 public abstract SparseArray<? extends Uid> getUidStats();
2101
2102 /**
2103 * Returns the current battery uptime in microseconds.
2104 *
2105 * @param curTime the amount of elapsed realtime in microseconds.
2106 */
2107 public abstract long getBatteryUptime(long curTime);
2108
2109 /**
2110 * Returns the current battery realtime in microseconds.
2111 *
2112 * @param curTime the amount of elapsed realtime in microseconds.
2113 */
2114 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002115
2116 /**
Evan Millar633a1742009-04-02 16:36:33 -07002117 * Returns the battery percentage level at the last time the device was unplugged from power, or
2118 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002119 */
Evan Millar633a1742009-04-02 16:36:33 -07002120 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002121
2122 /**
Evan Millar633a1742009-04-02 16:36:33 -07002123 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2124 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002125 */
Evan Millar633a1742009-04-02 16:36:33 -07002126 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127
2128 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002129 * Get the amount the battery has discharged since the stats were
2130 * last reset after charging, as a lower-end approximation.
2131 */
2132 public abstract int getLowDischargeAmountSinceCharge();
2133
2134 /**
2135 * Get the amount the battery has discharged since the stats were
2136 * last reset after charging, as an upper-end approximation.
2137 */
2138 public abstract int getHighDischargeAmountSinceCharge();
2139
2140 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002141 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2142 */
2143 public abstract int getDischargeAmount(int which);
2144
2145 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002146 * Get the amount the battery has discharged while the screen was on,
2147 * since the last time power was unplugged.
2148 */
2149 public abstract int getDischargeAmountScreenOn();
2150
2151 /**
2152 * Get the amount the battery has discharged while the screen was on,
2153 * since the last time the device was charged.
2154 */
2155 public abstract int getDischargeAmountScreenOnSinceCharge();
2156
2157 /**
2158 * Get the amount the battery has discharged while the screen was off,
2159 * since the last time power was unplugged.
2160 */
2161 public abstract int getDischargeAmountScreenOff();
2162
2163 /**
2164 * Get the amount the battery has discharged while the screen was off,
2165 * since the last time the device was charged.
2166 */
2167 public abstract int getDischargeAmountScreenOffSinceCharge();
2168
2169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 * Returns the total, last, or current battery uptime in microseconds.
2171 *
2172 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002173 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 */
2175 public abstract long computeBatteryUptime(long curTime, int which);
2176
2177 /**
2178 * Returns the total, last, or current battery realtime in microseconds.
2179 *
2180 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002181 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 */
2183 public abstract long computeBatteryRealtime(long curTime, int which);
2184
2185 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002186 * Returns the total, last, or current battery screen off uptime in microseconds.
2187 *
2188 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002189 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002190 */
2191 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2192
2193 /**
2194 * Returns the total, last, or current battery screen off realtime in microseconds.
2195 *
2196 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002197 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002198 */
2199 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2200
2201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 * Returns the total, last, or current uptime in microseconds.
2203 *
2204 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002205 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 */
2207 public abstract long computeUptime(long curTime, int which);
2208
2209 /**
2210 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002213 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 */
2215 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002216
2217 /**
2218 * Compute an approximation for how much run time (in microseconds) is remaining on
2219 * the battery. Returns -1 if no time can be computed: either there is not
2220 * enough current data to make a decision, or the battery is currently
2221 * charging.
2222 *
2223 * @param curTime The current elepsed realtime in microseconds.
2224 */
2225 public abstract long computeBatteryTimeRemaining(long curTime);
2226
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002227 // The part of a step duration that is the actual time.
2228 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2229
2230 // Bits in a step duration that are the new battery level we are at.
2231 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002232 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002233
2234 // Bits in a step duration that are the initial mode we were in at that step.
2235 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002236 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002237
2238 // Bits in a step duration that indicate which modes changed during that step.
2239 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002240 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002241
2242 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2243 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2244
2245 // Step duration mode: power save is on.
2246 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2247
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002248 // Step duration mode: device is currently in idle mode.
2249 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2250
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002251 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2252 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002253 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2254 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002255 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2256 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2257 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2258 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2259 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002260 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2261 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002262 };
2263 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2264 (Display.STATE_OFF-1),
2265 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002266 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002267 (Display.STATE_ON-1),
2268 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2269 (Display.STATE_DOZE-1),
2270 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2271 (Display.STATE_DOZE_SUSPEND-1),
2272 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002273 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002274 };
2275 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2276 "screen off",
2277 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002278 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002279 "screen on",
2280 "screen on power save",
2281 "screen doze",
2282 "screen doze power save",
2283 "screen doze-suspend",
2284 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002285 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002286 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002287
2288 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002289 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002290 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002291 public abstract LevelStepTracker getDischargeLevelStepTracker();
2292
2293 /**
2294 * Return the array of daily discharge step durations.
2295 */
2296 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002297
2298 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002299 * Compute an approximation for how much time (in microseconds) remains until the battery
2300 * is fully charged. Returns -1 if no time can be computed: either there is not
2301 * enough current data to make a decision, or the battery is currently
2302 * discharging.
2303 *
2304 * @param curTime The current elepsed realtime in microseconds.
2305 */
2306 public abstract long computeChargeTimeRemaining(long curTime);
2307
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002308 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002309 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002310 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002311 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002312
2313 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002314 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002315 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002316 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002317
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002318 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2319
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002320 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002321
Evan Millarc64edde2009-04-18 12:26:32 -07002322 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002324 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2325
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002326 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 long days = seconds / (60 * 60 * 24);
2328 if (days != 0) {
2329 out.append(days);
2330 out.append("d ");
2331 }
2332 long used = days * 60 * 60 * 24;
2333
2334 long hours = (seconds - used) / (60 * 60);
2335 if (hours != 0 || used != 0) {
2336 out.append(hours);
2337 out.append("h ");
2338 }
2339 used += hours * 60 * 60;
2340
2341 long mins = (seconds-used) / 60;
2342 if (mins != 0 || used != 0) {
2343 out.append(mins);
2344 out.append("m ");
2345 }
2346 used += mins * 60;
2347
2348 if (seconds != 0 || used != 0) {
2349 out.append(seconds-used);
2350 out.append("s ");
2351 }
2352 }
2353
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002354 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002356 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 sb.append(time - (sec * 1000));
2358 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002361 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002362 long sec = time / 1000;
2363 formatTimeRaw(sb, sec);
2364 sb.append(time - (sec * 1000));
2365 sb.append("ms");
2366 }
2367
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002368 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002370 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372 float perc = ((float)num) / ((float)den) * 100;
2373 mFormatBuilder.setLength(0);
2374 mFormatter.format("%.1f%%", perc);
2375 return mFormatBuilder.toString();
2376 }
2377
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002378 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002379 mFormatBuilder.setLength(0);
2380
2381 if (bytes < BYTES_PER_KB) {
2382 return bytes + "B";
2383 } else if (bytes < BYTES_PER_MB) {
2384 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2385 return mFormatBuilder.toString();
2386 } else if (bytes < BYTES_PER_GB){
2387 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2388 return mFormatBuilder.toString();
2389 } else {
2390 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2391 return mFormatBuilder.toString();
2392 }
2393 }
2394
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002395 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002396 if (timer != null) {
2397 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002398 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002399 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2400 return totalTimeMillis;
2401 }
2402 return 0;
2403 }
2404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 /**
2406 *
2407 * @param sb a StringBuilder object.
2408 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002409 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002411 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 * @param linePrefix a String to be prepended to each line of output.
2413 * @return the line prefix
2414 */
2415 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002416 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002419 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002420
Evan Millarc64edde2009-04-18 12:26:32 -07002421 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 if (totalTimeMillis != 0) {
2423 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002424 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002425 if (name != null) {
2426 sb.append(name);
2427 sb.append(' ');
2428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 sb.append('(');
2430 sb.append(count);
2431 sb.append(" times)");
2432 return ", ";
2433 }
2434 }
2435 return linePrefix;
2436 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002437
2438 /**
2439 *
2440 * @param pw a PrintWriter object to print to.
2441 * @param sb a StringBuilder object.
2442 * @param timer a Timer object contining the wakelock times.
2443 * @param rawRealtime the current on-battery time in microseconds.
2444 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2445 * @param prefix a String to be prepended to each line of output.
2446 * @param type the name of the timer.
2447 */
2448 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2449 long rawRealtime, int which, String prefix, String type) {
2450 if (timer != null) {
2451 // Convert from microseconds to milliseconds with rounding
2452 final long totalTime = (timer.getTotalTimeLocked(
2453 rawRealtime, which) + 500) / 1000;
2454 final int count = timer.getCountLocked(which);
2455 if (totalTime != 0) {
2456 sb.setLength(0);
2457 sb.append(prefix);
2458 sb.append(" ");
2459 sb.append(type);
2460 sb.append(": ");
2461 formatTimeMs(sb, totalTime);
2462 sb.append("realtime (");
2463 sb.append(count);
2464 sb.append(" times)");
2465 pw.println(sb.toString());
2466 return true;
2467 }
2468 }
2469 return false;
2470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471
2472 /**
2473 * Checkin version of wakelock printer. Prints simple comma-separated list.
2474 *
2475 * @param sb a StringBuilder object.
2476 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002477 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002479 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 * @param linePrefix a String to be prepended to each line of output.
2481 * @return the line prefix
2482 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002483 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2484 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 long totalTimeMicros = 0;
2486 int count = 0;
2487 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002488 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002489 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
2491 sb.append(linePrefix);
2492 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2493 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002494 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 sb.append(count);
2496 return ",";
2497 }
2498
2499 /**
2500 * Dump a comma-separated line of values for terse checkin mode.
2501 *
2502 * @param pw the PageWriter to dump log to
2503 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2504 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2505 * @param args type-dependent data arguments
2506 */
2507 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2508 Object... args ) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002509 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2510 pw.print(',');
2511 pw.print(uid);
2512 pw.print(',');
2513 pw.print(category);
2514 pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002515 pw.print(type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002516
2517 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002518 pw.print(',');
2519 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002521 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002523
2524 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002525 * Dump a given timer stat for terse checkin mode.
2526 *
2527 * @param pw the PageWriter to dump log to
2528 * @param uid the UID to log
2529 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2530 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2531 * @param timer a {@link Timer} to dump stats for
2532 * @param rawRealtime the current elapsed realtime of the system in microseconds
2533 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2534 */
2535 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2536 Timer timer, long rawRealtime, int which) {
2537 if (timer != null) {
2538 // Convert from microseconds to milliseconds with rounding
2539 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2540 / 1000;
2541 final int count = timer.getCountLocked(which);
2542 if (totalTime != 0) {
2543 dumpLine(pw, uid, category, type, totalTime, count);
2544 }
2545 }
2546 }
2547
2548 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002549 * Temporary for settings.
2550 */
2551 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2552 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2553 }
2554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 /**
2556 * Checkin server version of dump to produce more compact, computer-readable log.
2557 *
2558 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002560 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2561 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2563 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2564 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2566 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002567 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2568 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2569 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 final long totalRealtime = computeRealtime(rawRealtime, which);
2571 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002572 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002573 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002574 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002575 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2576 rawRealtime, which);
2577 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
2578 rawRealtime, which);
2579 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2580 rawRealtime, which);
2581 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
2582 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002583 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002584 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002585
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002586 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002588 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002589 final int NU = uidStats.size();
2590
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002591 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 // Dump "battery" stat
2594 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002595 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002596 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002597 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002598 getStartClockTime(),
2599 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002601 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002602 long fullWakeLockTimeTotal = 0;
2603 long partialWakeLockTimeTotal = 0;
2604
2605 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002606 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002607
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002608 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2609 = u.getWakelockStats();
2610 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2611 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002612
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002613 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2614 if (fullWakeTimer != null) {
2615 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2616 which);
2617 }
2618
2619 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2620 if (partialWakeTimer != null) {
2621 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2622 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002623 }
2624 }
2625 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002626
2627 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002628 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2629 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2630 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2631 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2632 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2633 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2634 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2635 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002636 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2637 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
2638 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
2639
Adam Lesinskie283d332015-04-16 12:29:25 -07002640 // Dump Wifi controller stats
2641 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2642 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2643 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2644 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2645 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
2646 final long wifiPowerMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
2647 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA,
2648 wifiOnTime / 1000, wifiRunningTime / 1000,
2649 wifiIdleTimeMs, wifiRxTimeMs, wifiTxTimeMs, wifiPowerMaMs / (1000*60*60));
2650
2651 // Dump Bluetooth controller stats
2652 final long btIdleTimeMs = getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
2653 final long btRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
2654 final long btTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
2655 final long btPowerMaMs = getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which);
2656 dumpLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_DATA,
2657 btIdleTimeMs, btRxTimeMs, btTxTimeMs, btPowerMaMs / (1000*60*60));
2658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 // Dump misc stats
2660 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002661 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002662 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002663 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002664 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002665 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
2666 getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which), deviceIdlingTime / 1000,
2667 getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07002668 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002669 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
2670 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
2671 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
2672 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
2673 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002674
2675 // Dump screen brightness stats
2676 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2677 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002678 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002679 }
2680 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002681
Dianne Hackborn627bba72009-03-24 22:32:56 -07002682 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002683 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2684 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002685 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002686 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002687 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002688 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002689 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002690 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002691 args[i] = getPhoneSignalStrengthCount(i, which);
2692 }
2693 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002694
Dianne Hackborn627bba72009-03-24 22:32:56 -07002695 // Dump network type stats
2696 args = new Object[NUM_DATA_CONNECTION_TYPES];
2697 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002698 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002699 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002700 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2701 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2702 args[i] = getPhoneDataConnectionCount(i, which);
2703 }
2704 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002705
2706 // Dump wifi state stats
2707 args = new Object[NUM_WIFI_STATES];
2708 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002709 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002710 }
2711 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2712 for (int i=0; i<NUM_WIFI_STATES; i++) {
2713 args[i] = getWifiStateCount(i, which);
2714 }
2715 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2716
Dianne Hackborn3251b902014-06-20 14:40:53 -07002717 // Dump wifi suppl state stats
2718 args = new Object[NUM_WIFI_SUPPL_STATES];
2719 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2720 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2721 }
2722 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2723 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2724 args[i] = getWifiSupplStateCount(i, which);
2725 }
2726 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2727
2728 // Dump wifi signal strength stats
2729 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2730 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2731 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2732 }
2733 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2734 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2735 args[i] = getWifiSignalStrengthCount(i, which);
2736 }
2737 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2738
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002739 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002740 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002741 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002744 if (which == STATS_SINCE_UNPLUGGED) {
2745 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2746 getDischargeStartLevel()-getDischargeCurrentLevel(),
2747 getDischargeStartLevel()-getDischargeCurrentLevel(),
2748 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2749 } else {
2750 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2751 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002752 getDischargeAmountScreenOnSinceCharge(),
2753 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002754 }
2755
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002756 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002757 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002758 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002759 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002760 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002761 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002762 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002763 sb.toString());
2764 }
Evan Millarc64edde2009-04-18 12:26:32 -07002765 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002766 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002767 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002768 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2769 // Not doing the regular wake lock formatting to remain compatible
2770 // with the old checkin format.
2771 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2772 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002773 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002774 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002775 }
2776 }
Evan Millarc64edde2009-04-18 12:26:32 -07002777 }
2778
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002779 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002780 helper.create(this);
2781 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002782 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002783 if (sippers != null && sippers.size() > 0) {
2784 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2785 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002786 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002787 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2788 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2789 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002790 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002791 int uid = 0;
2792 String label;
2793 switch (bs.drainType) {
2794 case IDLE:
2795 label="idle";
2796 break;
2797 case CELL:
2798 label="cell";
2799 break;
2800 case PHONE:
2801 label="phone";
2802 break;
2803 case WIFI:
2804 label="wifi";
2805 break;
2806 case BLUETOOTH:
2807 label="blue";
2808 break;
2809 case SCREEN:
2810 label="scrn";
2811 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002812 case FLASHLIGHT:
2813 label="flashlight";
2814 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002815 case APP:
2816 uid = bs.uidObj.getUid();
2817 label = "uid";
2818 break;
2819 case USER:
2820 uid = UserHandle.getUid(bs.userId, 0);
2821 label = "user";
2822 break;
2823 case UNACCOUNTED:
2824 label = "unacc";
2825 break;
2826 case OVERCOUNTED:
2827 label = "over";
2828 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002829 case CAMERA:
2830 label = "camera";
2831 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002832 default:
2833 label = "???";
2834 }
2835 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07002836 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002837 }
2838 }
2839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 for (int iu = 0; iu < NU; iu++) {
2841 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842 if (reqUid >= 0 && uid != reqUid) {
2843 continue;
2844 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002845 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07002846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002848 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2849 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2850 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2851 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2852 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2853 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2854 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
2855 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
2856 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2857 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002858 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2859 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002860 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002861 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2862 wifiBytesRx, wifiBytesTx,
2863 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002864 wifiPacketsRx, wifiPacketsTx,
2865 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002866 }
2867
Adam Lesinskie283d332015-04-16 12:29:25 -07002868 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2869 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2870 final int wifiScanCount = u.getWifiScanCount(which);
2871 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2872 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2873 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2874 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07002875 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinskie283d332015-04-16 12:29:25 -07002876 || uidWifiRunningTime != 0 || uidWifiIdleTimeMs != 0 || uidWifiRxTimeMs != 0
2877 || uidWifiTxTimeMs != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002878 dumpLine(pw, uid, category, WIFI_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002879 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount,
2880 uidWifiIdleTimeMs, uidWifiRxTimeMs, uidWifiTxTimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07002881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882
Dianne Hackborn617f8772009-03-31 15:04:46 -07002883 if (u.hasUserActivity()) {
2884 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2885 boolean hasData = false;
2886 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2887 int val = u.getUserActivityCount(i, which);
2888 args[i] = val;
2889 if (val != 0) hasData = true;
2890 }
2891 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07002892 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002893 }
2894 }
2895
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002896 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
2897 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2898 final Uid.Wakelock wl = wakelocks.valueAt(iw);
2899 String linePrefix = "";
2900 sb.setLength(0);
2901 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
2902 rawRealtime, "f", which, linePrefix);
2903 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
2904 rawRealtime, "p", which, linePrefix);
2905 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
2906 rawRealtime, "w", which, linePrefix);
2907
2908 // Only log if we had at lease one wakelock...
2909 if (sb.length() > 0) {
2910 String name = wakelocks.keyAt(iw);
2911 if (name.indexOf(',') >= 0) {
2912 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002914 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
2916 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002917
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002918 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
2919 for (int isy=syncs.size()-1; isy>=0; isy--) {
2920 final Timer timer = syncs.valueAt(isy);
2921 // Convert from microseconds to milliseconds with rounding
2922 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2923 final int count = timer.getCountLocked(which);
2924 if (totalTime != 0) {
2925 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002926 }
2927 }
2928
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002929 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
2930 for (int ij=jobs.size()-1; ij>=0; ij--) {
2931 final Timer timer = jobs.valueAt(ij);
2932 // Convert from microseconds to milliseconds with rounding
2933 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2934 final int count = timer.getCountLocked(which);
2935 if (totalTime != 0) {
2936 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002937 }
2938 }
2939
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002940 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
2941 rawRealtime, which);
2942 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
2943 rawRealtime, which);
2944 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
2945 rawRealtime, which);
2946 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
2947 rawRealtime, which);
2948
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002949 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2950 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002951 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002952 final Uid.Sensor se = sensors.valueAt(ise);
2953 final int sensorNumber = sensors.keyAt(ise);
2954 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002955 if (timer != null) {
2956 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002957 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2958 / 1000;
2959 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002960 if (totalTime != 0) {
2961 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 }
2964 }
2965
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002966 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
2967 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002968
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002969 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
2970 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002971
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002972 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07002973 long totalStateTime = 0;
2974 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08002975 final long time = u.getProcessStateTime(ips, rawRealtime, which);
2976 totalStateTime += time;
2977 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07002978 }
2979 if (totalStateTime > 0) {
2980 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2981 }
2982
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07002983 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
2984 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
2985 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
2986 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
2987 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
2988 powerCpuMaUs / 1000);
2989 }
2990
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002991 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
2992 = u.getProcessStats();
2993 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
2994 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002995
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002996 final long userMillis = ps.getUserTime(which);
2997 final long systemMillis = ps.getSystemTime(which);
2998 final long foregroundMillis = ps.getForegroundTime(which);
2999 final int starts = ps.getStarts(which);
3000 final int numCrashes = ps.getNumCrashes(which);
3001 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003002
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003003 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3004 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3005 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3006 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 }
3008 }
3009
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003010 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3011 = u.getPackageStats();
3012 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3013 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3014 int wakeups = 0;
3015 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3016 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3017 wakeups += alarms.valueAt(iwa).getCountLocked(which);
3018 }
3019 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3020 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3021 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3022 final long startTime = ss.getStartTime(batteryUptime, which);
3023 final int starts = ss.getStarts(which);
3024 final int launches = ss.getLaunches(which);
3025 if (startTime != 0 || starts != 0 || launches != 0) {
3026 dumpLine(pw, uid, category, APK_DATA,
3027 wakeups, // wakeup alarms
3028 packageStats.keyAt(ipkg), // Apk
3029 serviceStats.keyAt(isvc), // service
3030 startTime / 1000, // time spent started, in ms
3031 starts,
3032 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 }
3034 }
3035 }
3036 }
3037 }
3038
Dianne Hackborn81038902012-11-26 17:04:09 -08003039 static final class TimerEntry {
3040 final String mName;
3041 final int mId;
3042 final BatteryStats.Timer mTimer;
3043 final long mTime;
3044 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3045 mName = name;
3046 mId = id;
3047 mTimer = timer;
3048 mTime = time;
3049 }
3050 }
3051
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003052 private void printmAh(PrintWriter printer, double power) {
3053 printer.print(BatteryStatsHelper.makemAh(power));
3054 }
3055
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003056 private void printmAh(StringBuilder sb, double power) {
3057 sb.append(BatteryStatsHelper.makemAh(power));
3058 }
3059
Dianne Hackbornd953c532014-08-16 18:17:38 -07003060 /**
3061 * Temporary for settings.
3062 */
3063 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3064 int reqUid) {
3065 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3066 }
3067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003069 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003070 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3072 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3073 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074
3075 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3076 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3077 final long totalRealtime = computeRealtime(rawRealtime, which);
3078 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003079 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3080 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3081 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003082 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3083 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003085 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003086
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003087 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003088 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003090 sb.setLength(0);
3091 sb.append(prefix);
3092 sb.append(" Time on battery: ");
3093 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3094 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3095 sb.append(") realtime, ");
3096 formatTimeMs(sb, whichBatteryUptime / 1000);
3097 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3098 sb.append(") uptime");
3099 pw.println(sb.toString());
3100 sb.setLength(0);
3101 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003102 sb.append(" Time on battery screen off: ");
3103 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3104 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3105 sb.append(") realtime, ");
3106 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3107 sb.append("(");
3108 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3109 sb.append(") uptime");
3110 pw.println(sb.toString());
3111 sb.setLength(0);
3112 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003113 sb.append(" Total run time: ");
3114 formatTimeMs(sb, totalRealtime / 1000);
3115 sb.append("realtime, ");
3116 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003117 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003118 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003119 if (batteryTimeRemaining >= 0) {
3120 sb.setLength(0);
3121 sb.append(prefix);
3122 sb.append(" Battery time remaining: ");
3123 formatTimeMs(sb, batteryTimeRemaining / 1000);
3124 pw.println(sb.toString());
3125 }
3126 if (chargeTimeRemaining >= 0) {
3127 sb.setLength(0);
3128 sb.append(prefix);
3129 sb.append(" Charge time remaining: ");
3130 formatTimeMs(sb, chargeTimeRemaining / 1000);
3131 pw.println(sb.toString());
3132 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003133 pw.print(" Start clock time: ");
3134 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3135
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003136 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003137 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003138 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003139 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3140 rawRealtime, which);
3141 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
3142 rawRealtime, which);
3143 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3144 rawRealtime, which);
3145 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
3146 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003147 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3148 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3149 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003150 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003151 sb.append(prefix);
3152 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3153 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003154 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003155 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3156 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003157 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003158 pw.println(sb.toString());
3159 sb.setLength(0);
3160 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003161 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003162 boolean didOne = false;
3163 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003164 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003165 if (time == 0) {
3166 continue;
3167 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003168 sb.append("\n ");
3169 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003170 didOne = true;
3171 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3172 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003173 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003174 sb.append("(");
3175 sb.append(formatRatioLocked(time, screenOnTime));
3176 sb.append(")");
3177 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003178 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003179 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003180 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003181 sb.setLength(0);
3182 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003183 sb.append(" Power save mode enabled: ");
3184 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003185 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003186 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003187 sb.append(")");
3188 pw.println(sb.toString());
3189 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003190 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003191 sb.setLength(0);
3192 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003193 sb.append(" Device light idling: ");
3194 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003195 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003196 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3197 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003198 sb.append("x");
3199 pw.println(sb.toString());
3200 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003201 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003202 sb.setLength(0);
3203 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003204 sb.append(" Idle mode light time: ");
3205 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003206 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003207 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3208 sb.append(") ");
3209 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003210 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003211 sb.append(" -- longest ");
3212 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3213 pw.println(sb.toString());
3214 }
3215 if (deviceIdlingTime != 0) {
3216 sb.setLength(0);
3217 sb.append(prefix);
3218 sb.append(" Device full idling: ");
3219 formatTimeMs(sb, deviceIdlingTime / 1000);
3220 sb.append("(");
3221 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3222 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which));
3223 sb.append("x");
3224 pw.println(sb.toString());
3225 }
3226 if (deviceIdleModeFullTime != 0) {
3227 sb.setLength(0);
3228 sb.append(prefix);
3229 sb.append(" Idle mode full time: ");
3230 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3231 sb.append("(");
3232 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3233 sb.append(") ");
3234 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which));
3235 sb.append("x");
3236 sb.append(" -- longest ");
3237 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003238 pw.println(sb.toString());
3239 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003240 if (phoneOnTime != 0) {
3241 sb.setLength(0);
3242 sb.append(prefix);
3243 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3244 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003245 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003246 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003247 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003248 if (connChanges != 0) {
3249 pw.print(prefix);
3250 pw.print(" Connectivity changes: "); pw.println(connChanges);
3251 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003252
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003253 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003254 long fullWakeLockTimeTotalMicros = 0;
3255 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003256
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003257 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003258
Evan Millar22ac0432009-03-31 11:33:18 -07003259 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003260 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003261
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003262 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3263 = u.getWakelockStats();
3264 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3265 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003266
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003267 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3268 if (fullWakeTimer != null) {
3269 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3270 rawRealtime, which);
3271 }
3272
3273 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3274 if (partialWakeTimer != null) {
3275 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3276 rawRealtime, which);
3277 if (totalTimeMicros > 0) {
3278 if (reqUid < 0) {
3279 // Only show the ordered list of all wake
3280 // locks if the caller is not asking for data
3281 // about a specific uid.
3282 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3283 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003284 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003285 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003286 }
3287 }
3288 }
3289 }
3290
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003291 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3292 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3293 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3294 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3295 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3296 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3297 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3298 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003299 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3300 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003301
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003302 if (fullWakeLockTimeTotalMicros != 0) {
3303 sb.setLength(0);
3304 sb.append(prefix);
3305 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3306 (fullWakeLockTimeTotalMicros + 500) / 1000);
3307 pw.println(sb.toString());
3308 }
3309
3310 if (partialWakeLockTimeTotalMicros != 0) {
3311 sb.setLength(0);
3312 sb.append(prefix);
3313 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3314 (partialWakeLockTimeTotalMicros + 500) / 1000);
3315 pw.println(sb.toString());
3316 }
3317
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003318 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003319 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3320 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3321 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3322 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003323 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003324 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003325 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003326 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003327 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003328 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003329 if (time == 0) {
3330 continue;
3331 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003332 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003333 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003334 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003335 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003336 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003337 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003338 sb.append("(");
3339 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003340 sb.append(") ");
3341 sb.append(getPhoneSignalStrengthCount(i, which));
3342 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003343 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003344 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003345 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003346
3347 sb.setLength(0);
3348 sb.append(prefix);
3349 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003350 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003351 pw.println(sb.toString());
3352
Dianne Hackborn627bba72009-03-24 22:32:56 -07003353 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003354 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003355 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003356 didOne = false;
3357 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003358 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003359 if (time == 0) {
3360 continue;
3361 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003362 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003363 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003364 didOne = true;
3365 sb.append(DATA_CONNECTION_NAMES[i]);
3366 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003367 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003368 sb.append("(");
3369 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003370 sb.append(") ");
3371 sb.append(getPhoneDataConnectionCount(i, which));
3372 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003373 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003374 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003375 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003376
3377 sb.setLength(0);
3378 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003379 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003380 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003381 formatTimeMs(sb, mobileActiveTime / 1000);
3382 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3383 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3384 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003385 pw.println(sb.toString());
3386
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003387 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3388 if (mobileActiveUnknownTime != 0) {
3389 sb.setLength(0);
3390 sb.append(prefix);
3391 sb.append(" Mobile radio active unknown time: ");
3392 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3393 sb.append("(");
3394 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3395 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3396 sb.append("x");
3397 pw.println(sb.toString());
3398 }
3399
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003400 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3401 if (mobileActiveAdjustedTime != 0) {
3402 sb.setLength(0);
3403 sb.append(prefix);
3404 sb.append(" Mobile radio active adjusted time: ");
3405 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3406 sb.append("(");
3407 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3408 sb.append(")");
3409 pw.println(sb.toString());
3410 }
3411
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003412 pw.print(prefix);
3413 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3414 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3415 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3416 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003417 sb.setLength(0);
3418 sb.append(prefix);
3419 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3420 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3421 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3422 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003423 sb.append(")");
3424 pw.println(sb.toString());
3425
3426 sb.setLength(0);
3427 sb.append(prefix);
3428 sb.append(" Wifi states:");
3429 didOne = false;
3430 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003431 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003432 if (time == 0) {
3433 continue;
3434 }
3435 sb.append("\n ");
3436 didOne = true;
3437 sb.append(WIFI_STATE_NAMES[i]);
3438 sb.append(" ");
3439 formatTimeMs(sb, time/1000);
3440 sb.append("(");
3441 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3442 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003443 sb.append(getWifiStateCount(i, which));
3444 sb.append("x");
3445 }
3446 if (!didOne) sb.append(" (no activity)");
3447 pw.println(sb.toString());
3448
3449 sb.setLength(0);
3450 sb.append(prefix);
3451 sb.append(" Wifi supplicant states:");
3452 didOne = false;
3453 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3454 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3455 if (time == 0) {
3456 continue;
3457 }
3458 sb.append("\n ");
3459 didOne = true;
3460 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3461 sb.append(" ");
3462 formatTimeMs(sb, time/1000);
3463 sb.append("(");
3464 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3465 sb.append(") ");
3466 sb.append(getWifiSupplStateCount(i, which));
3467 sb.append("x");
3468 }
3469 if (!didOne) sb.append(" (no activity)");
3470 pw.println(sb.toString());
3471
3472 sb.setLength(0);
3473 sb.append(prefix);
3474 sb.append(" Wifi signal levels:");
3475 didOne = false;
3476 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3477 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3478 if (time == 0) {
3479 continue;
3480 }
3481 sb.append("\n ");
3482 sb.append(prefix);
3483 didOne = true;
3484 sb.append("level(");
3485 sb.append(i);
3486 sb.append(") ");
3487 formatTimeMs(sb, time/1000);
3488 sb.append("(");
3489 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3490 sb.append(") ");
3491 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003492 sb.append("x");
3493 }
3494 if (!didOne) sb.append(" (no activity)");
3495 pw.println(sb.toString());
3496
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003497 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3498 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3499 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003500 final long wifiPowerDrainMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
Adam Lesinski33dac552015-03-09 15:24:48 -07003501 final long wifiTotalTimeMs = wifiIdleTimeMs + wifiRxTimeMs + wifiTxTimeMs;
3502
3503 sb.setLength(0);
3504 sb.append(prefix);
3505 sb.append(" WiFi Idle time: "); formatTimeMs(sb, wifiIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003506 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003507 sb.append(formatRatioLocked(wifiIdleTimeMs, wifiTotalTimeMs));
3508 sb.append(")");
3509 pw.println(sb.toString());
3510
3511 sb.setLength(0);
3512 sb.append(prefix);
3513 sb.append(" WiFi Rx time: "); formatTimeMs(sb, wifiRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003514 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003515 sb.append(formatRatioLocked(wifiRxTimeMs, wifiTotalTimeMs));
3516 sb.append(")");
3517 pw.println(sb.toString());
3518
3519 sb.setLength(0);
3520 sb.append(prefix);
3521 sb.append(" WiFi Tx time: "); formatTimeMs(sb, wifiTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003522 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003523 sb.append(formatRatioLocked(wifiTxTimeMs, wifiTotalTimeMs));
3524 sb.append(")");
3525 pw.println(sb.toString());
3526
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003527 sb.setLength(0);
3528 sb.append(prefix);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003529 sb.append(" WiFi Power drain: ").append(
3530 BatteryStatsHelper.makemAh(wifiPowerDrainMaMs / (double) (1000*60*60)));
3531 sb.append("mAh");
Adam Lesinskie08af192015-03-25 16:42:59 -07003532 pw.println(sb.toString());
3533
Adam Lesinski50e47602015-12-04 17:04:54 -08003534 pw.print(prefix);
3535 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3536 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3537
Adam Lesinski33dac552015-03-09 15:24:48 -07003538 final long bluetoothIdleTimeMs =
3539 getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
3540 final long bluetoothRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
3541 final long bluetoothTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
3542 final long bluetoothTotalTimeMs = bluetoothIdleTimeMs + bluetoothRxTimeMs +
3543 bluetoothTxTimeMs;
3544
3545 sb.setLength(0);
3546 sb.append(prefix);
3547 sb.append(" Bluetooth Idle time: "); formatTimeMs(sb, bluetoothIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003548 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003549 sb.append(formatRatioLocked(bluetoothIdleTimeMs, bluetoothTotalTimeMs));
3550 sb.append(")");
3551 pw.println(sb.toString());
3552
3553 sb.setLength(0);
3554 sb.append(prefix);
3555 sb.append(" Bluetooth Rx time: "); formatTimeMs(sb, bluetoothRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003556 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003557 sb.append(formatRatioLocked(bluetoothRxTimeMs, bluetoothTotalTimeMs));
3558 sb.append(")");
3559 pw.println(sb.toString());
3560
3561 sb.setLength(0);
3562 sb.append(prefix);
3563 sb.append(" Bluetooth Tx time: "); formatTimeMs(sb, bluetoothTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003564 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003565 sb.append(formatRatioLocked(bluetoothTxTimeMs, bluetoothTotalTimeMs));
3566 sb.append(")");
3567 pw.println(sb.toString());
3568
Adam Lesinskie283d332015-04-16 12:29:25 -07003569 sb.setLength(0);
3570 sb.append(prefix);
3571 sb.append(" Bluetooth Power drain: ").append(BatteryStatsHelper.makemAh(
3572 getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which) /
3573 (double)(1000*60*60)));
Adam Lesinski049c88b2015-05-28 11:38:12 -07003574 sb.append("mAh");
Adam Lesinskie283d332015-04-16 12:29:25 -07003575 pw.println(sb.toString());
3576
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003577 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003578
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003579 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003580 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003581 pw.print(prefix); pw.println(" Device is currently unplugged");
3582 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3583 pw.println(getDischargeStartLevel());
3584 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3585 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003586 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003587 pw.print(prefix); pw.println(" Device is currently plugged into power");
3588 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3589 pw.println(getDischargeStartLevel());
3590 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3591 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003592 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003593 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3594 pw.println(getDischargeAmountScreenOn());
3595 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3596 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003597 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003598 } else {
3599 pw.print(prefix); pw.println(" Device battery use since last full charge");
3600 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3601 pw.println(getLowDischargeAmountSinceCharge());
3602 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3603 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003604 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3605 pw.println(getDischargeAmountScreenOnSinceCharge());
3606 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3607 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003608 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003609 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003610
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003611 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003612 helper.create(this);
3613 helper.refreshStats(which, UserHandle.USER_ALL);
3614 List<BatterySipper> sippers = helper.getUsageList();
3615 if (sippers != null && sippers.size() > 0) {
3616 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3617 pw.print(prefix); pw.print(" Capacity: ");
3618 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003619 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003620 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3621 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3622 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3623 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003624 pw.println();
3625 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003626 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003627 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003628 switch (bs.drainType) {
3629 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003630 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003631 break;
3632 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003633 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003634 break;
3635 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003636 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003637 break;
3638 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003639 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003640 break;
3641 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003642 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003643 break;
3644 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003645 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003646 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003647 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003648 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003649 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003650 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003651 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003652 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003653 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003654 break;
3655 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003656 pw.print(" User "); pw.print(bs.userId);
3657 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003658 break;
3659 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003660 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003661 break;
3662 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003663 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003664 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003665 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003666 pw.print(" Camera: ");
3667 break;
3668 default:
3669 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003670 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003671 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003672 printmAh(pw, bs.totalPowerMah);
3673
Adam Lesinski57123002015-06-12 16:12:07 -07003674 if (bs.usagePowerMah != bs.totalPowerMah) {
3675 // If the usage (generic power) isn't the whole amount, we list out
3676 // what components are involved in the calculation.
3677
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003678 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003679 if (bs.usagePowerMah != 0) {
3680 pw.print(" usage=");
3681 printmAh(pw, bs.usagePowerMah);
3682 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003683 if (bs.cpuPowerMah != 0) {
3684 pw.print(" cpu=");
3685 printmAh(pw, bs.cpuPowerMah);
3686 }
3687 if (bs.wakeLockPowerMah != 0) {
3688 pw.print(" wake=");
3689 printmAh(pw, bs.wakeLockPowerMah);
3690 }
3691 if (bs.mobileRadioPowerMah != 0) {
3692 pw.print(" radio=");
3693 printmAh(pw, bs.mobileRadioPowerMah);
3694 }
3695 if (bs.wifiPowerMah != 0) {
3696 pw.print(" wifi=");
3697 printmAh(pw, bs.wifiPowerMah);
3698 }
3699 if (bs.gpsPowerMah != 0) {
3700 pw.print(" gps=");
3701 printmAh(pw, bs.gpsPowerMah);
3702 }
3703 if (bs.sensorPowerMah != 0) {
3704 pw.print(" sensor=");
3705 printmAh(pw, bs.sensorPowerMah);
3706 }
3707 if (bs.cameraPowerMah != 0) {
3708 pw.print(" camera=");
3709 printmAh(pw, bs.cameraPowerMah);
3710 }
3711 if (bs.flashlightPowerMah != 0) {
3712 pw.print(" flash=");
3713 printmAh(pw, bs.flashlightPowerMah);
3714 }
3715 pw.print(" )");
3716 }
3717 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003718 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003719 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003720 }
3721
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003722 sippers = helper.getMobilemsppList();
3723 if (sippers != null && sippers.size() > 0) {
3724 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003725 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003726 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003727 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003728 sb.setLength(0);
3729 sb.append(prefix); sb.append(" Uid ");
3730 UserHandle.formatUid(sb, bs.uidObj.getUid());
3731 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3732 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3733 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003734 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003735 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003736 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003737 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003738 sb.setLength(0);
3739 sb.append(prefix);
3740 sb.append(" TOTAL TIME: ");
3741 formatTimeMs(sb, totalTime);
3742 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3743 sb.append(")");
3744 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003745 pw.println();
3746 }
3747
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003748 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3749 @Override
3750 public int compare(TimerEntry lhs, TimerEntry rhs) {
3751 long lhsTime = lhs.mTime;
3752 long rhsTime = rhs.mTime;
3753 if (lhsTime < rhsTime) {
3754 return 1;
3755 }
3756 if (lhsTime > rhsTime) {
3757 return -1;
3758 }
3759 return 0;
3760 }
3761 };
3762
3763 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003764 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3765 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003766 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003767 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3768 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3769 : kernelWakelocks.entrySet()) {
3770 final BatteryStats.Timer timer = ent.getValue();
3771 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003772 if (totalTimeMillis > 0) {
3773 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3774 }
3775 }
3776 if (ktimers.size() > 0) {
3777 Collections.sort(ktimers, timerComparator);
3778 pw.print(prefix); pw.println(" All kernel wake locks:");
3779 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003780 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003781 String linePrefix = ": ";
3782 sb.setLength(0);
3783 sb.append(prefix);
3784 sb.append(" Kernel Wake lock ");
3785 sb.append(timer.mName);
3786 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3787 which, linePrefix);
3788 if (!linePrefix.equals(": ")) {
3789 sb.append(" realtime");
3790 // Only print out wake locks that were held
3791 pw.println(sb.toString());
3792 }
3793 }
3794 pw.println();
3795 }
3796 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003797
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003798 if (timers.size() > 0) {
3799 Collections.sort(timers, timerComparator);
3800 pw.print(prefix); pw.println(" All partial wake locks:");
3801 for (int i=0; i<timers.size(); i++) {
3802 TimerEntry timer = timers.get(i);
3803 sb.setLength(0);
3804 sb.append(" Wake lock ");
3805 UserHandle.formatUid(sb, timer.mId);
3806 sb.append(" ");
3807 sb.append(timer.mName);
3808 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3809 sb.append(" realtime");
3810 pw.println(sb.toString());
3811 }
3812 timers.clear();
3813 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003814 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003815
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003816 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003817 if (wakeupReasons.size() > 0) {
3818 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003819 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003820 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003821 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003822 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3823 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003824 }
3825 Collections.sort(reasons, timerComparator);
3826 for (int i=0; i<reasons.size(); i++) {
3827 TimerEntry timer = reasons.get(i);
3828 String linePrefix = ": ";
3829 sb.setLength(0);
3830 sb.append(prefix);
3831 sb.append(" Wakeup reason ");
3832 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003833 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3834 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003835 pw.println(sb.toString());
3836 }
3837 pw.println();
3838 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003839 }
Evan Millar22ac0432009-03-31 11:33:18 -07003840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 for (int iu=0; iu<NU; iu++) {
3842 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003843 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003844 continue;
3845 }
3846
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003847 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003848
3849 pw.print(prefix);
3850 pw.print(" ");
3851 UserHandle.formatUid(pw, uid);
3852 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003854
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003855 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3856 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3857 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3858 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003859 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3860 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
3861
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003862 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3863 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003864 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3865 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003866
3867 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3868 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
3869
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003870 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3871 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3872 final int wifiScanCount = u.getWifiScanCount(which);
3873 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003874
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003875 if (mobileRxBytes > 0 || mobileTxBytes > 0
3876 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003877 pw.print(prefix); pw.print(" Mobile network: ");
3878 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003879 pw.print(formatBytesLocked(mobileTxBytes));
3880 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3881 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003883 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3884 sb.setLength(0);
3885 sb.append(prefix); sb.append(" Mobile radio active: ");
3886 formatTimeMs(sb, uidMobileActiveTime / 1000);
3887 sb.append("(");
3888 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3889 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3890 long packets = mobileRxPackets + mobileTxPackets;
3891 if (packets == 0) {
3892 packets = 1;
3893 }
3894 sb.append(" @ ");
3895 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3896 sb.append(" mspp");
3897 pw.println(sb.toString());
3898 }
3899
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003900 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003901 pw.print(prefix); pw.print(" Wi-Fi network: ");
3902 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003903 pw.print(formatBytesLocked(wifiTxBytes));
3904 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3905 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003906 }
3907
Dianne Hackborn62793e42015-03-09 11:15:41 -07003908 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003909 || uidWifiRunningTime != 0) {
3910 sb.setLength(0);
3911 sb.append(prefix); sb.append(" Wifi Running: ");
3912 formatTimeMs(sb, uidWifiRunningTime / 1000);
3913 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3914 whichBatteryRealtime)); sb.append(")\n");
3915 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3916 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3917 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3918 whichBatteryRealtime)); sb.append(")\n");
3919 sb.append(prefix); sb.append(" Wifi Scan: ");
3920 formatTimeMs(sb, wifiScanTime / 1000);
3921 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07003922 whichBatteryRealtime)); sb.append(") ");
3923 sb.append(wifiScanCount);
3924 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003925 pw.println(sb.toString());
3926 }
3927
Adam Lesinski049c88b2015-05-28 11:38:12 -07003928 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3929 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3930 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
3931 final long uidWifiTotalTimeMs = uidWifiIdleTimeMs + uidWifiRxTimeMs + uidWifiTxTimeMs;
3932 if (uidWifiTotalTimeMs > 0) {
3933 sb.setLength(0);
3934 sb.append(prefix).append(" WiFi Idle time: ");
3935 formatTimeMs(sb, uidWifiIdleTimeMs);
3936 sb.append("(").append(formatRatioLocked(uidWifiIdleTimeMs, uidWifiTotalTimeMs))
3937 .append(")\n");
3938
3939 sb.append(prefix).append(" WiFi Rx time: "); formatTimeMs(sb, uidWifiRxTimeMs);
3940 sb.append("(").append(formatRatioLocked(uidWifiRxTimeMs, uidWifiTotalTimeMs))
3941 .append(")\n");
3942
3943 sb.append(prefix).append(" WiFi Tx time: "); formatTimeMs(sb, uidWifiTxTimeMs);
3944 sb.append("(").append(formatRatioLocked(uidWifiTxTimeMs, uidWifiTotalTimeMs))
3945 .append(")");
3946 pw.println(sb.toString());
3947 }
3948
Adam Lesinski50e47602015-12-04 17:04:54 -08003949 if (btRxBytes > 0 || btTxBytes > 0) {
3950 pw.print(prefix); pw.print(" Bluetooth network: ");
3951 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
3952 pw.print(formatBytesLocked(btTxBytes));
3953 pw.println(" sent");
3954 }
3955
3956 final long uidBtIdleTimeMs = u.getBluetoothControllerActivity(CONTROLLER_IDLE_TIME,
3957 which);
3958 final long uidBtRxTimeMs = u.getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
3959 final long uidBtTxTimeMs = u.getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
3960 final long uidBtTotalTimeMs = uidBtIdleTimeMs + uidBtRxTimeMs + uidBtTxTimeMs;
3961 if (uidBtTotalTimeMs > 0) {
3962 sb.setLength(0);
3963 sb.append(prefix).append(" Bluetooth Idle time: ");
3964 formatTimeMs(sb, uidBtIdleTimeMs);
3965 sb.append("(").append(formatRatioLocked(uidBtIdleTimeMs, uidBtTotalTimeMs))
3966 .append(")\n");
3967
3968 sb.append(prefix).append(" Bluetooth Rx time: ");
3969 formatTimeMs(sb, uidBtRxTimeMs);
3970 sb.append("(").append(formatRatioLocked(uidBtRxTimeMs, uidBtTotalTimeMs))
3971 .append(")\n");
3972
3973 sb.append(prefix).append(" Bluetooth Tx time: ");
3974 formatTimeMs(sb, uidBtTxTimeMs);
3975 sb.append("(").append(formatRatioLocked(uidBtTxTimeMs, uidBtTotalTimeMs))
3976 .append(")");
3977 pw.println(sb.toString());
3978 }
3979
Dianne Hackborn617f8772009-03-31 15:04:46 -07003980 if (u.hasUserActivity()) {
3981 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07003982 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003983 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003984 if (val != 0) {
3985 if (!hasData) {
3986 sb.setLength(0);
3987 sb.append(" User activity: ");
3988 hasData = true;
3989 } else {
3990 sb.append(", ");
3991 }
3992 sb.append(val);
3993 sb.append(" ");
3994 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3995 }
3996 }
3997 if (hasData) {
3998 pw.println(sb.toString());
3999 }
4000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004002 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4003 = u.getWakelockStats();
4004 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004005 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004006 int countWakelock = 0;
4007 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4008 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4009 String linePrefix = ": ";
4010 sb.setLength(0);
4011 sb.append(prefix);
4012 sb.append(" Wake lock ");
4013 sb.append(wakelocks.keyAt(iw));
4014 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4015 "full", which, linePrefix);
4016 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4017 "partial", which, linePrefix);
4018 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4019 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004020 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4021 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004022 sb.append(" realtime");
4023 pw.println(sb.toString());
4024 uidActivity = true;
4025 countWakelock++;
4026
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004027 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4028 rawRealtime, which);
4029 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4030 rawRealtime, which);
4031 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4032 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004033 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004034 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004035 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004036 if (countWakelock > 1) {
4037 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4038 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004039 sb.setLength(0);
4040 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004041 sb.append(" TOTAL wake: ");
4042 boolean needComma = false;
4043 if (totalFullWakelock != 0) {
4044 needComma = true;
4045 formatTimeMs(sb, totalFullWakelock);
4046 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004047 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004048 if (totalPartialWakelock != 0) {
4049 if (needComma) {
4050 sb.append(", ");
4051 }
4052 needComma = true;
4053 formatTimeMs(sb, totalPartialWakelock);
4054 sb.append("partial");
4055 }
4056 if (totalWindowWakelock != 0) {
4057 if (needComma) {
4058 sb.append(", ");
4059 }
4060 needComma = true;
4061 formatTimeMs(sb, totalWindowWakelock);
4062 sb.append("window");
4063 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004064 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004065 if (needComma) {
4066 sb.append(",");
4067 }
4068 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004069 formatTimeMs(sb, totalDrawWakelock);
4070 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004071 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004072 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004073 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004074 }
4075 }
4076
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004077 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4078 for (int isy=syncs.size()-1; isy>=0; isy--) {
4079 final Timer timer = syncs.valueAt(isy);
4080 // Convert from microseconds to milliseconds with rounding
4081 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4082 final int count = timer.getCountLocked(which);
4083 sb.setLength(0);
4084 sb.append(prefix);
4085 sb.append(" Sync ");
4086 sb.append(syncs.keyAt(isy));
4087 sb.append(": ");
4088 if (totalTime != 0) {
4089 formatTimeMs(sb, totalTime);
4090 sb.append("realtime (");
4091 sb.append(count);
4092 sb.append(" times)");
4093 } else {
4094 sb.append("(not used)");
4095 }
4096 pw.println(sb.toString());
4097 uidActivity = true;
4098 }
4099
4100 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4101 for (int ij=jobs.size()-1; ij>=0; ij--) {
4102 final Timer timer = jobs.valueAt(ij);
4103 // Convert from microseconds to milliseconds with rounding
4104 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4105 final int count = timer.getCountLocked(which);
4106 sb.setLength(0);
4107 sb.append(prefix);
4108 sb.append(" Job ");
4109 sb.append(jobs.keyAt(ij));
4110 sb.append(": ");
4111 if (totalTime != 0) {
4112 formatTimeMs(sb, totalTime);
4113 sb.append("realtime (");
4114 sb.append(count);
4115 sb.append(" times)");
4116 } else {
4117 sb.append("(not used)");
4118 }
4119 pw.println(sb.toString());
4120 uidActivity = true;
4121 }
4122
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004123 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4124 prefix, "Flashlight");
4125 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4126 prefix, "Camera");
4127 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4128 prefix, "Video");
4129 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4130 prefix, "Audio");
4131
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004132 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4133 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004134 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 final Uid.Sensor se = sensors.valueAt(ise);
4136 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004137 sb.setLength(0);
4138 sb.append(prefix);
4139 sb.append(" Sensor ");
4140 int handle = se.getHandle();
4141 if (handle == Uid.Sensor.GPS) {
4142 sb.append("GPS");
4143 } else {
4144 sb.append(handle);
4145 }
4146 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004148 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004149 if (timer != null) {
4150 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004151 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004152 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004153 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004154 //timer.logState();
4155 if (totalTime != 0) {
4156 formatTimeMs(sb, totalTime);
4157 sb.append("realtime (");
4158 sb.append(count);
4159 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 } else {
4161 sb.append("(not used)");
4162 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004163 } else {
4164 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004166
4167 pw.println(sb.toString());
4168 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 }
4170
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004171 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4172 "Vibrator");
4173 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4174 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004175
Dianne Hackborn61659e52014-07-09 16:13:01 -07004176 long totalStateTime = 0;
4177 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4178 long time = u.getProcessStateTime(ips, rawRealtime, which);
4179 if (time > 0) {
4180 totalStateTime += time;
4181 sb.setLength(0);
4182 sb.append(prefix);
4183 sb.append(" ");
4184 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4185 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004186 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004187 pw.println(sb.toString());
4188 uidActivity = true;
4189 }
4190 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004191 if (totalStateTime > 0) {
4192 sb.setLength(0);
4193 sb.append(prefix);
4194 sb.append(" Total running: ");
4195 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4196 pw.println(sb.toString());
4197 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004198
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004199 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4200 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004201 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4202 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004203 sb.setLength(0);
4204 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004205 sb.append(" Total cpu time: u=");
4206 formatTimeMs(sb, userCpuTimeUs / 1000);
4207 sb.append("s=");
4208 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004209 sb.append("p=");
4210 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4211 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004212 pw.println(sb.toString());
4213 }
4214
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004215 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4216 = u.getProcessStats();
4217 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4218 final Uid.Proc ps = processStats.valueAt(ipr);
4219 long userTime;
4220 long systemTime;
4221 long foregroundTime;
4222 int starts;
4223 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004225 userTime = ps.getUserTime(which);
4226 systemTime = ps.getSystemTime(which);
4227 foregroundTime = ps.getForegroundTime(which);
4228 starts = ps.getStarts(which);
4229 final int numCrashes = ps.getNumCrashes(which);
4230 final int numAnrs = ps.getNumAnrs(which);
4231 numExcessive = which == STATS_SINCE_CHARGED
4232 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004234 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4235 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4236 sb.setLength(0);
4237 sb.append(prefix); sb.append(" Proc ");
4238 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4239 sb.append(prefix); sb.append(" CPU: ");
4240 formatTimeMs(sb, userTime); sb.append("usr + ");
4241 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4242 formatTimeMs(sb, foregroundTime); sb.append("fg");
4243 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4244 sb.append("\n"); sb.append(prefix); sb.append(" ");
4245 boolean hasOne = false;
4246 if (starts != 0) {
4247 hasOne = true;
4248 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004249 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004250 if (numCrashes != 0) {
4251 if (hasOne) {
4252 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004253 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004254 hasOne = true;
4255 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004256 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004257 if (numAnrs != 0) {
4258 if (hasOne) {
4259 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004261 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 }
4263 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004264 pw.println(sb.toString());
4265 for (int e=0; e<numExcessive; e++) {
4266 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4267 if (ew != null) {
4268 pw.print(prefix); pw.print(" * Killed for ");
4269 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4270 pw.print("wake lock");
4271 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4272 pw.print("cpu");
4273 } else {
4274 pw.print("unknown");
4275 }
4276 pw.print(" use: ");
4277 TimeUtils.formatDuration(ew.usedTime, pw);
4278 pw.print(" over ");
4279 TimeUtils.formatDuration(ew.overTime, pw);
4280 if (ew.overTime != 0) {
4281 pw.print(" (");
4282 pw.print((ew.usedTime*100)/ew.overTime);
4283 pw.println("%)");
4284 }
4285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
4287 uidActivity = true;
4288 }
4289 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004290
4291 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4292 = u.getPackageStats();
4293 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4294 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4295 pw.println(":");
4296 boolean apkActivity = false;
4297 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4298 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4299 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4300 pw.print(prefix); pw.print(" Wakeup alarm ");
4301 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4302 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4303 pw.println(" times");
4304 apkActivity = true;
4305 }
4306 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4307 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4308 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4309 final long startTime = ss.getStartTime(batteryUptime, which);
4310 final int starts = ss.getStarts(which);
4311 final int launches = ss.getLaunches(which);
4312 if (startTime != 0 || starts != 0 || launches != 0) {
4313 sb.setLength(0);
4314 sb.append(prefix); sb.append(" Service ");
4315 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4316 sb.append(prefix); sb.append(" Created for: ");
4317 formatTimeMs(sb, startTime / 1000);
4318 sb.append("uptime\n");
4319 sb.append(prefix); sb.append(" Starts: ");
4320 sb.append(starts);
4321 sb.append(", launches: "); sb.append(launches);
4322 pw.println(sb.toString());
4323 apkActivity = true;
4324 }
4325 }
4326 if (!apkActivity) {
4327 pw.print(prefix); pw.println(" (nothing executed)");
4328 }
4329 uidActivity = true;
4330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004332 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 }
4334 }
4335 }
4336
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004337 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004338 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004339 int diff = oldval ^ newval;
4340 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004341 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004342 for (int i=0; i<descriptions.length; i++) {
4343 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004344 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004345 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004346 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004347 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004348 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004349 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4350 didWake = true;
4351 pw.print("=");
4352 if (longNames) {
4353 UserHandle.formatUid(pw, wakelockTag.uid);
4354 pw.print(":\"");
4355 pw.print(wakelockTag.string);
4356 pw.print("\"");
4357 } else {
4358 pw.print(wakelockTag.poolIdx);
4359 }
4360 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004361 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004362 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004363 pw.print("=");
4364 int val = (newval&bd.mask)>>bd.shift;
4365 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004366 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004367 } else {
4368 pw.print(val);
4369 }
4370 }
4371 }
4372 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004373 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004374 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004375 if (longNames) {
4376 UserHandle.formatUid(pw, wakelockTag.uid);
4377 pw.print(":\"");
4378 pw.print(wakelockTag.string);
4379 pw.print("\"");
4380 } else {
4381 pw.print(wakelockTag.poolIdx);
4382 }
4383 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004384 }
4385
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004386 public void prepareForDumpLocked() {
4387 }
4388
4389 public static class HistoryPrinter {
4390 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004391 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004392 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004393 int oldStatus = -1;
4394 int oldHealth = -1;
4395 int oldPlug = -1;
4396 int oldTemp = -1;
4397 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004398 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004399
Dianne Hackborn3251b902014-06-20 14:40:53 -07004400 void reset() {
4401 oldState = oldState2 = 0;
4402 oldLevel = -1;
4403 oldStatus = -1;
4404 oldHealth = -1;
4405 oldPlug = -1;
4406 oldTemp = -1;
4407 oldVolt = -1;
4408 }
4409
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004410 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004411 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004412 if (!checkin) {
4413 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004414 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004415 pw.print(" (");
4416 pw.print(rec.numReadInts);
4417 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004418 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004419 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4420 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004421 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004422 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004423 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004424 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004425 }
4426 lastTime = rec.time;
4427 }
4428 if (rec.cmd == HistoryItem.CMD_START) {
4429 if (checkin) {
4430 pw.print(":");
4431 }
4432 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004433 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004434 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4435 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004436 if (checkin) {
4437 pw.print(":");
4438 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004439 if (rec.cmd == HistoryItem.CMD_RESET) {
4440 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004441 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004442 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004443 pw.print("TIME:");
4444 if (checkin) {
4445 pw.println(rec.currentTime);
4446 } else {
4447 pw.print(" ");
4448 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4449 rec.currentTime).toString());
4450 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004451 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4452 if (checkin) {
4453 pw.print(":");
4454 }
4455 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004456 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4457 if (checkin) {
4458 pw.print(":");
4459 }
4460 pw.println("*OVERFLOW*");
4461 } else {
4462 if (!checkin) {
4463 if (rec.batteryLevel < 10) pw.print("00");
4464 else if (rec.batteryLevel < 100) pw.print("0");
4465 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004466 if (verbose) {
4467 pw.print(" ");
4468 if (rec.states < 0) ;
4469 else if (rec.states < 0x10) pw.print("0000000");
4470 else if (rec.states < 0x100) pw.print("000000");
4471 else if (rec.states < 0x1000) pw.print("00000");
4472 else if (rec.states < 0x10000) pw.print("0000");
4473 else if (rec.states < 0x100000) pw.print("000");
4474 else if (rec.states < 0x1000000) pw.print("00");
4475 else if (rec.states < 0x10000000) pw.print("0");
4476 pw.print(Integer.toHexString(rec.states));
4477 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004478 } else {
4479 if (oldLevel != rec.batteryLevel) {
4480 oldLevel = rec.batteryLevel;
4481 pw.print(",Bl="); pw.print(rec.batteryLevel);
4482 }
4483 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004484 if (oldStatus != rec.batteryStatus) {
4485 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004486 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004487 switch (oldStatus) {
4488 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004489 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004490 break;
4491 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004492 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004493 break;
4494 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004495 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004496 break;
4497 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004498 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004499 break;
4500 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004501 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004502 break;
4503 default:
4504 pw.print(oldStatus);
4505 break;
4506 }
4507 }
4508 if (oldHealth != rec.batteryHealth) {
4509 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004510 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004511 switch (oldHealth) {
4512 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004513 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004514 break;
4515 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004516 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004517 break;
4518 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004519 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004520 break;
4521 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004522 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004523 break;
4524 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004525 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004526 break;
4527 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004528 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004529 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004530 case BatteryManager.BATTERY_HEALTH_COLD:
4531 pw.print(checkin ? "c" : "cold");
4532 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004533 default:
4534 pw.print(oldHealth);
4535 break;
4536 }
4537 }
4538 if (oldPlug != rec.batteryPlugType) {
4539 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004540 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004541 switch (oldPlug) {
4542 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004543 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004544 break;
4545 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004546 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004547 break;
4548 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004549 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004550 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004551 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004552 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004553 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004554 default:
4555 pw.print(oldPlug);
4556 break;
4557 }
4558 }
4559 if (oldTemp != rec.batteryTemperature) {
4560 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004561 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004562 pw.print(oldTemp);
4563 }
4564 if (oldVolt != rec.batteryVoltage) {
4565 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004566 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004567 pw.print(oldVolt);
4568 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004569 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004570 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004571 printBitDescriptions(pw, oldState2, rec.states2, null,
4572 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004573 if (rec.wakeReasonTag != null) {
4574 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004575 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004576 pw.print(rec.wakeReasonTag.poolIdx);
4577 } else {
4578 pw.print(" wake_reason=");
4579 pw.print(rec.wakeReasonTag.uid);
4580 pw.print(":\"");
4581 pw.print(rec.wakeReasonTag.string);
4582 pw.print("\"");
4583 }
4584 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004585 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004586 pw.print(checkin ? "," : " ");
4587 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4588 pw.print("+");
4589 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4590 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004591 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004592 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4593 : HISTORY_EVENT_NAMES;
4594 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4595 | HistoryItem.EVENT_FLAG_FINISH);
4596 if (idx >= 0 && idx < eventNames.length) {
4597 pw.print(eventNames[idx]);
4598 } else {
4599 pw.print(checkin ? "Ev" : "event");
4600 pw.print(idx);
4601 }
4602 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004603 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004604 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004605 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004606 UserHandle.formatUid(pw, rec.eventTag.uid);
4607 pw.print(":\"");
4608 pw.print(rec.eventTag.string);
4609 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004610 }
4611 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004612 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004613 if (rec.stepDetails != null) {
4614 if (!checkin) {
4615 pw.print(" Details: cpu=");
4616 pw.print(rec.stepDetails.userTime);
4617 pw.print("u+");
4618 pw.print(rec.stepDetails.systemTime);
4619 pw.print("s");
4620 if (rec.stepDetails.appCpuUid1 >= 0) {
4621 pw.print(" (");
4622 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4623 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4624 if (rec.stepDetails.appCpuUid2 >= 0) {
4625 pw.print(", ");
4626 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4627 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4628 }
4629 if (rec.stepDetails.appCpuUid3 >= 0) {
4630 pw.print(", ");
4631 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4632 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4633 }
4634 pw.print(')');
4635 }
4636 pw.println();
4637 pw.print(" /proc/stat=");
4638 pw.print(rec.stepDetails.statUserTime);
4639 pw.print(" usr, ");
4640 pw.print(rec.stepDetails.statSystemTime);
4641 pw.print(" sys, ");
4642 pw.print(rec.stepDetails.statIOWaitTime);
4643 pw.print(" io, ");
4644 pw.print(rec.stepDetails.statIrqTime);
4645 pw.print(" irq, ");
4646 pw.print(rec.stepDetails.statSoftIrqTime);
4647 pw.print(" sirq, ");
4648 pw.print(rec.stepDetails.statIdlTime);
4649 pw.print(" idle");
4650 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4651 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4652 + rec.stepDetails.statSoftIrqTime;
4653 int total = totalRun + rec.stepDetails.statIdlTime;
4654 if (total > 0) {
4655 pw.print(" (");
4656 float perc = ((float)totalRun) / ((float)total) * 100;
4657 pw.print(String.format("%.1f%%", perc));
4658 pw.print(" of ");
4659 StringBuilder sb = new StringBuilder(64);
4660 formatTimeMsNoSpace(sb, total*10);
4661 pw.print(sb);
4662 pw.print(")");
4663 }
4664 pw.println();
4665 } else {
4666 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4667 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4668 pw.print(rec.stepDetails.userTime);
4669 pw.print(":");
4670 pw.print(rec.stepDetails.systemTime);
4671 if (rec.stepDetails.appCpuUid1 >= 0) {
4672 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4673 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4674 if (rec.stepDetails.appCpuUid2 >= 0) {
4675 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4676 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4677 }
4678 if (rec.stepDetails.appCpuUid3 >= 0) {
4679 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4680 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4681 }
4682 }
4683 pw.println();
4684 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4685 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4686 pw.print(rec.stepDetails.statUserTime);
4687 pw.print(',');
4688 pw.print(rec.stepDetails.statSystemTime);
4689 pw.print(',');
4690 pw.print(rec.stepDetails.statIOWaitTime);
4691 pw.print(',');
4692 pw.print(rec.stepDetails.statIrqTime);
4693 pw.print(',');
4694 pw.print(rec.stepDetails.statSoftIrqTime);
4695 pw.print(',');
4696 pw.print(rec.stepDetails.statIdlTime);
4697 pw.println();
4698 }
4699 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004700 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004701 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004702 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004703 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004704
4705 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4706 UserHandle.formatUid(pw, uid);
4707 pw.print("=");
4708 pw.print(utime);
4709 pw.print("u+");
4710 pw.print(stime);
4711 pw.print("s");
4712 }
4713
4714 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4715 pw.print('/');
4716 pw.print(uid);
4717 pw.print(":");
4718 pw.print(utime);
4719 pw.print(":");
4720 pw.print(stime);
4721 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004722 }
4723
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004724 private void printSizeValue(PrintWriter pw, long size) {
4725 float result = size;
4726 String suffix = "";
4727 if (result >= 10*1024) {
4728 suffix = "KB";
4729 result = result / 1024;
4730 }
4731 if (result >= 10*1024) {
4732 suffix = "MB";
4733 result = result / 1024;
4734 }
4735 if (result >= 10*1024) {
4736 suffix = "GB";
4737 result = result / 1024;
4738 }
4739 if (result >= 10*1024) {
4740 suffix = "TB";
4741 result = result / 1024;
4742 }
4743 if (result >= 10*1024) {
4744 suffix = "PB";
4745 result = result / 1024;
4746 }
4747 pw.print((int)result);
4748 pw.print(suffix);
4749 }
4750
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004751 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4752 String label3, long estimatedTime) {
4753 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004754 return false;
4755 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004756 pw.print(label1);
4757 pw.print(label2);
4758 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004759 StringBuilder sb = new StringBuilder(64);
4760 formatTimeMs(sb, estimatedTime);
4761 pw.print(sb);
4762 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004763 return true;
4764 }
4765
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004766 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4767 LevelStepTracker steps, boolean checkin) {
4768 if (steps == null) {
4769 return false;
4770 }
4771 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004772 if (count <= 0) {
4773 return false;
4774 }
4775 if (!checkin) {
4776 pw.println(header);
4777 }
Kweku Adams030980a2015-04-01 16:07:48 -07004778 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004779 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004780 long duration = steps.getDurationAt(i);
4781 int level = steps.getLevelAt(i);
4782 long initMode = steps.getInitModeAt(i);
4783 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004784 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004785 lineArgs[0] = Long.toString(duration);
4786 lineArgs[1] = Integer.toString(level);
4787 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4788 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4789 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4790 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4791 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4792 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004793 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004794 }
4795 } else {
4796 lineArgs[2] = "";
4797 }
4798 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4799 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4800 } else {
4801 lineArgs[3] = "";
4802 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004803 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004804 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004805 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004806 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004807 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004808 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4809 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004810 pw.print(prefix);
4811 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004812 TimeUtils.formatDuration(duration, pw);
4813 pw.print(" to "); pw.print(level);
4814 boolean haveModes = false;
4815 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4816 pw.print(" (");
4817 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4818 case Display.STATE_OFF: pw.print("screen-off"); break;
4819 case Display.STATE_ON: pw.print("screen-on"); break;
4820 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4821 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004822 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004823 }
4824 haveModes = true;
4825 }
4826 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4827 pw.print(haveModes ? ", " : " (");
4828 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4829 ? "power-save-on" : "power-save-off");
4830 haveModes = true;
4831 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004832 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4833 pw.print(haveModes ? ", " : " (");
4834 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4835 ? "device-idle-on" : "device-idle-off");
4836 haveModes = true;
4837 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004838 if (haveModes) {
4839 pw.print(")");
4840 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004841 pw.println();
4842 }
4843 }
4844 return true;
4845 }
4846
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004847 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004848 public static final int DUMP_DAILY_ONLY = 1<<2;
4849 public static final int DUMP_HISTORY_ONLY = 1<<3;
4850 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4851 public static final int DUMP_VERBOSE = 1<<5;
4852 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004853
Dianne Hackborn37de0982014-05-09 09:32:18 -07004854 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4855 final HistoryPrinter hprinter = new HistoryPrinter();
4856 final HistoryItem rec = new HistoryItem();
4857 long lastTime = -1;
4858 long baseTime = -1;
4859 boolean printed = false;
4860 HistoryEventTracker tracker = null;
4861 while (getNextHistoryLocked(rec)) {
4862 lastTime = rec.time;
4863 if (baseTime < 0) {
4864 baseTime = lastTime;
4865 }
4866 if (rec.time >= histStart) {
4867 if (histStart >= 0 && !printed) {
4868 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004869 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004870 || rec.cmd == HistoryItem.CMD_START
4871 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004872 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004873 hprinter.printNextItem(pw, rec, baseTime, checkin,
4874 (flags&DUMP_VERBOSE) != 0);
4875 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004876 } else if (rec.currentTime != 0) {
4877 printed = true;
4878 byte cmd = rec.cmd;
4879 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004880 hprinter.printNextItem(pw, rec, baseTime, checkin,
4881 (flags&DUMP_VERBOSE) != 0);
4882 rec.cmd = cmd;
4883 }
4884 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004885 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4886 hprinter.printNextItem(pw, rec, baseTime, checkin,
4887 (flags&DUMP_VERBOSE) != 0);
4888 rec.cmd = HistoryItem.CMD_UPDATE;
4889 }
4890 int oldEventCode = rec.eventCode;
4891 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004892 rec.eventTag = new HistoryTag();
4893 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4894 HashMap<String, SparseIntArray> active
4895 = tracker.getStateForEvent(i);
4896 if (active == null) {
4897 continue;
4898 }
4899 for (HashMap.Entry<String, SparseIntArray> ent
4900 : active.entrySet()) {
4901 SparseIntArray uids = ent.getValue();
4902 for (int j=0; j<uids.size(); j++) {
4903 rec.eventCode = i;
4904 rec.eventTag.string = ent.getKey();
4905 rec.eventTag.uid = uids.keyAt(j);
4906 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07004907 hprinter.printNextItem(pw, rec, baseTime, checkin,
4908 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004909 rec.wakeReasonTag = null;
4910 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004911 }
4912 }
4913 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004914 rec.eventCode = oldEventCode;
4915 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004916 tracker = null;
4917 }
4918 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004919 hprinter.printNextItem(pw, rec, baseTime, checkin,
4920 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004921 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
4922 // This is an attempt to aggregate the previous state and generate
4923 // fake events to reflect that state at the point where we start
4924 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07004925 if (tracker == null) {
4926 tracker = new HistoryEventTracker();
4927 }
4928 tracker.updateState(rec.eventCode, rec.eventTag.string,
4929 rec.eventTag.uid, rec.eventTag.poolIdx);
4930 }
4931 }
4932 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004933 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004934 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
4935 }
4936 }
4937
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004938 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
4939 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
4940 if (steps == null) {
4941 return;
4942 }
4943 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
4944 if (timeRemaining >= 0) {
4945 pw.print(prefix); pw.print(label); pw.print(" total time: ");
4946 tmpSb.setLength(0);
4947 formatTimeMs(tmpSb, timeRemaining);
4948 pw.print(tmpSb);
4949 pw.print(" (from "); pw.print(tmpOutInt[0]);
4950 pw.println(" steps)");
4951 }
4952 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4953 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4954 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
4955 if (estimatedTime > 0) {
4956 pw.print(prefix); pw.print(label); pw.print(" ");
4957 pw.print(STEP_LEVEL_MODE_LABELS[i]);
4958 pw.print(" time: ");
4959 tmpSb.setLength(0);
4960 formatTimeMs(tmpSb, estimatedTime);
4961 pw.print(tmpSb);
4962 pw.print(" (from "); pw.print(tmpOutInt[0]);
4963 pw.println(" steps)");
4964 }
4965 }
4966 }
4967
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004968 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
4969 ArrayList<PackageChange> changes) {
4970 if (changes == null) {
4971 return;
4972 }
4973 pw.print(prefix); pw.println("Package changes:");
4974 for (int i=0; i<changes.size(); i++) {
4975 PackageChange pc = changes.get(i);
4976 if (pc.mUpdate) {
4977 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
4978 pw.print(" vers="); pw.println(pc.mVersionCode);
4979 } else {
4980 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
4981 }
4982 }
4983 }
4984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 /**
4986 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
4987 *
4988 * @param pw a Printer to receive the dump output.
4989 */
4990 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004991 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004992 prepareForDumpLocked();
4993
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004994 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004995 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004996
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004997 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004998 final long historyTotalSize = getHistoryTotalSize();
4999 final long historyUsedSize = getHistoryUsedSize();
5000 if (startIteratingHistoryLocked()) {
5001 try {
5002 pw.print("Battery History (");
5003 pw.print((100*historyUsedSize)/historyTotalSize);
5004 pw.print("% used, ");
5005 printSizeValue(pw, historyUsedSize);
5006 pw.print(" used of ");
5007 printSizeValue(pw, historyTotalSize);
5008 pw.print(", ");
5009 pw.print(getHistoryStringPoolSize());
5010 pw.print(" strings using ");
5011 printSizeValue(pw, getHistoryStringPoolBytes());
5012 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005013 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005014 pw.println();
5015 } finally {
5016 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005017 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005018 }
5019
5020 if (startIteratingOldHistoryLocked()) {
5021 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005022 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005023 pw.println("Old battery History:");
5024 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005025 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005026 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005027 if (baseTime < 0) {
5028 baseTime = rec.time;
5029 }
5030 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005031 }
5032 pw.println();
5033 } finally {
5034 finishIteratingOldHistoryLocked();
5035 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005036 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005037 }
5038
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005039 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005040 return;
5041 }
5042
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005043 if (!filtering) {
5044 SparseArray<? extends Uid> uidStats = getUidStats();
5045 final int NU = uidStats.size();
5046 boolean didPid = false;
5047 long nowRealtime = SystemClock.elapsedRealtime();
5048 for (int i=0; i<NU; i++) {
5049 Uid uid = uidStats.valueAt(i);
5050 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5051 if (pids != null) {
5052 for (int j=0; j<pids.size(); j++) {
5053 Uid.Pid pid = pids.valueAt(j);
5054 if (!didPid) {
5055 pw.println("Per-PID Stats:");
5056 didPid = true;
5057 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005058 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5059 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005060 pw.print(" PID "); pw.print(pids.keyAt(j));
5061 pw.print(" wake time: ");
5062 TimeUtils.formatDuration(time, pw);
5063 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005064 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005065 }
5066 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005067 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005068 pw.println();
5069 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005070 }
5071
5072 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005073 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5074 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005075 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5076 if (timeRemaining >= 0) {
5077 pw.print(" Estimated discharge time remaining: ");
5078 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5079 pw.println();
5080 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005081 final LevelStepTracker steps = getDischargeLevelStepTracker();
5082 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5083 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5084 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5085 STEP_LEVEL_MODE_VALUES[i], null));
5086 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005087 pw.println();
5088 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005089 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5090 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005091 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5092 if (timeRemaining >= 0) {
5093 pw.print(" Estimated charge time remaining: ");
5094 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5095 pw.println();
5096 }
5097 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005098 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005099 }
5100 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5101 pw.println("Daily stats:");
5102 pw.print(" Current start time: ");
5103 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5104 getCurrentDailyStartTime()).toString());
5105 pw.print(" Next min deadline: ");
5106 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5107 getNextMinDailyDeadline()).toString());
5108 pw.print(" Next max deadline: ");
5109 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5110 getNextMaxDailyDeadline()).toString());
5111 StringBuilder sb = new StringBuilder(64);
5112 int[] outInt = new int[1];
5113 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5114 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005115 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5116 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005117 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005118 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5119 dsteps, false)) {
5120 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5121 sb, outInt);
5122 }
5123 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5124 csteps, false)) {
5125 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5126 sb, outInt);
5127 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005128 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005129 } else {
5130 pw.println(" Current daily steps:");
5131 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5132 sb, outInt);
5133 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5134 sb, outInt);
5135 }
5136 }
5137 DailyItem dit;
5138 int curIndex = 0;
5139 while ((dit=getDailyItemLocked(curIndex)) != null) {
5140 curIndex++;
5141 if ((flags&DUMP_DAILY_ONLY) != 0) {
5142 pw.println();
5143 }
5144 pw.print(" Daily from ");
5145 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5146 pw.print(" to ");
5147 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5148 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005149 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005150 if (dumpDurationSteps(pw, " ",
5151 " Discharge step durations:", dit.mDischargeSteps, false)) {
5152 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5153 sb, outInt);
5154 }
5155 if (dumpDurationSteps(pw, " ",
5156 " Charge step durations:", dit.mChargeSteps, false)) {
5157 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5158 sb, outInt);
5159 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005160 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005161 } else {
5162 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5163 sb, outInt);
5164 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5165 sb, outInt);
5166 }
5167 }
5168 pw.println();
5169 }
5170 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005171 pw.println("Statistics since last charge:");
5172 pw.println(" System starts: " + getStartCount()
5173 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005174 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5175 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005176 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 }
5179
5180 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005181 public void dumpCheckinLocked(Context context, PrintWriter pw,
5182 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005183 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005184
5185 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005186 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5187 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005188
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005189 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5190
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005191 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005192 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005193
5194 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005195 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005196 try {
5197 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5198 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5199 pw.print(HISTORY_STRING_POOL); pw.print(',');
5200 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005201 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005202 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005203 pw.print(",\"");
5204 String str = getHistoryTagPoolString(i);
5205 str = str.replace("\\", "\\\\");
5206 str = str.replace("\"", "\\\"");
5207 pw.print(str);
5208 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005209 pw.println();
5210 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005211 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005212 } finally {
5213 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005214 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005215 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005216 }
5217
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005218 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005219 return;
5220 }
5221
Dianne Hackborne4a59512010-12-07 11:08:07 -08005222 if (apps != null) {
5223 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5224 for (int i=0; i<apps.size(); i++) {
5225 ApplicationInfo ai = apps.get(i);
5226 ArrayList<String> pkgs = uids.get(ai.uid);
5227 if (pkgs == null) {
5228 pkgs = new ArrayList<String>();
5229 uids.put(ai.uid, pkgs);
5230 }
5231 pkgs.add(ai.packageName);
5232 }
5233 SparseArray<? extends Uid> uidStats = getUidStats();
5234 final int NU = uidStats.size();
5235 String[] lineArgs = new String[2];
5236 for (int i=0; i<NU; i++) {
5237 int uid = uidStats.keyAt(i);
5238 ArrayList<String> pkgs = uids.get(uid);
5239 if (pkgs != null) {
5240 for (int j=0; j<pkgs.size(); j++) {
5241 lineArgs[0] = Integer.toString(uid);
5242 lineArgs[1] = pkgs.get(j);
5243 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5244 (Object[])lineArgs);
5245 }
5246 }
5247 }
5248 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005249 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005250 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005251 String[] lineArgs = new String[1];
5252 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5253 if (timeRemaining >= 0) {
5254 lineArgs[0] = Long.toString(timeRemaining);
5255 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5256 (Object[])lineArgs);
5257 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005258 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005259 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5260 if (timeRemaining >= 0) {
5261 lineArgs[0] = Long.toString(timeRemaining);
5262 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5263 (Object[])lineArgs);
5264 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005265 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5266 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269}