blob: bce38f40682a2f8dba3abd6401d326ca9ccf2fbe [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 /**
381 * {@hide}
382 */
383 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700384
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800385 public abstract void noteWifiRunningLocked(long elapsedRealtime);
386 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
387 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
388 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
389 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
390 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
391 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
392 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
393 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
394 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800395 public abstract void noteActivityResumedLocked(long elapsedRealtime);
396 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800397 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
398 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
399 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700400 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800401 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700402 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800403 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700404 public abstract Timer getAudioTurnedOnTimer();
405 public abstract Timer getVideoTurnedOnTimer();
406 public abstract Timer getFlashlightTurnedOnTimer();
407 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700408 public abstract Timer getForegroundActivityTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700409
Dianne Hackborna8d10942015-11-19 17:55:19 -0800410 // Time this uid has any processes in the top state.
411 public static final int PROCESS_STATE_TOP = 0;
412 // Time this uid has any process with a started out bound foreground service.
413 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
414 // Time this uid has any process that is top while the device is sleeping.
415 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
416 // Time this uid has any process in an active foreground state.
417 public static final int PROCESS_STATE_FOREGROUND = 3;
418 // Time this uid has any process in an active background state.
419 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700420 // Time this uid has any processes running at all.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800421 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700422 // Total number of process states we track.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800423 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700424
425 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800426 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700427 };
428
429 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
430
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800431 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
Robert Greenwalta029ea12013-09-25 16:38:12 -0700433 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
434
Dianne Hackborn617f8772009-03-31 15:04:46 -0700435 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700436 * Note that these must match the constants in android.os.PowerManager.
437 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
438 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700439 */
440 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700441 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700442 };
443
Jeff Browndf693de2012-07-27 12:03:38 -0700444 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700445
Dianne Hackborn617f8772009-03-31 15:04:46 -0700446 public abstract void noteUserActivityLocked(int type);
447 public abstract boolean hasUserActivity();
448 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700449
450 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800451 public abstract long getNetworkActivityBytes(int type, int which);
452 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800453 public abstract long getMobileRadioActiveTime(int which);
454 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700455
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700456 /**
457 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
458 */
459 public abstract long getUserCpuTimeUs(int which);
460
461 /**
462 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
463 */
464 public abstract long getSystemCpuTimeUs(int which);
465
466 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700467 * Get the total cpu power consumed (in milli-ampere-microseconds).
468 */
469 public abstract long getCpuPowerMaUs(int which);
470
471 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700472 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
473 * given CPU cluster.
474 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700475 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700476 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700477 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
478 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700479 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700480 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800483 /*
484 * FIXME: it's not correct to use this magic value because it
485 * could clash with a sensor handle (which are defined by
486 * the sensor HAL, and therefore out of our control
487 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 // Magic sensor number for the GPS.
489 public static final int GPS = -10000;
490
491 public abstract int getHandle();
492
493 public abstract Timer getSensorTime();
494 }
495
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700496 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800497 public int mWakeNesting;
498 public long mWakeSumMs;
499 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700500 }
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
503 * The statistics associated with a particular process.
504 */
505 public static abstract class Proc {
506
Dianne Hackborn287952c2010-09-22 22:34:31 -0700507 public static class ExcessivePower {
508 public static final int TYPE_WAKE = 1;
509 public static final int TYPE_CPU = 2;
510
511 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700512 public long overTime;
513 public long usedTime;
514 }
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800517 * Returns true if this process is still active in the battery stats.
518 */
519 public abstract boolean isActive();
520
521 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700522 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700524 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 */
526 public abstract long getUserTime(int which);
527
528 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700529 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700531 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 */
533 public abstract long getSystemTime(int which);
534
535 /**
536 * Returns the number of times the process has been started.
537 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700538 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 */
540 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700541
542 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800543 * Returns the number of times the process has crashed.
544 *
545 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
546 */
547 public abstract int getNumCrashes(int which);
548
549 /**
550 * Returns the number of times the process has ANRed.
551 *
552 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
553 */
554 public abstract int getNumAnrs(int which);
555
556 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700557 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700558 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700559 * @return foreground cpu time in microseconds
560 */
561 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700562
Dianne Hackborn287952c2010-09-22 22:34:31 -0700563 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700564
Dianne Hackborn287952c2010-09-22 22:34:31 -0700565 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
568 /**
569 * The statistics associated with a particular package.
570 */
571 public static abstract class Pkg {
572
573 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700574 * Returns information about all wakeup alarms that have been triggered for this
575 * package. The mapping keys are tag names for the alarms, the counter contains
576 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700578 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579
580 /**
581 * Returns a mapping containing service statistics.
582 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700583 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584
585 /**
586 * The statistics associated with a particular service.
587 */
588 public abstract class Serv {
589
590 /**
591 * Returns the amount of time spent started.
592 *
593 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700594 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * @return
596 */
597 public abstract long getStartTime(long batteryUptime, int which);
598
599 /**
600 * Returns the total number of times startService() has been called.
601 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700602 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 */
604 public abstract int getStarts(int which);
605
606 /**
607 * Returns the total number times the service has been launched.
608 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700609 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 */
611 public abstract int getLaunches(int which);
612 }
613 }
614 }
615
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800616 public static final class LevelStepTracker {
617 public long mLastStepTime = -1;
618 public int mNumStepDurations;
619 public final long[] mStepDurations;
620
621 public LevelStepTracker(int maxLevelSteps) {
622 mStepDurations = new long[maxLevelSteps];
623 }
624
625 public LevelStepTracker(int numSteps, long[] steps) {
626 mNumStepDurations = numSteps;
627 mStepDurations = new long[numSteps];
628 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
629 }
630
631 public long getDurationAt(int index) {
632 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
633 }
634
635 public int getLevelAt(int index) {
636 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
637 >> STEP_LEVEL_LEVEL_SHIFT);
638 }
639
640 public int getInitModeAt(int index) {
641 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
642 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
643 }
644
645 public int getModModeAt(int index) {
646 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
647 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
648 }
649
650 private void appendHex(long val, int topOffset, StringBuilder out) {
651 boolean hasData = false;
652 while (topOffset >= 0) {
653 int digit = (int)( (val>>topOffset) & 0xf );
654 topOffset -= 4;
655 if (!hasData && digit == 0) {
656 continue;
657 }
658 hasData = true;
659 if (digit >= 0 && digit <= 9) {
660 out.append((char)('0' + digit));
661 } else {
662 out.append((char)('a' + digit - 10));
663 }
664 }
665 }
666
667 public void encodeEntryAt(int index, StringBuilder out) {
668 long item = mStepDurations[index];
669 long duration = item & STEP_LEVEL_TIME_MASK;
670 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
671 >> STEP_LEVEL_LEVEL_SHIFT);
672 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
673 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
674 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
675 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
676 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
677 case Display.STATE_OFF: out.append('f'); break;
678 case Display.STATE_ON: out.append('o'); break;
679 case Display.STATE_DOZE: out.append('d'); break;
680 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
681 }
682 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
683 out.append('p');
684 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700685 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
686 out.append('i');
687 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800688 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
689 case Display.STATE_OFF: out.append('F'); break;
690 case Display.STATE_ON: out.append('O'); break;
691 case Display.STATE_DOZE: out.append('D'); break;
692 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
693 }
694 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
695 out.append('P');
696 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700697 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
698 out.append('I');
699 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800700 out.append('-');
701 appendHex(level, 4, out);
702 out.append('-');
703 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
704 }
705
706 public void decodeEntryAt(int index, String value) {
707 final int N = value.length();
708 int i = 0;
709 char c;
710 long out = 0;
711 while (i < N && (c=value.charAt(i)) != '-') {
712 i++;
713 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800714 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800715 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800716 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800717 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800718 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800719 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800720 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
721 << STEP_LEVEL_INITIAL_MODE_SHIFT);
722 break;
723 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
724 << STEP_LEVEL_INITIAL_MODE_SHIFT);
725 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700726 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
727 << STEP_LEVEL_INITIAL_MODE_SHIFT);
728 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800729 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
730 break;
731 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
732 break;
733 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
734 break;
735 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
736 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
737 break;
738 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
739 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800740 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700741 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
742 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
743 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800744 }
745 }
746 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800747 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800748 while (i < N && (c=value.charAt(i)) != '-') {
749 i++;
750 level <<= 4;
751 if (c >= '0' && c <= '9') {
752 level += c - '0';
753 } else if (c >= 'a' && c <= 'f') {
754 level += c - 'a' + 10;
755 } else if (c >= 'A' && c <= 'F') {
756 level += c - 'A' + 10;
757 }
758 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800759 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800760 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
761 long duration = 0;
762 while (i < N && (c=value.charAt(i)) != '-') {
763 i++;
764 duration <<= 4;
765 if (c >= '0' && c <= '9') {
766 duration += c - '0';
767 } else if (c >= 'a' && c <= 'f') {
768 duration += c - 'a' + 10;
769 } else if (c >= 'A' && c <= 'F') {
770 duration += c - 'A' + 10;
771 }
772 }
773 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
774 }
775
776 public void init() {
777 mLastStepTime = -1;
778 mNumStepDurations = 0;
779 }
780
781 public void clearTime() {
782 mLastStepTime = -1;
783 }
784
785 public long computeTimePerLevel() {
786 final long[] steps = mStepDurations;
787 final int numSteps = mNumStepDurations;
788
789 // For now we'll do a simple average across all steps.
790 if (numSteps <= 0) {
791 return -1;
792 }
793 long total = 0;
794 for (int i=0; i<numSteps; i++) {
795 total += steps[i] & STEP_LEVEL_TIME_MASK;
796 }
797 return total / numSteps;
798 /*
799 long[] buckets = new long[numSteps];
800 int numBuckets = 0;
801 int numToAverage = 4;
802 int i = 0;
803 while (i < numSteps) {
804 long totalTime = 0;
805 int num = 0;
806 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
807 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
808 num++;
809 }
810 buckets[numBuckets] = totalTime / num;
811 numBuckets++;
812 numToAverage *= 2;
813 i += num;
814 }
815 if (numBuckets < 1) {
816 return -1;
817 }
818 long averageTime = buckets[numBuckets-1];
819 for (i=numBuckets-2; i>=0; i--) {
820 averageTime = (averageTime + buckets[i]) / 2;
821 }
822 return averageTime;
823 */
824 }
825
826 public long computeTimeEstimate(long modesOfInterest, long modeValues,
827 int[] outNumOfInterest) {
828 final long[] steps = mStepDurations;
829 final int count = mNumStepDurations;
830 if (count <= 0) {
831 return -1;
832 }
833 long total = 0;
834 int numOfInterest = 0;
835 for (int i=0; i<count; i++) {
836 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
837 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
838 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
839 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
840 // If the modes of interest didn't change during this step period...
841 if ((modMode&modesOfInterest) == 0) {
842 // And the mode values during this period match those we are measuring...
843 if ((initMode&modesOfInterest) == modeValues) {
844 // Then this can be used to estimate the total time!
845 numOfInterest++;
846 total += steps[i] & STEP_LEVEL_TIME_MASK;
847 }
848 }
849 }
850 if (numOfInterest <= 0) {
851 return -1;
852 }
853
854 if (outNumOfInterest != null) {
855 outNumOfInterest[0] = numOfInterest;
856 }
857
858 // The estimated time is the average time we spend in each level, multipled
859 // by 100 -- the total number of battery levels
860 return (total / numOfInterest) * 100;
861 }
862
863 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
864 int stepCount = mNumStepDurations;
865 final long lastStepTime = mLastStepTime;
866 if (lastStepTime >= 0 && numStepLevels > 0) {
867 final long[] steps = mStepDurations;
868 long duration = elapsedRealtime - lastStepTime;
869 for (int i=0; i<numStepLevels; i++) {
870 System.arraycopy(steps, 0, steps, 1, steps.length-1);
871 long thisDuration = duration / (numStepLevels-i);
872 duration -= thisDuration;
873 if (thisDuration > STEP_LEVEL_TIME_MASK) {
874 thisDuration = STEP_LEVEL_TIME_MASK;
875 }
876 steps[0] = thisDuration | modeBits;
877 }
878 stepCount += numStepLevels;
879 if (stepCount > steps.length) {
880 stepCount = steps.length;
881 }
882 }
883 mNumStepDurations = stepCount;
884 mLastStepTime = elapsedRealtime;
885 }
886
887 public void readFromParcel(Parcel in) {
888 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -0700889 if (N > mStepDurations.length) {
890 throw new ParcelFormatException("more step durations than available: " + N);
891 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800892 mNumStepDurations = N;
893 for (int i=0; i<N; i++) {
894 mStepDurations[i] = in.readLong();
895 }
896 }
897
898 public void writeToParcel(Parcel out) {
899 final int N = mNumStepDurations;
900 out.writeInt(N);
901 for (int i=0; i<N; i++) {
902 out.writeLong(mStepDurations[i]);
903 }
904 }
905 }
906
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700907 public static final class PackageChange {
908 public String mPackageName;
909 public boolean mUpdate;
910 public int mVersionCode;
911 }
912
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800913 public static final class DailyItem {
914 public long mStartTime;
915 public long mEndTime;
916 public LevelStepTracker mDischargeSteps;
917 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700918 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800919 }
920
921 public abstract DailyItem getDailyItemLocked(int daysAgo);
922
923 public abstract long getCurrentDailyStartTime();
924
925 public abstract long getNextMinDailyDeadline();
926
927 public abstract long getNextMaxDailyDeadline();
928
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800929 public final static class HistoryTag {
930 public String string;
931 public int uid;
932
933 public int poolIdx;
934
935 public void setTo(HistoryTag o) {
936 string = o.string;
937 uid = o.uid;
938 poolIdx = o.poolIdx;
939 }
940
941 public void setTo(String _string, int _uid) {
942 string = _string;
943 uid = _uid;
944 poolIdx = -1;
945 }
946
947 public void writeToParcel(Parcel dest, int flags) {
948 dest.writeString(string);
949 dest.writeInt(uid);
950 }
951
952 public void readFromParcel(Parcel src) {
953 string = src.readString();
954 uid = src.readInt();
955 poolIdx = -1;
956 }
957
958 @Override
959 public boolean equals(Object o) {
960 if (this == o) return true;
961 if (o == null || getClass() != o.getClass()) return false;
962
963 HistoryTag that = (HistoryTag) o;
964
965 if (uid != that.uid) return false;
966 if (!string.equals(that.string)) return false;
967
968 return true;
969 }
970
971 @Override
972 public int hashCode() {
973 int result = string.hashCode();
974 result = 31 * result + uid;
975 return result;
976 }
977 }
978
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800979 /**
980 * Optional detailed information that can go into a history step. This is typically
981 * generated each time the battery level changes.
982 */
983 public final static class HistoryStepDetails {
984 // Time (in 1/100 second) spent in user space and the kernel since the last step.
985 public int userTime;
986 public int systemTime;
987
988 // Top three apps using CPU in the last step, with times in 1/100 second.
989 public int appCpuUid1;
990 public int appCpuUTime1;
991 public int appCpuSTime1;
992 public int appCpuUid2;
993 public int appCpuUTime2;
994 public int appCpuSTime2;
995 public int appCpuUid3;
996 public int appCpuUTime3;
997 public int appCpuSTime3;
998
999 // Information from /proc/stat
1000 public int statUserTime;
1001 public int statSystemTime;
1002 public int statIOWaitTime;
1003 public int statIrqTime;
1004 public int statSoftIrqTime;
1005 public int statIdlTime;
1006
1007 public HistoryStepDetails() {
1008 clear();
1009 }
1010
1011 public void clear() {
1012 userTime = systemTime = 0;
1013 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1014 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1015 = appCpuUTime3 = appCpuSTime3 = 0;
1016 }
1017
1018 public void writeToParcel(Parcel out) {
1019 out.writeInt(userTime);
1020 out.writeInt(systemTime);
1021 out.writeInt(appCpuUid1);
1022 out.writeInt(appCpuUTime1);
1023 out.writeInt(appCpuSTime1);
1024 out.writeInt(appCpuUid2);
1025 out.writeInt(appCpuUTime2);
1026 out.writeInt(appCpuSTime2);
1027 out.writeInt(appCpuUid3);
1028 out.writeInt(appCpuUTime3);
1029 out.writeInt(appCpuSTime3);
1030 out.writeInt(statUserTime);
1031 out.writeInt(statSystemTime);
1032 out.writeInt(statIOWaitTime);
1033 out.writeInt(statIrqTime);
1034 out.writeInt(statSoftIrqTime);
1035 out.writeInt(statIdlTime);
1036 }
1037
1038 public void readFromParcel(Parcel in) {
1039 userTime = in.readInt();
1040 systemTime = in.readInt();
1041 appCpuUid1 = in.readInt();
1042 appCpuUTime1 = in.readInt();
1043 appCpuSTime1 = in.readInt();
1044 appCpuUid2 = in.readInt();
1045 appCpuUTime2 = in.readInt();
1046 appCpuSTime2 = in.readInt();
1047 appCpuUid3 = in.readInt();
1048 appCpuUTime3 = in.readInt();
1049 appCpuSTime3 = in.readInt();
1050 statUserTime = in.readInt();
1051 statSystemTime = in.readInt();
1052 statIOWaitTime = in.readInt();
1053 statIrqTime = in.readInt();
1054 statSoftIrqTime = in.readInt();
1055 statIdlTime = in.readInt();
1056 }
1057 }
1058
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001059 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001060 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001061
1062 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001063 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001064
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001065 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001066 public static final byte CMD_NULL = -1;
1067 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001068 public static final byte CMD_CURRENT_TIME = 5;
1069 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001070 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001071 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001072
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001073 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001074
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001075 /**
1076 * Return whether the command code is a delta data update.
1077 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001078 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001079 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001080 }
1081
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001082 public byte batteryLevel;
1083 public byte batteryStatus;
1084 public byte batteryHealth;
1085 public byte batteryPlugType;
1086
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001087 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001088 public char batteryVoltage;
1089
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001090 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001091 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001092 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001093 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001094 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1095 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001096 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001097 public static final int STATE_PHONE_STATE_SHIFT = 6;
1098 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001099 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001100 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001101 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001102
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001103 // These states always appear directly in the first int token
1104 // of a delta change; they should be ones that change relatively
1105 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001106 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1107 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001108 public static final int STATE_GPS_ON_FLAG = 1<<29;
1109 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001110 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001111 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001112 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001113 // These are on the lower bits used for the command; if they change
1114 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001115 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001116 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001117 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001118 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1119 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1120 // empty slot
1121 // empty slot
1122 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001123
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001124 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001125 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1126
1127 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001128
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001129 public int states;
1130
Dianne Hackborn3251b902014-06-20 14:40:53 -07001131 // Constants from WIFI_SUPPL_STATE_*
1132 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1133 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1134 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1135 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1136 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1137 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1138
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001139 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001140 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1141 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1142 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001143 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001144 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1145 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1146 public static final int STATE2_CHARGING_FLAG = 1<<24;
1147 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1148 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1149 public static final int STATE2_CAMERA_FLAG = 1<<21;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001150
1151 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001152 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001153 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1154
1155 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001156
Dianne Hackborn40c87252014-03-19 16:55:40 -07001157 public int states2;
1158
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001159 // The wake lock that was acquired at this point.
1160 public HistoryTag wakelockTag;
1161
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001162 // Kernel wakeup reason at this point.
1163 public HistoryTag wakeReasonTag;
1164
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001165 // Non-null when there is more detailed information at this step.
1166 public HistoryStepDetails stepDetails;
1167
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001168 public static final int EVENT_FLAG_START = 0x8000;
1169 public static final int EVENT_FLAG_FINISH = 0x4000;
1170
1171 // No event in this item.
1172 public static final int EVENT_NONE = 0x0000;
1173 // Event is about a process that is running.
1174 public static final int EVENT_PROC = 0x0001;
1175 // Event is about an application package that is in the foreground.
1176 public static final int EVENT_FOREGROUND = 0x0002;
1177 // Event is about an application package that is at the top of the screen.
1178 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001179 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001180 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001181 // Events for all additional wake locks aquired/release within a wake block.
1182 // These are not generated by default.
1183 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001184 // Event is about an application executing a scheduled job.
1185 public static final int EVENT_JOB = 0x0006;
1186 // Events for users running.
1187 public static final int EVENT_USER_RUNNING = 0x0007;
1188 // Events for foreground user.
1189 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001190 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001191 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001192 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001193 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001194 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001195 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001196 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001197 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001198 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001199 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001200 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001201 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001202 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001203 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001204 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001205 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001206 // Event for a package being on the temporary whitelist.
1207 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001208 // Event for the screen waking up.
1209 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Amith Yamasani67768492015-06-09 12:23:58 -07001210
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001211 // Number of event types.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001212 public static final int EVENT_COUNT = 0x0013;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001213 // Mask to extract out only the type part of the event.
1214 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001215
1216 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1217 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1218 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1219 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1220 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1221 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001222 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1223 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001224 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1225 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001226 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1227 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1228 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1229 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1230 public static final int EVENT_USER_FOREGROUND_START =
1231 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1232 public static final int EVENT_USER_FOREGROUND_FINISH =
1233 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001234 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1235 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001236 public static final int EVENT_TEMP_WHITELIST_START =
1237 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1238 public static final int EVENT_TEMP_WHITELIST_FINISH =
1239 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001240
1241 // For CMD_EVENT.
1242 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001243 public HistoryTag eventTag;
1244
Dianne Hackborn9a755432014-05-15 17:05:22 -07001245 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001246 public long currentTime;
1247
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001248 // Meta-data when reading.
1249 public int numReadInts;
1250
1251 // Pre-allocated objects.
1252 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001253 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001254 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001255
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001256 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001257 }
1258
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001259 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001260 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001261 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001262 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001263 }
1264
1265 public int describeContents() {
1266 return 0;
1267 }
1268
1269 public void writeToParcel(Parcel dest, int flags) {
1270 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001271 int bat = (((int)cmd)&0xff)
1272 | ((((int)batteryLevel)<<8)&0xff00)
1273 | ((((int)batteryStatus)<<16)&0xf0000)
1274 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001275 | ((((int)batteryPlugType)<<24)&0xf000000)
1276 | (wakelockTag != null ? 0x10000000 : 0)
1277 | (wakeReasonTag != null ? 0x20000000 : 0)
1278 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001279 dest.writeInt(bat);
1280 bat = (((int)batteryTemperature)&0xffff)
1281 | ((((int)batteryVoltage)<<16)&0xffff0000);
1282 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001283 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001284 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001285 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001286 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001287 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001288 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001289 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001290 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001291 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001292 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001293 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001294 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001295 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001296 dest.writeLong(currentTime);
1297 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001298 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001299
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001300 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001301 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001302 int bat = src.readInt();
1303 cmd = (byte)(bat&0xff);
1304 batteryLevel = (byte)((bat>>8)&0xff);
1305 batteryStatus = (byte)((bat>>16)&0xf);
1306 batteryHealth = (byte)((bat>>20)&0xf);
1307 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001308 int bat2 = src.readInt();
1309 batteryTemperature = (short)(bat2&0xffff);
1310 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001311 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001312 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001313 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001314 wakelockTag = localWakelockTag;
1315 wakelockTag.readFromParcel(src);
1316 } else {
1317 wakelockTag = null;
1318 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001319 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001320 wakeReasonTag = localWakeReasonTag;
1321 wakeReasonTag.readFromParcel(src);
1322 } else {
1323 wakeReasonTag = null;
1324 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001325 if ((bat&0x40000000) != 0) {
1326 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001327 eventTag = localEventTag;
1328 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001329 } else {
1330 eventCode = EVENT_NONE;
1331 eventTag = null;
1332 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001333 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001334 currentTime = src.readLong();
1335 } else {
1336 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001337 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001338 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001339 }
1340
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001341 public void clear() {
1342 time = 0;
1343 cmd = CMD_NULL;
1344 batteryLevel = 0;
1345 batteryStatus = 0;
1346 batteryHealth = 0;
1347 batteryPlugType = 0;
1348 batteryTemperature = 0;
1349 batteryVoltage = 0;
1350 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001351 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001352 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001353 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001354 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001355 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001356 }
1357
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001358 public void setTo(HistoryItem o) {
1359 time = o.time;
1360 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001361 setToCommon(o);
1362 }
1363
1364 public void setTo(long time, byte cmd, HistoryItem o) {
1365 this.time = time;
1366 this.cmd = cmd;
1367 setToCommon(o);
1368 }
1369
1370 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001371 batteryLevel = o.batteryLevel;
1372 batteryStatus = o.batteryStatus;
1373 batteryHealth = o.batteryHealth;
1374 batteryPlugType = o.batteryPlugType;
1375 batteryTemperature = o.batteryTemperature;
1376 batteryVoltage = o.batteryVoltage;
1377 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001378 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001379 if (o.wakelockTag != null) {
1380 wakelockTag = localWakelockTag;
1381 wakelockTag.setTo(o.wakelockTag);
1382 } else {
1383 wakelockTag = null;
1384 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001385 if (o.wakeReasonTag != null) {
1386 wakeReasonTag = localWakeReasonTag;
1387 wakeReasonTag.setTo(o.wakeReasonTag);
1388 } else {
1389 wakeReasonTag = null;
1390 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001391 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001392 if (o.eventTag != null) {
1393 eventTag = localEventTag;
1394 eventTag.setTo(o.eventTag);
1395 } else {
1396 eventTag = null;
1397 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001398 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001399 }
1400
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001401 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001402 return batteryLevel == o.batteryLevel
1403 && batteryStatus == o.batteryStatus
1404 && batteryHealth == o.batteryHealth
1405 && batteryPlugType == o.batteryPlugType
1406 && batteryTemperature == o.batteryTemperature
1407 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001408 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001409 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001410 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001411 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001412
1413 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001414 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001415 return false;
1416 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001417 if (wakelockTag != o.wakelockTag) {
1418 if (wakelockTag == null || o.wakelockTag == null) {
1419 return false;
1420 }
1421 if (!wakelockTag.equals(o.wakelockTag)) {
1422 return false;
1423 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001425 if (wakeReasonTag != o.wakeReasonTag) {
1426 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1427 return false;
1428 }
1429 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1430 return false;
1431 }
1432 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001433 if (eventTag != o.eventTag) {
1434 if (eventTag == null || o.eventTag == null) {
1435 return false;
1436 }
1437 if (!eventTag.equals(o.eventTag)) {
1438 return false;
1439 }
1440 }
1441 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001442 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001443 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001444
1445 public final static class HistoryEventTracker {
1446 private final HashMap<String, SparseIntArray>[] mActiveEvents
1447 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1448
1449 public boolean updateState(int code, String name, int uid, int poolIdx) {
1450 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1451 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1452 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1453 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001454 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001455 mActiveEvents[idx] = active;
1456 }
1457 SparseIntArray uids = active.get(name);
1458 if (uids == null) {
1459 uids = new SparseIntArray();
1460 active.put(name, uids);
1461 }
1462 if (uids.indexOfKey(uid) >= 0) {
1463 // Already set, nothing to do!
1464 return false;
1465 }
1466 uids.put(uid, poolIdx);
1467 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1468 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1469 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1470 if (active == null) {
1471 // not currently active, nothing to do.
1472 return false;
1473 }
1474 SparseIntArray uids = active.get(name);
1475 if (uids == null) {
1476 // not currently active, nothing to do.
1477 return false;
1478 }
1479 idx = uids.indexOfKey(uid);
1480 if (idx < 0) {
1481 // not currently active, nothing to do.
1482 return false;
1483 }
1484 uids.removeAt(idx);
1485 if (uids.size() <= 0) {
1486 active.remove(name);
1487 }
1488 }
1489 return true;
1490 }
1491
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001492 public void removeEvents(int code) {
1493 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1494 mActiveEvents[idx] = null;
1495 }
1496
Dianne Hackborn37de0982014-05-09 09:32:18 -07001497 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1498 return mActiveEvents[code];
1499 }
1500 }
1501
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001502 public static final class BitDescription {
1503 public final int mask;
1504 public final int shift;
1505 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001506 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001507 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001508 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001509
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001510 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001511 this.mask = mask;
1512 this.shift = -1;
1513 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001514 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001515 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001517 }
1518
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001519 public BitDescription(int mask, int shift, String name, String shortName,
1520 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001521 this.mask = mask;
1522 this.shift = shift;
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 = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001527 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001528 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001529
Dianne Hackbornfc064132014-06-02 12:42:12 -07001530 /**
1531 * Don't allow any more batching in to the current history event. This
1532 * is called when printing partial histories, so to ensure that the next
1533 * history event will go in to a new batch after what was printed in the
1534 * last partial history.
1535 */
1536 public abstract void commitCurrentHistoryBatchLocked();
1537
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001538 public abstract int getHistoryTotalSize();
1539
1540 public abstract int getHistoryUsedSize();
1541
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001542 public abstract boolean startIteratingHistoryLocked();
1543
Dianne Hackborn099bc622014-01-22 13:39:16 -08001544 public abstract int getHistoryStringPoolSize();
1545
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001546 public abstract int getHistoryStringPoolBytes();
1547
1548 public abstract String getHistoryTagPoolString(int index);
1549
1550 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001551
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001552 public abstract boolean getNextHistoryLocked(HistoryItem out);
1553
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001554 public abstract void finishIteratingHistoryLocked();
1555
1556 public abstract boolean startIteratingOldHistoryLocked();
1557
1558 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1559
1560 public abstract void finishIteratingOldHistoryLocked();
1561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001563 * Return the base time offset for the battery history.
1564 */
1565 public abstract long getHistoryBaseTime();
1566
1567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 * Returns the number of times the device has been started.
1569 */
1570 public abstract int getStartCount();
1571
1572 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001573 * 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 -08001574 * running on battery.
1575 *
1576 * {@hide}
1577 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001578 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001580 /**
1581 * Returns the number of times the screen was turned on.
1582 *
1583 * {@hide}
1584 */
1585 public abstract int getScreenOnCount(int which);
1586
Jeff Browne95c3cd2014-05-02 16:59:26 -07001587 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1588
Dianne Hackborn617f8772009-03-31 15:04:46 -07001589 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1590 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1591 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1592 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1593 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1594
1595 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1596 "dark", "dim", "medium", "light", "bright"
1597 };
1598
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001599 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1600 "0", "1", "2", "3", "4"
1601 };
1602
Dianne Hackborn617f8772009-03-31 15:04:46 -07001603 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001604
Dianne Hackborn617f8772009-03-31 15:04:46 -07001605 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001606 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001607 * the given brightness
1608 *
1609 * {@hide}
1610 */
1611 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001615 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001616 * running on battery.
1617 *
1618 * {@hide}
1619 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001620 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001621
1622 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001623 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001624 *
1625 * {@hide}
1626 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001627 public abstract int getPowerSaveModeEnabledCount(int which);
1628
1629 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001630 * Constant for device idle mode: not active.
1631 */
1632 public static final int DEVICE_IDLE_MODE_OFF = 0;
1633
1634 /**
1635 * Constant for device idle mode: active in lightweight mode.
1636 */
1637 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1638
1639 /**
1640 * Constant for device idle mode: active in full mode.
1641 */
1642 public static final int DEVICE_IDLE_MODE_FULL = 2;
1643
1644 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001645 * Returns the time in microseconds that device has been in idle mode while
1646 * running on battery.
1647 *
1648 * {@hide}
1649 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001650 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001651
1652 /**
1653 * Returns the number of times that the devie has gone in to idle mode.
1654 *
1655 * {@hide}
1656 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001657 public abstract int getDeviceIdleModeCount(int mode, int which);
1658
1659 /**
1660 * Return the longest duration we spent in a particular device idle mode (fully in the
1661 * mode, not in idle maintenance etc).
1662 */
1663 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001664
1665 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001666 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001667 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001668 * counts all of the time that we consider the device to be idle, whether or not
1669 * it is currently in the actual device idle mode.
1670 *
1671 * {@hide}
1672 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001673 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001674
1675 /**
1676 * Returns the number of times that the devie has started idling.
1677 *
1678 * {@hide}
1679 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001680 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001681
1682 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001683 * Returns the number of times that connectivity state changed.
1684 *
1685 * {@hide}
1686 */
1687 public abstract int getNumConnectivityChange(int which);
1688
1689 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001690 * 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 -08001691 * running on battery.
1692 *
1693 * {@hide}
1694 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001695 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001696
1697 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001698 * Returns the number of times a phone call was activated.
1699 *
1700 * {@hide}
1701 */
1702 public abstract int getPhoneOnCount(int which);
1703
1704 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001705 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001706 * the given signal strength.
1707 *
1708 * {@hide}
1709 */
1710 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001711 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001712
Dianne Hackborn617f8772009-03-31 15:04:46 -07001713 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001714 * Returns the time in microseconds that the phone has been trying to
1715 * acquire a signal.
1716 *
1717 * {@hide}
1718 */
1719 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001720 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001721
1722 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001723 * Returns the number of times the phone has entered the given signal strength.
1724 *
1725 * {@hide}
1726 */
1727 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1728
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001729 /**
1730 * Returns the time in microseconds that the mobile network has been active
1731 * (in a high power state).
1732 *
1733 * {@hide}
1734 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001735 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001736
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001737 /**
1738 * Returns the number of times that the mobile network has transitioned to the
1739 * active state.
1740 *
1741 * {@hide}
1742 */
1743 public abstract int getMobileRadioActiveCount(int which);
1744
1745 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001746 * Returns the time in microseconds that is the difference between the mobile radio
1747 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1748 * from the radio.
1749 *
1750 * {@hide}
1751 */
1752 public abstract long getMobileRadioActiveAdjustedTime(int which);
1753
1754 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001755 * Returns the time in microseconds that the mobile network has been active
1756 * (in a high power state) but not being able to blame on an app.
1757 *
1758 * {@hide}
1759 */
1760 public abstract long getMobileRadioActiveUnknownTime(int which);
1761
1762 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001763 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001764 *
1765 * {@hide}
1766 */
1767 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001768
Dianne Hackborn627bba72009-03-24 22:32:56 -07001769 public static final int DATA_CONNECTION_NONE = 0;
1770 public static final int DATA_CONNECTION_GPRS = 1;
1771 public static final int DATA_CONNECTION_EDGE = 2;
1772 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001773 public static final int DATA_CONNECTION_CDMA = 4;
1774 public static final int DATA_CONNECTION_EVDO_0 = 5;
1775 public static final int DATA_CONNECTION_EVDO_A = 6;
1776 public static final int DATA_CONNECTION_1xRTT = 7;
1777 public static final int DATA_CONNECTION_HSDPA = 8;
1778 public static final int DATA_CONNECTION_HSUPA = 9;
1779 public static final int DATA_CONNECTION_HSPA = 10;
1780 public static final int DATA_CONNECTION_IDEN = 11;
1781 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001782 public static final int DATA_CONNECTION_LTE = 13;
1783 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001784 public static final int DATA_CONNECTION_HSPAP = 15;
1785 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001786
Dianne Hackborn627bba72009-03-24 22:32:56 -07001787 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001788 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001789 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001790 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001791 };
1792
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001793 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001794
1795 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001796 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001797 * the given data connection.
1798 *
1799 * {@hide}
1800 */
1801 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001802 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001805 * Returns the number of times the phone has entered the given data
1806 * connection type.
1807 *
1808 * {@hide}
1809 */
1810 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001811
Dianne Hackborn3251b902014-06-20 14:40:53 -07001812 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1813 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1814 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1815 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1816 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1817 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1818 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1819 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1820 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1821 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1822 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1823 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1824 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1825
1826 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1827
1828 static final String[] WIFI_SUPPL_STATE_NAMES = {
1829 "invalid", "disconn", "disabled", "inactive", "scanning",
1830 "authenticating", "associating", "associated", "4-way-handshake",
1831 "group-handshake", "completed", "dormant", "uninit"
1832 };
1833
1834 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1835 "inv", "dsc", "dis", "inact", "scan",
1836 "auth", "ascing", "asced", "4-way",
1837 "group", "compl", "dorm", "uninit"
1838 };
1839
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001840 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1841 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001842 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001843 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1844 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001845 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001846 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1847 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1848 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001849 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001850 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001851 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001852 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001853 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1854 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001855 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1856 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1857 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1858 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1859 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1860 new String[] {"in", "out", "emergency", "off"},
1861 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001862 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1863 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1864 SignalStrength.SIGNAL_STRENGTH_NAMES,
1865 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001866 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1867 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1868 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001869 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001870
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001871 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1872 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001873 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001874 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001875 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001876 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001877 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001878 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
1879 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
1880 new String[] { "off", "light", "full", "???" },
1881 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001882 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1883 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1884 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001885 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1886 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1887 new String[] { "0", "1", "2", "3", "4" },
1888 new String[] { "0", "1", "2", "3", "4" }),
1889 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1890 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1891 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001892 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001893 };
1894
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001895 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001896 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001897 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
1898 "screenwake",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001899 };
1900
1901 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001902 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001903 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
1904 "Esw",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001905 };
1906
Dianne Hackborn617f8772009-03-31 15:04:46 -07001907 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001908 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001909 * running on battery.
1910 *
1911 * {@hide}
1912 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001913 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001914
1915 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001916 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001917 * been in the running state while the device was running on battery.
1918 *
1919 * {@hide}
1920 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001921 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001922
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001923 public static final int WIFI_STATE_OFF = 0;
1924 public static final int WIFI_STATE_OFF_SCANNING = 1;
1925 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1926 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1927 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1928 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1929 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1930 public static final int WIFI_STATE_SOFT_AP = 7;
1931
1932 static final String[] WIFI_STATE_NAMES = {
1933 "off", "scanning", "no_net", "disconn",
1934 "sta", "p2p", "sta_p2p", "soft_ap"
1935 };
1936
1937 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1938
1939 /**
1940 * Returns the time in microseconds that WiFi has been running in the given state.
1941 *
1942 * {@hide}
1943 */
1944 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001945 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001946
1947 /**
1948 * Returns the number of times that WiFi has entered the given state.
1949 *
1950 * {@hide}
1951 */
1952 public abstract int getWifiStateCount(int wifiState, int which);
1953
The Android Open Source Project10592532009-03-18 17:39:46 -07001954 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001955 * Returns the time in microseconds that the wifi supplicant has been
1956 * in a given state.
1957 *
1958 * {@hide}
1959 */
1960 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1961
1962 /**
1963 * Returns the number of times that the wifi supplicant has transitioned
1964 * to a given state.
1965 *
1966 * {@hide}
1967 */
1968 public abstract int getWifiSupplStateCount(int state, int which);
1969
1970 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1971
1972 /**
1973 * Returns the time in microseconds that WIFI has been running with
1974 * the given signal strength.
1975 *
1976 * {@hide}
1977 */
1978 public abstract long getWifiSignalStrengthTime(int strengthBin,
1979 long elapsedRealtimeUs, int which);
1980
1981 /**
1982 * Returns the number of times WIFI has entered the given signal strength.
1983 *
1984 * {@hide}
1985 */
1986 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1987
1988 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001989 * Returns the time in microseconds that the flashlight has been on while the device was
1990 * running on battery.
1991 *
1992 * {@hide}
1993 */
1994 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1995
1996 /**
1997 * Returns the number of times that the flashlight has been turned on while the device was
1998 * running on battery.
1999 *
2000 * {@hide}
2001 */
2002 public abstract long getFlashlightOnCount(int which);
2003
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002004 /**
2005 * Returns the time in microseconds that the camera has been on while the device was
2006 * running on battery.
2007 *
2008 * {@hide}
2009 */
2010 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2011
2012
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002013 public static final int NETWORK_MOBILE_RX_DATA = 0;
2014 public static final int NETWORK_MOBILE_TX_DATA = 1;
2015 public static final int NETWORK_WIFI_RX_DATA = 2;
2016 public static final int NETWORK_WIFI_TX_DATA = 3;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002017 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002018
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002019 public abstract long getNetworkActivityBytes(int type, int which);
2020 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002021
Adam Lesinski33dac552015-03-09 15:24:48 -07002022 public static final int CONTROLLER_IDLE_TIME = 0;
2023 public static final int CONTROLLER_RX_TIME = 1;
2024 public static final int CONTROLLER_TX_TIME = 2;
Adam Lesinskie08af192015-03-25 16:42:59 -07002025 public static final int CONTROLLER_POWER_DRAIN = 3;
2026 public static final int NUM_CONTROLLER_ACTIVITY_TYPES = CONTROLLER_POWER_DRAIN + 1;
Adam Lesinski33dac552015-03-09 15:24:48 -07002027
Adam Lesinskie08af192015-03-25 16:42:59 -07002028 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002029 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2030 * When true, calling {@link #getBluetoothControllerActivity(int, int)} will yield the
2031 * actual power data.
2032 */
2033 public abstract boolean hasBluetoothActivityReporting();
2034
2035 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07002036 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
2037 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
2038 * respective state.
2039 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
2040 * milli-ampere-milliseconds (mAms).
2041 */
Adam Lesinski33dac552015-03-09 15:24:48 -07002042 public abstract long getBluetoothControllerActivity(int type, int which);
Adam Lesinskie08af192015-03-25 16:42:59 -07002043
2044 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002045 * Returns true if the BatteryStats object has detailed WiFi power reports.
2046 * When true, calling {@link #getWifiControllerActivity(int, int)} will yield the
2047 * actual power data.
2048 */
2049 public abstract boolean hasWifiActivityReporting();
2050
2051 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07002052 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
2053 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
2054 * respective state.
2055 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
2056 * milli-ampere-milliseconds (mAms).
2057 */
Adam Lesinski33dac552015-03-09 15:24:48 -07002058 public abstract long getWifiControllerActivity(int type, int which);
2059
The Android Open Source Project10592532009-03-18 17:39:46 -07002060 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002061 * Return the wall clock time when battery stats data collection started.
2062 */
2063 public abstract long getStartClockTime();
2064
2065 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002066 * Return platform version tag that we were running in when the battery stats started.
2067 */
2068 public abstract String getStartPlatformVersion();
2069
2070 /**
2071 * Return platform version tag that we were running in when the battery stats ended.
2072 */
2073 public abstract String getEndPlatformVersion();
2074
2075 /**
2076 * Return the internal version code of the parcelled format.
2077 */
2078 public abstract int getParcelVersion();
2079
2080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 * Return whether we are currently running on battery.
2082 */
2083 public abstract boolean getIsOnBattery();
2084
2085 /**
2086 * Returns a SparseArray containing the statistics for each uid.
2087 */
2088 public abstract SparseArray<? extends Uid> getUidStats();
2089
2090 /**
2091 * Returns the current battery uptime in microseconds.
2092 *
2093 * @param curTime the amount of elapsed realtime in microseconds.
2094 */
2095 public abstract long getBatteryUptime(long curTime);
2096
2097 /**
2098 * Returns the current battery realtime in microseconds.
2099 *
2100 * @param curTime the amount of elapsed realtime in microseconds.
2101 */
2102 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002103
2104 /**
Evan Millar633a1742009-04-02 16:36:33 -07002105 * Returns the battery percentage level at the last time the device was unplugged from power, or
2106 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002107 */
Evan Millar633a1742009-04-02 16:36:33 -07002108 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002109
2110 /**
Evan Millar633a1742009-04-02 16:36:33 -07002111 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2112 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002113 */
Evan Millar633a1742009-04-02 16:36:33 -07002114 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002117 * Get the amount the battery has discharged since the stats were
2118 * last reset after charging, as a lower-end approximation.
2119 */
2120 public abstract int getLowDischargeAmountSinceCharge();
2121
2122 /**
2123 * Get the amount the battery has discharged since the stats were
2124 * last reset after charging, as an upper-end approximation.
2125 */
2126 public abstract int getHighDischargeAmountSinceCharge();
2127
2128 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002129 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2130 */
2131 public abstract int getDischargeAmount(int which);
2132
2133 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002134 * Get the amount the battery has discharged while the screen was on,
2135 * since the last time power was unplugged.
2136 */
2137 public abstract int getDischargeAmountScreenOn();
2138
2139 /**
2140 * Get the amount the battery has discharged while the screen was on,
2141 * since the last time the device was charged.
2142 */
2143 public abstract int getDischargeAmountScreenOnSinceCharge();
2144
2145 /**
2146 * Get the amount the battery has discharged while the screen was off,
2147 * since the last time power was unplugged.
2148 */
2149 public abstract int getDischargeAmountScreenOff();
2150
2151 /**
2152 * Get the amount the battery has discharged while the screen was off,
2153 * since the last time the device was charged.
2154 */
2155 public abstract int getDischargeAmountScreenOffSinceCharge();
2156
2157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 * Returns the total, last, or current battery uptime in microseconds.
2159 *
2160 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002161 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 */
2163 public abstract long computeBatteryUptime(long curTime, int which);
2164
2165 /**
2166 * Returns the total, last, or current battery realtime in microseconds.
2167 *
2168 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002169 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 */
2171 public abstract long computeBatteryRealtime(long curTime, int which);
2172
2173 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002174 * Returns the total, last, or current battery screen off uptime in microseconds.
2175 *
2176 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002177 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002178 */
2179 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2180
2181 /**
2182 * Returns the total, last, or current battery screen off realtime in microseconds.
2183 *
2184 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002185 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002186 */
2187 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2188
2189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 * Returns the total, last, or current uptime in microseconds.
2191 *
2192 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002193 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 */
2195 public abstract long computeUptime(long curTime, int which);
2196
2197 /**
2198 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002201 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 */
2203 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002204
2205 /**
2206 * Compute an approximation for how much run time (in microseconds) is remaining on
2207 * the battery. Returns -1 if no time can be computed: either there is not
2208 * enough current data to make a decision, or the battery is currently
2209 * charging.
2210 *
2211 * @param curTime The current elepsed realtime in microseconds.
2212 */
2213 public abstract long computeBatteryTimeRemaining(long curTime);
2214
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002215 // The part of a step duration that is the actual time.
2216 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2217
2218 // Bits in a step duration that are the new battery level we are at.
2219 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002220 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002221
2222 // Bits in a step duration that are the initial mode we were in at that step.
2223 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002224 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002225
2226 // Bits in a step duration that indicate which modes changed during that step.
2227 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002228 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002229
2230 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2231 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2232
2233 // Step duration mode: power save is on.
2234 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2235
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002236 // Step duration mode: device is currently in idle mode.
2237 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2238
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002239 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2240 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002241 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2242 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002243 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2244 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2245 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2246 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2247 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002248 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2249 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002250 };
2251 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2252 (Display.STATE_OFF-1),
2253 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002254 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002255 (Display.STATE_ON-1),
2256 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2257 (Display.STATE_DOZE-1),
2258 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2259 (Display.STATE_DOZE_SUSPEND-1),
2260 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002261 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002262 };
2263 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2264 "screen off",
2265 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002266 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002267 "screen on",
2268 "screen on power save",
2269 "screen doze",
2270 "screen doze power save",
2271 "screen doze-suspend",
2272 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002273 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002274 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002275
2276 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002277 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002278 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002279 public abstract LevelStepTracker getDischargeLevelStepTracker();
2280
2281 /**
2282 * Return the array of daily discharge step durations.
2283 */
2284 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002285
2286 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002287 * Compute an approximation for how much time (in microseconds) remains until the battery
2288 * is fully charged. Returns -1 if no time can be computed: either there is not
2289 * enough current data to make a decision, or the battery is currently
2290 * discharging.
2291 *
2292 * @param curTime The current elepsed realtime in microseconds.
2293 */
2294 public abstract long computeChargeTimeRemaining(long curTime);
2295
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002296 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002297 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002298 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002299 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002300
2301 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002302 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002303 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002304 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002305
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002306 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2307
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002308 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002309
Evan Millarc64edde2009-04-18 12:26:32 -07002310 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002312 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2313
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002314 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 long days = seconds / (60 * 60 * 24);
2316 if (days != 0) {
2317 out.append(days);
2318 out.append("d ");
2319 }
2320 long used = days * 60 * 60 * 24;
2321
2322 long hours = (seconds - used) / (60 * 60);
2323 if (hours != 0 || used != 0) {
2324 out.append(hours);
2325 out.append("h ");
2326 }
2327 used += hours * 60 * 60;
2328
2329 long mins = (seconds-used) / 60;
2330 if (mins != 0 || used != 0) {
2331 out.append(mins);
2332 out.append("m ");
2333 }
2334 used += mins * 60;
2335
2336 if (seconds != 0 || used != 0) {
2337 out.append(seconds-used);
2338 out.append("s ");
2339 }
2340 }
2341
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002342 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002344 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 sb.append(time - (sec * 1000));
2346 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 }
2348
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002349 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002350 long sec = time / 1000;
2351 formatTimeRaw(sb, sec);
2352 sb.append(time - (sec * 1000));
2353 sb.append("ms");
2354 }
2355
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002356 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002358 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360 float perc = ((float)num) / ((float)den) * 100;
2361 mFormatBuilder.setLength(0);
2362 mFormatter.format("%.1f%%", perc);
2363 return mFormatBuilder.toString();
2364 }
2365
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002366 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002367 mFormatBuilder.setLength(0);
2368
2369 if (bytes < BYTES_PER_KB) {
2370 return bytes + "B";
2371 } else if (bytes < BYTES_PER_MB) {
2372 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2373 return mFormatBuilder.toString();
2374 } else if (bytes < BYTES_PER_GB){
2375 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2376 return mFormatBuilder.toString();
2377 } else {
2378 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2379 return mFormatBuilder.toString();
2380 }
2381 }
2382
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002383 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002384 if (timer != null) {
2385 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002386 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002387 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2388 return totalTimeMillis;
2389 }
2390 return 0;
2391 }
2392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 /**
2394 *
2395 * @param sb a StringBuilder object.
2396 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002397 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002399 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 * @param linePrefix a String to be prepended to each line of output.
2401 * @return the line prefix
2402 */
2403 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002404 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002407 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002408
Evan Millarc64edde2009-04-18 12:26:32 -07002409 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 if (totalTimeMillis != 0) {
2411 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002412 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002413 if (name != null) {
2414 sb.append(name);
2415 sb.append(' ');
2416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 sb.append('(');
2418 sb.append(count);
2419 sb.append(" times)");
2420 return ", ";
2421 }
2422 }
2423 return linePrefix;
2424 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002425
2426 /**
2427 *
2428 * @param pw a PrintWriter object to print to.
2429 * @param sb a StringBuilder object.
2430 * @param timer a Timer object contining the wakelock times.
2431 * @param rawRealtime the current on-battery time in microseconds.
2432 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2433 * @param prefix a String to be prepended to each line of output.
2434 * @param type the name of the timer.
2435 */
2436 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2437 long rawRealtime, int which, String prefix, String type) {
2438 if (timer != null) {
2439 // Convert from microseconds to milliseconds with rounding
2440 final long totalTime = (timer.getTotalTimeLocked(
2441 rawRealtime, which) + 500) / 1000;
2442 final int count = timer.getCountLocked(which);
2443 if (totalTime != 0) {
2444 sb.setLength(0);
2445 sb.append(prefix);
2446 sb.append(" ");
2447 sb.append(type);
2448 sb.append(": ");
2449 formatTimeMs(sb, totalTime);
2450 sb.append("realtime (");
2451 sb.append(count);
2452 sb.append(" times)");
2453 pw.println(sb.toString());
2454 return true;
2455 }
2456 }
2457 return false;
2458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459
2460 /**
2461 * Checkin version of wakelock printer. Prints simple comma-separated list.
2462 *
2463 * @param sb a StringBuilder object.
2464 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002465 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002467 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 * @param linePrefix a String to be prepended to each line of output.
2469 * @return the line prefix
2470 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002471 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2472 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 long totalTimeMicros = 0;
2474 int count = 0;
2475 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002476 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002477 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 }
2479 sb.append(linePrefix);
2480 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2481 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002482 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 sb.append(count);
2484 return ",";
2485 }
2486
2487 /**
2488 * Dump a comma-separated line of values for terse checkin mode.
2489 *
2490 * @param pw the PageWriter to dump log to
2491 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2492 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2493 * @param args type-dependent data arguments
2494 */
2495 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2496 Object... args ) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002497 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2498 pw.print(',');
2499 pw.print(uid);
2500 pw.print(',');
2501 pw.print(category);
2502 pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002503 pw.print(type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002504
2505 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002506 pw.print(',');
2507 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002509 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002511
2512 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002513 * Dump a given timer stat for terse checkin mode.
2514 *
2515 * @param pw the PageWriter to dump log to
2516 * @param uid the UID to log
2517 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2518 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2519 * @param timer a {@link Timer} to dump stats for
2520 * @param rawRealtime the current elapsed realtime of the system in microseconds
2521 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2522 */
2523 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2524 Timer timer, long rawRealtime, int which) {
2525 if (timer != null) {
2526 // Convert from microseconds to milliseconds with rounding
2527 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2528 / 1000;
2529 final int count = timer.getCountLocked(which);
2530 if (totalTime != 0) {
2531 dumpLine(pw, uid, category, type, totalTime, count);
2532 }
2533 }
2534 }
2535
2536 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002537 * Temporary for settings.
2538 */
2539 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2540 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2541 }
2542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 /**
2544 * Checkin server version of dump to produce more compact, computer-readable log.
2545 *
2546 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002548 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2549 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2551 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2552 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2554 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002555 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2556 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2557 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 final long totalRealtime = computeRealtime(rawRealtime, which);
2559 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002560 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002561 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002562 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002563 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2564 rawRealtime, which);
2565 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
2566 rawRealtime, which);
2567 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2568 rawRealtime, which);
2569 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
2570 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002571 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002572 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002573
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002574 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002576 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002577 final int NU = uidStats.size();
2578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002579 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 // Dump "battery" stat
2582 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002583 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002584 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002585 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002586 getStartClockTime(),
2587 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002589 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002590 long fullWakeLockTimeTotal = 0;
2591 long partialWakeLockTimeTotal = 0;
2592
2593 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002594 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002595
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002596 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2597 = u.getWakelockStats();
2598 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2599 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002600
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002601 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2602 if (fullWakeTimer != null) {
2603 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2604 which);
2605 }
2606
2607 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2608 if (partialWakeTimer != null) {
2609 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2610 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002611 }
2612 }
2613 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002614
2615 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002616 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2617 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2618 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2619 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2620 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2621 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2622 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2623 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002624 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2625 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
2626 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
2627
Adam Lesinskie283d332015-04-16 12:29:25 -07002628 // Dump Wifi controller stats
2629 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2630 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2631 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2632 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2633 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
2634 final long wifiPowerMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
2635 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA,
2636 wifiOnTime / 1000, wifiRunningTime / 1000,
2637 wifiIdleTimeMs, wifiRxTimeMs, wifiTxTimeMs, wifiPowerMaMs / (1000*60*60));
2638
2639 // Dump Bluetooth controller stats
2640 final long btIdleTimeMs = getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
2641 final long btRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
2642 final long btTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
2643 final long btPowerMaMs = getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which);
2644 dumpLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_DATA,
2645 btIdleTimeMs, btRxTimeMs, btTxTimeMs, btPowerMaMs / (1000*60*60));
2646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 // Dump misc stats
2648 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002649 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002650 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002651 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002652 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002653 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
2654 getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which), deviceIdlingTime / 1000,
2655 getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07002656 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002657 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
2658 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
2659 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
2660 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
2661 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002662
2663 // Dump screen brightness stats
2664 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2665 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002666 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002667 }
2668 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002669
Dianne Hackborn627bba72009-03-24 22:32:56 -07002670 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002671 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2672 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002673 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002674 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002675 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002676 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002677 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002678 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002679 args[i] = getPhoneSignalStrengthCount(i, which);
2680 }
2681 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002682
Dianne Hackborn627bba72009-03-24 22:32:56 -07002683 // Dump network type stats
2684 args = new Object[NUM_DATA_CONNECTION_TYPES];
2685 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002686 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002687 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002688 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2689 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2690 args[i] = getPhoneDataConnectionCount(i, which);
2691 }
2692 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002693
2694 // Dump wifi state stats
2695 args = new Object[NUM_WIFI_STATES];
2696 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002697 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002698 }
2699 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2700 for (int i=0; i<NUM_WIFI_STATES; i++) {
2701 args[i] = getWifiStateCount(i, which);
2702 }
2703 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2704
Dianne Hackborn3251b902014-06-20 14:40:53 -07002705 // Dump wifi suppl state stats
2706 args = new Object[NUM_WIFI_SUPPL_STATES];
2707 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2708 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2709 }
2710 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2711 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2712 args[i] = getWifiSupplStateCount(i, which);
2713 }
2714 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2715
2716 // Dump wifi signal strength stats
2717 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2718 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2719 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2720 }
2721 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2722 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2723 args[i] = getWifiSignalStrengthCount(i, which);
2724 }
2725 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2726
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002727 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002728 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002729 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002732 if (which == STATS_SINCE_UNPLUGGED) {
2733 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2734 getDischargeStartLevel()-getDischargeCurrentLevel(),
2735 getDischargeStartLevel()-getDischargeCurrentLevel(),
2736 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2737 } else {
2738 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2739 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002740 getDischargeAmountScreenOnSinceCharge(),
2741 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002742 }
2743
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002744 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002745 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002746 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002747 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002748 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002749 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002750 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002751 sb.toString());
2752 }
Evan Millarc64edde2009-04-18 12:26:32 -07002753 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002754 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002755 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002756 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2757 // Not doing the regular wake lock formatting to remain compatible
2758 // with the old checkin format.
2759 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2760 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002761 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002762 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002763 }
2764 }
Evan Millarc64edde2009-04-18 12:26:32 -07002765 }
2766
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002767 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002768 helper.create(this);
2769 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002770 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002771 if (sippers != null && sippers.size() > 0) {
2772 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2773 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002774 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002775 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2776 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2777 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002778 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002779 int uid = 0;
2780 String label;
2781 switch (bs.drainType) {
2782 case IDLE:
2783 label="idle";
2784 break;
2785 case CELL:
2786 label="cell";
2787 break;
2788 case PHONE:
2789 label="phone";
2790 break;
2791 case WIFI:
2792 label="wifi";
2793 break;
2794 case BLUETOOTH:
2795 label="blue";
2796 break;
2797 case SCREEN:
2798 label="scrn";
2799 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002800 case FLASHLIGHT:
2801 label="flashlight";
2802 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002803 case APP:
2804 uid = bs.uidObj.getUid();
2805 label = "uid";
2806 break;
2807 case USER:
2808 uid = UserHandle.getUid(bs.userId, 0);
2809 label = "user";
2810 break;
2811 case UNACCOUNTED:
2812 label = "unacc";
2813 break;
2814 case OVERCOUNTED:
2815 label = "over";
2816 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002817 case CAMERA:
2818 label = "camera";
2819 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002820 default:
2821 label = "???";
2822 }
2823 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07002824 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002825 }
2826 }
2827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 for (int iu = 0; iu < NU; iu++) {
2829 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002830 if (reqUid >= 0 && uid != reqUid) {
2831 continue;
2832 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002833 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07002834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002836 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2837 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2838 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2839 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2840 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2841 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2842 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
2843 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
2844 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2845 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002846 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2847 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002848 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002849 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2850 wifiBytesRx, wifiBytesTx,
2851 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002852 wifiPacketsRx, wifiPacketsTx,
2853 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002854 }
2855
Adam Lesinskie283d332015-04-16 12:29:25 -07002856 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2857 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2858 final int wifiScanCount = u.getWifiScanCount(which);
2859 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2860 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2861 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2862 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07002863 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinskie283d332015-04-16 12:29:25 -07002864 || uidWifiRunningTime != 0 || uidWifiIdleTimeMs != 0 || uidWifiRxTimeMs != 0
2865 || uidWifiTxTimeMs != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002866 dumpLine(pw, uid, category, WIFI_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002867 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount,
2868 uidWifiIdleTimeMs, uidWifiRxTimeMs, uidWifiTxTimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07002869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870
Dianne Hackborn617f8772009-03-31 15:04:46 -07002871 if (u.hasUserActivity()) {
2872 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2873 boolean hasData = false;
2874 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2875 int val = u.getUserActivityCount(i, which);
2876 args[i] = val;
2877 if (val != 0) hasData = true;
2878 }
2879 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07002880 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002881 }
2882 }
2883
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002884 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
2885 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2886 final Uid.Wakelock wl = wakelocks.valueAt(iw);
2887 String linePrefix = "";
2888 sb.setLength(0);
2889 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
2890 rawRealtime, "f", which, linePrefix);
2891 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
2892 rawRealtime, "p", which, linePrefix);
2893 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
2894 rawRealtime, "w", which, linePrefix);
2895
2896 // Only log if we had at lease one wakelock...
2897 if (sb.length() > 0) {
2898 String name = wakelocks.keyAt(iw);
2899 if (name.indexOf(',') >= 0) {
2900 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002902 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002905
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002906 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
2907 for (int isy=syncs.size()-1; isy>=0; isy--) {
2908 final Timer timer = syncs.valueAt(isy);
2909 // Convert from microseconds to milliseconds with rounding
2910 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2911 final int count = timer.getCountLocked(which);
2912 if (totalTime != 0) {
2913 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002914 }
2915 }
2916
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002917 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
2918 for (int ij=jobs.size()-1; ij>=0; ij--) {
2919 final Timer timer = jobs.valueAt(ij);
2920 // Convert from microseconds to milliseconds with rounding
2921 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2922 final int count = timer.getCountLocked(which);
2923 if (totalTime != 0) {
2924 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002925 }
2926 }
2927
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002928 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
2929 rawRealtime, which);
2930 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
2931 rawRealtime, which);
2932 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
2933 rawRealtime, which);
2934 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
2935 rawRealtime, which);
2936
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002937 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2938 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002939 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002940 final Uid.Sensor se = sensors.valueAt(ise);
2941 final int sensorNumber = sensors.keyAt(ise);
2942 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002943 if (timer != null) {
2944 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002945 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2946 / 1000;
2947 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002948 if (totalTime != 0) {
2949 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 }
2953
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002954 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
2955 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002956
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002957 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
2958 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002959
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002960 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07002961 long totalStateTime = 0;
2962 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08002963 final long time = u.getProcessStateTime(ips, rawRealtime, which);
2964 totalStateTime += time;
2965 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07002966 }
2967 if (totalStateTime > 0) {
2968 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2969 }
2970
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07002971 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
2972 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
2973 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
2974 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
2975 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
2976 powerCpuMaUs / 1000);
2977 }
2978
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002979 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
2980 = u.getProcessStats();
2981 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
2982 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002983
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002984 final long userMillis = ps.getUserTime(which);
2985 final long systemMillis = ps.getSystemTime(which);
2986 final long foregroundMillis = ps.getForegroundTime(which);
2987 final int starts = ps.getStarts(which);
2988 final int numCrashes = ps.getNumCrashes(which);
2989 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002990
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002991 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2992 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
2993 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
2994 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 }
2996 }
2997
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002998 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
2999 = u.getPackageStats();
3000 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3001 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3002 int wakeups = 0;
3003 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3004 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3005 wakeups += alarms.valueAt(iwa).getCountLocked(which);
3006 }
3007 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3008 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3009 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3010 final long startTime = ss.getStartTime(batteryUptime, which);
3011 final int starts = ss.getStarts(which);
3012 final int launches = ss.getLaunches(which);
3013 if (startTime != 0 || starts != 0 || launches != 0) {
3014 dumpLine(pw, uid, category, APK_DATA,
3015 wakeups, // wakeup alarms
3016 packageStats.keyAt(ipkg), // Apk
3017 serviceStats.keyAt(isvc), // service
3018 startTime / 1000, // time spent started, in ms
3019 starts,
3020 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 }
3022 }
3023 }
3024 }
3025 }
3026
Dianne Hackborn81038902012-11-26 17:04:09 -08003027 static final class TimerEntry {
3028 final String mName;
3029 final int mId;
3030 final BatteryStats.Timer mTimer;
3031 final long mTime;
3032 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3033 mName = name;
3034 mId = id;
3035 mTimer = timer;
3036 mTime = time;
3037 }
3038 }
3039
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003040 private void printmAh(PrintWriter printer, double power) {
3041 printer.print(BatteryStatsHelper.makemAh(power));
3042 }
3043
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003044 private void printmAh(StringBuilder sb, double power) {
3045 sb.append(BatteryStatsHelper.makemAh(power));
3046 }
3047
Dianne Hackbornd953c532014-08-16 18:17:38 -07003048 /**
3049 * Temporary for settings.
3050 */
3051 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3052 int reqUid) {
3053 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3054 }
3055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003057 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003058 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3060 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3061 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062
3063 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3064 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3065 final long totalRealtime = computeRealtime(rawRealtime, which);
3066 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003067 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3068 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3069 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003070 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3071 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003072
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003073 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003074
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003075 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003076 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003078 sb.setLength(0);
3079 sb.append(prefix);
3080 sb.append(" Time on battery: ");
3081 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3082 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3083 sb.append(") realtime, ");
3084 formatTimeMs(sb, whichBatteryUptime / 1000);
3085 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3086 sb.append(") uptime");
3087 pw.println(sb.toString());
3088 sb.setLength(0);
3089 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003090 sb.append(" Time on battery screen off: ");
3091 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3092 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3093 sb.append(") realtime, ");
3094 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3095 sb.append("(");
3096 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3097 sb.append(") uptime");
3098 pw.println(sb.toString());
3099 sb.setLength(0);
3100 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003101 sb.append(" Total run time: ");
3102 formatTimeMs(sb, totalRealtime / 1000);
3103 sb.append("realtime, ");
3104 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003105 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003106 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003107 if (batteryTimeRemaining >= 0) {
3108 sb.setLength(0);
3109 sb.append(prefix);
3110 sb.append(" Battery time remaining: ");
3111 formatTimeMs(sb, batteryTimeRemaining / 1000);
3112 pw.println(sb.toString());
3113 }
3114 if (chargeTimeRemaining >= 0) {
3115 sb.setLength(0);
3116 sb.append(prefix);
3117 sb.append(" Charge time remaining: ");
3118 formatTimeMs(sb, chargeTimeRemaining / 1000);
3119 pw.println(sb.toString());
3120 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003121 pw.print(" Start clock time: ");
3122 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3123
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003124 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003125 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003126 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003127 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3128 rawRealtime, which);
3129 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
3130 rawRealtime, which);
3131 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3132 rawRealtime, which);
3133 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
3134 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003135 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3136 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3137 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003138 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003139 sb.append(prefix);
3140 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3141 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003142 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003143 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3144 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003145 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003146 pw.println(sb.toString());
3147 sb.setLength(0);
3148 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003149 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003150 boolean didOne = false;
3151 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003152 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003153 if (time == 0) {
3154 continue;
3155 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003156 sb.append("\n ");
3157 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003158 didOne = true;
3159 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3160 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003161 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003162 sb.append("(");
3163 sb.append(formatRatioLocked(time, screenOnTime));
3164 sb.append(")");
3165 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003166 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003167 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003168 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003169 sb.setLength(0);
3170 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003171 sb.append(" Power save mode enabled: ");
3172 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003173 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003174 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003175 sb.append(")");
3176 pw.println(sb.toString());
3177 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003178 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003179 sb.setLength(0);
3180 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003181 sb.append(" Device light idling: ");
3182 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003183 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003184 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3185 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003186 sb.append("x");
3187 pw.println(sb.toString());
3188 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003189 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003190 sb.setLength(0);
3191 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003192 sb.append(" Idle mode light time: ");
3193 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003194 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003195 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3196 sb.append(") ");
3197 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003198 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003199 sb.append(" -- longest ");
3200 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3201 pw.println(sb.toString());
3202 }
3203 if (deviceIdlingTime != 0) {
3204 sb.setLength(0);
3205 sb.append(prefix);
3206 sb.append(" Device full idling: ");
3207 formatTimeMs(sb, deviceIdlingTime / 1000);
3208 sb.append("(");
3209 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3210 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which));
3211 sb.append("x");
3212 pw.println(sb.toString());
3213 }
3214 if (deviceIdleModeFullTime != 0) {
3215 sb.setLength(0);
3216 sb.append(prefix);
3217 sb.append(" Idle mode full time: ");
3218 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3219 sb.append("(");
3220 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3221 sb.append(") ");
3222 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which));
3223 sb.append("x");
3224 sb.append(" -- longest ");
3225 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003226 pw.println(sb.toString());
3227 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003228 if (phoneOnTime != 0) {
3229 sb.setLength(0);
3230 sb.append(prefix);
3231 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3232 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003233 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003234 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003235 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003236 if (connChanges != 0) {
3237 pw.print(prefix);
3238 pw.print(" Connectivity changes: "); pw.println(connChanges);
3239 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003240
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003241 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003242 long fullWakeLockTimeTotalMicros = 0;
3243 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003244
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003245 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003246
Evan Millar22ac0432009-03-31 11:33:18 -07003247 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003248 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003249
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003250 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3251 = u.getWakelockStats();
3252 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3253 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003254
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003255 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3256 if (fullWakeTimer != null) {
3257 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3258 rawRealtime, which);
3259 }
3260
3261 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3262 if (partialWakeTimer != null) {
3263 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3264 rawRealtime, which);
3265 if (totalTimeMicros > 0) {
3266 if (reqUid < 0) {
3267 // Only show the ordered list of all wake
3268 // locks if the caller is not asking for data
3269 // about a specific uid.
3270 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3271 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003272 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003273 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003274 }
3275 }
3276 }
3277 }
3278
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003279 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3280 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3281 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3282 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3283 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3284 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3285 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3286 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003287
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003288 if (fullWakeLockTimeTotalMicros != 0) {
3289 sb.setLength(0);
3290 sb.append(prefix);
3291 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3292 (fullWakeLockTimeTotalMicros + 500) / 1000);
3293 pw.println(sb.toString());
3294 }
3295
3296 if (partialWakeLockTimeTotalMicros != 0) {
3297 sb.setLength(0);
3298 sb.append(prefix);
3299 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3300 (partialWakeLockTimeTotalMicros + 500) / 1000);
3301 pw.println(sb.toString());
3302 }
3303
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003304 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003305 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3306 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3307 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3308 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003309 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003310 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003311 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003312 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003313 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003314 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003315 if (time == 0) {
3316 continue;
3317 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003318 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003319 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003320 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003321 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003322 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003323 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003324 sb.append("(");
3325 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003326 sb.append(") ");
3327 sb.append(getPhoneSignalStrengthCount(i, which));
3328 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003329 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003330 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003331 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003332
3333 sb.setLength(0);
3334 sb.append(prefix);
3335 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003336 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003337 pw.println(sb.toString());
3338
Dianne Hackborn627bba72009-03-24 22:32:56 -07003339 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003340 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003341 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003342 didOne = false;
3343 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003344 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003345 if (time == 0) {
3346 continue;
3347 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003348 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003349 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003350 didOne = true;
3351 sb.append(DATA_CONNECTION_NAMES[i]);
3352 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003353 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003354 sb.append("(");
3355 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003356 sb.append(") ");
3357 sb.append(getPhoneDataConnectionCount(i, which));
3358 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003359 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003360 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003361 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003362
3363 sb.setLength(0);
3364 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003365 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003366 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003367 formatTimeMs(sb, mobileActiveTime / 1000);
3368 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3369 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3370 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003371 pw.println(sb.toString());
3372
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003373 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3374 if (mobileActiveUnknownTime != 0) {
3375 sb.setLength(0);
3376 sb.append(prefix);
3377 sb.append(" Mobile radio active unknown time: ");
3378 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3379 sb.append("(");
3380 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3381 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3382 sb.append("x");
3383 pw.println(sb.toString());
3384 }
3385
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003386 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3387 if (mobileActiveAdjustedTime != 0) {
3388 sb.setLength(0);
3389 sb.append(prefix);
3390 sb.append(" Mobile radio active adjusted time: ");
3391 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3392 sb.append("(");
3393 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3394 sb.append(")");
3395 pw.println(sb.toString());
3396 }
3397
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003398 pw.print(prefix);
3399 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3400 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3401 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3402 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003403 sb.setLength(0);
3404 sb.append(prefix);
3405 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3406 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3407 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3408 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003409 sb.append(")");
3410 pw.println(sb.toString());
3411
3412 sb.setLength(0);
3413 sb.append(prefix);
3414 sb.append(" Wifi states:");
3415 didOne = false;
3416 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003417 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003418 if (time == 0) {
3419 continue;
3420 }
3421 sb.append("\n ");
3422 didOne = true;
3423 sb.append(WIFI_STATE_NAMES[i]);
3424 sb.append(" ");
3425 formatTimeMs(sb, time/1000);
3426 sb.append("(");
3427 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3428 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003429 sb.append(getWifiStateCount(i, which));
3430 sb.append("x");
3431 }
3432 if (!didOne) sb.append(" (no activity)");
3433 pw.println(sb.toString());
3434
3435 sb.setLength(0);
3436 sb.append(prefix);
3437 sb.append(" Wifi supplicant states:");
3438 didOne = false;
3439 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3440 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3441 if (time == 0) {
3442 continue;
3443 }
3444 sb.append("\n ");
3445 didOne = true;
3446 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3447 sb.append(" ");
3448 formatTimeMs(sb, time/1000);
3449 sb.append("(");
3450 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3451 sb.append(") ");
3452 sb.append(getWifiSupplStateCount(i, which));
3453 sb.append("x");
3454 }
3455 if (!didOne) sb.append(" (no activity)");
3456 pw.println(sb.toString());
3457
3458 sb.setLength(0);
3459 sb.append(prefix);
3460 sb.append(" Wifi signal levels:");
3461 didOne = false;
3462 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3463 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3464 if (time == 0) {
3465 continue;
3466 }
3467 sb.append("\n ");
3468 sb.append(prefix);
3469 didOne = true;
3470 sb.append("level(");
3471 sb.append(i);
3472 sb.append(") ");
3473 formatTimeMs(sb, time/1000);
3474 sb.append("(");
3475 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3476 sb.append(") ");
3477 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003478 sb.append("x");
3479 }
3480 if (!didOne) sb.append(" (no activity)");
3481 pw.println(sb.toString());
3482
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003483 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3484 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3485 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003486 final long wifiPowerDrainMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
Adam Lesinski33dac552015-03-09 15:24:48 -07003487 final long wifiTotalTimeMs = wifiIdleTimeMs + wifiRxTimeMs + wifiTxTimeMs;
3488
3489 sb.setLength(0);
3490 sb.append(prefix);
3491 sb.append(" WiFi Idle time: "); formatTimeMs(sb, wifiIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003492 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003493 sb.append(formatRatioLocked(wifiIdleTimeMs, wifiTotalTimeMs));
3494 sb.append(")");
3495 pw.println(sb.toString());
3496
3497 sb.setLength(0);
3498 sb.append(prefix);
3499 sb.append(" WiFi Rx time: "); formatTimeMs(sb, wifiRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003500 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003501 sb.append(formatRatioLocked(wifiRxTimeMs, wifiTotalTimeMs));
3502 sb.append(")");
3503 pw.println(sb.toString());
3504
3505 sb.setLength(0);
3506 sb.append(prefix);
3507 sb.append(" WiFi Tx time: "); formatTimeMs(sb, wifiTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003508 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003509 sb.append(formatRatioLocked(wifiTxTimeMs, wifiTotalTimeMs));
3510 sb.append(")");
3511 pw.println(sb.toString());
3512
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003513 sb.setLength(0);
3514 sb.append(prefix);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003515 sb.append(" WiFi Power drain: ").append(
3516 BatteryStatsHelper.makemAh(wifiPowerDrainMaMs / (double) (1000*60*60)));
3517 sb.append("mAh");
Adam Lesinskie08af192015-03-25 16:42:59 -07003518 pw.println(sb.toString());
3519
Adam Lesinski33dac552015-03-09 15:24:48 -07003520 final long bluetoothIdleTimeMs =
3521 getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
3522 final long bluetoothRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
3523 final long bluetoothTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
3524 final long bluetoothTotalTimeMs = bluetoothIdleTimeMs + bluetoothRxTimeMs +
3525 bluetoothTxTimeMs;
3526
3527 sb.setLength(0);
3528 sb.append(prefix);
3529 sb.append(" Bluetooth Idle time: "); formatTimeMs(sb, bluetoothIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003530 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003531 sb.append(formatRatioLocked(bluetoothIdleTimeMs, bluetoothTotalTimeMs));
3532 sb.append(")");
3533 pw.println(sb.toString());
3534
3535 sb.setLength(0);
3536 sb.append(prefix);
3537 sb.append(" Bluetooth Rx time: "); formatTimeMs(sb, bluetoothRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003538 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003539 sb.append(formatRatioLocked(bluetoothRxTimeMs, bluetoothTotalTimeMs));
3540 sb.append(")");
3541 pw.println(sb.toString());
3542
3543 sb.setLength(0);
3544 sb.append(prefix);
3545 sb.append(" Bluetooth Tx time: "); formatTimeMs(sb, bluetoothTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003546 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003547 sb.append(formatRatioLocked(bluetoothTxTimeMs, bluetoothTotalTimeMs));
3548 sb.append(")");
3549 pw.println(sb.toString());
3550
Adam Lesinskie283d332015-04-16 12:29:25 -07003551 sb.setLength(0);
3552 sb.append(prefix);
3553 sb.append(" Bluetooth Power drain: ").append(BatteryStatsHelper.makemAh(
3554 getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which) /
3555 (double)(1000*60*60)));
Adam Lesinski049c88b2015-05-28 11:38:12 -07003556 sb.append("mAh");
Adam Lesinskie283d332015-04-16 12:29:25 -07003557 pw.println(sb.toString());
3558
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003559 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003560
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003561 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003562 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003563 pw.print(prefix); pw.println(" Device is currently unplugged");
3564 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3565 pw.println(getDischargeStartLevel());
3566 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3567 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003568 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003569 pw.print(prefix); pw.println(" Device is currently plugged into power");
3570 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3571 pw.println(getDischargeStartLevel());
3572 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3573 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003574 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003575 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3576 pw.println(getDischargeAmountScreenOn());
3577 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3578 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003579 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003580 } else {
3581 pw.print(prefix); pw.println(" Device battery use since last full charge");
3582 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3583 pw.println(getLowDischargeAmountSinceCharge());
3584 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3585 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003586 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3587 pw.println(getDischargeAmountScreenOnSinceCharge());
3588 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3589 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003590 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003591 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003592
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003593 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003594 helper.create(this);
3595 helper.refreshStats(which, UserHandle.USER_ALL);
3596 List<BatterySipper> sippers = helper.getUsageList();
3597 if (sippers != null && sippers.size() > 0) {
3598 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3599 pw.print(prefix); pw.print(" Capacity: ");
3600 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003601 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003602 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3603 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3604 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3605 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003606 pw.println();
3607 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003608 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003609 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003610 switch (bs.drainType) {
3611 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003612 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003613 break;
3614 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003615 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003616 break;
3617 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003618 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003619 break;
3620 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003621 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003622 break;
3623 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003624 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003625 break;
3626 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003627 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003628 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003629 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003630 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003631 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003632 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003633 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003634 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003635 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003636 break;
3637 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003638 pw.print(" User "); pw.print(bs.userId);
3639 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003640 break;
3641 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003642 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003643 break;
3644 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003645 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003646 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003647 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003648 pw.print(" Camera: ");
3649 break;
3650 default:
3651 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003652 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003653 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003654 printmAh(pw, bs.totalPowerMah);
3655
Adam Lesinski57123002015-06-12 16:12:07 -07003656 if (bs.usagePowerMah != bs.totalPowerMah) {
3657 // If the usage (generic power) isn't the whole amount, we list out
3658 // what components are involved in the calculation.
3659
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003660 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003661 if (bs.usagePowerMah != 0) {
3662 pw.print(" usage=");
3663 printmAh(pw, bs.usagePowerMah);
3664 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003665 if (bs.cpuPowerMah != 0) {
3666 pw.print(" cpu=");
3667 printmAh(pw, bs.cpuPowerMah);
3668 }
3669 if (bs.wakeLockPowerMah != 0) {
3670 pw.print(" wake=");
3671 printmAh(pw, bs.wakeLockPowerMah);
3672 }
3673 if (bs.mobileRadioPowerMah != 0) {
3674 pw.print(" radio=");
3675 printmAh(pw, bs.mobileRadioPowerMah);
3676 }
3677 if (bs.wifiPowerMah != 0) {
3678 pw.print(" wifi=");
3679 printmAh(pw, bs.wifiPowerMah);
3680 }
3681 if (bs.gpsPowerMah != 0) {
3682 pw.print(" gps=");
3683 printmAh(pw, bs.gpsPowerMah);
3684 }
3685 if (bs.sensorPowerMah != 0) {
3686 pw.print(" sensor=");
3687 printmAh(pw, bs.sensorPowerMah);
3688 }
3689 if (bs.cameraPowerMah != 0) {
3690 pw.print(" camera=");
3691 printmAh(pw, bs.cameraPowerMah);
3692 }
3693 if (bs.flashlightPowerMah != 0) {
3694 pw.print(" flash=");
3695 printmAh(pw, bs.flashlightPowerMah);
3696 }
3697 pw.print(" )");
3698 }
3699 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003700 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003701 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003702 }
3703
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003704 sippers = helper.getMobilemsppList();
3705 if (sippers != null && sippers.size() > 0) {
3706 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003707 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003708 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003709 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003710 sb.setLength(0);
3711 sb.append(prefix); sb.append(" Uid ");
3712 UserHandle.formatUid(sb, bs.uidObj.getUid());
3713 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3714 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3715 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003716 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003717 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003718 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003719 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003720 sb.setLength(0);
3721 sb.append(prefix);
3722 sb.append(" TOTAL TIME: ");
3723 formatTimeMs(sb, totalTime);
3724 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3725 sb.append(")");
3726 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003727 pw.println();
3728 }
3729
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003730 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3731 @Override
3732 public int compare(TimerEntry lhs, TimerEntry rhs) {
3733 long lhsTime = lhs.mTime;
3734 long rhsTime = rhs.mTime;
3735 if (lhsTime < rhsTime) {
3736 return 1;
3737 }
3738 if (lhsTime > rhsTime) {
3739 return -1;
3740 }
3741 return 0;
3742 }
3743 };
3744
3745 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003746 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3747 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003748 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003749 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3750 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3751 : kernelWakelocks.entrySet()) {
3752 final BatteryStats.Timer timer = ent.getValue();
3753 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003754 if (totalTimeMillis > 0) {
3755 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3756 }
3757 }
3758 if (ktimers.size() > 0) {
3759 Collections.sort(ktimers, timerComparator);
3760 pw.print(prefix); pw.println(" All kernel wake locks:");
3761 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003762 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003763 String linePrefix = ": ";
3764 sb.setLength(0);
3765 sb.append(prefix);
3766 sb.append(" Kernel Wake lock ");
3767 sb.append(timer.mName);
3768 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3769 which, linePrefix);
3770 if (!linePrefix.equals(": ")) {
3771 sb.append(" realtime");
3772 // Only print out wake locks that were held
3773 pw.println(sb.toString());
3774 }
3775 }
3776 pw.println();
3777 }
3778 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003779
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003780 if (timers.size() > 0) {
3781 Collections.sort(timers, timerComparator);
3782 pw.print(prefix); pw.println(" All partial wake locks:");
3783 for (int i=0; i<timers.size(); i++) {
3784 TimerEntry timer = timers.get(i);
3785 sb.setLength(0);
3786 sb.append(" Wake lock ");
3787 UserHandle.formatUid(sb, timer.mId);
3788 sb.append(" ");
3789 sb.append(timer.mName);
3790 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3791 sb.append(" realtime");
3792 pw.println(sb.toString());
3793 }
3794 timers.clear();
3795 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003796 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003797
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003798 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003799 if (wakeupReasons.size() > 0) {
3800 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003801 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003802 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003803 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003804 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3805 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003806 }
3807 Collections.sort(reasons, timerComparator);
3808 for (int i=0; i<reasons.size(); i++) {
3809 TimerEntry timer = reasons.get(i);
3810 String linePrefix = ": ";
3811 sb.setLength(0);
3812 sb.append(prefix);
3813 sb.append(" Wakeup reason ");
3814 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003815 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3816 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003817 pw.println(sb.toString());
3818 }
3819 pw.println();
3820 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003821 }
Evan Millar22ac0432009-03-31 11:33:18 -07003822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 for (int iu=0; iu<NU; iu++) {
3824 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003825 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003826 continue;
3827 }
3828
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003829 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003830
3831 pw.print(prefix);
3832 pw.print(" ");
3833 UserHandle.formatUid(pw, uid);
3834 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003836
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003837 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3838 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3839 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3840 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3841 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3842 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3843 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3844 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
3845 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3846 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
3847 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3848 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3849 final int wifiScanCount = u.getWifiScanCount(which);
3850 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003851
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003852 if (mobileRxBytes > 0 || mobileTxBytes > 0
3853 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003854 pw.print(prefix); pw.print(" Mobile network: ");
3855 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003856 pw.print(formatBytesLocked(mobileTxBytes));
3857 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3858 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003860 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3861 sb.setLength(0);
3862 sb.append(prefix); sb.append(" Mobile radio active: ");
3863 formatTimeMs(sb, uidMobileActiveTime / 1000);
3864 sb.append("(");
3865 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3866 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3867 long packets = mobileRxPackets + mobileTxPackets;
3868 if (packets == 0) {
3869 packets = 1;
3870 }
3871 sb.append(" @ ");
3872 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3873 sb.append(" mspp");
3874 pw.println(sb.toString());
3875 }
3876
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003877 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003878 pw.print(prefix); pw.print(" Wi-Fi network: ");
3879 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003880 pw.print(formatBytesLocked(wifiTxBytes));
3881 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3882 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003883 }
3884
Dianne Hackborn62793e42015-03-09 11:15:41 -07003885 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003886 || uidWifiRunningTime != 0) {
3887 sb.setLength(0);
3888 sb.append(prefix); sb.append(" Wifi Running: ");
3889 formatTimeMs(sb, uidWifiRunningTime / 1000);
3890 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3891 whichBatteryRealtime)); sb.append(")\n");
3892 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3893 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3894 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3895 whichBatteryRealtime)); sb.append(")\n");
3896 sb.append(prefix); sb.append(" Wifi Scan: ");
3897 formatTimeMs(sb, wifiScanTime / 1000);
3898 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07003899 whichBatteryRealtime)); sb.append(") ");
3900 sb.append(wifiScanCount);
3901 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003902 pw.println(sb.toString());
3903 }
3904
Adam Lesinski049c88b2015-05-28 11:38:12 -07003905 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3906 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3907 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
3908 final long uidWifiTotalTimeMs = uidWifiIdleTimeMs + uidWifiRxTimeMs + uidWifiTxTimeMs;
3909 if (uidWifiTotalTimeMs > 0) {
3910 sb.setLength(0);
3911 sb.append(prefix).append(" WiFi Idle time: ");
3912 formatTimeMs(sb, uidWifiIdleTimeMs);
3913 sb.append("(").append(formatRatioLocked(uidWifiIdleTimeMs, uidWifiTotalTimeMs))
3914 .append(")\n");
3915
3916 sb.append(prefix).append(" WiFi Rx time: "); formatTimeMs(sb, uidWifiRxTimeMs);
3917 sb.append("(").append(formatRatioLocked(uidWifiRxTimeMs, uidWifiTotalTimeMs))
3918 .append(")\n");
3919
3920 sb.append(prefix).append(" WiFi Tx time: "); formatTimeMs(sb, uidWifiTxTimeMs);
3921 sb.append("(").append(formatRatioLocked(uidWifiTxTimeMs, uidWifiTotalTimeMs))
3922 .append(")");
3923 pw.println(sb.toString());
3924 }
3925
Dianne Hackborn617f8772009-03-31 15:04:46 -07003926 if (u.hasUserActivity()) {
3927 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07003928 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003929 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003930 if (val != 0) {
3931 if (!hasData) {
3932 sb.setLength(0);
3933 sb.append(" User activity: ");
3934 hasData = true;
3935 } else {
3936 sb.append(", ");
3937 }
3938 sb.append(val);
3939 sb.append(" ");
3940 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3941 }
3942 }
3943 if (hasData) {
3944 pw.println(sb.toString());
3945 }
3946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003948 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3949 = u.getWakelockStats();
3950 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003951 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003952 int countWakelock = 0;
3953 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3954 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3955 String linePrefix = ": ";
3956 sb.setLength(0);
3957 sb.append(prefix);
3958 sb.append(" Wake lock ");
3959 sb.append(wakelocks.keyAt(iw));
3960 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
3961 "full", which, linePrefix);
3962 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
3963 "partial", which, linePrefix);
3964 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
3965 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003966 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
3967 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07003968 sb.append(" realtime");
3969 pw.println(sb.toString());
3970 uidActivity = true;
3971 countWakelock++;
3972
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003973 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
3974 rawRealtime, which);
3975 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
3976 rawRealtime, which);
3977 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
3978 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003979 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07003980 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003981 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003982 if (countWakelock > 1) {
3983 if (totalFullWakelock != 0 || totalPartialWakelock != 0
3984 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003985 sb.setLength(0);
3986 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003987 sb.append(" TOTAL wake: ");
3988 boolean needComma = false;
3989 if (totalFullWakelock != 0) {
3990 needComma = true;
3991 formatTimeMs(sb, totalFullWakelock);
3992 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003993 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003994 if (totalPartialWakelock != 0) {
3995 if (needComma) {
3996 sb.append(", ");
3997 }
3998 needComma = true;
3999 formatTimeMs(sb, totalPartialWakelock);
4000 sb.append("partial");
4001 }
4002 if (totalWindowWakelock != 0) {
4003 if (needComma) {
4004 sb.append(", ");
4005 }
4006 needComma = true;
4007 formatTimeMs(sb, totalWindowWakelock);
4008 sb.append("window");
4009 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004010 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004011 if (needComma) {
4012 sb.append(",");
4013 }
4014 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004015 formatTimeMs(sb, totalDrawWakelock);
4016 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004017 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004018 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004019 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004020 }
4021 }
4022
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004023 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4024 for (int isy=syncs.size()-1; isy>=0; isy--) {
4025 final Timer timer = syncs.valueAt(isy);
4026 // Convert from microseconds to milliseconds with rounding
4027 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4028 final int count = timer.getCountLocked(which);
4029 sb.setLength(0);
4030 sb.append(prefix);
4031 sb.append(" Sync ");
4032 sb.append(syncs.keyAt(isy));
4033 sb.append(": ");
4034 if (totalTime != 0) {
4035 formatTimeMs(sb, totalTime);
4036 sb.append("realtime (");
4037 sb.append(count);
4038 sb.append(" times)");
4039 } else {
4040 sb.append("(not used)");
4041 }
4042 pw.println(sb.toString());
4043 uidActivity = true;
4044 }
4045
4046 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4047 for (int ij=jobs.size()-1; ij>=0; ij--) {
4048 final Timer timer = jobs.valueAt(ij);
4049 // Convert from microseconds to milliseconds with rounding
4050 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4051 final int count = timer.getCountLocked(which);
4052 sb.setLength(0);
4053 sb.append(prefix);
4054 sb.append(" Job ");
4055 sb.append(jobs.keyAt(ij));
4056 sb.append(": ");
4057 if (totalTime != 0) {
4058 formatTimeMs(sb, totalTime);
4059 sb.append("realtime (");
4060 sb.append(count);
4061 sb.append(" times)");
4062 } else {
4063 sb.append("(not used)");
4064 }
4065 pw.println(sb.toString());
4066 uidActivity = true;
4067 }
4068
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004069 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4070 prefix, "Flashlight");
4071 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4072 prefix, "Camera");
4073 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4074 prefix, "Video");
4075 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4076 prefix, "Audio");
4077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004078 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4079 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004080 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004081 final Uid.Sensor se = sensors.valueAt(ise);
4082 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004083 sb.setLength(0);
4084 sb.append(prefix);
4085 sb.append(" Sensor ");
4086 int handle = se.getHandle();
4087 if (handle == Uid.Sensor.GPS) {
4088 sb.append("GPS");
4089 } else {
4090 sb.append(handle);
4091 }
4092 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004094 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004095 if (timer != null) {
4096 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004097 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004098 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004099 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004100 //timer.logState();
4101 if (totalTime != 0) {
4102 formatTimeMs(sb, totalTime);
4103 sb.append("realtime (");
4104 sb.append(count);
4105 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 } else {
4107 sb.append("(not used)");
4108 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004109 } else {
4110 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004112
4113 pw.println(sb.toString());
4114 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 }
4116
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004117 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4118 "Vibrator");
4119 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4120 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004121
Dianne Hackborn61659e52014-07-09 16:13:01 -07004122 long totalStateTime = 0;
4123 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4124 long time = u.getProcessStateTime(ips, rawRealtime, which);
4125 if (time > 0) {
4126 totalStateTime += time;
4127 sb.setLength(0);
4128 sb.append(prefix);
4129 sb.append(" ");
4130 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4131 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004132 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004133 pw.println(sb.toString());
4134 uidActivity = true;
4135 }
4136 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004137 if (totalStateTime > 0) {
4138 sb.setLength(0);
4139 sb.append(prefix);
4140 sb.append(" Total running: ");
4141 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4142 pw.println(sb.toString());
4143 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004144
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004145 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4146 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004147 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4148 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004149 sb.setLength(0);
4150 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004151 sb.append(" Total cpu time: u=");
4152 formatTimeMs(sb, userCpuTimeUs / 1000);
4153 sb.append("s=");
4154 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004155 sb.append("p=");
4156 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4157 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004158 pw.println(sb.toString());
4159 }
4160
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004161 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4162 = u.getProcessStats();
4163 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4164 final Uid.Proc ps = processStats.valueAt(ipr);
4165 long userTime;
4166 long systemTime;
4167 long foregroundTime;
4168 int starts;
4169 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004171 userTime = ps.getUserTime(which);
4172 systemTime = ps.getSystemTime(which);
4173 foregroundTime = ps.getForegroundTime(which);
4174 starts = ps.getStarts(which);
4175 final int numCrashes = ps.getNumCrashes(which);
4176 final int numAnrs = ps.getNumAnrs(which);
4177 numExcessive = which == STATS_SINCE_CHARGED
4178 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004180 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4181 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4182 sb.setLength(0);
4183 sb.append(prefix); sb.append(" Proc ");
4184 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4185 sb.append(prefix); sb.append(" CPU: ");
4186 formatTimeMs(sb, userTime); sb.append("usr + ");
4187 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4188 formatTimeMs(sb, foregroundTime); sb.append("fg");
4189 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4190 sb.append("\n"); sb.append(prefix); sb.append(" ");
4191 boolean hasOne = false;
4192 if (starts != 0) {
4193 hasOne = true;
4194 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004195 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004196 if (numCrashes != 0) {
4197 if (hasOne) {
4198 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004199 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004200 hasOne = true;
4201 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004202 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004203 if (numAnrs != 0) {
4204 if (hasOne) {
4205 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004207 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 }
4209 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004210 pw.println(sb.toString());
4211 for (int e=0; e<numExcessive; e++) {
4212 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4213 if (ew != null) {
4214 pw.print(prefix); pw.print(" * Killed for ");
4215 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4216 pw.print("wake lock");
4217 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4218 pw.print("cpu");
4219 } else {
4220 pw.print("unknown");
4221 }
4222 pw.print(" use: ");
4223 TimeUtils.formatDuration(ew.usedTime, pw);
4224 pw.print(" over ");
4225 TimeUtils.formatDuration(ew.overTime, pw);
4226 if (ew.overTime != 0) {
4227 pw.print(" (");
4228 pw.print((ew.usedTime*100)/ew.overTime);
4229 pw.println("%)");
4230 }
4231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 }
4233 uidActivity = true;
4234 }
4235 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004236
4237 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4238 = u.getPackageStats();
4239 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4240 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4241 pw.println(":");
4242 boolean apkActivity = false;
4243 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4244 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4245 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4246 pw.print(prefix); pw.print(" Wakeup alarm ");
4247 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4248 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4249 pw.println(" times");
4250 apkActivity = true;
4251 }
4252 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4253 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4254 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4255 final long startTime = ss.getStartTime(batteryUptime, which);
4256 final int starts = ss.getStarts(which);
4257 final int launches = ss.getLaunches(which);
4258 if (startTime != 0 || starts != 0 || launches != 0) {
4259 sb.setLength(0);
4260 sb.append(prefix); sb.append(" Service ");
4261 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4262 sb.append(prefix); sb.append(" Created for: ");
4263 formatTimeMs(sb, startTime / 1000);
4264 sb.append("uptime\n");
4265 sb.append(prefix); sb.append(" Starts: ");
4266 sb.append(starts);
4267 sb.append(", launches: "); sb.append(launches);
4268 pw.println(sb.toString());
4269 apkActivity = true;
4270 }
4271 }
4272 if (!apkActivity) {
4273 pw.print(prefix); pw.println(" (nothing executed)");
4274 }
4275 uidActivity = true;
4276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004278 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 }
4280 }
4281 }
4282
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004283 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004284 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004285 int diff = oldval ^ newval;
4286 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004287 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004288 for (int i=0; i<descriptions.length; i++) {
4289 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004290 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004291 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004292 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004293 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004294 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004295 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4296 didWake = true;
4297 pw.print("=");
4298 if (longNames) {
4299 UserHandle.formatUid(pw, wakelockTag.uid);
4300 pw.print(":\"");
4301 pw.print(wakelockTag.string);
4302 pw.print("\"");
4303 } else {
4304 pw.print(wakelockTag.poolIdx);
4305 }
4306 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004307 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004308 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004309 pw.print("=");
4310 int val = (newval&bd.mask)>>bd.shift;
4311 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004312 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004313 } else {
4314 pw.print(val);
4315 }
4316 }
4317 }
4318 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004319 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004320 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004321 if (longNames) {
4322 UserHandle.formatUid(pw, wakelockTag.uid);
4323 pw.print(":\"");
4324 pw.print(wakelockTag.string);
4325 pw.print("\"");
4326 } else {
4327 pw.print(wakelockTag.poolIdx);
4328 }
4329 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004330 }
4331
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004332 public void prepareForDumpLocked() {
4333 }
4334
4335 public static class HistoryPrinter {
4336 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004337 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004338 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004339 int oldStatus = -1;
4340 int oldHealth = -1;
4341 int oldPlug = -1;
4342 int oldTemp = -1;
4343 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004344 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004345
Dianne Hackborn3251b902014-06-20 14:40:53 -07004346 void reset() {
4347 oldState = oldState2 = 0;
4348 oldLevel = -1;
4349 oldStatus = -1;
4350 oldHealth = -1;
4351 oldPlug = -1;
4352 oldTemp = -1;
4353 oldVolt = -1;
4354 }
4355
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004356 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004357 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004358 if (!checkin) {
4359 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004360 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004361 pw.print(" (");
4362 pw.print(rec.numReadInts);
4363 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004364 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004365 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4366 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004367 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004368 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004369 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004370 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004371 }
4372 lastTime = rec.time;
4373 }
4374 if (rec.cmd == HistoryItem.CMD_START) {
4375 if (checkin) {
4376 pw.print(":");
4377 }
4378 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004379 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004380 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4381 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004382 if (checkin) {
4383 pw.print(":");
4384 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004385 if (rec.cmd == HistoryItem.CMD_RESET) {
4386 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004387 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004388 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004389 pw.print("TIME:");
4390 if (checkin) {
4391 pw.println(rec.currentTime);
4392 } else {
4393 pw.print(" ");
4394 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4395 rec.currentTime).toString());
4396 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004397 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4398 if (checkin) {
4399 pw.print(":");
4400 }
4401 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004402 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4403 if (checkin) {
4404 pw.print(":");
4405 }
4406 pw.println("*OVERFLOW*");
4407 } else {
4408 if (!checkin) {
4409 if (rec.batteryLevel < 10) pw.print("00");
4410 else if (rec.batteryLevel < 100) pw.print("0");
4411 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004412 if (verbose) {
4413 pw.print(" ");
4414 if (rec.states < 0) ;
4415 else if (rec.states < 0x10) pw.print("0000000");
4416 else if (rec.states < 0x100) pw.print("000000");
4417 else if (rec.states < 0x1000) pw.print("00000");
4418 else if (rec.states < 0x10000) pw.print("0000");
4419 else if (rec.states < 0x100000) pw.print("000");
4420 else if (rec.states < 0x1000000) pw.print("00");
4421 else if (rec.states < 0x10000000) pw.print("0");
4422 pw.print(Integer.toHexString(rec.states));
4423 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004424 } else {
4425 if (oldLevel != rec.batteryLevel) {
4426 oldLevel = rec.batteryLevel;
4427 pw.print(",Bl="); pw.print(rec.batteryLevel);
4428 }
4429 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004430 if (oldStatus != rec.batteryStatus) {
4431 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004432 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004433 switch (oldStatus) {
4434 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004435 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004436 break;
4437 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004438 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004439 break;
4440 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004441 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004442 break;
4443 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004444 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004445 break;
4446 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004447 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004448 break;
4449 default:
4450 pw.print(oldStatus);
4451 break;
4452 }
4453 }
4454 if (oldHealth != rec.batteryHealth) {
4455 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004456 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004457 switch (oldHealth) {
4458 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004459 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004460 break;
4461 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004462 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004463 break;
4464 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004465 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004466 break;
4467 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004468 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004469 break;
4470 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004471 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004472 break;
4473 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004474 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004475 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004476 case BatteryManager.BATTERY_HEALTH_COLD:
4477 pw.print(checkin ? "c" : "cold");
4478 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004479 default:
4480 pw.print(oldHealth);
4481 break;
4482 }
4483 }
4484 if (oldPlug != rec.batteryPlugType) {
4485 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004486 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004487 switch (oldPlug) {
4488 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004489 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004490 break;
4491 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004492 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004493 break;
4494 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004495 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004496 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004497 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004498 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004499 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004500 default:
4501 pw.print(oldPlug);
4502 break;
4503 }
4504 }
4505 if (oldTemp != rec.batteryTemperature) {
4506 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004507 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004508 pw.print(oldTemp);
4509 }
4510 if (oldVolt != rec.batteryVoltage) {
4511 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004512 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004513 pw.print(oldVolt);
4514 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004515 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004516 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004517 printBitDescriptions(pw, oldState2, rec.states2, null,
4518 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004519 if (rec.wakeReasonTag != null) {
4520 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004521 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004522 pw.print(rec.wakeReasonTag.poolIdx);
4523 } else {
4524 pw.print(" wake_reason=");
4525 pw.print(rec.wakeReasonTag.uid);
4526 pw.print(":\"");
4527 pw.print(rec.wakeReasonTag.string);
4528 pw.print("\"");
4529 }
4530 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004531 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004532 pw.print(checkin ? "," : " ");
4533 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4534 pw.print("+");
4535 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4536 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004537 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004538 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4539 : HISTORY_EVENT_NAMES;
4540 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4541 | HistoryItem.EVENT_FLAG_FINISH);
4542 if (idx >= 0 && idx < eventNames.length) {
4543 pw.print(eventNames[idx]);
4544 } else {
4545 pw.print(checkin ? "Ev" : "event");
4546 pw.print(idx);
4547 }
4548 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004549 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004550 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004551 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004552 UserHandle.formatUid(pw, rec.eventTag.uid);
4553 pw.print(":\"");
4554 pw.print(rec.eventTag.string);
4555 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004556 }
4557 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004558 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004559 if (rec.stepDetails != null) {
4560 if (!checkin) {
4561 pw.print(" Details: cpu=");
4562 pw.print(rec.stepDetails.userTime);
4563 pw.print("u+");
4564 pw.print(rec.stepDetails.systemTime);
4565 pw.print("s");
4566 if (rec.stepDetails.appCpuUid1 >= 0) {
4567 pw.print(" (");
4568 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4569 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4570 if (rec.stepDetails.appCpuUid2 >= 0) {
4571 pw.print(", ");
4572 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4573 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4574 }
4575 if (rec.stepDetails.appCpuUid3 >= 0) {
4576 pw.print(", ");
4577 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4578 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4579 }
4580 pw.print(')');
4581 }
4582 pw.println();
4583 pw.print(" /proc/stat=");
4584 pw.print(rec.stepDetails.statUserTime);
4585 pw.print(" usr, ");
4586 pw.print(rec.stepDetails.statSystemTime);
4587 pw.print(" sys, ");
4588 pw.print(rec.stepDetails.statIOWaitTime);
4589 pw.print(" io, ");
4590 pw.print(rec.stepDetails.statIrqTime);
4591 pw.print(" irq, ");
4592 pw.print(rec.stepDetails.statSoftIrqTime);
4593 pw.print(" sirq, ");
4594 pw.print(rec.stepDetails.statIdlTime);
4595 pw.print(" idle");
4596 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4597 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4598 + rec.stepDetails.statSoftIrqTime;
4599 int total = totalRun + rec.stepDetails.statIdlTime;
4600 if (total > 0) {
4601 pw.print(" (");
4602 float perc = ((float)totalRun) / ((float)total) * 100;
4603 pw.print(String.format("%.1f%%", perc));
4604 pw.print(" of ");
4605 StringBuilder sb = new StringBuilder(64);
4606 formatTimeMsNoSpace(sb, total*10);
4607 pw.print(sb);
4608 pw.print(")");
4609 }
4610 pw.println();
4611 } else {
4612 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4613 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4614 pw.print(rec.stepDetails.userTime);
4615 pw.print(":");
4616 pw.print(rec.stepDetails.systemTime);
4617 if (rec.stepDetails.appCpuUid1 >= 0) {
4618 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4619 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4620 if (rec.stepDetails.appCpuUid2 >= 0) {
4621 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4622 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4623 }
4624 if (rec.stepDetails.appCpuUid3 >= 0) {
4625 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4626 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4627 }
4628 }
4629 pw.println();
4630 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4631 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4632 pw.print(rec.stepDetails.statUserTime);
4633 pw.print(',');
4634 pw.print(rec.stepDetails.statSystemTime);
4635 pw.print(',');
4636 pw.print(rec.stepDetails.statIOWaitTime);
4637 pw.print(',');
4638 pw.print(rec.stepDetails.statIrqTime);
4639 pw.print(',');
4640 pw.print(rec.stepDetails.statSoftIrqTime);
4641 pw.print(',');
4642 pw.print(rec.stepDetails.statIdlTime);
4643 pw.println();
4644 }
4645 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004646 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004647 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004648 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004649 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004650
4651 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4652 UserHandle.formatUid(pw, uid);
4653 pw.print("=");
4654 pw.print(utime);
4655 pw.print("u+");
4656 pw.print(stime);
4657 pw.print("s");
4658 }
4659
4660 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4661 pw.print('/');
4662 pw.print(uid);
4663 pw.print(":");
4664 pw.print(utime);
4665 pw.print(":");
4666 pw.print(stime);
4667 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004668 }
4669
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004670 private void printSizeValue(PrintWriter pw, long size) {
4671 float result = size;
4672 String suffix = "";
4673 if (result >= 10*1024) {
4674 suffix = "KB";
4675 result = result / 1024;
4676 }
4677 if (result >= 10*1024) {
4678 suffix = "MB";
4679 result = result / 1024;
4680 }
4681 if (result >= 10*1024) {
4682 suffix = "GB";
4683 result = result / 1024;
4684 }
4685 if (result >= 10*1024) {
4686 suffix = "TB";
4687 result = result / 1024;
4688 }
4689 if (result >= 10*1024) {
4690 suffix = "PB";
4691 result = result / 1024;
4692 }
4693 pw.print((int)result);
4694 pw.print(suffix);
4695 }
4696
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004697 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4698 String label3, long estimatedTime) {
4699 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004700 return false;
4701 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004702 pw.print(label1);
4703 pw.print(label2);
4704 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004705 StringBuilder sb = new StringBuilder(64);
4706 formatTimeMs(sb, estimatedTime);
4707 pw.print(sb);
4708 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004709 return true;
4710 }
4711
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004712 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4713 LevelStepTracker steps, boolean checkin) {
4714 if (steps == null) {
4715 return false;
4716 }
4717 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004718 if (count <= 0) {
4719 return false;
4720 }
4721 if (!checkin) {
4722 pw.println(header);
4723 }
Kweku Adams030980a2015-04-01 16:07:48 -07004724 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004725 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004726 long duration = steps.getDurationAt(i);
4727 int level = steps.getLevelAt(i);
4728 long initMode = steps.getInitModeAt(i);
4729 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004730 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004731 lineArgs[0] = Long.toString(duration);
4732 lineArgs[1] = Integer.toString(level);
4733 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4734 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4735 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4736 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4737 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4738 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004739 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004740 }
4741 } else {
4742 lineArgs[2] = "";
4743 }
4744 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4745 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4746 } else {
4747 lineArgs[3] = "";
4748 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004749 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004750 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004751 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004752 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004753 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004754 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4755 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004756 pw.print(prefix);
4757 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004758 TimeUtils.formatDuration(duration, pw);
4759 pw.print(" to "); pw.print(level);
4760 boolean haveModes = false;
4761 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4762 pw.print(" (");
4763 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4764 case Display.STATE_OFF: pw.print("screen-off"); break;
4765 case Display.STATE_ON: pw.print("screen-on"); break;
4766 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4767 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004768 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004769 }
4770 haveModes = true;
4771 }
4772 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4773 pw.print(haveModes ? ", " : " (");
4774 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4775 ? "power-save-on" : "power-save-off");
4776 haveModes = true;
4777 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004778 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4779 pw.print(haveModes ? ", " : " (");
4780 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4781 ? "device-idle-on" : "device-idle-off");
4782 haveModes = true;
4783 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004784 if (haveModes) {
4785 pw.print(")");
4786 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004787 pw.println();
4788 }
4789 }
4790 return true;
4791 }
4792
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004793 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004794 public static final int DUMP_DAILY_ONLY = 1<<2;
4795 public static final int DUMP_HISTORY_ONLY = 1<<3;
4796 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4797 public static final int DUMP_VERBOSE = 1<<5;
4798 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004799
Dianne Hackborn37de0982014-05-09 09:32:18 -07004800 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4801 final HistoryPrinter hprinter = new HistoryPrinter();
4802 final HistoryItem rec = new HistoryItem();
4803 long lastTime = -1;
4804 long baseTime = -1;
4805 boolean printed = false;
4806 HistoryEventTracker tracker = null;
4807 while (getNextHistoryLocked(rec)) {
4808 lastTime = rec.time;
4809 if (baseTime < 0) {
4810 baseTime = lastTime;
4811 }
4812 if (rec.time >= histStart) {
4813 if (histStart >= 0 && !printed) {
4814 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004815 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004816 || rec.cmd == HistoryItem.CMD_START
4817 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004818 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004819 hprinter.printNextItem(pw, rec, baseTime, checkin,
4820 (flags&DUMP_VERBOSE) != 0);
4821 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004822 } else if (rec.currentTime != 0) {
4823 printed = true;
4824 byte cmd = rec.cmd;
4825 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004826 hprinter.printNextItem(pw, rec, baseTime, checkin,
4827 (flags&DUMP_VERBOSE) != 0);
4828 rec.cmd = cmd;
4829 }
4830 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004831 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4832 hprinter.printNextItem(pw, rec, baseTime, checkin,
4833 (flags&DUMP_VERBOSE) != 0);
4834 rec.cmd = HistoryItem.CMD_UPDATE;
4835 }
4836 int oldEventCode = rec.eventCode;
4837 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004838 rec.eventTag = new HistoryTag();
4839 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4840 HashMap<String, SparseIntArray> active
4841 = tracker.getStateForEvent(i);
4842 if (active == null) {
4843 continue;
4844 }
4845 for (HashMap.Entry<String, SparseIntArray> ent
4846 : active.entrySet()) {
4847 SparseIntArray uids = ent.getValue();
4848 for (int j=0; j<uids.size(); j++) {
4849 rec.eventCode = i;
4850 rec.eventTag.string = ent.getKey();
4851 rec.eventTag.uid = uids.keyAt(j);
4852 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07004853 hprinter.printNextItem(pw, rec, baseTime, checkin,
4854 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004855 rec.wakeReasonTag = null;
4856 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004857 }
4858 }
4859 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004860 rec.eventCode = oldEventCode;
4861 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004862 tracker = null;
4863 }
4864 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004865 hprinter.printNextItem(pw, rec, baseTime, checkin,
4866 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004867 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
4868 // This is an attempt to aggregate the previous state and generate
4869 // fake events to reflect that state at the point where we start
4870 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07004871 if (tracker == null) {
4872 tracker = new HistoryEventTracker();
4873 }
4874 tracker.updateState(rec.eventCode, rec.eventTag.string,
4875 rec.eventTag.uid, rec.eventTag.poolIdx);
4876 }
4877 }
4878 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004879 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004880 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
4881 }
4882 }
4883
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004884 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
4885 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
4886 if (steps == null) {
4887 return;
4888 }
4889 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
4890 if (timeRemaining >= 0) {
4891 pw.print(prefix); pw.print(label); pw.print(" total time: ");
4892 tmpSb.setLength(0);
4893 formatTimeMs(tmpSb, timeRemaining);
4894 pw.print(tmpSb);
4895 pw.print(" (from "); pw.print(tmpOutInt[0]);
4896 pw.println(" steps)");
4897 }
4898 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4899 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4900 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
4901 if (estimatedTime > 0) {
4902 pw.print(prefix); pw.print(label); pw.print(" ");
4903 pw.print(STEP_LEVEL_MODE_LABELS[i]);
4904 pw.print(" time: ");
4905 tmpSb.setLength(0);
4906 formatTimeMs(tmpSb, estimatedTime);
4907 pw.print(tmpSb);
4908 pw.print(" (from "); pw.print(tmpOutInt[0]);
4909 pw.println(" steps)");
4910 }
4911 }
4912 }
4913
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004914 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
4915 ArrayList<PackageChange> changes) {
4916 if (changes == null) {
4917 return;
4918 }
4919 pw.print(prefix); pw.println("Package changes:");
4920 for (int i=0; i<changes.size(); i++) {
4921 PackageChange pc = changes.get(i);
4922 if (pc.mUpdate) {
4923 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
4924 pw.print(" vers="); pw.println(pc.mVersionCode);
4925 } else {
4926 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
4927 }
4928 }
4929 }
4930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 /**
4932 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
4933 *
4934 * @param pw a Printer to receive the dump output.
4935 */
4936 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004937 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004938 prepareForDumpLocked();
4939
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004940 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004941 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004942
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004943 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004944 final long historyTotalSize = getHistoryTotalSize();
4945 final long historyUsedSize = getHistoryUsedSize();
4946 if (startIteratingHistoryLocked()) {
4947 try {
4948 pw.print("Battery History (");
4949 pw.print((100*historyUsedSize)/historyTotalSize);
4950 pw.print("% used, ");
4951 printSizeValue(pw, historyUsedSize);
4952 pw.print(" used of ");
4953 printSizeValue(pw, historyTotalSize);
4954 pw.print(", ");
4955 pw.print(getHistoryStringPoolSize());
4956 pw.print(" strings using ");
4957 printSizeValue(pw, getHistoryStringPoolBytes());
4958 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07004959 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004960 pw.println();
4961 } finally {
4962 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004963 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004964 }
4965
4966 if (startIteratingOldHistoryLocked()) {
4967 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004968 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004969 pw.println("Old battery History:");
4970 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004971 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004972 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004973 if (baseTime < 0) {
4974 baseTime = rec.time;
4975 }
4976 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004977 }
4978 pw.println();
4979 } finally {
4980 finishIteratingOldHistoryLocked();
4981 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004982 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004983 }
4984
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004985 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004986 return;
4987 }
4988
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004989 if (!filtering) {
4990 SparseArray<? extends Uid> uidStats = getUidStats();
4991 final int NU = uidStats.size();
4992 boolean didPid = false;
4993 long nowRealtime = SystemClock.elapsedRealtime();
4994 for (int i=0; i<NU; i++) {
4995 Uid uid = uidStats.valueAt(i);
4996 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
4997 if (pids != null) {
4998 for (int j=0; j<pids.size(); j++) {
4999 Uid.Pid pid = pids.valueAt(j);
5000 if (!didPid) {
5001 pw.println("Per-PID Stats:");
5002 didPid = true;
5003 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005004 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5005 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005006 pw.print(" PID "); pw.print(pids.keyAt(j));
5007 pw.print(" wake time: ");
5008 TimeUtils.formatDuration(time, pw);
5009 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005010 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005011 }
5012 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005013 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005014 pw.println();
5015 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005016 }
5017
5018 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005019 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5020 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005021 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5022 if (timeRemaining >= 0) {
5023 pw.print(" Estimated discharge time remaining: ");
5024 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5025 pw.println();
5026 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005027 final LevelStepTracker steps = getDischargeLevelStepTracker();
5028 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5029 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5030 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5031 STEP_LEVEL_MODE_VALUES[i], null));
5032 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005033 pw.println();
5034 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005035 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5036 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005037 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5038 if (timeRemaining >= 0) {
5039 pw.print(" Estimated charge time remaining: ");
5040 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5041 pw.println();
5042 }
5043 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005044 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005045 }
5046 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5047 pw.println("Daily stats:");
5048 pw.print(" Current start time: ");
5049 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5050 getCurrentDailyStartTime()).toString());
5051 pw.print(" Next min deadline: ");
5052 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5053 getNextMinDailyDeadline()).toString());
5054 pw.print(" Next max deadline: ");
5055 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5056 getNextMaxDailyDeadline()).toString());
5057 StringBuilder sb = new StringBuilder(64);
5058 int[] outInt = new int[1];
5059 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5060 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005061 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5062 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005063 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005064 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5065 dsteps, false)) {
5066 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5067 sb, outInt);
5068 }
5069 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5070 csteps, false)) {
5071 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5072 sb, outInt);
5073 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005074 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005075 } else {
5076 pw.println(" Current daily steps:");
5077 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5078 sb, outInt);
5079 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5080 sb, outInt);
5081 }
5082 }
5083 DailyItem dit;
5084 int curIndex = 0;
5085 while ((dit=getDailyItemLocked(curIndex)) != null) {
5086 curIndex++;
5087 if ((flags&DUMP_DAILY_ONLY) != 0) {
5088 pw.println();
5089 }
5090 pw.print(" Daily from ");
5091 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5092 pw.print(" to ");
5093 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5094 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005095 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005096 if (dumpDurationSteps(pw, " ",
5097 " Discharge step durations:", dit.mDischargeSteps, false)) {
5098 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5099 sb, outInt);
5100 }
5101 if (dumpDurationSteps(pw, " ",
5102 " Charge step durations:", dit.mChargeSteps, false)) {
5103 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5104 sb, outInt);
5105 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005106 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005107 } else {
5108 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5109 sb, outInt);
5110 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5111 sb, outInt);
5112 }
5113 }
5114 pw.println();
5115 }
5116 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005117 pw.println("Statistics since last charge:");
5118 pw.println(" System starts: " + getStartCount()
5119 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005120 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5121 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005122 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 }
5125
5126 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005127 public void dumpCheckinLocked(Context context, PrintWriter pw,
5128 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005129 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005130
5131 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005132 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5133 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005134
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005135 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5136
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005137 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005138 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005139
5140 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005141 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005142 try {
5143 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5144 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5145 pw.print(HISTORY_STRING_POOL); pw.print(',');
5146 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005147 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005148 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005149 pw.print(",\"");
5150 String str = getHistoryTagPoolString(i);
5151 str = str.replace("\\", "\\\\");
5152 str = str.replace("\"", "\\\"");
5153 pw.print(str);
5154 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005155 pw.println();
5156 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005157 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005158 } finally {
5159 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005160 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005161 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005162 }
5163
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005164 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005165 return;
5166 }
5167
Dianne Hackborne4a59512010-12-07 11:08:07 -08005168 if (apps != null) {
5169 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5170 for (int i=0; i<apps.size(); i++) {
5171 ApplicationInfo ai = apps.get(i);
5172 ArrayList<String> pkgs = uids.get(ai.uid);
5173 if (pkgs == null) {
5174 pkgs = new ArrayList<String>();
5175 uids.put(ai.uid, pkgs);
5176 }
5177 pkgs.add(ai.packageName);
5178 }
5179 SparseArray<? extends Uid> uidStats = getUidStats();
5180 final int NU = uidStats.size();
5181 String[] lineArgs = new String[2];
5182 for (int i=0; i<NU; i++) {
5183 int uid = uidStats.keyAt(i);
5184 ArrayList<String> pkgs = uids.get(uid);
5185 if (pkgs != null) {
5186 for (int j=0; j<pkgs.size(); j++) {
5187 lineArgs[0] = Integer.toString(uid);
5188 lineArgs[1] = pkgs.get(j);
5189 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5190 (Object[])lineArgs);
5191 }
5192 }
5193 }
5194 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005195 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005196 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005197 String[] lineArgs = new String[1];
5198 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5199 if (timeRemaining >= 0) {
5200 lineArgs[0] = Long.toString(timeRemaining);
5201 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5202 (Object[])lineArgs);
5203 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005204 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005205 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5206 if (timeRemaining >= 0) {
5207 lineArgs[0] = Long.toString(timeRemaining);
5208 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5209 (Object[])lineArgs);
5210 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005211 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5212 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215}