blob: fe323f3b1af160c7f481d5431a6c668cbceb1bbd [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 */
172 static final String CHECKIN_VERSION = "14";
173
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
410 // Time this uid has any processes in foreground state.
411 public static final int PROCESS_STATE_FOREGROUND = 0;
412 // Time this uid has any process in active state (not cached).
413 public static final int PROCESS_STATE_ACTIVE = 1;
414 // Time this uid has any processes running at all.
415 public static final int PROCESS_STATE_RUNNING = 2;
416 // Total number of process states we track.
417 public static final int NUM_PROCESS_STATE = 3;
418
419 static final String[] PROCESS_STATE_NAMES = {
420 "Foreground", "Active", "Running"
421 };
422
423 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
424
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800425 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Robert Greenwalta029ea12013-09-25 16:38:12 -0700427 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
428
Dianne Hackborn617f8772009-03-31 15:04:46 -0700429 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700430 * Note that these must match the constants in android.os.PowerManager.
431 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
432 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700433 */
434 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700435 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700436 };
437
Jeff Browndf693de2012-07-27 12:03:38 -0700438 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700439
Dianne Hackborn617f8772009-03-31 15:04:46 -0700440 public abstract void noteUserActivityLocked(int type);
441 public abstract boolean hasUserActivity();
442 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700443
444 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800445 public abstract long getNetworkActivityBytes(int type, int which);
446 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800447 public abstract long getMobileRadioActiveTime(int which);
448 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700449
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700450 /**
451 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
452 */
453 public abstract long getUserCpuTimeUs(int which);
454
455 /**
456 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
457 */
458 public abstract long getSystemCpuTimeUs(int which);
459
460 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700461 * Get the total cpu power consumed (in milli-ampere-microseconds).
462 */
463 public abstract long getCpuPowerMaUs(int which);
464
465 /**
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700466 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700467 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700468 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
469 * @see BatteryStats#getCpuSpeedSteps()
470 */
471 public abstract long getTimeAtCpuSpeed(int step, int which);
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800474 /*
475 * FIXME: it's not correct to use this magic value because it
476 * could clash with a sensor handle (which are defined by
477 * the sensor HAL, and therefore out of our control
478 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 // Magic sensor number for the GPS.
480 public static final int GPS = -10000;
481
482 public abstract int getHandle();
483
484 public abstract Timer getSensorTime();
485 }
486
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700487 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800488 public int mWakeNesting;
489 public long mWakeSumMs;
490 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700491 }
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 /**
494 * The statistics associated with a particular process.
495 */
496 public static abstract class Proc {
497
Dianne Hackborn287952c2010-09-22 22:34:31 -0700498 public static class ExcessivePower {
499 public static final int TYPE_WAKE = 1;
500 public static final int TYPE_CPU = 2;
501
502 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700503 public long overTime;
504 public long usedTime;
505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800508 * Returns true if this process is still active in the battery stats.
509 */
510 public abstract boolean isActive();
511
512 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700513 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700515 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
517 public abstract long getUserTime(int which);
518
519 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700520 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700522 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 */
524 public abstract long getSystemTime(int which);
525
526 /**
527 * Returns the number of times the process has been started.
528 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700529 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
531 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700532
533 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800534 * Returns the number of times the process has crashed.
535 *
536 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
537 */
538 public abstract int getNumCrashes(int which);
539
540 /**
541 * Returns the number of times the process has ANRed.
542 *
543 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
544 */
545 public abstract int getNumAnrs(int which);
546
547 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700548 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700549 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700550 * @return foreground cpu time in microseconds
551 */
552 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700553
Dianne Hackborn287952c2010-09-22 22:34:31 -0700554 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700555
Dianne Hackborn287952c2010-09-22 22:34:31 -0700556 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558
559 /**
560 * The statistics associated with a particular package.
561 */
562 public static abstract class Pkg {
563
564 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700565 * Returns information about all wakeup alarms that have been triggered for this
566 * package. The mapping keys are tag names for the alarms, the counter contains
567 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700569 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
572 * Returns a mapping containing service statistics.
573 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700574 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575
576 /**
577 * The statistics associated with a particular service.
578 */
579 public abstract class Serv {
580
581 /**
582 * Returns the amount of time spent started.
583 *
584 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700585 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @return
587 */
588 public abstract long getStartTime(long batteryUptime, int which);
589
590 /**
591 * Returns the total number of times startService() has been called.
592 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700593 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 */
595 public abstract int getStarts(int which);
596
597 /**
598 * Returns the total number times the service has been launched.
599 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700600 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 */
602 public abstract int getLaunches(int which);
603 }
604 }
605 }
606
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800607 public static final class LevelStepTracker {
608 public long mLastStepTime = -1;
609 public int mNumStepDurations;
610 public final long[] mStepDurations;
611
612 public LevelStepTracker(int maxLevelSteps) {
613 mStepDurations = new long[maxLevelSteps];
614 }
615
616 public LevelStepTracker(int numSteps, long[] steps) {
617 mNumStepDurations = numSteps;
618 mStepDurations = new long[numSteps];
619 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
620 }
621
622 public long getDurationAt(int index) {
623 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
624 }
625
626 public int getLevelAt(int index) {
627 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
628 >> STEP_LEVEL_LEVEL_SHIFT);
629 }
630
631 public int getInitModeAt(int index) {
632 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
633 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
634 }
635
636 public int getModModeAt(int index) {
637 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
638 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
639 }
640
641 private void appendHex(long val, int topOffset, StringBuilder out) {
642 boolean hasData = false;
643 while (topOffset >= 0) {
644 int digit = (int)( (val>>topOffset) & 0xf );
645 topOffset -= 4;
646 if (!hasData && digit == 0) {
647 continue;
648 }
649 hasData = true;
650 if (digit >= 0 && digit <= 9) {
651 out.append((char)('0' + digit));
652 } else {
653 out.append((char)('a' + digit - 10));
654 }
655 }
656 }
657
658 public void encodeEntryAt(int index, StringBuilder out) {
659 long item = mStepDurations[index];
660 long duration = item & STEP_LEVEL_TIME_MASK;
661 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
662 >> STEP_LEVEL_LEVEL_SHIFT);
663 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
664 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
665 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
666 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
667 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
668 case Display.STATE_OFF: out.append('f'); break;
669 case Display.STATE_ON: out.append('o'); break;
670 case Display.STATE_DOZE: out.append('d'); break;
671 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
672 }
673 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
674 out.append('p');
675 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700676 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
677 out.append('i');
678 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800679 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
680 case Display.STATE_OFF: out.append('F'); break;
681 case Display.STATE_ON: out.append('O'); break;
682 case Display.STATE_DOZE: out.append('D'); break;
683 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
684 }
685 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
686 out.append('P');
687 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700688 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
689 out.append('I');
690 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800691 out.append('-');
692 appendHex(level, 4, out);
693 out.append('-');
694 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
695 }
696
697 public void decodeEntryAt(int index, String value) {
698 final int N = value.length();
699 int i = 0;
700 char c;
701 long out = 0;
702 while (i < N && (c=value.charAt(i)) != '-') {
703 i++;
704 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800705 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800706 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800707 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800708 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800709 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800710 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800711 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
712 << STEP_LEVEL_INITIAL_MODE_SHIFT);
713 break;
714 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
715 << STEP_LEVEL_INITIAL_MODE_SHIFT);
716 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700717 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
718 << STEP_LEVEL_INITIAL_MODE_SHIFT);
719 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800720 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
721 break;
722 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
723 break;
724 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
725 break;
726 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
727 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
728 break;
729 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
730 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800731 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700732 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
733 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
734 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800735 }
736 }
737 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800738 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800739 while (i < N && (c=value.charAt(i)) != '-') {
740 i++;
741 level <<= 4;
742 if (c >= '0' && c <= '9') {
743 level += c - '0';
744 } else if (c >= 'a' && c <= 'f') {
745 level += c - 'a' + 10;
746 } else if (c >= 'A' && c <= 'F') {
747 level += c - 'A' + 10;
748 }
749 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800750 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800751 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
752 long duration = 0;
753 while (i < N && (c=value.charAt(i)) != '-') {
754 i++;
755 duration <<= 4;
756 if (c >= '0' && c <= '9') {
757 duration += c - '0';
758 } else if (c >= 'a' && c <= 'f') {
759 duration += c - 'a' + 10;
760 } else if (c >= 'A' && c <= 'F') {
761 duration += c - 'A' + 10;
762 }
763 }
764 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
765 }
766
767 public void init() {
768 mLastStepTime = -1;
769 mNumStepDurations = 0;
770 }
771
772 public void clearTime() {
773 mLastStepTime = -1;
774 }
775
776 public long computeTimePerLevel() {
777 final long[] steps = mStepDurations;
778 final int numSteps = mNumStepDurations;
779
780 // For now we'll do a simple average across all steps.
781 if (numSteps <= 0) {
782 return -1;
783 }
784 long total = 0;
785 for (int i=0; i<numSteps; i++) {
786 total += steps[i] & STEP_LEVEL_TIME_MASK;
787 }
788 return total / numSteps;
789 /*
790 long[] buckets = new long[numSteps];
791 int numBuckets = 0;
792 int numToAverage = 4;
793 int i = 0;
794 while (i < numSteps) {
795 long totalTime = 0;
796 int num = 0;
797 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
798 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
799 num++;
800 }
801 buckets[numBuckets] = totalTime / num;
802 numBuckets++;
803 numToAverage *= 2;
804 i += num;
805 }
806 if (numBuckets < 1) {
807 return -1;
808 }
809 long averageTime = buckets[numBuckets-1];
810 for (i=numBuckets-2; i>=0; i--) {
811 averageTime = (averageTime + buckets[i]) / 2;
812 }
813 return averageTime;
814 */
815 }
816
817 public long computeTimeEstimate(long modesOfInterest, long modeValues,
818 int[] outNumOfInterest) {
819 final long[] steps = mStepDurations;
820 final int count = mNumStepDurations;
821 if (count <= 0) {
822 return -1;
823 }
824 long total = 0;
825 int numOfInterest = 0;
826 for (int i=0; i<count; i++) {
827 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
828 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
829 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
830 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
831 // If the modes of interest didn't change during this step period...
832 if ((modMode&modesOfInterest) == 0) {
833 // And the mode values during this period match those we are measuring...
834 if ((initMode&modesOfInterest) == modeValues) {
835 // Then this can be used to estimate the total time!
836 numOfInterest++;
837 total += steps[i] & STEP_LEVEL_TIME_MASK;
838 }
839 }
840 }
841 if (numOfInterest <= 0) {
842 return -1;
843 }
844
845 if (outNumOfInterest != null) {
846 outNumOfInterest[0] = numOfInterest;
847 }
848
849 // The estimated time is the average time we spend in each level, multipled
850 // by 100 -- the total number of battery levels
851 return (total / numOfInterest) * 100;
852 }
853
854 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
855 int stepCount = mNumStepDurations;
856 final long lastStepTime = mLastStepTime;
857 if (lastStepTime >= 0 && numStepLevels > 0) {
858 final long[] steps = mStepDurations;
859 long duration = elapsedRealtime - lastStepTime;
860 for (int i=0; i<numStepLevels; i++) {
861 System.arraycopy(steps, 0, steps, 1, steps.length-1);
862 long thisDuration = duration / (numStepLevels-i);
863 duration -= thisDuration;
864 if (thisDuration > STEP_LEVEL_TIME_MASK) {
865 thisDuration = STEP_LEVEL_TIME_MASK;
866 }
867 steps[0] = thisDuration | modeBits;
868 }
869 stepCount += numStepLevels;
870 if (stepCount > steps.length) {
871 stepCount = steps.length;
872 }
873 }
874 mNumStepDurations = stepCount;
875 mLastStepTime = elapsedRealtime;
876 }
877
878 public void readFromParcel(Parcel in) {
879 final int N = in.readInt();
880 mNumStepDurations = N;
881 for (int i=0; i<N; i++) {
882 mStepDurations[i] = in.readLong();
883 }
884 }
885
886 public void writeToParcel(Parcel out) {
887 final int N = mNumStepDurations;
888 out.writeInt(N);
889 for (int i=0; i<N; i++) {
890 out.writeLong(mStepDurations[i]);
891 }
892 }
893 }
894
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700895 public static final class PackageChange {
896 public String mPackageName;
897 public boolean mUpdate;
898 public int mVersionCode;
899 }
900
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800901 public static final class DailyItem {
902 public long mStartTime;
903 public long mEndTime;
904 public LevelStepTracker mDischargeSteps;
905 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700906 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800907 }
908
909 public abstract DailyItem getDailyItemLocked(int daysAgo);
910
911 public abstract long getCurrentDailyStartTime();
912
913 public abstract long getNextMinDailyDeadline();
914
915 public abstract long getNextMaxDailyDeadline();
916
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800917 public final static class HistoryTag {
918 public String string;
919 public int uid;
920
921 public int poolIdx;
922
923 public void setTo(HistoryTag o) {
924 string = o.string;
925 uid = o.uid;
926 poolIdx = o.poolIdx;
927 }
928
929 public void setTo(String _string, int _uid) {
930 string = _string;
931 uid = _uid;
932 poolIdx = -1;
933 }
934
935 public void writeToParcel(Parcel dest, int flags) {
936 dest.writeString(string);
937 dest.writeInt(uid);
938 }
939
940 public void readFromParcel(Parcel src) {
941 string = src.readString();
942 uid = src.readInt();
943 poolIdx = -1;
944 }
945
946 @Override
947 public boolean equals(Object o) {
948 if (this == o) return true;
949 if (o == null || getClass() != o.getClass()) return false;
950
951 HistoryTag that = (HistoryTag) o;
952
953 if (uid != that.uid) return false;
954 if (!string.equals(that.string)) return false;
955
956 return true;
957 }
958
959 @Override
960 public int hashCode() {
961 int result = string.hashCode();
962 result = 31 * result + uid;
963 return result;
964 }
965 }
966
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800967 /**
968 * Optional detailed information that can go into a history step. This is typically
969 * generated each time the battery level changes.
970 */
971 public final static class HistoryStepDetails {
972 // Time (in 1/100 second) spent in user space and the kernel since the last step.
973 public int userTime;
974 public int systemTime;
975
976 // Top three apps using CPU in the last step, with times in 1/100 second.
977 public int appCpuUid1;
978 public int appCpuUTime1;
979 public int appCpuSTime1;
980 public int appCpuUid2;
981 public int appCpuUTime2;
982 public int appCpuSTime2;
983 public int appCpuUid3;
984 public int appCpuUTime3;
985 public int appCpuSTime3;
986
987 // Information from /proc/stat
988 public int statUserTime;
989 public int statSystemTime;
990 public int statIOWaitTime;
991 public int statIrqTime;
992 public int statSoftIrqTime;
993 public int statIdlTime;
994
995 public HistoryStepDetails() {
996 clear();
997 }
998
999 public void clear() {
1000 userTime = systemTime = 0;
1001 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1002 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1003 = appCpuUTime3 = appCpuSTime3 = 0;
1004 }
1005
1006 public void writeToParcel(Parcel out) {
1007 out.writeInt(userTime);
1008 out.writeInt(systemTime);
1009 out.writeInt(appCpuUid1);
1010 out.writeInt(appCpuUTime1);
1011 out.writeInt(appCpuSTime1);
1012 out.writeInt(appCpuUid2);
1013 out.writeInt(appCpuUTime2);
1014 out.writeInt(appCpuSTime2);
1015 out.writeInt(appCpuUid3);
1016 out.writeInt(appCpuUTime3);
1017 out.writeInt(appCpuSTime3);
1018 out.writeInt(statUserTime);
1019 out.writeInt(statSystemTime);
1020 out.writeInt(statIOWaitTime);
1021 out.writeInt(statIrqTime);
1022 out.writeInt(statSoftIrqTime);
1023 out.writeInt(statIdlTime);
1024 }
1025
1026 public void readFromParcel(Parcel in) {
1027 userTime = in.readInt();
1028 systemTime = in.readInt();
1029 appCpuUid1 = in.readInt();
1030 appCpuUTime1 = in.readInt();
1031 appCpuSTime1 = in.readInt();
1032 appCpuUid2 = in.readInt();
1033 appCpuUTime2 = in.readInt();
1034 appCpuSTime2 = in.readInt();
1035 appCpuUid3 = in.readInt();
1036 appCpuUTime3 = in.readInt();
1037 appCpuSTime3 = in.readInt();
1038 statUserTime = in.readInt();
1039 statSystemTime = in.readInt();
1040 statIOWaitTime = in.readInt();
1041 statIrqTime = in.readInt();
1042 statSoftIrqTime = in.readInt();
1043 statIdlTime = in.readInt();
1044 }
1045 }
1046
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001047 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001048 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001049
1050 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001051 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001052
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001053 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001054 public static final byte CMD_NULL = -1;
1055 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001056 public static final byte CMD_CURRENT_TIME = 5;
1057 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001058 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001059 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001060
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001061 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001062
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001063 /**
1064 * Return whether the command code is a delta data update.
1065 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001066 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001067 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001068 }
1069
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001070 public byte batteryLevel;
1071 public byte batteryStatus;
1072 public byte batteryHealth;
1073 public byte batteryPlugType;
1074
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001075 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001076 public char batteryVoltage;
1077
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001078 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001079 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001080 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001081 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001082 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1083 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001084 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001085 public static final int STATE_PHONE_STATE_SHIFT = 6;
1086 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001087 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001088 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001089 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001090
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001091 // These states always appear directly in the first int token
1092 // of a delta change; they should be ones that change relatively
1093 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001094 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1095 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001096 public static final int STATE_GPS_ON_FLAG = 1<<29;
1097 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001098 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001099 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001100 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001101 // These are on the lower bits used for the command; if they change
1102 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001103 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001104 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001105 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001106 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1107 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1108 // empty slot
1109 // empty slot
1110 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001111
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001112 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001113 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1114
1115 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001116
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001117 public int states;
1118
Dianne Hackborn3251b902014-06-20 14:40:53 -07001119 // Constants from WIFI_SUPPL_STATE_*
1120 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1121 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1122 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1123 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1124 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1125 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1126
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001127 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001128 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1129 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1130 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001131 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001132 public static final int STATE2_DEVICE_IDLE_FLAG = 1<<26;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001133 public static final int STATE2_CHARGING_FLAG = 1<<25;
1134 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<24;
1135 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<23;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001136 public static final int STATE2_CAMERA_FLAG = 1<<22;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001137
1138 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001139 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_FLAG
1140 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1141
1142 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001143
Dianne Hackborn40c87252014-03-19 16:55:40 -07001144 public int states2;
1145
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001146 // The wake lock that was acquired at this point.
1147 public HistoryTag wakelockTag;
1148
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001149 // Kernel wakeup reason at this point.
1150 public HistoryTag wakeReasonTag;
1151
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001152 // Non-null when there is more detailed information at this step.
1153 public HistoryStepDetails stepDetails;
1154
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001155 public static final int EVENT_FLAG_START = 0x8000;
1156 public static final int EVENT_FLAG_FINISH = 0x4000;
1157
1158 // No event in this item.
1159 public static final int EVENT_NONE = 0x0000;
1160 // Event is about a process that is running.
1161 public static final int EVENT_PROC = 0x0001;
1162 // Event is about an application package that is in the foreground.
1163 public static final int EVENT_FOREGROUND = 0x0002;
1164 // Event is about an application package that is at the top of the screen.
1165 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001166 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001167 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001168 // Events for all additional wake locks aquired/release within a wake block.
1169 // These are not generated by default.
1170 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001171 // Event is about an application executing a scheduled job.
1172 public static final int EVENT_JOB = 0x0006;
1173 // Events for users running.
1174 public static final int EVENT_USER_RUNNING = 0x0007;
1175 // Events for foreground user.
1176 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001177 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001178 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001179 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001180 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001181 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001182 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001183 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001184 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001185 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001186 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001187 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001188 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001189 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001190 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001191 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001192 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Amith Yamasani67768492015-06-09 12:23:58 -07001193
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001194 // Number of event types.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001195 public static final int EVENT_COUNT = 0x0011;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001196 // Mask to extract out only the type part of the event.
1197 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001198
1199 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1200 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1201 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1202 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1203 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1204 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001205 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1206 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001207 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1208 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001209 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1210 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1211 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1212 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1213 public static final int EVENT_USER_FOREGROUND_START =
1214 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1215 public static final int EVENT_USER_FOREGROUND_FINISH =
1216 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001217 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1218 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001219
1220 // For CMD_EVENT.
1221 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001222 public HistoryTag eventTag;
1223
Dianne Hackborn9a755432014-05-15 17:05:22 -07001224 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001225 public long currentTime;
1226
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001227 // Meta-data when reading.
1228 public int numReadInts;
1229
1230 // Pre-allocated objects.
1231 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001232 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001233 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001234
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001235 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001236 }
1237
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001238 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001239 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001240 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001241 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001242 }
1243
1244 public int describeContents() {
1245 return 0;
1246 }
1247
1248 public void writeToParcel(Parcel dest, int flags) {
1249 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001250 int bat = (((int)cmd)&0xff)
1251 | ((((int)batteryLevel)<<8)&0xff00)
1252 | ((((int)batteryStatus)<<16)&0xf0000)
1253 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001254 | ((((int)batteryPlugType)<<24)&0xf000000)
1255 | (wakelockTag != null ? 0x10000000 : 0)
1256 | (wakeReasonTag != null ? 0x20000000 : 0)
1257 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001258 dest.writeInt(bat);
1259 bat = (((int)batteryTemperature)&0xffff)
1260 | ((((int)batteryVoltage)<<16)&0xffff0000);
1261 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001262 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001263 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001264 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001265 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001266 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001267 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001268 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001269 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001270 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001271 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001272 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001273 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001274 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001275 dest.writeLong(currentTime);
1276 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001277 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001278
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001279 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001280 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001281 int bat = src.readInt();
1282 cmd = (byte)(bat&0xff);
1283 batteryLevel = (byte)((bat>>8)&0xff);
1284 batteryStatus = (byte)((bat>>16)&0xf);
1285 batteryHealth = (byte)((bat>>20)&0xf);
1286 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001287 int bat2 = src.readInt();
1288 batteryTemperature = (short)(bat2&0xffff);
1289 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001290 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001291 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001292 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001293 wakelockTag = localWakelockTag;
1294 wakelockTag.readFromParcel(src);
1295 } else {
1296 wakelockTag = null;
1297 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001298 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001299 wakeReasonTag = localWakeReasonTag;
1300 wakeReasonTag.readFromParcel(src);
1301 } else {
1302 wakeReasonTag = null;
1303 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001304 if ((bat&0x40000000) != 0) {
1305 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001306 eventTag = localEventTag;
1307 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001308 } else {
1309 eventCode = EVENT_NONE;
1310 eventTag = null;
1311 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001312 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001313 currentTime = src.readLong();
1314 } else {
1315 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001316 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001317 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001318 }
1319
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001320 public void clear() {
1321 time = 0;
1322 cmd = CMD_NULL;
1323 batteryLevel = 0;
1324 batteryStatus = 0;
1325 batteryHealth = 0;
1326 batteryPlugType = 0;
1327 batteryTemperature = 0;
1328 batteryVoltage = 0;
1329 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001330 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001331 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001332 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001333 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001334 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001335 }
1336
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001337 public void setTo(HistoryItem o) {
1338 time = o.time;
1339 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001340 setToCommon(o);
1341 }
1342
1343 public void setTo(long time, byte cmd, HistoryItem o) {
1344 this.time = time;
1345 this.cmd = cmd;
1346 setToCommon(o);
1347 }
1348
1349 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001350 batteryLevel = o.batteryLevel;
1351 batteryStatus = o.batteryStatus;
1352 batteryHealth = o.batteryHealth;
1353 batteryPlugType = o.batteryPlugType;
1354 batteryTemperature = o.batteryTemperature;
1355 batteryVoltage = o.batteryVoltage;
1356 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001357 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001358 if (o.wakelockTag != null) {
1359 wakelockTag = localWakelockTag;
1360 wakelockTag.setTo(o.wakelockTag);
1361 } else {
1362 wakelockTag = null;
1363 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001364 if (o.wakeReasonTag != null) {
1365 wakeReasonTag = localWakeReasonTag;
1366 wakeReasonTag.setTo(o.wakeReasonTag);
1367 } else {
1368 wakeReasonTag = null;
1369 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001370 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001371 if (o.eventTag != null) {
1372 eventTag = localEventTag;
1373 eventTag.setTo(o.eventTag);
1374 } else {
1375 eventTag = null;
1376 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001377 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001378 }
1379
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001380 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001381 return batteryLevel == o.batteryLevel
1382 && batteryStatus == o.batteryStatus
1383 && batteryHealth == o.batteryHealth
1384 && batteryPlugType == o.batteryPlugType
1385 && batteryTemperature == o.batteryTemperature
1386 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001387 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001388 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001389 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001390 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001391
1392 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001393 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001394 return false;
1395 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001396 if (wakelockTag != o.wakelockTag) {
1397 if (wakelockTag == null || o.wakelockTag == null) {
1398 return false;
1399 }
1400 if (!wakelockTag.equals(o.wakelockTag)) {
1401 return false;
1402 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001403 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001404 if (wakeReasonTag != o.wakeReasonTag) {
1405 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1406 return false;
1407 }
1408 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1409 return false;
1410 }
1411 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001412 if (eventTag != o.eventTag) {
1413 if (eventTag == null || o.eventTag == null) {
1414 return false;
1415 }
1416 if (!eventTag.equals(o.eventTag)) {
1417 return false;
1418 }
1419 }
1420 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001421 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001422 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001423
1424 public final static class HistoryEventTracker {
1425 private final HashMap<String, SparseIntArray>[] mActiveEvents
1426 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1427
1428 public boolean updateState(int code, String name, int uid, int poolIdx) {
1429 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1430 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1431 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1432 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001433 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001434 mActiveEvents[idx] = active;
1435 }
1436 SparseIntArray uids = active.get(name);
1437 if (uids == null) {
1438 uids = new SparseIntArray();
1439 active.put(name, uids);
1440 }
1441 if (uids.indexOfKey(uid) >= 0) {
1442 // Already set, nothing to do!
1443 return false;
1444 }
1445 uids.put(uid, poolIdx);
1446 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1447 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1448 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1449 if (active == null) {
1450 // not currently active, nothing to do.
1451 return false;
1452 }
1453 SparseIntArray uids = active.get(name);
1454 if (uids == null) {
1455 // not currently active, nothing to do.
1456 return false;
1457 }
1458 idx = uids.indexOfKey(uid);
1459 if (idx < 0) {
1460 // not currently active, nothing to do.
1461 return false;
1462 }
1463 uids.removeAt(idx);
1464 if (uids.size() <= 0) {
1465 active.remove(name);
1466 }
1467 }
1468 return true;
1469 }
1470
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001471 public void removeEvents(int code) {
1472 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1473 mActiveEvents[idx] = null;
1474 }
1475
Dianne Hackborn37de0982014-05-09 09:32:18 -07001476 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1477 return mActiveEvents[code];
1478 }
1479 }
1480
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001481 public static final class BitDescription {
1482 public final int mask;
1483 public final int shift;
1484 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001485 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001486 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001487 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001488
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001489 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001490 this.mask = mask;
1491 this.shift = -1;
1492 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001493 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001494 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001495 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001496 }
1497
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001498 public BitDescription(int mask, int shift, String name, String shortName,
1499 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001500 this.mask = mask;
1501 this.shift = shift;
1502 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001503 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001504 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001505 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001506 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001507 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001508
Dianne Hackbornfc064132014-06-02 12:42:12 -07001509 /**
1510 * Don't allow any more batching in to the current history event. This
1511 * is called when printing partial histories, so to ensure that the next
1512 * history event will go in to a new batch after what was printed in the
1513 * last partial history.
1514 */
1515 public abstract void commitCurrentHistoryBatchLocked();
1516
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 public abstract int getHistoryTotalSize();
1518
1519 public abstract int getHistoryUsedSize();
1520
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001521 public abstract boolean startIteratingHistoryLocked();
1522
Dianne Hackborn099bc622014-01-22 13:39:16 -08001523 public abstract int getHistoryStringPoolSize();
1524
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001525 public abstract int getHistoryStringPoolBytes();
1526
1527 public abstract String getHistoryTagPoolString(int index);
1528
1529 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001530
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001531 public abstract boolean getNextHistoryLocked(HistoryItem out);
1532
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001533 public abstract void finishIteratingHistoryLocked();
1534
1535 public abstract boolean startIteratingOldHistoryLocked();
1536
1537 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1538
1539 public abstract void finishIteratingOldHistoryLocked();
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001542 * Return the base time offset for the battery history.
1543 */
1544 public abstract long getHistoryBaseTime();
1545
1546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * Returns the number of times the device has been started.
1548 */
1549 public abstract int getStartCount();
1550
1551 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001552 * 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 -08001553 * running on battery.
1554 *
1555 * {@hide}
1556 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001557 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001559 /**
1560 * Returns the number of times the screen was turned on.
1561 *
1562 * {@hide}
1563 */
1564 public abstract int getScreenOnCount(int which);
1565
Jeff Browne95c3cd2014-05-02 16:59:26 -07001566 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1567
Dianne Hackborn617f8772009-03-31 15:04:46 -07001568 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1569 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1570 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1571 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1572 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1573
1574 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1575 "dark", "dim", "medium", "light", "bright"
1576 };
1577
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001578 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1579 "0", "1", "2", "3", "4"
1580 };
1581
Dianne Hackborn617f8772009-03-31 15:04:46 -07001582 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001583
Dianne Hackborn617f8772009-03-31 15:04:46 -07001584 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001585 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001586 * the given brightness
1587 *
1588 * {@hide}
1589 */
1590 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001591 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001594 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001595 * running on battery.
1596 *
1597 * {@hide}
1598 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001599 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001600
1601 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001602 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001603 *
1604 * {@hide}
1605 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001606 public abstract int getPowerSaveModeEnabledCount(int which);
1607
1608 /**
1609 * Returns the time in microseconds that device has been in idle mode while
1610 * running on battery.
1611 *
1612 * {@hide}
1613 */
1614 public abstract long getDeviceIdleModeEnabledTime(long elapsedRealtimeUs, int which);
1615
1616 /**
1617 * Returns the number of times that the devie has gone in to idle mode.
1618 *
1619 * {@hide}
1620 */
1621 public abstract int getDeviceIdleModeEnabledCount(int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001622
1623 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001624 * Returns the time in microseconds that device has been in idling while on
1625 * battery. This is broader than {@link #getDeviceIdleModeEnabledTime} -- it
1626 * counts all of the time that we consider the device to be idle, whether or not
1627 * it is currently in the actual device idle mode.
1628 *
1629 * {@hide}
1630 */
1631 public abstract long getDeviceIdlingTime(long elapsedRealtimeUs, int which);
1632
1633 /**
1634 * Returns the number of times that the devie has started idling.
1635 *
1636 * {@hide}
1637 */
1638 public abstract int getDeviceIdlingCount(int which);
1639
1640 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001641 * Returns the number of times that connectivity state changed.
1642 *
1643 * {@hide}
1644 */
1645 public abstract int getNumConnectivityChange(int which);
1646
1647 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001648 * 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 -08001649 * running on battery.
1650 *
1651 * {@hide}
1652 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001653 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001654
1655 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001656 * Returns the number of times a phone call was activated.
1657 *
1658 * {@hide}
1659 */
1660 public abstract int getPhoneOnCount(int which);
1661
1662 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001663 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001664 * the given signal strength.
1665 *
1666 * {@hide}
1667 */
1668 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001669 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001670
Dianne Hackborn617f8772009-03-31 15:04:46 -07001671 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001672 * Returns the time in microseconds that the phone has been trying to
1673 * acquire a signal.
1674 *
1675 * {@hide}
1676 */
1677 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001679
1680 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001681 * Returns the number of times the phone has entered the given signal strength.
1682 *
1683 * {@hide}
1684 */
1685 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1686
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001687 /**
1688 * Returns the time in microseconds that the mobile network has been active
1689 * (in a high power state).
1690 *
1691 * {@hide}
1692 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001693 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001694
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001695 /**
1696 * Returns the number of times that the mobile network has transitioned to the
1697 * active state.
1698 *
1699 * {@hide}
1700 */
1701 public abstract int getMobileRadioActiveCount(int which);
1702
1703 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001704 * Returns the time in microseconds that is the difference between the mobile radio
1705 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1706 * from the radio.
1707 *
1708 * {@hide}
1709 */
1710 public abstract long getMobileRadioActiveAdjustedTime(int which);
1711
1712 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001713 * Returns the time in microseconds that the mobile network has been active
1714 * (in a high power state) but not being able to blame on an app.
1715 *
1716 * {@hide}
1717 */
1718 public abstract long getMobileRadioActiveUnknownTime(int which);
1719
1720 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001721 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001722 *
1723 * {@hide}
1724 */
1725 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001726
Dianne Hackborn627bba72009-03-24 22:32:56 -07001727 public static final int DATA_CONNECTION_NONE = 0;
1728 public static final int DATA_CONNECTION_GPRS = 1;
1729 public static final int DATA_CONNECTION_EDGE = 2;
1730 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001731 public static final int DATA_CONNECTION_CDMA = 4;
1732 public static final int DATA_CONNECTION_EVDO_0 = 5;
1733 public static final int DATA_CONNECTION_EVDO_A = 6;
1734 public static final int DATA_CONNECTION_1xRTT = 7;
1735 public static final int DATA_CONNECTION_HSDPA = 8;
1736 public static final int DATA_CONNECTION_HSUPA = 9;
1737 public static final int DATA_CONNECTION_HSPA = 10;
1738 public static final int DATA_CONNECTION_IDEN = 11;
1739 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001740 public static final int DATA_CONNECTION_LTE = 13;
1741 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001742 public static final int DATA_CONNECTION_HSPAP = 15;
1743 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001744
Dianne Hackborn627bba72009-03-24 22:32:56 -07001745 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001746 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001747 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001748 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001749 };
1750
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001751 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001752
1753 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001754 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001755 * the given data connection.
1756 *
1757 * {@hide}
1758 */
1759 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001760 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001763 * Returns the number of times the phone has entered the given data
1764 * connection type.
1765 *
1766 * {@hide}
1767 */
1768 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001769
Dianne Hackborn3251b902014-06-20 14:40:53 -07001770 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1771 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1772 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1773 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1774 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1775 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1776 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1777 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1778 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1779 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1780 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1781 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1782 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1783
1784 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1785
1786 static final String[] WIFI_SUPPL_STATE_NAMES = {
1787 "invalid", "disconn", "disabled", "inactive", "scanning",
1788 "authenticating", "associating", "associated", "4-way-handshake",
1789 "group-handshake", "completed", "dormant", "uninit"
1790 };
1791
1792 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1793 "inv", "dsc", "dis", "inact", "scan",
1794 "auth", "ascing", "asced", "4-way",
1795 "group", "compl", "dorm", "uninit"
1796 };
1797
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001798 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1799 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001800 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001801 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1802 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001804 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1805 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1806 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001807 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001808 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001809 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001811 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1812 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001813 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1814 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1815 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1816 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1817 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1818 new String[] {"in", "out", "emergency", "off"},
1819 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001820 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1821 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1822 SignalStrength.SIGNAL_STRENGTH_NAMES,
1823 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001824 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1825 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1826 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001827 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001828
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001829 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1830 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001831 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001832 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001833 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001834 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001835 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001836 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_FLAG, "device_idle", "di"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001837 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1838 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1839 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001840 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1841 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1842 new String[] { "0", "1", "2", "3", "4" },
1843 new String[] { "0", "1", "2", "3", "4" }),
1844 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1845 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1846 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001847 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001848 };
1849
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001850 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001851 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001852 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001853 };
1854
1855 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001856 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001857 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001858 };
1859
Dianne Hackborn617f8772009-03-31 15:04:46 -07001860 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001861 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001862 * running on battery.
1863 *
1864 * {@hide}
1865 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001866 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001867
1868 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001869 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001870 * been in the running state while the device was running on battery.
1871 *
1872 * {@hide}
1873 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001874 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001875
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001876 public static final int WIFI_STATE_OFF = 0;
1877 public static final int WIFI_STATE_OFF_SCANNING = 1;
1878 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1879 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1880 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1881 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1882 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1883 public static final int WIFI_STATE_SOFT_AP = 7;
1884
1885 static final String[] WIFI_STATE_NAMES = {
1886 "off", "scanning", "no_net", "disconn",
1887 "sta", "p2p", "sta_p2p", "soft_ap"
1888 };
1889
1890 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1891
1892 /**
1893 * Returns the time in microseconds that WiFi has been running in the given state.
1894 *
1895 * {@hide}
1896 */
1897 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001898 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001899
1900 /**
1901 * Returns the number of times that WiFi has entered the given state.
1902 *
1903 * {@hide}
1904 */
1905 public abstract int getWifiStateCount(int wifiState, int which);
1906
The Android Open Source Project10592532009-03-18 17:39:46 -07001907 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001908 * Returns the time in microseconds that the wifi supplicant has been
1909 * in a given state.
1910 *
1911 * {@hide}
1912 */
1913 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1914
1915 /**
1916 * Returns the number of times that the wifi supplicant has transitioned
1917 * to a given state.
1918 *
1919 * {@hide}
1920 */
1921 public abstract int getWifiSupplStateCount(int state, int which);
1922
1923 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1924
1925 /**
1926 * Returns the time in microseconds that WIFI has been running with
1927 * the given signal strength.
1928 *
1929 * {@hide}
1930 */
1931 public abstract long getWifiSignalStrengthTime(int strengthBin,
1932 long elapsedRealtimeUs, int which);
1933
1934 /**
1935 * Returns the number of times WIFI has entered the given signal strength.
1936 *
1937 * {@hide}
1938 */
1939 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1940
1941 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001942 * Returns the time in microseconds that the flashlight has been on while the device was
1943 * running on battery.
1944 *
1945 * {@hide}
1946 */
1947 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1948
1949 /**
1950 * Returns the number of times that the flashlight has been turned on while the device was
1951 * running on battery.
1952 *
1953 * {@hide}
1954 */
1955 public abstract long getFlashlightOnCount(int which);
1956
Ruben Brunk5b1308f2015-06-03 18:49:27 -07001957 /**
1958 * Returns the time in microseconds that the camera has been on while the device was
1959 * running on battery.
1960 *
1961 * {@hide}
1962 */
1963 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
1964
1965
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001966 public static final int NETWORK_MOBILE_RX_DATA = 0;
1967 public static final int NETWORK_MOBILE_TX_DATA = 1;
1968 public static final int NETWORK_WIFI_RX_DATA = 2;
1969 public static final int NETWORK_WIFI_TX_DATA = 3;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001970 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001971
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001972 public abstract long getNetworkActivityBytes(int type, int which);
1973 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001974
Adam Lesinski33dac552015-03-09 15:24:48 -07001975 public static final int CONTROLLER_IDLE_TIME = 0;
1976 public static final int CONTROLLER_RX_TIME = 1;
1977 public static final int CONTROLLER_TX_TIME = 2;
Adam Lesinskie08af192015-03-25 16:42:59 -07001978 public static final int CONTROLLER_POWER_DRAIN = 3;
1979 public static final int NUM_CONTROLLER_ACTIVITY_TYPES = CONTROLLER_POWER_DRAIN + 1;
Adam Lesinski33dac552015-03-09 15:24:48 -07001980
Adam Lesinskie08af192015-03-25 16:42:59 -07001981 /**
Adam Lesinski17390762015-04-10 13:17:47 -07001982 * Returns true if the BatteryStats object has detailed bluetooth power reports.
1983 * When true, calling {@link #getBluetoothControllerActivity(int, int)} will yield the
1984 * actual power data.
1985 */
1986 public abstract boolean hasBluetoothActivityReporting();
1987
1988 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001989 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
1990 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
1991 * respective state.
1992 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
1993 * milli-ampere-milliseconds (mAms).
1994 */
Adam Lesinski33dac552015-03-09 15:24:48 -07001995 public abstract long getBluetoothControllerActivity(int type, int which);
Adam Lesinskie08af192015-03-25 16:42:59 -07001996
1997 /**
Adam Lesinski17390762015-04-10 13:17:47 -07001998 * Returns true if the BatteryStats object has detailed WiFi power reports.
1999 * When true, calling {@link #getWifiControllerActivity(int, int)} will yield the
2000 * actual power data.
2001 */
2002 public abstract boolean hasWifiActivityReporting();
2003
2004 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07002005 * For {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, and
2006 * {@link #CONTROLLER_TX_TIME}, returns the time spent (in milliseconds) in the
2007 * respective state.
2008 * For {@link #CONTROLLER_POWER_DRAIN}, returns the power used by the controller in
2009 * milli-ampere-milliseconds (mAms).
2010 */
Adam Lesinski33dac552015-03-09 15:24:48 -07002011 public abstract long getWifiControllerActivity(int type, int which);
2012
The Android Open Source Project10592532009-03-18 17:39:46 -07002013 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002014 * Return the wall clock time when battery stats data collection started.
2015 */
2016 public abstract long getStartClockTime();
2017
2018 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002019 * Return platform version tag that we were running in when the battery stats started.
2020 */
2021 public abstract String getStartPlatformVersion();
2022
2023 /**
2024 * Return platform version tag that we were running in when the battery stats ended.
2025 */
2026 public abstract String getEndPlatformVersion();
2027
2028 /**
2029 * Return the internal version code of the parcelled format.
2030 */
2031 public abstract int getParcelVersion();
2032
2033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 * Return whether we are currently running on battery.
2035 */
2036 public abstract boolean getIsOnBattery();
2037
2038 /**
2039 * Returns a SparseArray containing the statistics for each uid.
2040 */
2041 public abstract SparseArray<? extends Uid> getUidStats();
2042
2043 /**
2044 * Returns the current battery uptime in microseconds.
2045 *
2046 * @param curTime the amount of elapsed realtime in microseconds.
2047 */
2048 public abstract long getBatteryUptime(long curTime);
2049
2050 /**
2051 * Returns the current battery realtime in microseconds.
2052 *
2053 * @param curTime the amount of elapsed realtime in microseconds.
2054 */
2055 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002056
2057 /**
Evan Millar633a1742009-04-02 16:36:33 -07002058 * Returns the battery percentage level at the last time the device was unplugged from power, or
2059 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002060 */
Evan Millar633a1742009-04-02 16:36:33 -07002061 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002062
2063 /**
Evan Millar633a1742009-04-02 16:36:33 -07002064 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2065 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002066 */
Evan Millar633a1742009-04-02 16:36:33 -07002067 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068
2069 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002070 * Get the amount the battery has discharged since the stats were
2071 * last reset after charging, as a lower-end approximation.
2072 */
2073 public abstract int getLowDischargeAmountSinceCharge();
2074
2075 /**
2076 * Get the amount the battery has discharged since the stats were
2077 * last reset after charging, as an upper-end approximation.
2078 */
2079 public abstract int getHighDischargeAmountSinceCharge();
2080
2081 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002082 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2083 */
2084 public abstract int getDischargeAmount(int which);
2085
2086 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002087 * Get the amount the battery has discharged while the screen was on,
2088 * since the last time power was unplugged.
2089 */
2090 public abstract int getDischargeAmountScreenOn();
2091
2092 /**
2093 * Get the amount the battery has discharged while the screen was on,
2094 * since the last time the device was charged.
2095 */
2096 public abstract int getDischargeAmountScreenOnSinceCharge();
2097
2098 /**
2099 * Get the amount the battery has discharged while the screen was off,
2100 * since the last time power was unplugged.
2101 */
2102 public abstract int getDischargeAmountScreenOff();
2103
2104 /**
2105 * Get the amount the battery has discharged while the screen was off,
2106 * since the last time the device was charged.
2107 */
2108 public abstract int getDischargeAmountScreenOffSinceCharge();
2109
2110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 * Returns the total, last, or current battery uptime in microseconds.
2112 *
2113 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002114 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 */
2116 public abstract long computeBatteryUptime(long curTime, int which);
2117
2118 /**
2119 * Returns the total, last, or current battery realtime in microseconds.
2120 *
2121 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002122 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 */
2124 public abstract long computeBatteryRealtime(long curTime, int which);
2125
2126 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002127 * Returns the total, last, or current battery screen off uptime in microseconds.
2128 *
2129 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002130 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002131 */
2132 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2133
2134 /**
2135 * Returns the total, last, or current battery screen off realtime in microseconds.
2136 *
2137 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002138 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002139 */
2140 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2141
2142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 * Returns the total, last, or current uptime in microseconds.
2144 *
2145 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002146 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 */
2148 public abstract long computeUptime(long curTime, int which);
2149
2150 /**
2151 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002154 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 */
2156 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002157
2158 /**
2159 * Compute an approximation for how much run time (in microseconds) is remaining on
2160 * the battery. Returns -1 if no time can be computed: either there is not
2161 * enough current data to make a decision, or the battery is currently
2162 * charging.
2163 *
2164 * @param curTime The current elepsed realtime in microseconds.
2165 */
2166 public abstract long computeBatteryTimeRemaining(long curTime);
2167
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002168 // The part of a step duration that is the actual time.
2169 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2170
2171 // Bits in a step duration that are the new battery level we are at.
2172 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002173 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002174
2175 // Bits in a step duration that are the initial mode we were in at that step.
2176 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002177 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002178
2179 // Bits in a step duration that indicate which modes changed during that step.
2180 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002181 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002182
2183 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2184 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2185
2186 // Step duration mode: power save is on.
2187 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2188
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002189 // Step duration mode: device is currently in idle mode.
2190 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2191
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002192 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2193 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002194 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2195 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002196 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2197 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2198 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2199 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2200 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002201 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2202 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002203 };
2204 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2205 (Display.STATE_OFF-1),
2206 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002207 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002208 (Display.STATE_ON-1),
2209 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2210 (Display.STATE_DOZE-1),
2211 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2212 (Display.STATE_DOZE_SUSPEND-1),
2213 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002214 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002215 };
2216 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2217 "screen off",
2218 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002219 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002220 "screen on",
2221 "screen on power save",
2222 "screen doze",
2223 "screen doze power save",
2224 "screen doze-suspend",
2225 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002226 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002227 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002228
2229 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002230 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002231 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002232 public abstract LevelStepTracker getDischargeLevelStepTracker();
2233
2234 /**
2235 * Return the array of daily discharge step durations.
2236 */
2237 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002238
2239 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002240 * Compute an approximation for how much time (in microseconds) remains until the battery
2241 * is fully charged. Returns -1 if no time can be computed: either there is not
2242 * enough current data to make a decision, or the battery is currently
2243 * discharging.
2244 *
2245 * @param curTime The current elepsed realtime in microseconds.
2246 */
2247 public abstract long computeChargeTimeRemaining(long curTime);
2248
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002249 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002250 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002251 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002252 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002253
2254 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002255 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002256 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002257 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002258
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002259 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2260
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002261 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002262
Evan Millarc64edde2009-04-18 12:26:32 -07002263 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264
Amith Yamasanie43530a2009-08-21 13:11:37 -07002265 /** Returns the number of different speeds that the CPU can run at */
2266 public abstract int getCpuSpeedSteps();
2267
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002268 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2269
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002270 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 long days = seconds / (60 * 60 * 24);
2272 if (days != 0) {
2273 out.append(days);
2274 out.append("d ");
2275 }
2276 long used = days * 60 * 60 * 24;
2277
2278 long hours = (seconds - used) / (60 * 60);
2279 if (hours != 0 || used != 0) {
2280 out.append(hours);
2281 out.append("h ");
2282 }
2283 used += hours * 60 * 60;
2284
2285 long mins = (seconds-used) / 60;
2286 if (mins != 0 || used != 0) {
2287 out.append(mins);
2288 out.append("m ");
2289 }
2290 used += mins * 60;
2291
2292 if (seconds != 0 || used != 0) {
2293 out.append(seconds-used);
2294 out.append("s ");
2295 }
2296 }
2297
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002298 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002300 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 sb.append(time - (sec * 1000));
2302 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002305 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002306 long sec = time / 1000;
2307 formatTimeRaw(sb, sec);
2308 sb.append(time - (sec * 1000));
2309 sb.append("ms");
2310 }
2311
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002312 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002314 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
2316 float perc = ((float)num) / ((float)den) * 100;
2317 mFormatBuilder.setLength(0);
2318 mFormatter.format("%.1f%%", perc);
2319 return mFormatBuilder.toString();
2320 }
2321
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002322 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002323 mFormatBuilder.setLength(0);
2324
2325 if (bytes < BYTES_PER_KB) {
2326 return bytes + "B";
2327 } else if (bytes < BYTES_PER_MB) {
2328 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2329 return mFormatBuilder.toString();
2330 } else if (bytes < BYTES_PER_GB){
2331 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2332 return mFormatBuilder.toString();
2333 } else {
2334 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2335 return mFormatBuilder.toString();
2336 }
2337 }
2338
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002339 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002340 if (timer != null) {
2341 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002342 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002343 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2344 return totalTimeMillis;
2345 }
2346 return 0;
2347 }
2348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 /**
2350 *
2351 * @param sb a StringBuilder object.
2352 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002353 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002355 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 * @param linePrefix a String to be prepended to each line of output.
2357 * @return the line prefix
2358 */
2359 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002360 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002363 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002364
Evan Millarc64edde2009-04-18 12:26:32 -07002365 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 if (totalTimeMillis != 0) {
2367 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002368 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002369 if (name != null) {
2370 sb.append(name);
2371 sb.append(' ');
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 sb.append('(');
2374 sb.append(count);
2375 sb.append(" times)");
2376 return ", ";
2377 }
2378 }
2379 return linePrefix;
2380 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002381
2382 /**
2383 *
2384 * @param pw a PrintWriter object to print to.
2385 * @param sb a StringBuilder object.
2386 * @param timer a Timer object contining the wakelock times.
2387 * @param rawRealtime the current on-battery time in microseconds.
2388 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2389 * @param prefix a String to be prepended to each line of output.
2390 * @param type the name of the timer.
2391 */
2392 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2393 long rawRealtime, int which, String prefix, String type) {
2394 if (timer != null) {
2395 // Convert from microseconds to milliseconds with rounding
2396 final long totalTime = (timer.getTotalTimeLocked(
2397 rawRealtime, which) + 500) / 1000;
2398 final int count = timer.getCountLocked(which);
2399 if (totalTime != 0) {
2400 sb.setLength(0);
2401 sb.append(prefix);
2402 sb.append(" ");
2403 sb.append(type);
2404 sb.append(": ");
2405 formatTimeMs(sb, totalTime);
2406 sb.append("realtime (");
2407 sb.append(count);
2408 sb.append(" times)");
2409 pw.println(sb.toString());
2410 return true;
2411 }
2412 }
2413 return false;
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415
2416 /**
2417 * Checkin version of wakelock printer. Prints simple comma-separated list.
2418 *
2419 * @param sb a StringBuilder object.
2420 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002421 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002423 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 * @param linePrefix a String to be prepended to each line of output.
2425 * @return the line prefix
2426 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002427 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2428 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 long totalTimeMicros = 0;
2430 int count = 0;
2431 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002432 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002433 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 }
2435 sb.append(linePrefix);
2436 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2437 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002438 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 sb.append(count);
2440 return ",";
2441 }
2442
2443 /**
2444 * Dump a comma-separated line of values for terse checkin mode.
2445 *
2446 * @param pw the PageWriter to dump log to
2447 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2448 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2449 * @param args type-dependent data arguments
2450 */
2451 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2452 Object... args ) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002453 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2454 pw.print(',');
2455 pw.print(uid);
2456 pw.print(',');
2457 pw.print(category);
2458 pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002459 pw.print(type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002460
2461 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002462 pw.print(',');
2463 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002465 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002467
2468 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002469 * Dump a given timer stat for terse checkin mode.
2470 *
2471 * @param pw the PageWriter to dump log to
2472 * @param uid the UID to log
2473 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2474 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2475 * @param timer a {@link Timer} to dump stats for
2476 * @param rawRealtime the current elapsed realtime of the system in microseconds
2477 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2478 */
2479 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2480 Timer timer, long rawRealtime, int which) {
2481 if (timer != null) {
2482 // Convert from microseconds to milliseconds with rounding
2483 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2484 / 1000;
2485 final int count = timer.getCountLocked(which);
2486 if (totalTime != 0) {
2487 dumpLine(pw, uid, category, type, totalTime, count);
2488 }
2489 }
2490 }
2491
2492 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002493 * Temporary for settings.
2494 */
2495 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2496 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2497 }
2498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 /**
2500 * Checkin server version of dump to produce more compact, computer-readable log.
2501 *
2502 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002504 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2505 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2507 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2508 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2510 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002511 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2512 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2513 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 final long totalRealtime = computeRealtime(rawRealtime, which);
2515 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002516 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002517 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002518 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
2519 final long deviceIdleModeEnabledTime = getDeviceIdleModeEnabledTime(rawRealtime, which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002520 final long deviceIdlingTime = getDeviceIdlingTime(rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002521 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002522 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002523
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002524 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002526 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002527 final int NU = uidStats.size();
2528
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002529 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 // Dump "battery" stat
2532 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002533 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002534 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002535 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002536 getStartClockTime(),
2537 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002539 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002540 long fullWakeLockTimeTotal = 0;
2541 long partialWakeLockTimeTotal = 0;
2542
2543 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002544 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002545
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002546 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2547 = u.getWakelockStats();
2548 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2549 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002550
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002551 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2552 if (fullWakeTimer != null) {
2553 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2554 which);
2555 }
2556
2557 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2558 if (partialWakeTimer != null) {
2559 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2560 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002561 }
2562 }
2563 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002564
2565 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002566 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2567 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2568 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2569 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2570 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2571 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2572 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2573 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002574 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2575 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
2576 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
2577
Adam Lesinskie283d332015-04-16 12:29:25 -07002578 // Dump Wifi controller stats
2579 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2580 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2581 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2582 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2583 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
2584 final long wifiPowerMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
2585 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA,
2586 wifiOnTime / 1000, wifiRunningTime / 1000,
2587 wifiIdleTimeMs, wifiRxTimeMs, wifiTxTimeMs, wifiPowerMaMs / (1000*60*60));
2588
2589 // Dump Bluetooth controller stats
2590 final long btIdleTimeMs = getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
2591 final long btRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
2592 final long btTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
2593 final long btPowerMaMs = getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which);
2594 dumpLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_DATA,
2595 btIdleTimeMs, btRxTimeMs, btTxTimeMs, btPowerMaMs / (1000*60*60));
2596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 // Dump misc stats
2598 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002599 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002600 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002601 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002602 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002603 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeEnabledTime / 1000,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002604 getDeviceIdleModeEnabledCount(which), deviceIdlingTime / 1000,
2605 getDeviceIdlingCount(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002606
2607 // Dump screen brightness stats
2608 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2609 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002610 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002611 }
2612 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002613
Dianne Hackborn627bba72009-03-24 22:32:56 -07002614 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002615 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2616 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002617 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002618 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002619 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002620 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002621 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002622 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002623 args[i] = getPhoneSignalStrengthCount(i, which);
2624 }
2625 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002626
Dianne Hackborn627bba72009-03-24 22:32:56 -07002627 // Dump network type stats
2628 args = new Object[NUM_DATA_CONNECTION_TYPES];
2629 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002630 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002631 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002632 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2633 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2634 args[i] = getPhoneDataConnectionCount(i, which);
2635 }
2636 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002637
2638 // Dump wifi state stats
2639 args = new Object[NUM_WIFI_STATES];
2640 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002641 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002642 }
2643 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2644 for (int i=0; i<NUM_WIFI_STATES; i++) {
2645 args[i] = getWifiStateCount(i, which);
2646 }
2647 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2648
Dianne Hackborn3251b902014-06-20 14:40:53 -07002649 // Dump wifi suppl state stats
2650 args = new Object[NUM_WIFI_SUPPL_STATES];
2651 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2652 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2653 }
2654 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2655 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2656 args[i] = getWifiSupplStateCount(i, which);
2657 }
2658 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2659
2660 // Dump wifi signal strength stats
2661 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2662 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2663 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2664 }
2665 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2666 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2667 args[i] = getWifiSignalStrengthCount(i, which);
2668 }
2669 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2670
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002671 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002672 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002673 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002676 if (which == STATS_SINCE_UNPLUGGED) {
2677 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2678 getDischargeStartLevel()-getDischargeCurrentLevel(),
2679 getDischargeStartLevel()-getDischargeCurrentLevel(),
2680 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2681 } else {
2682 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2683 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002684 getDischargeAmountScreenOnSinceCharge(),
2685 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002686 }
2687
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002688 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002689 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002690 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002691 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002692 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002693 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002694 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002695 sb.toString());
2696 }
Evan Millarc64edde2009-04-18 12:26:32 -07002697 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002698 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002699 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002700 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2701 // Not doing the regular wake lock formatting to remain compatible
2702 // with the old checkin format.
2703 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2704 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002705 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002706 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002707 }
2708 }
Evan Millarc64edde2009-04-18 12:26:32 -07002709 }
2710
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002711 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002712 helper.create(this);
2713 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002714 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002715 if (sippers != null && sippers.size() > 0) {
2716 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2717 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002718 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002719 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2720 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2721 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002722 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002723 int uid = 0;
2724 String label;
2725 switch (bs.drainType) {
2726 case IDLE:
2727 label="idle";
2728 break;
2729 case CELL:
2730 label="cell";
2731 break;
2732 case PHONE:
2733 label="phone";
2734 break;
2735 case WIFI:
2736 label="wifi";
2737 break;
2738 case BLUETOOTH:
2739 label="blue";
2740 break;
2741 case SCREEN:
2742 label="scrn";
2743 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002744 case FLASHLIGHT:
2745 label="flashlight";
2746 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002747 case APP:
2748 uid = bs.uidObj.getUid();
2749 label = "uid";
2750 break;
2751 case USER:
2752 uid = UserHandle.getUid(bs.userId, 0);
2753 label = "user";
2754 break;
2755 case UNACCOUNTED:
2756 label = "unacc";
2757 break;
2758 case OVERCOUNTED:
2759 label = "over";
2760 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002761 case CAMERA:
2762 label = "camera";
2763 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002764 default:
2765 label = "???";
2766 }
2767 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07002768 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002769 }
2770 }
2771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 for (int iu = 0; iu < NU; iu++) {
2773 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002774 if (reqUid >= 0 && uid != reqUid) {
2775 continue;
2776 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002777 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07002778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002780 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2781 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2782 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2783 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2784 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2785 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2786 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
2787 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
2788 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2789 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002790 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2791 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002792 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002793 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2794 wifiBytesRx, wifiBytesTx,
2795 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002796 wifiPacketsRx, wifiPacketsTx,
2797 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002798 }
2799
Adam Lesinskie283d332015-04-16 12:29:25 -07002800 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2801 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2802 final int wifiScanCount = u.getWifiScanCount(which);
2803 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2804 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
2805 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
2806 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07002807 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinskie283d332015-04-16 12:29:25 -07002808 || uidWifiRunningTime != 0 || uidWifiIdleTimeMs != 0 || uidWifiRxTimeMs != 0
2809 || uidWifiTxTimeMs != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07002810 dumpLine(pw, uid, category, WIFI_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002811 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime, wifiScanCount,
2812 uidWifiIdleTimeMs, uidWifiRxTimeMs, uidWifiTxTimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07002813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814
Dianne Hackborn617f8772009-03-31 15:04:46 -07002815 if (u.hasUserActivity()) {
2816 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2817 boolean hasData = false;
2818 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2819 int val = u.getUserActivityCount(i, which);
2820 args[i] = val;
2821 if (val != 0) hasData = true;
2822 }
2823 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07002824 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002825 }
2826 }
2827
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002828 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
2829 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2830 final Uid.Wakelock wl = wakelocks.valueAt(iw);
2831 String linePrefix = "";
2832 sb.setLength(0);
2833 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
2834 rawRealtime, "f", which, linePrefix);
2835 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
2836 rawRealtime, "p", which, linePrefix);
2837 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
2838 rawRealtime, "w", which, linePrefix);
2839
2840 // Only log if we had at lease one wakelock...
2841 if (sb.length() > 0) {
2842 String name = wakelocks.keyAt(iw);
2843 if (name.indexOf(',') >= 0) {
2844 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002846 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 }
2848 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002849
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002850 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
2851 for (int isy=syncs.size()-1; isy>=0; isy--) {
2852 final Timer timer = syncs.valueAt(isy);
2853 // Convert from microseconds to milliseconds with rounding
2854 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2855 final int count = timer.getCountLocked(which);
2856 if (totalTime != 0) {
2857 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002858 }
2859 }
2860
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002861 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
2862 for (int ij=jobs.size()-1; ij>=0; ij--) {
2863 final Timer timer = jobs.valueAt(ij);
2864 // Convert from microseconds to milliseconds with rounding
2865 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2866 final int count = timer.getCountLocked(which);
2867 if (totalTime != 0) {
2868 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002869 }
2870 }
2871
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002872 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
2873 rawRealtime, which);
2874 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
2875 rawRealtime, which);
2876 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
2877 rawRealtime, which);
2878 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
2879 rawRealtime, which);
2880
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002881 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2882 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002883 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002884 final Uid.Sensor se = sensors.valueAt(ise);
2885 final int sensorNumber = sensors.keyAt(ise);
2886 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002887 if (timer != null) {
2888 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002889 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2890 / 1000;
2891 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002892 if (totalTime != 0) {
2893 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 }
2896 }
2897
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002898 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
2899 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002900
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002901 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
2902 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002903
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002904 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07002905 long totalStateTime = 0;
2906 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
2907 totalStateTime += u.getProcessStateTime(ips, rawRealtime, which);
2908 stateTimes[ips] = (totalStateTime + 500) / 1000;
2909 }
2910 if (totalStateTime > 0) {
2911 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2912 }
2913
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07002914 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
2915 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
2916 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
2917 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
2918 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
2919 powerCpuMaUs / 1000);
2920 }
2921
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002922 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
2923 = u.getProcessStats();
2924 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
2925 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002926
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002927 final long userMillis = ps.getUserTime(which);
2928 final long systemMillis = ps.getSystemTime(which);
2929 final long foregroundMillis = ps.getForegroundTime(which);
2930 final int starts = ps.getStarts(which);
2931 final int numCrashes = ps.getNumCrashes(which);
2932 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002933
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002934 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2935 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
2936 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
2937 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 }
2939 }
2940
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002941 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
2942 = u.getPackageStats();
2943 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
2944 final Uid.Pkg ps = packageStats.valueAt(ipkg);
2945 int wakeups = 0;
2946 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
2947 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
2948 wakeups += alarms.valueAt(iwa).getCountLocked(which);
2949 }
2950 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2951 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
2952 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
2953 final long startTime = ss.getStartTime(batteryUptime, which);
2954 final int starts = ss.getStarts(which);
2955 final int launches = ss.getLaunches(which);
2956 if (startTime != 0 || starts != 0 || launches != 0) {
2957 dumpLine(pw, uid, category, APK_DATA,
2958 wakeups, // wakeup alarms
2959 packageStats.keyAt(ipkg), // Apk
2960 serviceStats.keyAt(isvc), // service
2961 startTime / 1000, // time spent started, in ms
2962 starts,
2963 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965 }
2966 }
2967 }
2968 }
2969
Dianne Hackborn81038902012-11-26 17:04:09 -08002970 static final class TimerEntry {
2971 final String mName;
2972 final int mId;
2973 final BatteryStats.Timer mTimer;
2974 final long mTime;
2975 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2976 mName = name;
2977 mId = id;
2978 mTimer = timer;
2979 mTime = time;
2980 }
2981 }
2982
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002983 private void printmAh(PrintWriter printer, double power) {
2984 printer.print(BatteryStatsHelper.makemAh(power));
2985 }
2986
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07002987 private void printmAh(StringBuilder sb, double power) {
2988 sb.append(BatteryStatsHelper.makemAh(power));
2989 }
2990
Dianne Hackbornd953c532014-08-16 18:17:38 -07002991 /**
2992 * Temporary for settings.
2993 */
2994 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
2995 int reqUid) {
2996 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2997 }
2998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003000 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003001 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3003 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3004 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
3006 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3007 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3008 final long totalRealtime = computeRealtime(rawRealtime, which);
3009 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003010 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3011 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3012 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003013 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3014 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003015
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003016 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003017
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003018 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003019 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003021 sb.setLength(0);
3022 sb.append(prefix);
3023 sb.append(" Time on battery: ");
3024 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3025 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3026 sb.append(") realtime, ");
3027 formatTimeMs(sb, whichBatteryUptime / 1000);
3028 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3029 sb.append(") uptime");
3030 pw.println(sb.toString());
3031 sb.setLength(0);
3032 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003033 sb.append(" Time on battery screen off: ");
3034 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3035 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3036 sb.append(") realtime, ");
3037 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3038 sb.append("(");
3039 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3040 sb.append(") uptime");
3041 pw.println(sb.toString());
3042 sb.setLength(0);
3043 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003044 sb.append(" Total run time: ");
3045 formatTimeMs(sb, totalRealtime / 1000);
3046 sb.append("realtime, ");
3047 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003048 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003049 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003050 if (batteryTimeRemaining >= 0) {
3051 sb.setLength(0);
3052 sb.append(prefix);
3053 sb.append(" Battery time remaining: ");
3054 formatTimeMs(sb, batteryTimeRemaining / 1000);
3055 pw.println(sb.toString());
3056 }
3057 if (chargeTimeRemaining >= 0) {
3058 sb.setLength(0);
3059 sb.append(prefix);
3060 sb.append(" Charge time remaining: ");
3061 formatTimeMs(sb, chargeTimeRemaining / 1000);
3062 pw.println(sb.toString());
3063 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003064 pw.print(" Start clock time: ");
3065 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3066
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003067 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003068 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003069 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
3070 final long deviceIdleModeEnabledTime = getDeviceIdleModeEnabledTime(rawRealtime, which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003071 final long deviceIdlingTime = getDeviceIdlingTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003072 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3073 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3074 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003075 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003076 sb.append(prefix);
3077 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3078 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003079 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003080 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3081 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003082 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003083 pw.println(sb.toString());
3084 sb.setLength(0);
3085 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003086 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003087 boolean didOne = false;
3088 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003089 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003090 if (time == 0) {
3091 continue;
3092 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003093 sb.append("\n ");
3094 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003095 didOne = true;
3096 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3097 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003098 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003099 sb.append("(");
3100 sb.append(formatRatioLocked(time, screenOnTime));
3101 sb.append(")");
3102 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003103 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003104 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003105 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003106 sb.setLength(0);
3107 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003108 sb.append(" Power save mode enabled: ");
3109 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003110 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003111 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003112 sb.append(")");
3113 pw.println(sb.toString());
3114 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003115 if (deviceIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003116 sb.setLength(0);
3117 sb.append(prefix);
3118 sb.append(" Device idling: ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003119 formatTimeMs(sb, deviceIdlingTime / 1000);
3120 sb.append("(");
3121 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3122 sb.append(") "); sb.append(getDeviceIdlingCount(which));
3123 sb.append("x");
3124 pw.println(sb.toString());
3125 }
3126 if (deviceIdleModeEnabledTime != 0) {
3127 sb.setLength(0);
3128 sb.append(prefix);
3129 sb.append(" Idle mode time: ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003130 formatTimeMs(sb, deviceIdleModeEnabledTime / 1000);
3131 sb.append("(");
3132 sb.append(formatRatioLocked(deviceIdleModeEnabledTime, whichBatteryRealtime));
3133 sb.append(") "); sb.append(getDeviceIdleModeEnabledCount(which));
3134 sb.append("x");
3135 pw.println(sb.toString());
3136 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003137 if (phoneOnTime != 0) {
3138 sb.setLength(0);
3139 sb.append(prefix);
3140 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3141 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003142 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003143 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003144 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003145 if (connChanges != 0) {
3146 pw.print(prefix);
3147 pw.print(" Connectivity changes: "); pw.println(connChanges);
3148 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003149
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003150 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003151 long fullWakeLockTimeTotalMicros = 0;
3152 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003153
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003154 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003155
Evan Millar22ac0432009-03-31 11:33:18 -07003156 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003157 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003158
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003159 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3160 = u.getWakelockStats();
3161 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3162 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003163
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003164 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3165 if (fullWakeTimer != null) {
3166 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3167 rawRealtime, which);
3168 }
3169
3170 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3171 if (partialWakeTimer != null) {
3172 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3173 rawRealtime, which);
3174 if (totalTimeMicros > 0) {
3175 if (reqUid < 0) {
3176 // Only show the ordered list of all wake
3177 // locks if the caller is not asking for data
3178 // about a specific uid.
3179 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3180 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003181 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003182 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003183 }
3184 }
3185 }
3186 }
3187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003188 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3189 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3190 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3191 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3192 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3193 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3194 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3195 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003196
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003197 if (fullWakeLockTimeTotalMicros != 0) {
3198 sb.setLength(0);
3199 sb.append(prefix);
3200 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3201 (fullWakeLockTimeTotalMicros + 500) / 1000);
3202 pw.println(sb.toString());
3203 }
3204
3205 if (partialWakeLockTimeTotalMicros != 0) {
3206 sb.setLength(0);
3207 sb.append(prefix);
3208 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3209 (partialWakeLockTimeTotalMicros + 500) / 1000);
3210 pw.println(sb.toString());
3211 }
3212
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003213 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003214 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3215 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3216 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3217 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003218 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003219 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003220 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003221 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003222 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003223 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003224 if (time == 0) {
3225 continue;
3226 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003227 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003228 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003229 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003230 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003231 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003232 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003233 sb.append("(");
3234 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003235 sb.append(") ");
3236 sb.append(getPhoneSignalStrengthCount(i, which));
3237 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003238 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003239 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003240 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003241
3242 sb.setLength(0);
3243 sb.append(prefix);
3244 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003245 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003246 pw.println(sb.toString());
3247
Dianne Hackborn627bba72009-03-24 22:32:56 -07003248 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003249 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003250 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003251 didOne = false;
3252 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003253 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003254 if (time == 0) {
3255 continue;
3256 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003257 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003258 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003259 didOne = true;
3260 sb.append(DATA_CONNECTION_NAMES[i]);
3261 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003262 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003263 sb.append("(");
3264 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003265 sb.append(") ");
3266 sb.append(getPhoneDataConnectionCount(i, which));
3267 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003268 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003269 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003270 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003271
3272 sb.setLength(0);
3273 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003274 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003275 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003276 formatTimeMs(sb, mobileActiveTime / 1000);
3277 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3278 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3279 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003280 pw.println(sb.toString());
3281
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003282 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3283 if (mobileActiveUnknownTime != 0) {
3284 sb.setLength(0);
3285 sb.append(prefix);
3286 sb.append(" Mobile radio active unknown time: ");
3287 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3288 sb.append("(");
3289 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3290 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3291 sb.append("x");
3292 pw.println(sb.toString());
3293 }
3294
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003295 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3296 if (mobileActiveAdjustedTime != 0) {
3297 sb.setLength(0);
3298 sb.append(prefix);
3299 sb.append(" Mobile radio active adjusted time: ");
3300 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3301 sb.append("(");
3302 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3303 sb.append(")");
3304 pw.println(sb.toString());
3305 }
3306
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003307 pw.print(prefix);
3308 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3309 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3310 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3311 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003312 sb.setLength(0);
3313 sb.append(prefix);
3314 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3315 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3316 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3317 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003318 sb.append(")");
3319 pw.println(sb.toString());
3320
3321 sb.setLength(0);
3322 sb.append(prefix);
3323 sb.append(" Wifi states:");
3324 didOne = false;
3325 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003326 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003327 if (time == 0) {
3328 continue;
3329 }
3330 sb.append("\n ");
3331 didOne = true;
3332 sb.append(WIFI_STATE_NAMES[i]);
3333 sb.append(" ");
3334 formatTimeMs(sb, time/1000);
3335 sb.append("(");
3336 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3337 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003338 sb.append(getWifiStateCount(i, which));
3339 sb.append("x");
3340 }
3341 if (!didOne) sb.append(" (no activity)");
3342 pw.println(sb.toString());
3343
3344 sb.setLength(0);
3345 sb.append(prefix);
3346 sb.append(" Wifi supplicant states:");
3347 didOne = false;
3348 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3349 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3350 if (time == 0) {
3351 continue;
3352 }
3353 sb.append("\n ");
3354 didOne = true;
3355 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3356 sb.append(" ");
3357 formatTimeMs(sb, time/1000);
3358 sb.append("(");
3359 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3360 sb.append(") ");
3361 sb.append(getWifiSupplStateCount(i, which));
3362 sb.append("x");
3363 }
3364 if (!didOne) sb.append(" (no activity)");
3365 pw.println(sb.toString());
3366
3367 sb.setLength(0);
3368 sb.append(prefix);
3369 sb.append(" Wifi signal levels:");
3370 didOne = false;
3371 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3372 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3373 if (time == 0) {
3374 continue;
3375 }
3376 sb.append("\n ");
3377 sb.append(prefix);
3378 didOne = true;
3379 sb.append("level(");
3380 sb.append(i);
3381 sb.append(") ");
3382 formatTimeMs(sb, time/1000);
3383 sb.append("(");
3384 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3385 sb.append(") ");
3386 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003387 sb.append("x");
3388 }
3389 if (!didOne) sb.append(" (no activity)");
3390 pw.println(sb.toString());
3391
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003392 final long wifiIdleTimeMs = getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3393 final long wifiRxTimeMs = getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3394 final long wifiTxTimeMs = getWifiControllerActivity(CONTROLLER_TX_TIME, which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003395 final long wifiPowerDrainMaMs = getWifiControllerActivity(CONTROLLER_POWER_DRAIN, which);
Adam Lesinski33dac552015-03-09 15:24:48 -07003396 final long wifiTotalTimeMs = wifiIdleTimeMs + wifiRxTimeMs + wifiTxTimeMs;
3397
3398 sb.setLength(0);
3399 sb.append(prefix);
3400 sb.append(" WiFi Idle time: "); formatTimeMs(sb, wifiIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003401 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003402 sb.append(formatRatioLocked(wifiIdleTimeMs, wifiTotalTimeMs));
3403 sb.append(")");
3404 pw.println(sb.toString());
3405
3406 sb.setLength(0);
3407 sb.append(prefix);
3408 sb.append(" WiFi Rx time: "); formatTimeMs(sb, wifiRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003409 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003410 sb.append(formatRatioLocked(wifiRxTimeMs, wifiTotalTimeMs));
3411 sb.append(")");
3412 pw.println(sb.toString());
3413
3414 sb.setLength(0);
3415 sb.append(prefix);
3416 sb.append(" WiFi Tx time: "); formatTimeMs(sb, wifiTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003417 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003418 sb.append(formatRatioLocked(wifiTxTimeMs, wifiTotalTimeMs));
3419 sb.append(")");
3420 pw.println(sb.toString());
3421
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003422 sb.setLength(0);
3423 sb.append(prefix);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003424 sb.append(" WiFi Power drain: ").append(
3425 BatteryStatsHelper.makemAh(wifiPowerDrainMaMs / (double) (1000*60*60)));
3426 sb.append("mAh");
Adam Lesinskie08af192015-03-25 16:42:59 -07003427 pw.println(sb.toString());
3428
Adam Lesinski33dac552015-03-09 15:24:48 -07003429 final long bluetoothIdleTimeMs =
3430 getBluetoothControllerActivity(CONTROLLER_IDLE_TIME, which);
3431 final long bluetoothRxTimeMs = getBluetoothControllerActivity(CONTROLLER_RX_TIME, which);
3432 final long bluetoothTxTimeMs = getBluetoothControllerActivity(CONTROLLER_TX_TIME, which);
3433 final long bluetoothTotalTimeMs = bluetoothIdleTimeMs + bluetoothRxTimeMs +
3434 bluetoothTxTimeMs;
3435
3436 sb.setLength(0);
3437 sb.append(prefix);
3438 sb.append(" Bluetooth Idle time: "); formatTimeMs(sb, bluetoothIdleTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003439 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003440 sb.append(formatRatioLocked(bluetoothIdleTimeMs, bluetoothTotalTimeMs));
3441 sb.append(")");
3442 pw.println(sb.toString());
3443
3444 sb.setLength(0);
3445 sb.append(prefix);
3446 sb.append(" Bluetooth Rx time: "); formatTimeMs(sb, bluetoothRxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003447 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003448 sb.append(formatRatioLocked(bluetoothRxTimeMs, bluetoothTotalTimeMs));
3449 sb.append(")");
3450 pw.println(sb.toString());
3451
3452 sb.setLength(0);
3453 sb.append(prefix);
3454 sb.append(" Bluetooth Tx time: "); formatTimeMs(sb, bluetoothTxTimeMs);
Adam Lesinski049c88b2015-05-28 11:38:12 -07003455 sb.append("(");
Adam Lesinski33dac552015-03-09 15:24:48 -07003456 sb.append(formatRatioLocked(bluetoothTxTimeMs, bluetoothTotalTimeMs));
3457 sb.append(")");
3458 pw.println(sb.toString());
3459
Adam Lesinskie283d332015-04-16 12:29:25 -07003460 sb.setLength(0);
3461 sb.append(prefix);
3462 sb.append(" Bluetooth Power drain: ").append(BatteryStatsHelper.makemAh(
3463 getBluetoothControllerActivity(CONTROLLER_POWER_DRAIN, which) /
3464 (double)(1000*60*60)));
Adam Lesinski049c88b2015-05-28 11:38:12 -07003465 sb.append("mAh");
Adam Lesinskie283d332015-04-16 12:29:25 -07003466 pw.println(sb.toString());
3467
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003468 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003469
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003470 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003471 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003472 pw.print(prefix); pw.println(" Device is currently unplugged");
3473 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3474 pw.println(getDischargeStartLevel());
3475 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3476 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003477 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003478 pw.print(prefix); pw.println(" Device is currently plugged into power");
3479 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3480 pw.println(getDischargeStartLevel());
3481 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3482 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003483 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003484 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3485 pw.println(getDischargeAmountScreenOn());
3486 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3487 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003488 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003489 } else {
3490 pw.print(prefix); pw.println(" Device battery use since last full charge");
3491 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3492 pw.println(getLowDischargeAmountSinceCharge());
3493 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3494 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003495 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3496 pw.println(getDischargeAmountScreenOnSinceCharge());
3497 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3498 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003499 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003500 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003501
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003502 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003503 helper.create(this);
3504 helper.refreshStats(which, UserHandle.USER_ALL);
3505 List<BatterySipper> sippers = helper.getUsageList();
3506 if (sippers != null && sippers.size() > 0) {
3507 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3508 pw.print(prefix); pw.print(" Capacity: ");
3509 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003510 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003511 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3512 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3513 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3514 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003515 pw.println();
3516 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003517 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003518 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003519 switch (bs.drainType) {
3520 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003521 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003522 break;
3523 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003524 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003525 break;
3526 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003527 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003528 break;
3529 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003530 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003531 break;
3532 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003533 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003534 break;
3535 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003536 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003537 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003538 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003539 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003540 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003541 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003542 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003543 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003544 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003545 break;
3546 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003547 pw.print(" User "); pw.print(bs.userId);
3548 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003549 break;
3550 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003551 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003552 break;
3553 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003554 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003555 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003556 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003557 pw.print(" Camera: ");
3558 break;
3559 default:
3560 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003561 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003562 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003563 printmAh(pw, bs.totalPowerMah);
3564
Adam Lesinski57123002015-06-12 16:12:07 -07003565 if (bs.usagePowerMah != bs.totalPowerMah) {
3566 // If the usage (generic power) isn't the whole amount, we list out
3567 // what components are involved in the calculation.
3568
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003569 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003570 if (bs.usagePowerMah != 0) {
3571 pw.print(" usage=");
3572 printmAh(pw, bs.usagePowerMah);
3573 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003574 if (bs.cpuPowerMah != 0) {
3575 pw.print(" cpu=");
3576 printmAh(pw, bs.cpuPowerMah);
3577 }
3578 if (bs.wakeLockPowerMah != 0) {
3579 pw.print(" wake=");
3580 printmAh(pw, bs.wakeLockPowerMah);
3581 }
3582 if (bs.mobileRadioPowerMah != 0) {
3583 pw.print(" radio=");
3584 printmAh(pw, bs.mobileRadioPowerMah);
3585 }
3586 if (bs.wifiPowerMah != 0) {
3587 pw.print(" wifi=");
3588 printmAh(pw, bs.wifiPowerMah);
3589 }
3590 if (bs.gpsPowerMah != 0) {
3591 pw.print(" gps=");
3592 printmAh(pw, bs.gpsPowerMah);
3593 }
3594 if (bs.sensorPowerMah != 0) {
3595 pw.print(" sensor=");
3596 printmAh(pw, bs.sensorPowerMah);
3597 }
3598 if (bs.cameraPowerMah != 0) {
3599 pw.print(" camera=");
3600 printmAh(pw, bs.cameraPowerMah);
3601 }
3602 if (bs.flashlightPowerMah != 0) {
3603 pw.print(" flash=");
3604 printmAh(pw, bs.flashlightPowerMah);
3605 }
3606 pw.print(" )");
3607 }
3608 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003609 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003610 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003611 }
3612
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003613 sippers = helper.getMobilemsppList();
3614 if (sippers != null && sippers.size() > 0) {
3615 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003616 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003617 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003618 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003619 sb.setLength(0);
3620 sb.append(prefix); sb.append(" Uid ");
3621 UserHandle.formatUid(sb, bs.uidObj.getUid());
3622 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3623 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3624 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003625 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003626 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003627 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003628 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003629 sb.setLength(0);
3630 sb.append(prefix);
3631 sb.append(" TOTAL TIME: ");
3632 formatTimeMs(sb, totalTime);
3633 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3634 sb.append(")");
3635 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003636 pw.println();
3637 }
3638
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003639 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3640 @Override
3641 public int compare(TimerEntry lhs, TimerEntry rhs) {
3642 long lhsTime = lhs.mTime;
3643 long rhsTime = rhs.mTime;
3644 if (lhsTime < rhsTime) {
3645 return 1;
3646 }
3647 if (lhsTime > rhsTime) {
3648 return -1;
3649 }
3650 return 0;
3651 }
3652 };
3653
3654 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003655 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3656 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003657 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003658 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3659 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3660 : kernelWakelocks.entrySet()) {
3661 final BatteryStats.Timer timer = ent.getValue();
3662 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003663 if (totalTimeMillis > 0) {
3664 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3665 }
3666 }
3667 if (ktimers.size() > 0) {
3668 Collections.sort(ktimers, timerComparator);
3669 pw.print(prefix); pw.println(" All kernel wake locks:");
3670 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003671 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003672 String linePrefix = ": ";
3673 sb.setLength(0);
3674 sb.append(prefix);
3675 sb.append(" Kernel Wake lock ");
3676 sb.append(timer.mName);
3677 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3678 which, linePrefix);
3679 if (!linePrefix.equals(": ")) {
3680 sb.append(" realtime");
3681 // Only print out wake locks that were held
3682 pw.println(sb.toString());
3683 }
3684 }
3685 pw.println();
3686 }
3687 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003688
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003689 if (timers.size() > 0) {
3690 Collections.sort(timers, timerComparator);
3691 pw.print(prefix); pw.println(" All partial wake locks:");
3692 for (int i=0; i<timers.size(); i++) {
3693 TimerEntry timer = timers.get(i);
3694 sb.setLength(0);
3695 sb.append(" Wake lock ");
3696 UserHandle.formatUid(sb, timer.mId);
3697 sb.append(" ");
3698 sb.append(timer.mName);
3699 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3700 sb.append(" realtime");
3701 pw.println(sb.toString());
3702 }
3703 timers.clear();
3704 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003705 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003706
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003707 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003708 if (wakeupReasons.size() > 0) {
3709 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003710 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003711 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003712 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003713 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3714 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003715 }
3716 Collections.sort(reasons, timerComparator);
3717 for (int i=0; i<reasons.size(); i++) {
3718 TimerEntry timer = reasons.get(i);
3719 String linePrefix = ": ";
3720 sb.setLength(0);
3721 sb.append(prefix);
3722 sb.append(" Wakeup reason ");
3723 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003724 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3725 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003726 pw.println(sb.toString());
3727 }
3728 pw.println();
3729 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003730 }
Evan Millar22ac0432009-03-31 11:33:18 -07003731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 for (int iu=0; iu<NU; iu++) {
3733 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003734 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003735 continue;
3736 }
3737
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003738 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003739
3740 pw.print(prefix);
3741 pw.print(" ");
3742 UserHandle.formatUid(pw, uid);
3743 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003745
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003746 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3747 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3748 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3749 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3750 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3751 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3752 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
3753 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
3754 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3755 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
3756 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3757 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3758 final int wifiScanCount = u.getWifiScanCount(which);
3759 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003760
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003761 if (mobileRxBytes > 0 || mobileTxBytes > 0
3762 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003763 pw.print(prefix); pw.print(" Mobile network: ");
3764 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003765 pw.print(formatBytesLocked(mobileTxBytes));
3766 pw.print(" sent (packets "); pw.print(mobileRxPackets);
3767 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003769 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
3770 sb.setLength(0);
3771 sb.append(prefix); sb.append(" Mobile radio active: ");
3772 formatTimeMs(sb, uidMobileActiveTime / 1000);
3773 sb.append("(");
3774 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
3775 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
3776 long packets = mobileRxPackets + mobileTxPackets;
3777 if (packets == 0) {
3778 packets = 1;
3779 }
3780 sb.append(" @ ");
3781 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3782 sb.append(" mspp");
3783 pw.println(sb.toString());
3784 }
3785
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003786 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003787 pw.print(prefix); pw.print(" Wi-Fi network: ");
3788 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003789 pw.print(formatBytesLocked(wifiTxBytes));
3790 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3791 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003792 }
3793
Dianne Hackborn62793e42015-03-09 11:15:41 -07003794 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003795 || uidWifiRunningTime != 0) {
3796 sb.setLength(0);
3797 sb.append(prefix); sb.append(" Wifi Running: ");
3798 formatTimeMs(sb, uidWifiRunningTime / 1000);
3799 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3800 whichBatteryRealtime)); sb.append(")\n");
3801 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3802 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3803 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3804 whichBatteryRealtime)); sb.append(")\n");
3805 sb.append(prefix); sb.append(" Wifi Scan: ");
3806 formatTimeMs(sb, wifiScanTime / 1000);
3807 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07003808 whichBatteryRealtime)); sb.append(") ");
3809 sb.append(wifiScanCount);
3810 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003811 pw.println(sb.toString());
3812 }
3813
Adam Lesinski049c88b2015-05-28 11:38:12 -07003814 final long uidWifiIdleTimeMs = u.getWifiControllerActivity(CONTROLLER_IDLE_TIME, which);
3815 final long uidWifiRxTimeMs = u.getWifiControllerActivity(CONTROLLER_RX_TIME, which);
3816 final long uidWifiTxTimeMs = u.getWifiControllerActivity(CONTROLLER_TX_TIME, which);
3817 final long uidWifiTotalTimeMs = uidWifiIdleTimeMs + uidWifiRxTimeMs + uidWifiTxTimeMs;
3818 if (uidWifiTotalTimeMs > 0) {
3819 sb.setLength(0);
3820 sb.append(prefix).append(" WiFi Idle time: ");
3821 formatTimeMs(sb, uidWifiIdleTimeMs);
3822 sb.append("(").append(formatRatioLocked(uidWifiIdleTimeMs, uidWifiTotalTimeMs))
3823 .append(")\n");
3824
3825 sb.append(prefix).append(" WiFi Rx time: "); formatTimeMs(sb, uidWifiRxTimeMs);
3826 sb.append("(").append(formatRatioLocked(uidWifiRxTimeMs, uidWifiTotalTimeMs))
3827 .append(")\n");
3828
3829 sb.append(prefix).append(" WiFi Tx time: "); formatTimeMs(sb, uidWifiTxTimeMs);
3830 sb.append("(").append(formatRatioLocked(uidWifiTxTimeMs, uidWifiTotalTimeMs))
3831 .append(")");
3832 pw.println(sb.toString());
3833 }
3834
Dianne Hackborn617f8772009-03-31 15:04:46 -07003835 if (u.hasUserActivity()) {
3836 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07003837 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003838 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003839 if (val != 0) {
3840 if (!hasData) {
3841 sb.setLength(0);
3842 sb.append(" User activity: ");
3843 hasData = true;
3844 } else {
3845 sb.append(", ");
3846 }
3847 sb.append(val);
3848 sb.append(" ");
3849 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3850 }
3851 }
3852 if (hasData) {
3853 pw.println(sb.toString());
3854 }
3855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003857 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3858 = u.getWakelockStats();
3859 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003860 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003861 int countWakelock = 0;
3862 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3863 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3864 String linePrefix = ": ";
3865 sb.setLength(0);
3866 sb.append(prefix);
3867 sb.append(" Wake lock ");
3868 sb.append(wakelocks.keyAt(iw));
3869 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
3870 "full", which, linePrefix);
3871 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
3872 "partial", which, linePrefix);
3873 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
3874 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003875 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
3876 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07003877 sb.append(" realtime");
3878 pw.println(sb.toString());
3879 uidActivity = true;
3880 countWakelock++;
3881
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003882 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
3883 rawRealtime, which);
3884 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
3885 rawRealtime, which);
3886 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
3887 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003888 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07003889 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003890 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003891 if (countWakelock > 1) {
3892 if (totalFullWakelock != 0 || totalPartialWakelock != 0
3893 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003894 sb.setLength(0);
3895 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003896 sb.append(" TOTAL wake: ");
3897 boolean needComma = false;
3898 if (totalFullWakelock != 0) {
3899 needComma = true;
3900 formatTimeMs(sb, totalFullWakelock);
3901 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003902 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003903 if (totalPartialWakelock != 0) {
3904 if (needComma) {
3905 sb.append(", ");
3906 }
3907 needComma = true;
3908 formatTimeMs(sb, totalPartialWakelock);
3909 sb.append("partial");
3910 }
3911 if (totalWindowWakelock != 0) {
3912 if (needComma) {
3913 sb.append(", ");
3914 }
3915 needComma = true;
3916 formatTimeMs(sb, totalWindowWakelock);
3917 sb.append("window");
3918 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003919 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07003920 if (needComma) {
3921 sb.append(",");
3922 }
3923 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07003924 formatTimeMs(sb, totalDrawWakelock);
3925 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07003926 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003927 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003928 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003929 }
3930 }
3931
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003932 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3933 for (int isy=syncs.size()-1; isy>=0; isy--) {
3934 final Timer timer = syncs.valueAt(isy);
3935 // Convert from microseconds to milliseconds with rounding
3936 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3937 final int count = timer.getCountLocked(which);
3938 sb.setLength(0);
3939 sb.append(prefix);
3940 sb.append(" Sync ");
3941 sb.append(syncs.keyAt(isy));
3942 sb.append(": ");
3943 if (totalTime != 0) {
3944 formatTimeMs(sb, totalTime);
3945 sb.append("realtime (");
3946 sb.append(count);
3947 sb.append(" times)");
3948 } else {
3949 sb.append("(not used)");
3950 }
3951 pw.println(sb.toString());
3952 uidActivity = true;
3953 }
3954
3955 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3956 for (int ij=jobs.size()-1; ij>=0; ij--) {
3957 final Timer timer = jobs.valueAt(ij);
3958 // Convert from microseconds to milliseconds with rounding
3959 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3960 final int count = timer.getCountLocked(which);
3961 sb.setLength(0);
3962 sb.append(prefix);
3963 sb.append(" Job ");
3964 sb.append(jobs.keyAt(ij));
3965 sb.append(": ");
3966 if (totalTime != 0) {
3967 formatTimeMs(sb, totalTime);
3968 sb.append("realtime (");
3969 sb.append(count);
3970 sb.append(" times)");
3971 } else {
3972 sb.append("(not used)");
3973 }
3974 pw.println(sb.toString());
3975 uidActivity = true;
3976 }
3977
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003978 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
3979 prefix, "Flashlight");
3980 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
3981 prefix, "Camera");
3982 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
3983 prefix, "Video");
3984 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
3985 prefix, "Audio");
3986
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003987 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3988 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003989 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003990 final Uid.Sensor se = sensors.valueAt(ise);
3991 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003992 sb.setLength(0);
3993 sb.append(prefix);
3994 sb.append(" Sensor ");
3995 int handle = se.getHandle();
3996 if (handle == Uid.Sensor.GPS) {
3997 sb.append("GPS");
3998 } else {
3999 sb.append(handle);
4000 }
4001 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004003 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004004 if (timer != null) {
4005 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004006 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004007 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004008 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004009 //timer.logState();
4010 if (totalTime != 0) {
4011 formatTimeMs(sb, totalTime);
4012 sb.append("realtime (");
4013 sb.append(count);
4014 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 } else {
4016 sb.append("(not used)");
4017 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004018 } else {
4019 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004021
4022 pw.println(sb.toString());
4023 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004026 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4027 "Vibrator");
4028 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4029 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004030
Dianne Hackborn61659e52014-07-09 16:13:01 -07004031 long totalStateTime = 0;
4032 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4033 long time = u.getProcessStateTime(ips, rawRealtime, which);
4034 if (time > 0) {
4035 totalStateTime += time;
4036 sb.setLength(0);
4037 sb.append(prefix);
4038 sb.append(" ");
4039 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4040 sb.append(" for: ");
4041 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4042 pw.println(sb.toString());
4043 uidActivity = true;
4044 }
4045 }
4046
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004047 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4048 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004049 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4050 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004051 sb.setLength(0);
4052 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004053 sb.append(" Total cpu time: u=");
4054 formatTimeMs(sb, userCpuTimeUs / 1000);
4055 sb.append("s=");
4056 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004057 sb.append("p=");
4058 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4059 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004060 pw.println(sb.toString());
4061 }
4062
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004063 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4064 = u.getProcessStats();
4065 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4066 final Uid.Proc ps = processStats.valueAt(ipr);
4067 long userTime;
4068 long systemTime;
4069 long foregroundTime;
4070 int starts;
4071 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004073 userTime = ps.getUserTime(which);
4074 systemTime = ps.getSystemTime(which);
4075 foregroundTime = ps.getForegroundTime(which);
4076 starts = ps.getStarts(which);
4077 final int numCrashes = ps.getNumCrashes(which);
4078 final int numAnrs = ps.getNumAnrs(which);
4079 numExcessive = which == STATS_SINCE_CHARGED
4080 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004082 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4083 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4084 sb.setLength(0);
4085 sb.append(prefix); sb.append(" Proc ");
4086 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4087 sb.append(prefix); sb.append(" CPU: ");
4088 formatTimeMs(sb, userTime); sb.append("usr + ");
4089 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4090 formatTimeMs(sb, foregroundTime); sb.append("fg");
4091 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4092 sb.append("\n"); sb.append(prefix); sb.append(" ");
4093 boolean hasOne = false;
4094 if (starts != 0) {
4095 hasOne = true;
4096 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004097 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004098 if (numCrashes != 0) {
4099 if (hasOne) {
4100 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004101 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004102 hasOne = true;
4103 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004104 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004105 if (numAnrs != 0) {
4106 if (hasOne) {
4107 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004109 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
4111 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004112 pw.println(sb.toString());
4113 for (int e=0; e<numExcessive; e++) {
4114 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4115 if (ew != null) {
4116 pw.print(prefix); pw.print(" * Killed for ");
4117 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4118 pw.print("wake lock");
4119 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4120 pw.print("cpu");
4121 } else {
4122 pw.print("unknown");
4123 }
4124 pw.print(" use: ");
4125 TimeUtils.formatDuration(ew.usedTime, pw);
4126 pw.print(" over ");
4127 TimeUtils.formatDuration(ew.overTime, pw);
4128 if (ew.overTime != 0) {
4129 pw.print(" (");
4130 pw.print((ew.usedTime*100)/ew.overTime);
4131 pw.println("%)");
4132 }
4133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
4135 uidActivity = true;
4136 }
4137 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004138
4139 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4140 = u.getPackageStats();
4141 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4142 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4143 pw.println(":");
4144 boolean apkActivity = false;
4145 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4146 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4147 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4148 pw.print(prefix); pw.print(" Wakeup alarm ");
4149 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4150 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4151 pw.println(" times");
4152 apkActivity = true;
4153 }
4154 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4155 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4156 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4157 final long startTime = ss.getStartTime(batteryUptime, which);
4158 final int starts = ss.getStarts(which);
4159 final int launches = ss.getLaunches(which);
4160 if (startTime != 0 || starts != 0 || launches != 0) {
4161 sb.setLength(0);
4162 sb.append(prefix); sb.append(" Service ");
4163 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4164 sb.append(prefix); sb.append(" Created for: ");
4165 formatTimeMs(sb, startTime / 1000);
4166 sb.append("uptime\n");
4167 sb.append(prefix); sb.append(" Starts: ");
4168 sb.append(starts);
4169 sb.append(", launches: "); sb.append(launches);
4170 pw.println(sb.toString());
4171 apkActivity = true;
4172 }
4173 }
4174 if (!apkActivity) {
4175 pw.print(prefix); pw.println(" (nothing executed)");
4176 }
4177 uidActivity = true;
4178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004180 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182 }
4183 }
4184
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004185 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004186 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004187 int diff = oldval ^ newval;
4188 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004189 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004190 for (int i=0; i<descriptions.length; i++) {
4191 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004192 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004193 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004194 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004195 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004196 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004197 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4198 didWake = true;
4199 pw.print("=");
4200 if (longNames) {
4201 UserHandle.formatUid(pw, wakelockTag.uid);
4202 pw.print(":\"");
4203 pw.print(wakelockTag.string);
4204 pw.print("\"");
4205 } else {
4206 pw.print(wakelockTag.poolIdx);
4207 }
4208 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004209 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004210 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004211 pw.print("=");
4212 int val = (newval&bd.mask)>>bd.shift;
4213 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004214 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004215 } else {
4216 pw.print(val);
4217 }
4218 }
4219 }
4220 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004221 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004222 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004223 if (longNames) {
4224 UserHandle.formatUid(pw, wakelockTag.uid);
4225 pw.print(":\"");
4226 pw.print(wakelockTag.string);
4227 pw.print("\"");
4228 } else {
4229 pw.print(wakelockTag.poolIdx);
4230 }
4231 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004232 }
4233
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004234 public void prepareForDumpLocked() {
4235 }
4236
4237 public static class HistoryPrinter {
4238 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004239 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004240 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004241 int oldStatus = -1;
4242 int oldHealth = -1;
4243 int oldPlug = -1;
4244 int oldTemp = -1;
4245 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004246 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004247
Dianne Hackborn3251b902014-06-20 14:40:53 -07004248 void reset() {
4249 oldState = oldState2 = 0;
4250 oldLevel = -1;
4251 oldStatus = -1;
4252 oldHealth = -1;
4253 oldPlug = -1;
4254 oldTemp = -1;
4255 oldVolt = -1;
4256 }
4257
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004258 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004259 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004260 if (!checkin) {
4261 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004262 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004263 pw.print(" (");
4264 pw.print(rec.numReadInts);
4265 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004266 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004267 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4268 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004269 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004270 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004271 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004272 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004273 }
4274 lastTime = rec.time;
4275 }
4276 if (rec.cmd == HistoryItem.CMD_START) {
4277 if (checkin) {
4278 pw.print(":");
4279 }
4280 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004281 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004282 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4283 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004284 if (checkin) {
4285 pw.print(":");
4286 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004287 if (rec.cmd == HistoryItem.CMD_RESET) {
4288 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004289 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004290 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004291 pw.print("TIME:");
4292 if (checkin) {
4293 pw.println(rec.currentTime);
4294 } else {
4295 pw.print(" ");
4296 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4297 rec.currentTime).toString());
4298 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004299 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4300 if (checkin) {
4301 pw.print(":");
4302 }
4303 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004304 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4305 if (checkin) {
4306 pw.print(":");
4307 }
4308 pw.println("*OVERFLOW*");
4309 } else {
4310 if (!checkin) {
4311 if (rec.batteryLevel < 10) pw.print("00");
4312 else if (rec.batteryLevel < 100) pw.print("0");
4313 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004314 if (verbose) {
4315 pw.print(" ");
4316 if (rec.states < 0) ;
4317 else if (rec.states < 0x10) pw.print("0000000");
4318 else if (rec.states < 0x100) pw.print("000000");
4319 else if (rec.states < 0x1000) pw.print("00000");
4320 else if (rec.states < 0x10000) pw.print("0000");
4321 else if (rec.states < 0x100000) pw.print("000");
4322 else if (rec.states < 0x1000000) pw.print("00");
4323 else if (rec.states < 0x10000000) pw.print("0");
4324 pw.print(Integer.toHexString(rec.states));
4325 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004326 } else {
4327 if (oldLevel != rec.batteryLevel) {
4328 oldLevel = rec.batteryLevel;
4329 pw.print(",Bl="); pw.print(rec.batteryLevel);
4330 }
4331 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004332 if (oldStatus != rec.batteryStatus) {
4333 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004334 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004335 switch (oldStatus) {
4336 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004337 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004338 break;
4339 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004340 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004341 break;
4342 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004343 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004344 break;
4345 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004346 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004347 break;
4348 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004349 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004350 break;
4351 default:
4352 pw.print(oldStatus);
4353 break;
4354 }
4355 }
4356 if (oldHealth != rec.batteryHealth) {
4357 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004358 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004359 switch (oldHealth) {
4360 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004361 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004362 break;
4363 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004364 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004365 break;
4366 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004367 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004368 break;
4369 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004370 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004371 break;
4372 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004373 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004374 break;
4375 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004376 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004377 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004378 case BatteryManager.BATTERY_HEALTH_COLD:
4379 pw.print(checkin ? "c" : "cold");
4380 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004381 default:
4382 pw.print(oldHealth);
4383 break;
4384 }
4385 }
4386 if (oldPlug != rec.batteryPlugType) {
4387 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004388 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004389 switch (oldPlug) {
4390 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004391 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004392 break;
4393 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004394 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004395 break;
4396 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004397 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004398 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004399 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004400 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004401 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004402 default:
4403 pw.print(oldPlug);
4404 break;
4405 }
4406 }
4407 if (oldTemp != rec.batteryTemperature) {
4408 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004409 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004410 pw.print(oldTemp);
4411 }
4412 if (oldVolt != rec.batteryVoltage) {
4413 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004414 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004415 pw.print(oldVolt);
4416 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004417 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004418 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004419 printBitDescriptions(pw, oldState2, rec.states2, null,
4420 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004421 if (rec.wakeReasonTag != null) {
4422 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004423 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004424 pw.print(rec.wakeReasonTag.poolIdx);
4425 } else {
4426 pw.print(" wake_reason=");
4427 pw.print(rec.wakeReasonTag.uid);
4428 pw.print(":\"");
4429 pw.print(rec.wakeReasonTag.string);
4430 pw.print("\"");
4431 }
4432 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004433 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004434 pw.print(checkin ? "," : " ");
4435 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4436 pw.print("+");
4437 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4438 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004439 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004440 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4441 : HISTORY_EVENT_NAMES;
4442 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4443 | HistoryItem.EVENT_FLAG_FINISH);
4444 if (idx >= 0 && idx < eventNames.length) {
4445 pw.print(eventNames[idx]);
4446 } else {
4447 pw.print(checkin ? "Ev" : "event");
4448 pw.print(idx);
4449 }
4450 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004451 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004452 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004453 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004454 UserHandle.formatUid(pw, rec.eventTag.uid);
4455 pw.print(":\"");
4456 pw.print(rec.eventTag.string);
4457 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004458 }
4459 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004460 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004461 if (rec.stepDetails != null) {
4462 if (!checkin) {
4463 pw.print(" Details: cpu=");
4464 pw.print(rec.stepDetails.userTime);
4465 pw.print("u+");
4466 pw.print(rec.stepDetails.systemTime);
4467 pw.print("s");
4468 if (rec.stepDetails.appCpuUid1 >= 0) {
4469 pw.print(" (");
4470 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4471 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4472 if (rec.stepDetails.appCpuUid2 >= 0) {
4473 pw.print(", ");
4474 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4475 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4476 }
4477 if (rec.stepDetails.appCpuUid3 >= 0) {
4478 pw.print(", ");
4479 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4480 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4481 }
4482 pw.print(')');
4483 }
4484 pw.println();
4485 pw.print(" /proc/stat=");
4486 pw.print(rec.stepDetails.statUserTime);
4487 pw.print(" usr, ");
4488 pw.print(rec.stepDetails.statSystemTime);
4489 pw.print(" sys, ");
4490 pw.print(rec.stepDetails.statIOWaitTime);
4491 pw.print(" io, ");
4492 pw.print(rec.stepDetails.statIrqTime);
4493 pw.print(" irq, ");
4494 pw.print(rec.stepDetails.statSoftIrqTime);
4495 pw.print(" sirq, ");
4496 pw.print(rec.stepDetails.statIdlTime);
4497 pw.print(" idle");
4498 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4499 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4500 + rec.stepDetails.statSoftIrqTime;
4501 int total = totalRun + rec.stepDetails.statIdlTime;
4502 if (total > 0) {
4503 pw.print(" (");
4504 float perc = ((float)totalRun) / ((float)total) * 100;
4505 pw.print(String.format("%.1f%%", perc));
4506 pw.print(" of ");
4507 StringBuilder sb = new StringBuilder(64);
4508 formatTimeMsNoSpace(sb, total*10);
4509 pw.print(sb);
4510 pw.print(")");
4511 }
4512 pw.println();
4513 } else {
4514 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4515 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4516 pw.print(rec.stepDetails.userTime);
4517 pw.print(":");
4518 pw.print(rec.stepDetails.systemTime);
4519 if (rec.stepDetails.appCpuUid1 >= 0) {
4520 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4521 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4522 if (rec.stepDetails.appCpuUid2 >= 0) {
4523 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4524 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4525 }
4526 if (rec.stepDetails.appCpuUid3 >= 0) {
4527 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4528 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4529 }
4530 }
4531 pw.println();
4532 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4533 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4534 pw.print(rec.stepDetails.statUserTime);
4535 pw.print(',');
4536 pw.print(rec.stepDetails.statSystemTime);
4537 pw.print(',');
4538 pw.print(rec.stepDetails.statIOWaitTime);
4539 pw.print(',');
4540 pw.print(rec.stepDetails.statIrqTime);
4541 pw.print(',');
4542 pw.print(rec.stepDetails.statSoftIrqTime);
4543 pw.print(',');
4544 pw.print(rec.stepDetails.statIdlTime);
4545 pw.println();
4546 }
4547 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004548 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004549 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004550 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004551 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004552
4553 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4554 UserHandle.formatUid(pw, uid);
4555 pw.print("=");
4556 pw.print(utime);
4557 pw.print("u+");
4558 pw.print(stime);
4559 pw.print("s");
4560 }
4561
4562 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4563 pw.print('/');
4564 pw.print(uid);
4565 pw.print(":");
4566 pw.print(utime);
4567 pw.print(":");
4568 pw.print(stime);
4569 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004570 }
4571
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004572 private void printSizeValue(PrintWriter pw, long size) {
4573 float result = size;
4574 String suffix = "";
4575 if (result >= 10*1024) {
4576 suffix = "KB";
4577 result = result / 1024;
4578 }
4579 if (result >= 10*1024) {
4580 suffix = "MB";
4581 result = result / 1024;
4582 }
4583 if (result >= 10*1024) {
4584 suffix = "GB";
4585 result = result / 1024;
4586 }
4587 if (result >= 10*1024) {
4588 suffix = "TB";
4589 result = result / 1024;
4590 }
4591 if (result >= 10*1024) {
4592 suffix = "PB";
4593 result = result / 1024;
4594 }
4595 pw.print((int)result);
4596 pw.print(suffix);
4597 }
4598
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004599 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4600 String label3, long estimatedTime) {
4601 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004602 return false;
4603 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004604 pw.print(label1);
4605 pw.print(label2);
4606 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004607 StringBuilder sb = new StringBuilder(64);
4608 formatTimeMs(sb, estimatedTime);
4609 pw.print(sb);
4610 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004611 return true;
4612 }
4613
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004614 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4615 LevelStepTracker steps, boolean checkin) {
4616 if (steps == null) {
4617 return false;
4618 }
4619 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004620 if (count <= 0) {
4621 return false;
4622 }
4623 if (!checkin) {
4624 pw.println(header);
4625 }
Kweku Adams030980a2015-04-01 16:07:48 -07004626 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004627 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004628 long duration = steps.getDurationAt(i);
4629 int level = steps.getLevelAt(i);
4630 long initMode = steps.getInitModeAt(i);
4631 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004632 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004633 lineArgs[0] = Long.toString(duration);
4634 lineArgs[1] = Integer.toString(level);
4635 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4636 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4637 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4638 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4639 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4640 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004641 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004642 }
4643 } else {
4644 lineArgs[2] = "";
4645 }
4646 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4647 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4648 } else {
4649 lineArgs[3] = "";
4650 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004651 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004652 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004653 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004654 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004655 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004656 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4657 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004658 pw.print(prefix);
4659 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004660 TimeUtils.formatDuration(duration, pw);
4661 pw.print(" to "); pw.print(level);
4662 boolean haveModes = false;
4663 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4664 pw.print(" (");
4665 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4666 case Display.STATE_OFF: pw.print("screen-off"); break;
4667 case Display.STATE_ON: pw.print("screen-on"); break;
4668 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4669 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004670 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004671 }
4672 haveModes = true;
4673 }
4674 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4675 pw.print(haveModes ? ", " : " (");
4676 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4677 ? "power-save-on" : "power-save-off");
4678 haveModes = true;
4679 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004680 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4681 pw.print(haveModes ? ", " : " (");
4682 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4683 ? "device-idle-on" : "device-idle-off");
4684 haveModes = true;
4685 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004686 if (haveModes) {
4687 pw.print(")");
4688 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004689 pw.println();
4690 }
4691 }
4692 return true;
4693 }
4694
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004695 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004696 public static final int DUMP_DAILY_ONLY = 1<<2;
4697 public static final int DUMP_HISTORY_ONLY = 1<<3;
4698 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4699 public static final int DUMP_VERBOSE = 1<<5;
4700 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004701
Dianne Hackborn37de0982014-05-09 09:32:18 -07004702 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4703 final HistoryPrinter hprinter = new HistoryPrinter();
4704 final HistoryItem rec = new HistoryItem();
4705 long lastTime = -1;
4706 long baseTime = -1;
4707 boolean printed = false;
4708 HistoryEventTracker tracker = null;
4709 while (getNextHistoryLocked(rec)) {
4710 lastTime = rec.time;
4711 if (baseTime < 0) {
4712 baseTime = lastTime;
4713 }
4714 if (rec.time >= histStart) {
4715 if (histStart >= 0 && !printed) {
4716 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004717 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004718 || rec.cmd == HistoryItem.CMD_START
4719 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004720 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004721 hprinter.printNextItem(pw, rec, baseTime, checkin,
4722 (flags&DUMP_VERBOSE) != 0);
4723 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004724 } else if (rec.currentTime != 0) {
4725 printed = true;
4726 byte cmd = rec.cmd;
4727 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004728 hprinter.printNextItem(pw, rec, baseTime, checkin,
4729 (flags&DUMP_VERBOSE) != 0);
4730 rec.cmd = cmd;
4731 }
4732 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004733 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4734 hprinter.printNextItem(pw, rec, baseTime, checkin,
4735 (flags&DUMP_VERBOSE) != 0);
4736 rec.cmd = HistoryItem.CMD_UPDATE;
4737 }
4738 int oldEventCode = rec.eventCode;
4739 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004740 rec.eventTag = new HistoryTag();
4741 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4742 HashMap<String, SparseIntArray> active
4743 = tracker.getStateForEvent(i);
4744 if (active == null) {
4745 continue;
4746 }
4747 for (HashMap.Entry<String, SparseIntArray> ent
4748 : active.entrySet()) {
4749 SparseIntArray uids = ent.getValue();
4750 for (int j=0; j<uids.size(); j++) {
4751 rec.eventCode = i;
4752 rec.eventTag.string = ent.getKey();
4753 rec.eventTag.uid = uids.keyAt(j);
4754 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07004755 hprinter.printNextItem(pw, rec, baseTime, checkin,
4756 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004757 rec.wakeReasonTag = null;
4758 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004759 }
4760 }
4761 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004762 rec.eventCode = oldEventCode;
4763 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004764 tracker = null;
4765 }
4766 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004767 hprinter.printNextItem(pw, rec, baseTime, checkin,
4768 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004769 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
4770 // This is an attempt to aggregate the previous state and generate
4771 // fake events to reflect that state at the point where we start
4772 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07004773 if (tracker == null) {
4774 tracker = new HistoryEventTracker();
4775 }
4776 tracker.updateState(rec.eventCode, rec.eventTag.string,
4777 rec.eventTag.uid, rec.eventTag.poolIdx);
4778 }
4779 }
4780 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004781 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004782 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
4783 }
4784 }
4785
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004786 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
4787 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
4788 if (steps == null) {
4789 return;
4790 }
4791 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
4792 if (timeRemaining >= 0) {
4793 pw.print(prefix); pw.print(label); pw.print(" total time: ");
4794 tmpSb.setLength(0);
4795 formatTimeMs(tmpSb, timeRemaining);
4796 pw.print(tmpSb);
4797 pw.print(" (from "); pw.print(tmpOutInt[0]);
4798 pw.println(" steps)");
4799 }
4800 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4801 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4802 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
4803 if (estimatedTime > 0) {
4804 pw.print(prefix); pw.print(label); pw.print(" ");
4805 pw.print(STEP_LEVEL_MODE_LABELS[i]);
4806 pw.print(" time: ");
4807 tmpSb.setLength(0);
4808 formatTimeMs(tmpSb, estimatedTime);
4809 pw.print(tmpSb);
4810 pw.print(" (from "); pw.print(tmpOutInt[0]);
4811 pw.println(" steps)");
4812 }
4813 }
4814 }
4815
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004816 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
4817 ArrayList<PackageChange> changes) {
4818 if (changes == null) {
4819 return;
4820 }
4821 pw.print(prefix); pw.println("Package changes:");
4822 for (int i=0; i<changes.size(); i++) {
4823 PackageChange pc = changes.get(i);
4824 if (pc.mUpdate) {
4825 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
4826 pw.print(" vers="); pw.println(pc.mVersionCode);
4827 } else {
4828 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
4829 }
4830 }
4831 }
4832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 /**
4834 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
4835 *
4836 * @param pw a Printer to receive the dump output.
4837 */
4838 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004839 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004840 prepareForDumpLocked();
4841
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004842 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004843 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004844
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004845 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004846 final long historyTotalSize = getHistoryTotalSize();
4847 final long historyUsedSize = getHistoryUsedSize();
4848 if (startIteratingHistoryLocked()) {
4849 try {
4850 pw.print("Battery History (");
4851 pw.print((100*historyUsedSize)/historyTotalSize);
4852 pw.print("% used, ");
4853 printSizeValue(pw, historyUsedSize);
4854 pw.print(" used of ");
4855 printSizeValue(pw, historyTotalSize);
4856 pw.print(", ");
4857 pw.print(getHistoryStringPoolSize());
4858 pw.print(" strings using ");
4859 printSizeValue(pw, getHistoryStringPoolBytes());
4860 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07004861 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004862 pw.println();
4863 } finally {
4864 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004865 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004866 }
4867
4868 if (startIteratingOldHistoryLocked()) {
4869 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004870 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004871 pw.println("Old battery History:");
4872 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004873 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004874 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004875 if (baseTime < 0) {
4876 baseTime = rec.time;
4877 }
4878 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004879 }
4880 pw.println();
4881 } finally {
4882 finishIteratingOldHistoryLocked();
4883 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004884 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004885 }
4886
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004887 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004888 return;
4889 }
4890
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004891 if (!filtering) {
4892 SparseArray<? extends Uid> uidStats = getUidStats();
4893 final int NU = uidStats.size();
4894 boolean didPid = false;
4895 long nowRealtime = SystemClock.elapsedRealtime();
4896 for (int i=0; i<NU; i++) {
4897 Uid uid = uidStats.valueAt(i);
4898 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
4899 if (pids != null) {
4900 for (int j=0; j<pids.size(); j++) {
4901 Uid.Pid pid = pids.valueAt(j);
4902 if (!didPid) {
4903 pw.println("Per-PID Stats:");
4904 didPid = true;
4905 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004906 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
4907 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004908 pw.print(" PID "); pw.print(pids.keyAt(j));
4909 pw.print(" wake time: ");
4910 TimeUtils.formatDuration(time, pw);
4911 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07004912 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07004913 }
4914 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004915 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004916 pw.println();
4917 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004918 }
4919
4920 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004921 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
4922 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004923 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4924 if (timeRemaining >= 0) {
4925 pw.print(" Estimated discharge time remaining: ");
4926 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4927 pw.println();
4928 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004929 final LevelStepTracker steps = getDischargeLevelStepTracker();
4930 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
4931 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
4932 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
4933 STEP_LEVEL_MODE_VALUES[i], null));
4934 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004935 pw.println();
4936 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004937 if (dumpDurationSteps(pw, " ", "Charge step durations:",
4938 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004939 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4940 if (timeRemaining >= 0) {
4941 pw.print(" Estimated charge time remaining: ");
4942 TimeUtils.formatDuration(timeRemaining / 1000, pw);
4943 pw.println();
4944 }
4945 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004946 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004947 }
4948 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
4949 pw.println("Daily stats:");
4950 pw.print(" Current start time: ");
4951 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4952 getCurrentDailyStartTime()).toString());
4953 pw.print(" Next min deadline: ");
4954 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4955 getNextMinDailyDeadline()).toString());
4956 pw.print(" Next max deadline: ");
4957 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4958 getNextMaxDailyDeadline()).toString());
4959 StringBuilder sb = new StringBuilder(64);
4960 int[] outInt = new int[1];
4961 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
4962 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004963 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
4964 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004965 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004966 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
4967 dsteps, false)) {
4968 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
4969 sb, outInt);
4970 }
4971 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
4972 csteps, false)) {
4973 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
4974 sb, outInt);
4975 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004976 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004977 } else {
4978 pw.println(" Current daily steps:");
4979 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
4980 sb, outInt);
4981 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
4982 sb, outInt);
4983 }
4984 }
4985 DailyItem dit;
4986 int curIndex = 0;
4987 while ((dit=getDailyItemLocked(curIndex)) != null) {
4988 curIndex++;
4989 if ((flags&DUMP_DAILY_ONLY) != 0) {
4990 pw.println();
4991 }
4992 pw.print(" Daily from ");
4993 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
4994 pw.print(" to ");
4995 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
4996 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004997 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004998 if (dumpDurationSteps(pw, " ",
4999 " Discharge step durations:", dit.mDischargeSteps, false)) {
5000 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5001 sb, outInt);
5002 }
5003 if (dumpDurationSteps(pw, " ",
5004 " Charge step durations:", dit.mChargeSteps, false)) {
5005 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5006 sb, outInt);
5007 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005008 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005009 } else {
5010 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5011 sb, outInt);
5012 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5013 sb, outInt);
5014 }
5015 }
5016 pw.println();
5017 }
5018 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005019 pw.println("Statistics since last charge:");
5020 pw.println(" System starts: " + getStartCount()
5021 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005022 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5023 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005024 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 }
5027
5028 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005029 public void dumpCheckinLocked(Context context, PrintWriter pw,
5030 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005031 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005032
5033 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005034 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5035 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005036
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005037 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5038
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005039 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005040 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005041
5042 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005043 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005044 try {
5045 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5046 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5047 pw.print(HISTORY_STRING_POOL); pw.print(',');
5048 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005049 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005050 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005051 pw.print(",\"");
5052 String str = getHistoryTagPoolString(i);
5053 str = str.replace("\\", "\\\\");
5054 str = str.replace("\"", "\\\"");
5055 pw.print(str);
5056 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005057 pw.println();
5058 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005059 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005060 } finally {
5061 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005062 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005063 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005064 }
5065
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005066 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005067 return;
5068 }
5069
Dianne Hackborne4a59512010-12-07 11:08:07 -08005070 if (apps != null) {
5071 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5072 for (int i=0; i<apps.size(); i++) {
5073 ApplicationInfo ai = apps.get(i);
5074 ArrayList<String> pkgs = uids.get(ai.uid);
5075 if (pkgs == null) {
5076 pkgs = new ArrayList<String>();
5077 uids.put(ai.uid, pkgs);
5078 }
5079 pkgs.add(ai.packageName);
5080 }
5081 SparseArray<? extends Uid> uidStats = getUidStats();
5082 final int NU = uidStats.size();
5083 String[] lineArgs = new String[2];
5084 for (int i=0; i<NU; i++) {
5085 int uid = uidStats.keyAt(i);
5086 ArrayList<String> pkgs = uids.get(uid);
5087 if (pkgs != null) {
5088 for (int j=0; j<pkgs.size(); j++) {
5089 lineArgs[0] = Integer.toString(uid);
5090 lineArgs[1] = pkgs.get(j);
5091 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5092 (Object[])lineArgs);
5093 }
5094 }
5095 }
5096 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005097 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005098 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005099 String[] lineArgs = new String[1];
5100 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5101 if (timeRemaining >= 0) {
5102 lineArgs[0] = Long.toString(timeRemaining);
5103 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5104 (Object[])lineArgs);
5105 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005106 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005107 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5108 if (timeRemaining >= 0) {
5109 lineArgs[0] = Long.toString(timeRemaining);
5110 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5111 (Object[])lineArgs);
5112 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005113 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5114 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117}