blob: f528d63b4844bbe6562b5f4e33ba70b73410ccc5 [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
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Bookatz8bdae8d2018-01-16 11:24:30 -080023import android.server.ServerProtoEnums;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070024import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080025import android.telephony.SignalStrength;
Tej Singheee317b2018-03-07 19:28:05 -080026import android.telephony.TelephonyManager;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080027import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070028import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070029import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070030import android.util.MutableBoolean;
31import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Printer;
33import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070034import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070035import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070036import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070037import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080038
Sudheer Shankab2f83c12017-11-13 19:25:01 -080039import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080040import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080041import com.android.internal.os.BatterySipper;
42import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Kweku Adams2f73ecd2017-09-27 16:59:19 -070044import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070045import java.io.PrintWriter;
46import java.util.ArrayList;
47import java.util.Collections;
48import java.util.Comparator;
49import java.util.Formatter;
50import java.util.HashMap;
51import java.util.List;
52import java.util.Map;
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054/**
55 * A class providing access to battery usage statistics, including information on
56 * wakelocks, processes, packages, and services. All times are represented in microseconds
57 * except where indicated otherwise.
58 * @hide
59 */
60public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070061 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070064 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
65 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070066
67 /** @hide */
68 public static final String SERVICE_NAME = "batterystats";
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 /**
71 * A constant indicating a partial wake lock timer.
72 */
73 public static final int WAKE_TYPE_PARTIAL = 0;
74
75 /**
76 * A constant indicating a full wake lock timer.
77 */
78 public static final int WAKE_TYPE_FULL = 1;
79
80 /**
81 * A constant indicating a window wake lock timer.
82 */
83 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
88 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070089
The Android Open Source Project10592532009-03-18 17:39:46 -070090 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070091 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070092 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070093 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070094
Dianne Hackborn617f8772009-03-31 15:04:46 -070095 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070096 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070097 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070098 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070099
The Android Open Source Project10592532009-03-18 17:39:46 -0700100 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700101 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700102 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700103 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Dianne Hackborn62793e42015-03-09 11:15:41 -0700105 /**
106 * A constant indicating a wifi multicast timer
107 */
108 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700111 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700112 */
113 public static final int VIDEO_TURNED_ON = 8;
114
115 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800116 * A constant indicating a vibrator on timer
117 */
118 public static final int VIBRATOR_ON = 9;
119
120 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700121 * A constant indicating a foreground activity timer
122 */
123 public static final int FOREGROUND_ACTIVITY = 10;
124
125 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700126 * A constant indicating a wifi batched scan is active
127 */
128 public static final int WIFI_BATCHED_SCAN = 11;
129
130 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700131 * A constant indicating a process state timer
132 */
133 public static final int PROCESS_STATE = 12;
134
135 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700136 * A constant indicating a sync timer
137 */
138 public static final int SYNC = 13;
139
140 /**
141 * A constant indicating a job timer
142 */
143 public static final int JOB = 14;
144
145 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800146 * A constant indicating an audio turn on timer
147 */
148 public static final int AUDIO_TURNED_ON = 15;
149
150 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700151 * A constant indicating a flashlight turn on timer
152 */
153 public static final int FLASHLIGHT_TURNED_ON = 16;
154
155 /**
156 * A constant indicating a camera turn on timer
157 */
158 public static final int CAMERA_TURNED_ON = 17;
159
160 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700161 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700162 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700163 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700164
165 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800166 * A constant indicating a bluetooth scan timer.
167 */
168 public static final int BLUETOOTH_SCAN_ON = 19;
169
170 /**
Bookatzc8c44962017-05-11 12:12:54 -0700171 * A constant indicating an aggregated partial wake lock timer.
172 */
173 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
174
175 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700176 * A constant indicating a bluetooth scan timer for unoptimized scans.
177 */
178 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
179
180 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700181 * A constant indicating a foreground service timer
182 */
183 public static final int FOREGROUND_SERVICE = 22;
184
185 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800186 * A constant indicating an aggregate wifi multicast timer
187 */
188 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
189
190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * Include all of the data in the stats, including previously saved data.
192 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700193 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * Include only the current run in the stats.
197 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700198 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 /**
201 * Include only the run since the last time the device was unplugged in the stats.
202 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700203 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700204
205 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700206 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700207 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700208 private static final String[] STAT_NAMES = { "l", "c", "u" };
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700211 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700212 *
213 * New in version 19:
214 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800215 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700216 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700217 * New in version 21:
218 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700219 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700220 * - BLE scan result count
221 * - CPU frequency time per uid
222 * New in version 22:
223 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700224 * - Background partial wakelock time & count
225 * New in version 23:
226 * - Logging smeared power model values
227 * New in version 24:
228 * - Fixed bugs in background timers and BLE scan time
229 * New in version 25:
230 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700231 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700232 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700233 * New in version 27:
234 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800235 * New in version 28:
236 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700237 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800238 * New in version 29:
239 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
240 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800241 * New in version 30:
242 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
243 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800244 * New in version 31:
245 * - New cellular network types.
246 * - Deferred job metrics.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700247 */
Kweku Adamsb78430e2018-02-20 18:06:09 -0800248 static final int CHECKIN_VERSION = 31;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700249
250 /**
251 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700253 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700254
Evan Millar22ac0432009-03-31 11:33:18 -0700255 private static final long BYTES_PER_KB = 1024;
256 private static final long BYTES_PER_MB = 1048576; // 1024^2
257 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700258
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700259 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800260 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700261 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700263 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700264 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700265 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
266 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700267 // rpm line is:
268 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
269 // screen-off time, screen-off count
270 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700271 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800272 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700273 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
274 // fgs line is:
275 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
276 // foreground service time, count
277 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700278 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700279 // wl line is:
280 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700281 // full totalTime, 'f', count, current duration, max duration, total duration,
282 // partial totalTime, 'p', count, current duration, max duration, total duration,
283 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
284 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700285 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700286 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700287 // awl line is:
288 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
289 // cumulative partial wakelock duration, cumulative background partial wakelock duration
290 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700291 private static final String SYNC_DATA = "sy";
292 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700293 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800294
295 /**
296 * jbd line is:
297 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800298 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
299 * count at latency < 1 hr, count at latency 1 to 2 hrs, 2 to 4 hrs, 4 to 8 hrs, and past 8 hrs
Amith Yamasani977e11f2018-02-16 11:29:54 -0800300 * <p>
301 * @see #JOB_FRESHNESS_BUCKETS
302 */
303 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700304 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700305 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700306 private static final String NETWORK_DATA = "nt";
307 private static final String USER_ACTIVITY_DATA = "ua";
308 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800309 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700310 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700311 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700312 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800313 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
314 private static final String WIFI_CONTROLLER_DATA = "wfcd";
315 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
316 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700317 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700318 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800319 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800320 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
321 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800322 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700323 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700324 private static final String SCREEN_BRIGHTNESS_DATA = "br";
325 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700326 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700327 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
328 private static final String DATA_CONNECTION_TIME_DATA = "dct";
329 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800330 private static final String WIFI_STATE_TIME_DATA = "wst";
331 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700332 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
333 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
334 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
335 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800336 private static final String POWER_USE_SUMMARY_DATA = "pws";
337 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700338 private static final String DISCHARGE_STEP_DATA = "dsd";
339 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700340 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
341 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700342 private static final String FLASHLIGHT_DATA = "fla";
343 private static final String CAMERA_DATA = "cam";
344 private static final String VIDEO_DATA = "vid";
345 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700346 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
347 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348
Adam Lesinski010bf372016-04-11 12:18:18 -0700349 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
350
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700351 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 private final Formatter mFormatter = new Formatter(mFormatBuilder);
353
Siddharth Rayb50a6842017-12-14 15:15:28 -0800354 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
355 private static final String WIFI_CONTROLLER_NAME = "WiFi";
356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700358 * Indicates times spent by the uid at each cpu frequency in all process states.
359 *
360 * Other types might include times spent in foreground, background etc.
361 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800362 @VisibleForTesting
363 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700364
365 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800366 * These are the thresholds for bucketing last time since a job was run for an app
367 * that just moved to ACTIVE due to a launch. So if the last time a job ran was less
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800368 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800369 * on.
370 */
371 public static final long[] JOB_FRESHNESS_BUCKETS = {
372 1 * 60 * 60 * 1000L,
373 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800374 4 * 60 * 60 * 1000L,
375 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800376 Long.MAX_VALUE
377 };
378
379 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700380 * State for keeping track of counting information.
381 */
382 public static abstract class Counter {
383
384 /**
385 * Returns the count associated with this Counter for the
386 * selected type of statistics.
387 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700388 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700389 */
Evan Millarc64edde2009-04-18 12:26:32 -0700390 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700391
392 /**
393 * Temporary for debugging.
394 */
395 public abstract void logState(Printer pw, String prefix);
396 }
397
398 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700399 * State for keeping track of long counting information.
400 */
401 public static abstract class LongCounter {
402
403 /**
404 * Returns the count associated with this Counter for the
405 * selected type of statistics.
406 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700407 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700408 */
409 public abstract long getCountLocked(int which);
410
411 /**
412 * Temporary for debugging.
413 */
414 public abstract void logState(Printer pw, String prefix);
415 }
416
417 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700418 * State for keeping track of array of long counting information.
419 */
420 public static abstract class LongCounterArray {
421 /**
422 * Returns the counts associated with this Counter for the
423 * selected type of statistics.
424 *
425 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
426 */
427 public abstract long[] getCountsLocked(int which);
428
429 /**
430 * Temporary for debugging.
431 */
432 public abstract void logState(Printer pw, String prefix);
433 }
434
435 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800436 * Container class that aggregates counters for transmit, receive, and idle state of a
437 * radio controller.
438 */
439 public static abstract class ControllerActivityCounter {
440 /**
441 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
442 * idle state.
443 */
444 public abstract LongCounter getIdleTimeCounter();
445
446 /**
447 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800448 * scan state.
449 */
450 public abstract LongCounter getScanTimeCounter();
451
Siddharth Rayed754702018-02-15 12:44:37 -0800452 /**
453 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
454 * sleep state.
455 */
456 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800457
458 /**
459 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800460 * receive state.
461 */
462 public abstract LongCounter getRxTimeCounter();
463
464 /**
465 * An array of {@link LongCounter}, representing various transmit levels, where each level
466 * may draw a different amount of power. The levels themselves are controller-specific.
467 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
468 * various transmit level states.
469 */
470 public abstract LongCounter[] getTxTimeCounters();
471
472 /**
473 * @return a non-null {@link LongCounter} representing the power consumed by the controller
474 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
475 * yield a value of 0 if the device doesn't support power calculations.
476 */
477 public abstract LongCounter getPowerCounter();
478 }
479
480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * State for keeping track of timing information.
482 */
483 public static abstract class Timer {
484
485 /**
486 * Returns the count associated with this Timer for the
487 * selected type of statistics.
488 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700489 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
Evan Millarc64edde2009-04-18 12:26:32 -0700491 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
493 /**
494 * Returns the total time in microseconds associated with this Timer for the
495 * selected type of statistics.
496 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800497 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700498 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * @return a time in microseconds
500 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800501 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700504 * Returns the total time in microseconds associated with this Timer since the
505 * 'mark' was last set.
506 *
507 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
508 * @return a time in microseconds
509 */
510 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
511
512 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700513 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700514 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700515 */
516 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
517 return -1;
518 }
519
520 /**
521 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700522 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700523 */
524 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
525 return -1;
526 }
527
528 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700529 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800530 *
531 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
532 * been on since reset.
533 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
534 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
535 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
536 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700537 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800538 */
539 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
540 return -1;
541 }
542
543 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700544 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
545 * used, for example, for tracking background usage. Secondary timers are never pooled.
546 *
547 * Not all Timer subclasses have a secondary timer; those that don't return null.
548 */
549 public Timer getSubTimer() {
550 return null;
551 }
552
553 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700554 * Returns whether the timer is currently running. Some types of timers
555 * (e.g. BatchTimers) don't know whether the event is currently active,
556 * and report false.
557 */
558 public boolean isRunningLocked() {
559 return false;
560 }
561
562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * Temporary for debugging.
564 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700565 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
568 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800569 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
570 */
571 public static int mapToInternalProcessState(int procState) {
572 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
573 return ActivityManager.PROCESS_STATE_NONEXISTENT;
574 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
575 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800576 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
577 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800578 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
579 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
580 // Persistent and other foreground states go here.
581 return Uid.PROCESS_STATE_FOREGROUND;
582 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
583 return Uid.PROCESS_STATE_BACKGROUND;
584 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
585 return Uid.PROCESS_STATE_TOP_SLEEPING;
586 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
587 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
588 } else {
589 return Uid.PROCESS_STATE_CACHED;
590 }
591 }
592
593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * The statistics associated with a particular uid.
595 */
596 public static abstract class Uid {
597
598 /**
599 * Returns a mapping containing wakelock statistics.
600 *
601 * @return a Map from Strings to Uid.Wakelock objects.
602 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700603 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
605 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700606 * Returns the WiFi Multicast Wakelock statistics.
607 *
608 * @return a Timer Object for the per uid Multicast statistics.
609 */
610 public abstract Timer getMulticastWakelockStats();
611
612 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700613 * Returns a mapping containing sync statistics.
614 *
615 * @return a Map from Strings to Timer objects.
616 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700617 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700618
619 /**
620 * Returns a mapping containing scheduled job statistics.
621 *
622 * @return a Map from Strings to Timer objects.
623 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700624 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700625
626 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700627 * Returns statistics about how jobs have completed.
628 *
629 * @return A Map of String job names to completion type -> count mapping.
630 */
631 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
632
633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 * The statistics associated with a particular wake lock.
635 */
636 public static abstract class Wakelock {
637 public abstract Timer getWakeTime(int type);
638 }
639
640 /**
Bookatzc8c44962017-05-11 12:12:54 -0700641 * The cumulative time the uid spent holding any partial wakelocks. This will generally
642 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
643 * wakelocks that overlap in time (and therefore over-counts).
644 */
645 public abstract Timer getAggregatedPartialWakelockTimer();
646
647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * Returns a mapping containing sensor statistics.
649 *
650 * @return a Map from Integer sensor ids to Uid.Sensor objects.
651 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700652 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
654 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700655 * Returns a mapping containing active process data.
656 */
657 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700658
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 * Returns a mapping containing process statistics.
661 *
662 * @return a Map from Strings to Uid.Proc objects.
663 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700664 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
666 /**
667 * Returns a mapping containing package statistics.
668 *
669 * @return a Map from Strings to Uid.Pkg objects.
670 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700671 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700672
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800673 public abstract ControllerActivityCounter getWifiControllerActivity();
674 public abstract ControllerActivityCounter getBluetoothControllerActivity();
675 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800676
677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 * {@hide}
679 */
680 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700681
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800682 public abstract void noteWifiRunningLocked(long elapsedRealtime);
683 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
684 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
685 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
686 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
687 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
688 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
689 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
690 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
691 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800692 public abstract void noteActivityResumedLocked(long elapsedRealtime);
693 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800694 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
695 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
696 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700697 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700698 /**
699 * Returns the timer keeping track of wifi scans.
700 */
701 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800702 public abstract int getWifiScanBackgroundCount(int which);
703 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
704 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700705 /**
706 * Returns the timer keeping track of background wifi scans.
707 */
708 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800709 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700710 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800711 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700712 public abstract Timer getAudioTurnedOnTimer();
713 public abstract Timer getVideoTurnedOnTimer();
714 public abstract Timer getFlashlightTurnedOnTimer();
715 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700716 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700717
718 /**
719 * Returns the timer keeping track of Foreground Service time
720 */
721 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800722 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800723 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700724 public abstract Timer getBluetoothUnoptimizedScanTimer();
725 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700726 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700727 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700728
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700729 public abstract long[] getCpuFreqTimes(int which);
730 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700731 /**
732 * Returns cpu active time of an uid.
733 */
734 public abstract long getCpuActiveTime();
735 /**
736 * Returns cpu times of an uid on each cluster
737 */
738 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700739
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800740 /**
741 * Returns cpu times of an uid at a particular process state.
742 */
743 public abstract long[] getCpuFreqTimes(int which, int procState);
744 /**
745 * Returns cpu times of an uid while the screen if off at a particular process state.
746 */
747 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
748
Dianne Hackborna0200e32016-03-30 18:01:41 -0700749 // Note: the following times are disjoint. They can be added together to find the
750 // total time a uid has had any processes running at all.
751
752 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800753 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700754 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800755 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700756 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800757 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700758 * none in the "top" state.
759 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800760 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700761 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700762 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800763 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700764 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800765 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700766 /**
767 * Time this uid has any process in an active background state, but none in the
768 * "foreground" or better state.
769 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800770 public static final int PROCESS_STATE_BACKGROUND = 3;
771 /**
772 * Time this uid has any process that is top while the device is sleeping, but not
773 * active for any other reason. We kind-of consider it a kind of cached process
774 * for execution restrictions.
775 */
776 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
777 /**
778 * Time this uid has any process that is in the background but it has an activity
779 * marked as "can't save state". This is essentially a cached process, though the
780 * system will try much harder than normal to avoid killing it.
781 */
782 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700783 /**
784 * Time this uid has any processes that are sitting around cached, not in one of the
785 * other active states.
786 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800787 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700788 /**
789 * Total number of process states we track.
790 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800791 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700792
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800793 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700794 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800795 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
796 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700797 };
798
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800799 // Used in checkin dump
800 @VisibleForTesting
801 public static final String[] UID_PROCESS_TYPES = {
802 "T", // TOP
803 "FS", // FOREGROUND_SERVICE
804 "F", // FOREGROUND
805 "B", // BACKGROUND
806 "TS", // TOP_SLEEPING
807 "HW", // HEAVY_WEIGHT
808 "C" // CACHED
809 };
810
811 /**
812 * When the process exits one of these states, we need to make sure cpu time in this state
813 * is not attributed to any non-critical process states.
814 */
815 public static final int[] CRITICAL_PROC_STATES = {
816 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
817 };
818
Dianne Hackborn61659e52014-07-09 16:13:01 -0700819 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800820 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700821
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800822 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
Robert Greenwalta029ea12013-09-25 16:38:12 -0700824 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
825
Dianne Hackborn617f8772009-03-31 15:04:46 -0700826 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700827 * Note that these must match the constants in android.os.PowerManager.
828 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
829 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700830 */
831 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700832 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700833 };
Bookatzc8c44962017-05-11 12:12:54 -0700834
Phil Weaverda80d672016-03-15 16:25:46 -0700835 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700836
Dianne Hackborn617f8772009-03-31 15:04:46 -0700837 public abstract void noteUserActivityLocked(int type);
838 public abstract boolean hasUserActivity();
839 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700840
841 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800842 public abstract long getNetworkActivityBytes(int type, int which);
843 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800844 public abstract long getMobileRadioActiveTime(int which);
845 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700846
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700847 /**
848 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
849 */
850 public abstract long getUserCpuTimeUs(int which);
851
852 /**
853 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
854 */
855 public abstract long getSystemCpuTimeUs(int which);
856
857 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700858 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700859 * given CPU cluster.
860 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700861 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700862 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700863 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
864 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700865 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700866 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700867
Adam Lesinski5f056f62016-07-14 16:56:08 -0700868 /**
869 * Returns the number of times this UID woke up the Application Processor to
870 * process a mobile radio packet.
871 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
872 */
873 public abstract long getMobileRadioApWakeupCount(int which);
874
875 /**
876 * Returns the number of times this UID woke up the Application Processor to
877 * process a WiFi packet.
878 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
879 */
880 public abstract long getWifiRadioApWakeupCount(int which);
881
Amith Yamasani977e11f2018-02-16 11:29:54 -0800882 /**
883 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
884 * @param sb StringBuilder that can be overwritten with the deferred jobs data
885 * @param which one of STATS_*
886 */
887 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
888
889 /**
890 * Appends the deferred jobs data to the StringBuilder passed in
891 * @param sb StringBuilder that can be overwritten with the deferred jobs data
892 * @param which one of STATS_*
893 */
894 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800897 /*
898 * FIXME: it's not correct to use this magic value because it
899 * could clash with a sensor handle (which are defined by
900 * the sensor HAL, and therefore out of our control
901 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Magic sensor number for the GPS.
903 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800908
Bookatz867c0d72017-03-07 18:23:42 -0800909 /** Returns a Timer for sensor usage when app is in the background. */
910 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700913 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800914 public int mWakeNesting;
915 public long mWakeSumMs;
916 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700917 }
918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 /**
920 * The statistics associated with a particular process.
921 */
922 public static abstract class Proc {
923
Dianne Hackborn287952c2010-09-22 22:34:31 -0700924 public static class ExcessivePower {
925 public static final int TYPE_WAKE = 1;
926 public static final int TYPE_CPU = 2;
927
928 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700929 public long overTime;
930 public long usedTime;
931 }
932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800934 * Returns true if this process is still active in the battery stats.
935 */
936 public abstract boolean isActive();
937
938 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700939 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700941 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 */
943 public abstract long getUserTime(int which);
944
945 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700946 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700948 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 */
950 public abstract long getSystemTime(int which);
951
952 /**
953 * Returns the number of times the process has been started.
954 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700955 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 */
957 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700958
959 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800960 * Returns the number of times the process has crashed.
961 *
962 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
963 */
964 public abstract int getNumCrashes(int which);
965
966 /**
967 * Returns the number of times the process has ANRed.
968 *
969 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
970 */
971 public abstract int getNumAnrs(int which);
972
973 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700974 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700975 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700976 * @return foreground cpu time in microseconds
977 */
978 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700979
Dianne Hackborn287952c2010-09-22 22:34:31 -0700980 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700981
Dianne Hackborn287952c2010-09-22 22:34:31 -0700982 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984
985 /**
986 * The statistics associated with a particular package.
987 */
988 public static abstract class Pkg {
989
990 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700991 * Returns information about all wakeup alarms that have been triggered for this
992 * package. The mapping keys are tag names for the alarms, the counter contains
993 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700995 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
997 /**
998 * Returns a mapping containing service statistics.
999 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001000 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 /**
1003 * The statistics associated with a particular service.
1004 */
Joe Onoratoabded112016-02-08 16:49:39 -08001005 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006
1007 /**
1008 * Returns the amount of time spent started.
1009 *
1010 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001011 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * @return
1013 */
1014 public abstract long getStartTime(long batteryUptime, int which);
1015
1016 /**
1017 * Returns the total number of times startService() has been called.
1018 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001019 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 */
1021 public abstract int getStarts(int which);
1022
1023 /**
1024 * Returns the total number times the service has been launched.
1025 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001026 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public abstract int getLaunches(int which);
1029 }
1030 }
1031 }
1032
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001033 public static final class LevelStepTracker {
1034 public long mLastStepTime = -1;
1035 public int mNumStepDurations;
1036 public final long[] mStepDurations;
1037
1038 public LevelStepTracker(int maxLevelSteps) {
1039 mStepDurations = new long[maxLevelSteps];
1040 }
1041
1042 public LevelStepTracker(int numSteps, long[] steps) {
1043 mNumStepDurations = numSteps;
1044 mStepDurations = new long[numSteps];
1045 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1046 }
1047
1048 public long getDurationAt(int index) {
1049 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1050 }
1051
1052 public int getLevelAt(int index) {
1053 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1054 >> STEP_LEVEL_LEVEL_SHIFT);
1055 }
1056
1057 public int getInitModeAt(int index) {
1058 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1059 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1060 }
1061
1062 public int getModModeAt(int index) {
1063 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1064 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1065 }
1066
1067 private void appendHex(long val, int topOffset, StringBuilder out) {
1068 boolean hasData = false;
1069 while (topOffset >= 0) {
1070 int digit = (int)( (val>>topOffset) & 0xf );
1071 topOffset -= 4;
1072 if (!hasData && digit == 0) {
1073 continue;
1074 }
1075 hasData = true;
1076 if (digit >= 0 && digit <= 9) {
1077 out.append((char)('0' + digit));
1078 } else {
1079 out.append((char)('a' + digit - 10));
1080 }
1081 }
1082 }
1083
1084 public void encodeEntryAt(int index, StringBuilder out) {
1085 long item = mStepDurations[index];
1086 long duration = item & STEP_LEVEL_TIME_MASK;
1087 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1088 >> STEP_LEVEL_LEVEL_SHIFT);
1089 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1090 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1091 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1092 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1093 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1094 case Display.STATE_OFF: out.append('f'); break;
1095 case Display.STATE_ON: out.append('o'); break;
1096 case Display.STATE_DOZE: out.append('d'); break;
1097 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1098 }
1099 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1100 out.append('p');
1101 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001102 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1103 out.append('i');
1104 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001105 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1106 case Display.STATE_OFF: out.append('F'); break;
1107 case Display.STATE_ON: out.append('O'); break;
1108 case Display.STATE_DOZE: out.append('D'); break;
1109 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1110 }
1111 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1112 out.append('P');
1113 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001114 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1115 out.append('I');
1116 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001117 out.append('-');
1118 appendHex(level, 4, out);
1119 out.append('-');
1120 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1121 }
1122
1123 public void decodeEntryAt(int index, String value) {
1124 final int N = value.length();
1125 int i = 0;
1126 char c;
1127 long out = 0;
1128 while (i < N && (c=value.charAt(i)) != '-') {
1129 i++;
1130 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001131 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001132 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001133 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001134 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001135 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001136 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001137 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1138 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1139 break;
1140 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1141 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1142 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001143 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1144 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1145 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001146 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1147 break;
1148 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1149 break;
1150 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1151 break;
1152 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1153 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1154 break;
1155 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1156 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001157 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001158 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1159 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1160 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001161 }
1162 }
1163 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001164 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001165 while (i < N && (c=value.charAt(i)) != '-') {
1166 i++;
1167 level <<= 4;
1168 if (c >= '0' && c <= '9') {
1169 level += c - '0';
1170 } else if (c >= 'a' && c <= 'f') {
1171 level += c - 'a' + 10;
1172 } else if (c >= 'A' && c <= 'F') {
1173 level += c - 'A' + 10;
1174 }
1175 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001176 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001177 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1178 long duration = 0;
1179 while (i < N && (c=value.charAt(i)) != '-') {
1180 i++;
1181 duration <<= 4;
1182 if (c >= '0' && c <= '9') {
1183 duration += c - '0';
1184 } else if (c >= 'a' && c <= 'f') {
1185 duration += c - 'a' + 10;
1186 } else if (c >= 'A' && c <= 'F') {
1187 duration += c - 'A' + 10;
1188 }
1189 }
1190 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1191 }
1192
1193 public void init() {
1194 mLastStepTime = -1;
1195 mNumStepDurations = 0;
1196 }
1197
1198 public void clearTime() {
1199 mLastStepTime = -1;
1200 }
1201
1202 public long computeTimePerLevel() {
1203 final long[] steps = mStepDurations;
1204 final int numSteps = mNumStepDurations;
1205
1206 // For now we'll do a simple average across all steps.
1207 if (numSteps <= 0) {
1208 return -1;
1209 }
1210 long total = 0;
1211 for (int i=0; i<numSteps; i++) {
1212 total += steps[i] & STEP_LEVEL_TIME_MASK;
1213 }
1214 return total / numSteps;
1215 /*
1216 long[] buckets = new long[numSteps];
1217 int numBuckets = 0;
1218 int numToAverage = 4;
1219 int i = 0;
1220 while (i < numSteps) {
1221 long totalTime = 0;
1222 int num = 0;
1223 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1224 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1225 num++;
1226 }
1227 buckets[numBuckets] = totalTime / num;
1228 numBuckets++;
1229 numToAverage *= 2;
1230 i += num;
1231 }
1232 if (numBuckets < 1) {
1233 return -1;
1234 }
1235 long averageTime = buckets[numBuckets-1];
1236 for (i=numBuckets-2; i>=0; i--) {
1237 averageTime = (averageTime + buckets[i]) / 2;
1238 }
1239 return averageTime;
1240 */
1241 }
1242
1243 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1244 int[] outNumOfInterest) {
1245 final long[] steps = mStepDurations;
1246 final int count = mNumStepDurations;
1247 if (count <= 0) {
1248 return -1;
1249 }
1250 long total = 0;
1251 int numOfInterest = 0;
1252 for (int i=0; i<count; i++) {
1253 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1254 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1255 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1256 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1257 // If the modes of interest didn't change during this step period...
1258 if ((modMode&modesOfInterest) == 0) {
1259 // And the mode values during this period match those we are measuring...
1260 if ((initMode&modesOfInterest) == modeValues) {
1261 // Then this can be used to estimate the total time!
1262 numOfInterest++;
1263 total += steps[i] & STEP_LEVEL_TIME_MASK;
1264 }
1265 }
1266 }
1267 if (numOfInterest <= 0) {
1268 return -1;
1269 }
1270
1271 if (outNumOfInterest != null) {
1272 outNumOfInterest[0] = numOfInterest;
1273 }
1274
1275 // The estimated time is the average time we spend in each level, multipled
1276 // by 100 -- the total number of battery levels
1277 return (total / numOfInterest) * 100;
1278 }
1279
1280 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1281 int stepCount = mNumStepDurations;
1282 final long lastStepTime = mLastStepTime;
1283 if (lastStepTime >= 0 && numStepLevels > 0) {
1284 final long[] steps = mStepDurations;
1285 long duration = elapsedRealtime - lastStepTime;
1286 for (int i=0; i<numStepLevels; i++) {
1287 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1288 long thisDuration = duration / (numStepLevels-i);
1289 duration -= thisDuration;
1290 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1291 thisDuration = STEP_LEVEL_TIME_MASK;
1292 }
1293 steps[0] = thisDuration | modeBits;
1294 }
1295 stepCount += numStepLevels;
1296 if (stepCount > steps.length) {
1297 stepCount = steps.length;
1298 }
1299 }
1300 mNumStepDurations = stepCount;
1301 mLastStepTime = elapsedRealtime;
1302 }
1303
1304 public void readFromParcel(Parcel in) {
1305 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001306 if (N > mStepDurations.length) {
1307 throw new ParcelFormatException("more step durations than available: " + N);
1308 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001309 mNumStepDurations = N;
1310 for (int i=0; i<N; i++) {
1311 mStepDurations[i] = in.readLong();
1312 }
1313 }
1314
1315 public void writeToParcel(Parcel out) {
1316 final int N = mNumStepDurations;
1317 out.writeInt(N);
1318 for (int i=0; i<N; i++) {
1319 out.writeLong(mStepDurations[i]);
1320 }
1321 }
1322 }
1323
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001324 public static final class PackageChange {
1325 public String mPackageName;
1326 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001327 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001328 }
1329
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001330 public static final class DailyItem {
1331 public long mStartTime;
1332 public long mEndTime;
1333 public LevelStepTracker mDischargeSteps;
1334 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001335 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001336 }
1337
1338 public abstract DailyItem getDailyItemLocked(int daysAgo);
1339
1340 public abstract long getCurrentDailyStartTime();
1341
1342 public abstract long getNextMinDailyDeadline();
1343
1344 public abstract long getNextMaxDailyDeadline();
1345
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001346 public abstract long[] getCpuFreqs();
1347
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001348 public final static class HistoryTag {
1349 public String string;
1350 public int uid;
1351
1352 public int poolIdx;
1353
1354 public void setTo(HistoryTag o) {
1355 string = o.string;
1356 uid = o.uid;
1357 poolIdx = o.poolIdx;
1358 }
1359
1360 public void setTo(String _string, int _uid) {
1361 string = _string;
1362 uid = _uid;
1363 poolIdx = -1;
1364 }
1365
1366 public void writeToParcel(Parcel dest, int flags) {
1367 dest.writeString(string);
1368 dest.writeInt(uid);
1369 }
1370
1371 public void readFromParcel(Parcel src) {
1372 string = src.readString();
1373 uid = src.readInt();
1374 poolIdx = -1;
1375 }
1376
1377 @Override
1378 public boolean equals(Object o) {
1379 if (this == o) return true;
1380 if (o == null || getClass() != o.getClass()) return false;
1381
1382 HistoryTag that = (HistoryTag) o;
1383
1384 if (uid != that.uid) return false;
1385 if (!string.equals(that.string)) return false;
1386
1387 return true;
1388 }
1389
1390 @Override
1391 public int hashCode() {
1392 int result = string.hashCode();
1393 result = 31 * result + uid;
1394 return result;
1395 }
1396 }
1397
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001398 /**
1399 * Optional detailed information that can go into a history step. This is typically
1400 * generated each time the battery level changes.
1401 */
1402 public final static class HistoryStepDetails {
1403 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1404 public int userTime;
1405 public int systemTime;
1406
1407 // Top three apps using CPU in the last step, with times in 1/100 second.
1408 public int appCpuUid1;
1409 public int appCpuUTime1;
1410 public int appCpuSTime1;
1411 public int appCpuUid2;
1412 public int appCpuUTime2;
1413 public int appCpuSTime2;
1414 public int appCpuUid3;
1415 public int appCpuUTime3;
1416 public int appCpuSTime3;
1417
1418 // Information from /proc/stat
1419 public int statUserTime;
1420 public int statSystemTime;
1421 public int statIOWaitTime;
1422 public int statIrqTime;
1423 public int statSoftIrqTime;
1424 public int statIdlTime;
1425
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001426 // Platform-level low power state stats
1427 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001428 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001429
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001430 public HistoryStepDetails() {
1431 clear();
1432 }
1433
1434 public void clear() {
1435 userTime = systemTime = 0;
1436 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1437 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1438 = appCpuUTime3 = appCpuSTime3 = 0;
1439 }
1440
1441 public void writeToParcel(Parcel out) {
1442 out.writeInt(userTime);
1443 out.writeInt(systemTime);
1444 out.writeInt(appCpuUid1);
1445 out.writeInt(appCpuUTime1);
1446 out.writeInt(appCpuSTime1);
1447 out.writeInt(appCpuUid2);
1448 out.writeInt(appCpuUTime2);
1449 out.writeInt(appCpuSTime2);
1450 out.writeInt(appCpuUid3);
1451 out.writeInt(appCpuUTime3);
1452 out.writeInt(appCpuSTime3);
1453 out.writeInt(statUserTime);
1454 out.writeInt(statSystemTime);
1455 out.writeInt(statIOWaitTime);
1456 out.writeInt(statIrqTime);
1457 out.writeInt(statSoftIrqTime);
1458 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001459 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001460 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001461 }
1462
1463 public void readFromParcel(Parcel in) {
1464 userTime = in.readInt();
1465 systemTime = in.readInt();
1466 appCpuUid1 = in.readInt();
1467 appCpuUTime1 = in.readInt();
1468 appCpuSTime1 = in.readInt();
1469 appCpuUid2 = in.readInt();
1470 appCpuUTime2 = in.readInt();
1471 appCpuSTime2 = in.readInt();
1472 appCpuUid3 = in.readInt();
1473 appCpuUTime3 = in.readInt();
1474 appCpuSTime3 = in.readInt();
1475 statUserTime = in.readInt();
1476 statSystemTime = in.readInt();
1477 statIOWaitTime = in.readInt();
1478 statIrqTime = in.readInt();
1479 statSoftIrqTime = in.readInt();
1480 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001481 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001482 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001483 }
1484 }
1485
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001486 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001487 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001488
1489 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001490 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001491
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001492 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001493 public static final byte CMD_NULL = -1;
1494 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001495 public static final byte CMD_CURRENT_TIME = 5;
1496 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001497 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001498 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001499
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001500 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001501
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001502 /**
1503 * Return whether the command code is a delta data update.
1504 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001505 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001506 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001507 }
1508
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001509 public byte batteryLevel;
1510 public byte batteryStatus;
1511 public byte batteryHealth;
1512 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001513
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001514 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001515 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001516
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001517 // The charge of the battery in micro-Ampere-hours.
1518 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001519
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001520 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001521 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001522 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001523 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001524 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1525 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001526 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001527 public static final int STATE_PHONE_STATE_SHIFT = 6;
1528 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001529 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001530 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001531 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001532
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001533 // These states always appear directly in the first int token
1534 // of a delta change; they should be ones that change relatively
1535 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001536 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1537 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001538 public static final int STATE_GPS_ON_FLAG = 1<<29;
1539 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001540 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001541 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001542 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001543 // Do not use, this is used for coulomb delta count.
1544 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001545 // These are on the lower bits used for the command; if they change
1546 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001547 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001548 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001549 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001550 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1551 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001552 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001553 // empty slot
1554 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001555
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001556 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001557 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001558
1559 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001560
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001561 public int states;
1562
Dianne Hackborn3251b902014-06-20 14:40:53 -07001563 // Constants from WIFI_SUPPL_STATE_*
1564 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1565 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1566 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1567 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1568 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1569 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001570 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1571 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1572 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1573 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001574
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001575 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001576 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1577 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1578 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001579 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001580 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1581 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1582 public static final int STATE2_CHARGING_FLAG = 1<<24;
1583 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1584 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1585 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001586 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001587 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001588
1589 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001590 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1591 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001592
1593 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001594
Dianne Hackborn40c87252014-03-19 16:55:40 -07001595 public int states2;
1596
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001597 // The wake lock that was acquired at this point.
1598 public HistoryTag wakelockTag;
1599
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001600 // Kernel wakeup reason at this point.
1601 public HistoryTag wakeReasonTag;
1602
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001603 // Non-null when there is more detailed information at this step.
1604 public HistoryStepDetails stepDetails;
1605
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001606 public static final int EVENT_FLAG_START = 0x8000;
1607 public static final int EVENT_FLAG_FINISH = 0x4000;
1608
1609 // No event in this item.
1610 public static final int EVENT_NONE = 0x0000;
1611 // Event is about a process that is running.
1612 public static final int EVENT_PROC = 0x0001;
1613 // Event is about an application package that is in the foreground.
1614 public static final int EVENT_FOREGROUND = 0x0002;
1615 // Event is about an application package that is at the top of the screen.
1616 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001617 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001618 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001619 // Events for all additional wake locks aquired/release within a wake block.
1620 // These are not generated by default.
1621 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001622 // Event is about an application executing a scheduled job.
1623 public static final int EVENT_JOB = 0x0006;
1624 // Events for users running.
1625 public static final int EVENT_USER_RUNNING = 0x0007;
1626 // Events for foreground user.
1627 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001628 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001629 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001630 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001631 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001632 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001633 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001634 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001635 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001636 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001637 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001638 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001639 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001640 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001641 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001642 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001643 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001644 // Event for a package being on the temporary whitelist.
1645 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001646 // Event for the screen waking up.
1647 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001648 // Event for the UID that woke up the application processor.
1649 // Used for wakeups coming from WiFi, modem, etc.
1650 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001651 // Event for reporting that a specific partial wake lock has been held for a long duration.
1652 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001653
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001654 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001655 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001656 // Mask to extract out only the type part of the event.
1657 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001658
1659 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1660 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1661 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1662 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1663 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1664 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001665 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1666 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001667 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1668 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001669 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1670 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1671 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1672 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1673 public static final int EVENT_USER_FOREGROUND_START =
1674 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1675 public static final int EVENT_USER_FOREGROUND_FINISH =
1676 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001677 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1678 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001679 public static final int EVENT_TEMP_WHITELIST_START =
1680 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1681 public static final int EVENT_TEMP_WHITELIST_FINISH =
1682 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001683 public static final int EVENT_LONG_WAKE_LOCK_START =
1684 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1685 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1686 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001687
1688 // For CMD_EVENT.
1689 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001690 public HistoryTag eventTag;
1691
Dianne Hackborn9a755432014-05-15 17:05:22 -07001692 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001693 public long currentTime;
1694
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001695 // Meta-data when reading.
1696 public int numReadInts;
1697
1698 // Pre-allocated objects.
1699 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001700 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001701 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001702
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001703 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001704 }
Bookatzc8c44962017-05-11 12:12:54 -07001705
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001706 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001707 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001708 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001709 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001710 }
Bookatzc8c44962017-05-11 12:12:54 -07001711
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001712 public int describeContents() {
1713 return 0;
1714 }
1715
1716 public void writeToParcel(Parcel dest, int flags) {
1717 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001718 int bat = (((int)cmd)&0xff)
1719 | ((((int)batteryLevel)<<8)&0xff00)
1720 | ((((int)batteryStatus)<<16)&0xf0000)
1721 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001722 | ((((int)batteryPlugType)<<24)&0xf000000)
1723 | (wakelockTag != null ? 0x10000000 : 0)
1724 | (wakeReasonTag != null ? 0x20000000 : 0)
1725 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001726 dest.writeInt(bat);
1727 bat = (((int)batteryTemperature)&0xffff)
1728 | ((((int)batteryVoltage)<<16)&0xffff0000);
1729 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001730 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001731 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001732 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001733 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001734 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001735 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001736 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001737 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001738 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001739 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001740 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001741 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001742 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001743 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001744 dest.writeLong(currentTime);
1745 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001746 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001747
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001748 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001749 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001750 int bat = src.readInt();
1751 cmd = (byte)(bat&0xff);
1752 batteryLevel = (byte)((bat>>8)&0xff);
1753 batteryStatus = (byte)((bat>>16)&0xf);
1754 batteryHealth = (byte)((bat>>20)&0xf);
1755 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001756 int bat2 = src.readInt();
1757 batteryTemperature = (short)(bat2&0xffff);
1758 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001759 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001760 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001761 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001762 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001763 wakelockTag = localWakelockTag;
1764 wakelockTag.readFromParcel(src);
1765 } else {
1766 wakelockTag = null;
1767 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001768 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001769 wakeReasonTag = localWakeReasonTag;
1770 wakeReasonTag.readFromParcel(src);
1771 } else {
1772 wakeReasonTag = null;
1773 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001774 if ((bat&0x40000000) != 0) {
1775 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001776 eventTag = localEventTag;
1777 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001778 } else {
1779 eventCode = EVENT_NONE;
1780 eventTag = null;
1781 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001782 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001783 currentTime = src.readLong();
1784 } else {
1785 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001786 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001787 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001788 }
1789
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001790 public void clear() {
1791 time = 0;
1792 cmd = CMD_NULL;
1793 batteryLevel = 0;
1794 batteryStatus = 0;
1795 batteryHealth = 0;
1796 batteryPlugType = 0;
1797 batteryTemperature = 0;
1798 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001799 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001800 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001801 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001802 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001803 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001804 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001805 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001806 }
Bookatzc8c44962017-05-11 12:12:54 -07001807
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001808 public void setTo(HistoryItem o) {
1809 time = o.time;
1810 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001811 setToCommon(o);
1812 }
1813
1814 public void setTo(long time, byte cmd, HistoryItem o) {
1815 this.time = time;
1816 this.cmd = cmd;
1817 setToCommon(o);
1818 }
1819
1820 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001821 batteryLevel = o.batteryLevel;
1822 batteryStatus = o.batteryStatus;
1823 batteryHealth = o.batteryHealth;
1824 batteryPlugType = o.batteryPlugType;
1825 batteryTemperature = o.batteryTemperature;
1826 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001827 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001828 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001829 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001830 if (o.wakelockTag != null) {
1831 wakelockTag = localWakelockTag;
1832 wakelockTag.setTo(o.wakelockTag);
1833 } else {
1834 wakelockTag = null;
1835 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001836 if (o.wakeReasonTag != null) {
1837 wakeReasonTag = localWakeReasonTag;
1838 wakeReasonTag.setTo(o.wakeReasonTag);
1839 } else {
1840 wakeReasonTag = null;
1841 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001842 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001843 if (o.eventTag != null) {
1844 eventTag = localEventTag;
1845 eventTag.setTo(o.eventTag);
1846 } else {
1847 eventTag = null;
1848 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001849 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001850 }
1851
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001852 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001853 return batteryLevel == o.batteryLevel
1854 && batteryStatus == o.batteryStatus
1855 && batteryHealth == o.batteryHealth
1856 && batteryPlugType == o.batteryPlugType
1857 && batteryTemperature == o.batteryTemperature
1858 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001859 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001860 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001861 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001862 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001863 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001864
1865 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001866 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001867 return false;
1868 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001869 if (wakelockTag != o.wakelockTag) {
1870 if (wakelockTag == null || o.wakelockTag == null) {
1871 return false;
1872 }
1873 if (!wakelockTag.equals(o.wakelockTag)) {
1874 return false;
1875 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001876 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001877 if (wakeReasonTag != o.wakeReasonTag) {
1878 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1879 return false;
1880 }
1881 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1882 return false;
1883 }
1884 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001885 if (eventTag != o.eventTag) {
1886 if (eventTag == null || o.eventTag == null) {
1887 return false;
1888 }
1889 if (!eventTag.equals(o.eventTag)) {
1890 return false;
1891 }
1892 }
1893 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001894 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001895 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001896
1897 public final static class HistoryEventTracker {
1898 private final HashMap<String, SparseIntArray>[] mActiveEvents
1899 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1900
1901 public boolean updateState(int code, String name, int uid, int poolIdx) {
1902 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1903 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1904 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1905 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001906 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001907 mActiveEvents[idx] = active;
1908 }
1909 SparseIntArray uids = active.get(name);
1910 if (uids == null) {
1911 uids = new SparseIntArray();
1912 active.put(name, uids);
1913 }
1914 if (uids.indexOfKey(uid) >= 0) {
1915 // Already set, nothing to do!
1916 return false;
1917 }
1918 uids.put(uid, poolIdx);
1919 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1920 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1921 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1922 if (active == null) {
1923 // not currently active, nothing to do.
1924 return false;
1925 }
1926 SparseIntArray uids = active.get(name);
1927 if (uids == null) {
1928 // not currently active, nothing to do.
1929 return false;
1930 }
1931 idx = uids.indexOfKey(uid);
1932 if (idx < 0) {
1933 // not currently active, nothing to do.
1934 return false;
1935 }
1936 uids.removeAt(idx);
1937 if (uids.size() <= 0) {
1938 active.remove(name);
1939 }
1940 }
1941 return true;
1942 }
1943
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001944 public void removeEvents(int code) {
1945 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1946 mActiveEvents[idx] = null;
1947 }
1948
Dianne Hackborn37de0982014-05-09 09:32:18 -07001949 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1950 return mActiveEvents[code];
1951 }
1952 }
1953
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001954 public static final class BitDescription {
1955 public final int mask;
1956 public final int shift;
1957 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001958 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001959 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001960 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001961
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001962 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001963 this.mask = mask;
1964 this.shift = -1;
1965 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001966 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001967 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001968 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001969 }
Bookatzc8c44962017-05-11 12:12:54 -07001970
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001971 public BitDescription(int mask, int shift, String name, String shortName,
1972 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001973 this.mask = mask;
1974 this.shift = shift;
1975 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001976 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001977 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001978 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001979 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001980 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001981
Dianne Hackbornfc064132014-06-02 12:42:12 -07001982 /**
1983 * Don't allow any more batching in to the current history event. This
1984 * is called when printing partial histories, so to ensure that the next
1985 * history event will go in to a new batch after what was printed in the
1986 * last partial history.
1987 */
1988 public abstract void commitCurrentHistoryBatchLocked();
1989
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001990 public abstract int getHistoryTotalSize();
1991
1992 public abstract int getHistoryUsedSize();
1993
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001994 public abstract boolean startIteratingHistoryLocked();
1995
Dianne Hackborn099bc622014-01-22 13:39:16 -08001996 public abstract int getHistoryStringPoolSize();
1997
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001998 public abstract int getHistoryStringPoolBytes();
1999
2000 public abstract String getHistoryTagPoolString(int index);
2001
2002 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002003
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002004 public abstract boolean getNextHistoryLocked(HistoryItem out);
2005
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002006 public abstract void finishIteratingHistoryLocked();
2007
2008 public abstract boolean startIteratingOldHistoryLocked();
2009
2010 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2011
2012 public abstract void finishIteratingOldHistoryLocked();
2013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002015 * Return the base time offset for the battery history.
2016 */
2017 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002018
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 * Returns the number of times the device has been started.
2021 */
2022 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002025 * 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 -08002026 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 * {@hide}
2029 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002030 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002031
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002032 /**
2033 * Returns the number of times the screen was turned on.
2034 *
2035 * {@hide}
2036 */
2037 public abstract int getScreenOnCount(int which);
2038
Mike Mac2f518a2017-09-19 16:06:03 -07002039 /**
2040 * Returns the time in microseconds that the screen has been dozing while the device was
2041 * running on battery.
2042 *
2043 * {@hide}
2044 */
2045 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2046
2047 /**
2048 * Returns the number of times the screen was turned dozing.
2049 *
2050 * {@hide}
2051 */
2052 public abstract int getScreenDozeCount(int which);
2053
Jeff Browne95c3cd2014-05-02 16:59:26 -07002054 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2055
Dianne Hackborn617f8772009-03-31 15:04:46 -07002056 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2057 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2058 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2059 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2060 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002061
Dianne Hackborn617f8772009-03-31 15:04:46 -07002062 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2063 "dark", "dim", "medium", "light", "bright"
2064 };
Bookatzc8c44962017-05-11 12:12:54 -07002065
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002066 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2067 "0", "1", "2", "3", "4"
2068 };
2069
Dianne Hackborn617f8772009-03-31 15:04:46 -07002070 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002071
Dianne Hackborn617f8772009-03-31 15:04:46 -07002072 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002073 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002074 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002075 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002076 * {@hide}
2077 */
2078 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002079 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002082 * Returns the {@link Timer} object that tracks the given screen brightness.
2083 *
2084 * {@hide}
2085 */
2086 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2087
2088 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002089 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002090 * running on battery.
2091 *
2092 * {@hide}
2093 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002094 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002095
2096 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002097 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002098 *
2099 * {@hide}
2100 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002101 public abstract int getPowerSaveModeEnabledCount(int which);
2102
2103 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002104 * Constant for device idle mode: not active.
2105 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002106 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002107
2108 /**
2109 * Constant for device idle mode: active in lightweight mode.
2110 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002111 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002112
2113 /**
2114 * Constant for device idle mode: active in full mode.
2115 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002116 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002117
2118 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002119 * Returns the time in microseconds that device has been in idle mode while
2120 * running on battery.
2121 *
2122 * {@hide}
2123 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002124 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002125
2126 /**
2127 * Returns the number of times that the devie has gone in to idle mode.
2128 *
2129 * {@hide}
2130 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002131 public abstract int getDeviceIdleModeCount(int mode, int which);
2132
2133 /**
2134 * Return the longest duration we spent in a particular device idle mode (fully in the
2135 * mode, not in idle maintenance etc).
2136 */
2137 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002138
2139 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002140 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002141 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002142 * counts all of the time that we consider the device to be idle, whether or not
2143 * it is currently in the actual device idle mode.
2144 *
2145 * {@hide}
2146 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002147 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002148
2149 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002150 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002151 *
2152 * {@hide}
2153 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002154 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002155
2156 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002157 * Returns the number of times that connectivity state changed.
2158 *
2159 * {@hide}
2160 */
2161 public abstract int getNumConnectivityChange(int which);
2162
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002163
2164 /**
2165 * Returns the time in microseconds that the phone has been running with
2166 * the given GPS signal quality level
2167 *
2168 * {@hide}
2169 */
2170 public abstract long getGpsSignalQualityTime(int strengthBin,
2171 long elapsedRealtimeUs, int which);
2172
2173 /**
2174 * Returns the GPS battery drain in mA-ms
2175 *
2176 * {@hide}
2177 */
2178 public abstract long getGpsBatteryDrainMaMs();
2179
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002180 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002181 * 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 -08002182 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 * {@hide}
2185 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002186 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002187
Dianne Hackborn627bba72009-03-24 22:32:56 -07002188 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002189 * Returns the number of times a phone call was activated.
2190 *
2191 * {@hide}
2192 */
2193 public abstract int getPhoneOnCount(int which);
2194
2195 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002196 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002197 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002198 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002199 * {@hide}
2200 */
2201 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002202 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002203
Dianne Hackborn617f8772009-03-31 15:04:46 -07002204 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002205 * Returns the time in microseconds that the phone has been trying to
2206 * acquire a signal.
2207 *
2208 * {@hide}
2209 */
2210 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002211 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002212
2213 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002214 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2215 * acquire a signal.
2216 *
2217 * {@hide}
2218 */
2219 public abstract Timer getPhoneSignalScanningTimer();
2220
2221 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002222 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002223 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002224 * {@hide}
2225 */
2226 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2227
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002228 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002229 * Return the {@link Timer} object used to track the given signal strength's duration and
2230 * counts.
2231 */
2232 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2233
2234 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002235 * Returns the time in microseconds that the mobile network has been active
2236 * (in a high power state).
2237 *
2238 * {@hide}
2239 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002240 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002241
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002242 /**
2243 * Returns the number of times that the mobile network has transitioned to the
2244 * active state.
2245 *
2246 * {@hide}
2247 */
2248 public abstract int getMobileRadioActiveCount(int which);
2249
2250 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002251 * Returns the time in microseconds that is the difference between the mobile radio
2252 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2253 * from the radio.
2254 *
2255 * {@hide}
2256 */
2257 public abstract long getMobileRadioActiveAdjustedTime(int which);
2258
2259 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002260 * Returns the time in microseconds that the mobile network has been active
2261 * (in a high power state) but not being able to blame on an app.
2262 *
2263 * {@hide}
2264 */
2265 public abstract long getMobileRadioActiveUnknownTime(int which);
2266
2267 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002268 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002269 *
2270 * {@hide}
2271 */
2272 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002273
Tej Singheee317b2018-03-07 19:28:05 -08002274 public static final int DATA_CONNECTION_NONE = 0;
2275 public static final int DATA_CONNECTION_OTHER = TelephonyManager.MAX_NETWORK_TYPE + 1;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002276
Dianne Hackborn627bba72009-03-24 22:32:56 -07002277 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002278 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002279 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Siddharth Rayc72081d2018-02-13 11:31:54 -08002280 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002281 };
Bookatzc8c44962017-05-11 12:12:54 -07002282
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002283 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002284
Dianne Hackborn627bba72009-03-24 22:32:56 -07002285 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002286 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002287 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002288 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002289 * {@hide}
2290 */
2291 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002292 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002295 * Returns the number of times the phone has entered the given data
2296 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002297 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002298 * {@hide}
2299 */
2300 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002301
Kweku Adams87b19ec2017-10-09 12:40:03 -07002302 /**
2303 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2304 */
2305 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2306
Dianne Hackborn3251b902014-06-20 14:40:53 -07002307 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2308 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2309 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2310 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2311 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2312 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2313 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2314 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2315 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2316 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2317 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2318 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2319 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2320
2321 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2322
2323 static final String[] WIFI_SUPPL_STATE_NAMES = {
2324 "invalid", "disconn", "disabled", "inactive", "scanning",
2325 "authenticating", "associating", "associated", "4-way-handshake",
2326 "group-handshake", "completed", "dormant", "uninit"
2327 };
2328
2329 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2330 "inv", "dsc", "dis", "inact", "scan",
2331 "auth", "ascing", "asced", "4-way",
2332 "group", "compl", "dorm", "uninit"
2333 };
2334
Mike Mac2f518a2017-09-19 16:06:03 -07002335 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002336 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002337 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2338 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002339 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002340 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2341 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2342 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002343 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002344 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002345 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002346 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002347 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2348 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002349 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002350 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2351 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2352 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2353 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2354 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2355 new String[] {"in", "out", "emergency", "off"},
2356 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002357 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2358 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2359 SignalStrength.SIGNAL_STRENGTH_NAMES,
2360 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002361 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2362 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2363 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002364 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002365
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002366 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2367 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002368 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002369 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002370 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002371 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002372 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002373 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2374 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2375 new String[] { "off", "light", "full", "???" },
2376 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002377 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2378 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2379 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002380 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2381 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2382 new String[] { "0", "1", "2", "3", "4" },
2383 new String[] { "0", "1", "2", "3", "4" }),
2384 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2385 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2386 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002387 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002388 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002389 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2390 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002391 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2392 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002393 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002394 };
2395
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002396 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002397 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002398 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2399 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002400 };
2401
2402 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002403 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002404 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002405 "Esw", "Ewa", "Elw", "Eec"
2406 };
2407
2408 @FunctionalInterface
2409 public interface IntToString {
2410 String applyAsString(int val);
2411 }
2412
2413 private static final IntToString sUidToString = UserHandle::formatUid;
2414 private static final IntToString sIntToString = Integer::toString;
2415
2416 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2417 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2418 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2419 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2420 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002421 };
2422
Dianne Hackborn617f8772009-03-31 15:04:46 -07002423 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002424 * Returns total time for WiFi Multicast Wakelock timer.
2425 * Note that this may be different from the sum of per uid timer values.
2426 *
2427 * {@hide}
2428 */
2429 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2430
2431 /**
2432 * Returns total time for WiFi Multicast Wakelock timer
2433 * Note that this may be different from the sum of per uid timer values.
2434 *
2435 * {@hide}
2436 */
2437 public abstract int getWifiMulticastWakelockCount(int which);
2438
2439 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002440 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002441 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002442 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002443 * {@hide}
2444 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002445 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002446
2447 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002448 * Returns the time in microseconds that wifi has been active while the device was
2449 * running on battery.
2450 *
2451 * {@hide}
2452 */
2453 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2454
2455 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002456 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002457 * been in the running state while the device was running on battery.
2458 *
2459 * {@hide}
2460 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002461 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002462
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002463 public static final int WIFI_STATE_OFF = 0;
2464 public static final int WIFI_STATE_OFF_SCANNING = 1;
2465 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2466 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2467 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2468 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2469 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2470 public static final int WIFI_STATE_SOFT_AP = 7;
2471
2472 static final String[] WIFI_STATE_NAMES = {
2473 "off", "scanning", "no_net", "disconn",
2474 "sta", "p2p", "sta_p2p", "soft_ap"
2475 };
2476
2477 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2478
2479 /**
2480 * Returns the time in microseconds that WiFi has been running in the given state.
2481 *
2482 * {@hide}
2483 */
2484 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002485 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002486
2487 /**
2488 * Returns the number of times that WiFi has entered the given state.
2489 *
2490 * {@hide}
2491 */
2492 public abstract int getWifiStateCount(int wifiState, int which);
2493
The Android Open Source Project10592532009-03-18 17:39:46 -07002494 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002495 * Returns the {@link Timer} object that tracks the given WiFi state.
2496 *
2497 * {@hide}
2498 */
2499 public abstract Timer getWifiStateTimer(int wifiState);
2500
2501 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002502 * Returns the time in microseconds that the wifi supplicant has been
2503 * in a given state.
2504 *
2505 * {@hide}
2506 */
2507 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2508
2509 /**
2510 * Returns the number of times that the wifi supplicant has transitioned
2511 * to a given state.
2512 *
2513 * {@hide}
2514 */
2515 public abstract int getWifiSupplStateCount(int state, int which);
2516
Kweku Adams87b19ec2017-10-09 12:40:03 -07002517 /**
2518 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2519 *
2520 * {@hide}
2521 */
2522 public abstract Timer getWifiSupplStateTimer(int state);
2523
Dianne Hackborn3251b902014-06-20 14:40:53 -07002524 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2525
2526 /**
2527 * Returns the time in microseconds that WIFI has been running with
2528 * the given signal strength.
2529 *
2530 * {@hide}
2531 */
2532 public abstract long getWifiSignalStrengthTime(int strengthBin,
2533 long elapsedRealtimeUs, int which);
2534
2535 /**
2536 * Returns the number of times WIFI has entered the given signal strength.
2537 *
2538 * {@hide}
2539 */
2540 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2541
2542 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002543 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2544 *
2545 * {@hide}
2546 */
2547 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2548
2549 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002550 * Returns the time in microseconds that the flashlight has been on while the device was
2551 * running on battery.
2552 *
2553 * {@hide}
2554 */
2555 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2556
2557 /**
2558 * Returns the number of times that the flashlight has been turned on while the device was
2559 * running on battery.
2560 *
2561 * {@hide}
2562 */
2563 public abstract long getFlashlightOnCount(int which);
2564
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002565 /**
2566 * Returns the time in microseconds that the camera has been on while the device was
2567 * running on battery.
2568 *
2569 * {@hide}
2570 */
2571 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2572
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002573 /**
2574 * Returns the time in microseconds that bluetooth scans were running while the device was
2575 * on battery.
2576 *
2577 * {@hide}
2578 */
2579 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002580
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002581 public static final int NETWORK_MOBILE_RX_DATA = 0;
2582 public static final int NETWORK_MOBILE_TX_DATA = 1;
2583 public static final int NETWORK_WIFI_RX_DATA = 2;
2584 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002585 public static final int NETWORK_BT_RX_DATA = 4;
2586 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002587 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2588 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2589 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2590 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2591 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002592
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002593 public abstract long getNetworkActivityBytes(int type, int which);
2594 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002595
Adam Lesinskie08af192015-03-25 16:42:59 -07002596 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002597 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002598 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002599 * actual power data.
2600 */
2601 public abstract boolean hasWifiActivityReporting();
2602
2603 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002604 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2605 * in various radio controller states, such as transmit, receive, and idle.
2606 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002607 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002608 public abstract ControllerActivityCounter getWifiControllerActivity();
2609
2610 /**
2611 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2612 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2613 * actual power data.
2614 */
2615 public abstract boolean hasBluetoothActivityReporting();
2616
2617 /**
2618 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2619 * in various radio controller states, such as transmit, receive, and idle.
2620 * @return non-null {@link ControllerActivityCounter}
2621 */
2622 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2623
2624 /**
2625 * Returns true if the BatteryStats object has detailed modem power reports.
2626 * When true, calling {@link #getModemControllerActivity()} will yield the
2627 * actual power data.
2628 */
2629 public abstract boolean hasModemActivityReporting();
2630
2631 /**
2632 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2633 * in various radio controller states, such as transmit, receive, and idle.
2634 * @return non-null {@link ControllerActivityCounter}
2635 */
2636 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002637
The Android Open Source Project10592532009-03-18 17:39:46 -07002638 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002639 * Return the wall clock time when battery stats data collection started.
2640 */
2641 public abstract long getStartClockTime();
2642
2643 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002644 * Return platform version tag that we were running in when the battery stats started.
2645 */
2646 public abstract String getStartPlatformVersion();
2647
2648 /**
2649 * Return platform version tag that we were running in when the battery stats ended.
2650 */
2651 public abstract String getEndPlatformVersion();
2652
2653 /**
2654 * Return the internal version code of the parcelled format.
2655 */
2656 public abstract int getParcelVersion();
2657
2658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 * Return whether we are currently running on battery.
2660 */
2661 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 /**
2664 * Returns a SparseArray containing the statistics for each uid.
2665 */
2666 public abstract SparseArray<? extends Uid> getUidStats();
2667
2668 /**
2669 * Returns the current battery uptime in microseconds.
2670 *
2671 * @param curTime the amount of elapsed realtime in microseconds.
2672 */
2673 public abstract long getBatteryUptime(long curTime);
2674
2675 /**
2676 * Returns the current battery realtime in microseconds.
2677 *
2678 * @param curTime the amount of elapsed realtime in microseconds.
2679 */
2680 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002681
The Android Open Source Project10592532009-03-18 17:39:46 -07002682 /**
Evan Millar633a1742009-04-02 16:36:33 -07002683 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002684 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002685 */
Evan Millar633a1742009-04-02 16:36:33 -07002686 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002687
The Android Open Source Project10592532009-03-18 17:39:46 -07002688 /**
Evan Millar633a1742009-04-02 16:36:33 -07002689 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2690 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002691 */
Evan Millar633a1742009-04-02 16:36:33 -07002692 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693
2694 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002695 * Get the amount the battery has discharged since the stats were
2696 * last reset after charging, as a lower-end approximation.
2697 */
2698 public abstract int getLowDischargeAmountSinceCharge();
2699
2700 /**
2701 * Get the amount the battery has discharged since the stats were
2702 * last reset after charging, as an upper-end approximation.
2703 */
2704 public abstract int getHighDischargeAmountSinceCharge();
2705
2706 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002707 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2708 */
2709 public abstract int getDischargeAmount(int which);
2710
2711 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002712 * Get the amount the battery has discharged while the screen was on,
2713 * since the last time power was unplugged.
2714 */
2715 public abstract int getDischargeAmountScreenOn();
2716
2717 /**
2718 * Get the amount the battery has discharged while the screen was on,
2719 * since the last time the device was charged.
2720 */
2721 public abstract int getDischargeAmountScreenOnSinceCharge();
2722
2723 /**
2724 * Get the amount the battery has discharged while the screen was off,
2725 * since the last time power was unplugged.
2726 */
2727 public abstract int getDischargeAmountScreenOff();
2728
2729 /**
2730 * Get the amount the battery has discharged while the screen was off,
2731 * since the last time the device was charged.
2732 */
2733 public abstract int getDischargeAmountScreenOffSinceCharge();
2734
2735 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002736 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002737 * since the last time power was unplugged.
2738 */
2739 public abstract int getDischargeAmountScreenDoze();
2740
2741 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002742 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002743 * since the last time the device was charged.
2744 */
2745 public abstract int getDischargeAmountScreenDozeSinceCharge();
2746
2747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 * Returns the total, last, or current battery uptime in microseconds.
2749 *
2750 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002751 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 */
2753 public abstract long computeBatteryUptime(long curTime, int which);
2754
2755 /**
2756 * Returns the total, last, or current battery realtime in microseconds.
2757 *
2758 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002759 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 */
2761 public abstract long computeBatteryRealtime(long curTime, int which);
2762
2763 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002764 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002765 *
2766 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002767 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002768 */
2769 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2770
2771 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002772 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002773 *
2774 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002775 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002776 */
2777 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2778
2779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 * Returns the total, last, or current uptime in microseconds.
2781 *
2782 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002783 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 */
2785 public abstract long computeUptime(long curTime, int which);
2786
2787 /**
2788 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002791 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 */
2793 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002794
2795 /**
2796 * Compute an approximation for how much run time (in microseconds) is remaining on
2797 * the battery. Returns -1 if no time can be computed: either there is not
2798 * enough current data to make a decision, or the battery is currently
2799 * charging.
2800 *
2801 * @param curTime The current elepsed realtime in microseconds.
2802 */
2803 public abstract long computeBatteryTimeRemaining(long curTime);
2804
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002805 // The part of a step duration that is the actual time.
2806 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2807
2808 // Bits in a step duration that are the new battery level we are at.
2809 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002810 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002811
2812 // Bits in a step duration that are the initial mode we were in at that step.
2813 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002814 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002815
2816 // Bits in a step duration that indicate which modes changed during that step.
2817 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002818 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002819
2820 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2821 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2822
Santos Cordone94f0502017-02-24 12:31:20 -08002823 // The largest value for screen state that is tracked in battery states. Any values above
2824 // this should be mapped back to one of the tracked values before being tracked here.
2825 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2826
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002827 // Step duration mode: power save is on.
2828 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2829
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002830 // Step duration mode: device is currently in idle mode.
2831 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2832
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002833 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2834 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002835 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2836 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002837 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2838 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2839 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2840 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2841 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002842 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2843 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002844 };
2845 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2846 (Display.STATE_OFF-1),
2847 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002848 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002849 (Display.STATE_ON-1),
2850 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2851 (Display.STATE_DOZE-1),
2852 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2853 (Display.STATE_DOZE_SUSPEND-1),
2854 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002855 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002856 };
2857 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2858 "screen off",
2859 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002860 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002861 "screen on",
2862 "screen on power save",
2863 "screen doze",
2864 "screen doze power save",
2865 "screen doze-suspend",
2866 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002867 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002868 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002869
2870 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002871 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002872 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2873 * a coulomb counter.
2874 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002875 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002876
2877 /**
2878 * Return the amount of battery discharge while the screen was in doze mode, measured in
2879 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2880 * a coulomb counter.
2881 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002882 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002883
2884 /**
2885 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2886 * non-zero only if the device's battery has a coulomb counter.
2887 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002888 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002889
2890 /**
Mike Ma15313c92017-11-15 17:58:21 -08002891 * @return the amount of battery discharge while the device is in light idle mode, measured in
2892 * micro-Ampere-hours.
2893 */
2894 public abstract long getUahDischargeLightDoze(int which);
2895
2896 /**
2897 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2898 * micro-Ampere-hours.
2899 */
2900 public abstract long getUahDischargeDeepDoze(int which);
2901
2902 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002903 * Returns the estimated real battery capacity, which may be less than the capacity
2904 * declared by the PowerProfile.
2905 * @return The estimated battery capacity in mAh.
2906 */
2907 public abstract int getEstimatedBatteryCapacity();
2908
2909 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002910 * @return The minimum learned battery capacity in uAh.
2911 */
2912 public abstract int getMinLearnedBatteryCapacity();
2913
2914 /**
2915 * @return The maximum learned battery capacity in uAh.
2916 */
2917 public abstract int getMaxLearnedBatteryCapacity() ;
2918
2919 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002920 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002921 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002922 public abstract LevelStepTracker getDischargeLevelStepTracker();
2923
2924 /**
2925 * Return the array of daily discharge step durations.
2926 */
2927 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002928
2929 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002930 * Compute an approximation for how much time (in microseconds) remains until the battery
2931 * is fully charged. Returns -1 if no time can be computed: either there is not
2932 * enough current data to make a decision, or the battery is currently
2933 * discharging.
2934 *
2935 * @param curTime The current elepsed realtime in microseconds.
2936 */
2937 public abstract long computeChargeTimeRemaining(long curTime);
2938
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002939 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002940 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002941 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002942 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002943
2944 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002945 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002946 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002947 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002948
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002949 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2950
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002951 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002952
Evan Millarc64edde2009-04-18 12:26:32 -07002953 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Bookatz50df7112017-08-04 14:53:26 -07002955 /**
2956 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2957 */
2958 public abstract Map<String, ? extends Timer> getRpmStats();
2959 /**
2960 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2961 */
2962 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2963
2964
James Carr2dd7e5e2016-07-20 18:48:39 -07002965 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2966
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002967 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2968
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002969 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 long days = seconds / (60 * 60 * 24);
2971 if (days != 0) {
2972 out.append(days);
2973 out.append("d ");
2974 }
2975 long used = days * 60 * 60 * 24;
2976
2977 long hours = (seconds - used) / (60 * 60);
2978 if (hours != 0 || used != 0) {
2979 out.append(hours);
2980 out.append("h ");
2981 }
2982 used += hours * 60 * 60;
2983
2984 long mins = (seconds-used) / 60;
2985 if (mins != 0 || used != 0) {
2986 out.append(mins);
2987 out.append("m ");
2988 }
2989 used += mins * 60;
2990
2991 if (seconds != 0 || used != 0) {
2992 out.append(seconds-used);
2993 out.append("s ");
2994 }
2995 }
2996
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002997 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002999 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 sb.append(time - (sec * 1000));
3001 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 }
3003
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003004 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003005 long sec = time / 1000;
3006 formatTimeRaw(sb, sec);
3007 sb.append(time - (sec * 1000));
3008 sb.append("ms");
3009 }
3010
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003011 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003013 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
3015 float perc = ((float)num) / ((float)den) * 100;
3016 mFormatBuilder.setLength(0);
3017 mFormatter.format("%.1f%%", perc);
3018 return mFormatBuilder.toString();
3019 }
3020
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003021 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003022 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003023
Evan Millar22ac0432009-03-31 11:33:18 -07003024 if (bytes < BYTES_PER_KB) {
3025 return bytes + "B";
3026 } else if (bytes < BYTES_PER_MB) {
3027 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3028 return mFormatBuilder.toString();
3029 } else if (bytes < BYTES_PER_GB){
3030 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3031 return mFormatBuilder.toString();
3032 } else {
3033 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3034 return mFormatBuilder.toString();
3035 }
3036 }
3037
Kweku Adams103351f2017-10-16 14:39:34 -07003038 private static long roundUsToMs(long timeUs) {
3039 return (timeUs + 500) / 1000;
3040 }
3041
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003042 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003043 if (timer != null) {
3044 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003045 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003046 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3047 return totalTimeMillis;
3048 }
3049 return 0;
3050 }
3051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 /**
3053 *
3054 * @param sb a StringBuilder object.
3055 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003056 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003058 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 * @param linePrefix a String to be prepended to each line of output.
3060 * @return the line prefix
3061 */
3062 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003063 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003066 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003067
Evan Millarc64edde2009-04-18 12:26:32 -07003068 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 if (totalTimeMillis != 0) {
3070 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003071 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003072 if (name != null) {
3073 sb.append(name);
3074 sb.append(' ');
3075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 sb.append('(');
3077 sb.append(count);
3078 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003079 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3080 if (maxDurationMs >= 0) {
3081 sb.append(" max=");
3082 sb.append(maxDurationMs);
3083 }
Bookatz506a8182017-05-01 14:18:42 -07003084 // Put actual time if it is available and different from totalTimeMillis.
3085 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3086 if (totalDurMs > totalTimeMillis) {
3087 sb.append(" actual=");
3088 sb.append(totalDurMs);
3089 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003090 if (timer.isRunningLocked()) {
3091 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3092 if (currentMs >= 0) {
3093 sb.append(" (running for ");
3094 sb.append(currentMs);
3095 sb.append("ms)");
3096 } else {
3097 sb.append(" (running)");
3098 }
3099 }
3100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 return ", ";
3102 }
3103 }
3104 return linePrefix;
3105 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003106
3107 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003108 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003109 *
3110 * @param pw a PrintWriter object to print to.
3111 * @param sb a StringBuilder object.
3112 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003113 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003114 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3115 * @param prefix a String to be prepended to each line of output.
3116 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003117 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003118 */
3119 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003120 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003121 if (timer != null) {
3122 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003123 final long totalTimeMs = (timer.getTotalTimeLocked(
3124 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003125 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003126 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003127 sb.setLength(0);
3128 sb.append(prefix);
3129 sb.append(" ");
3130 sb.append(type);
3131 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003132 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003133 sb.append("realtime (");
3134 sb.append(count);
3135 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003136 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3137 if (maxDurationMs >= 0) {
3138 sb.append(" max=");
3139 sb.append(maxDurationMs);
3140 }
3141 if (timer.isRunningLocked()) {
3142 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3143 if (currentMs >= 0) {
3144 sb.append(" (running for ");
3145 sb.append(currentMs);
3146 sb.append("ms)");
3147 } else {
3148 sb.append(" (running)");
3149 }
3150 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003151 pw.println(sb.toString());
3152 return true;
3153 }
3154 }
3155 return false;
3156 }
Bookatzc8c44962017-05-11 12:12:54 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 /**
3159 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 * @param sb a StringBuilder object.
3162 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003163 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003165 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 * @param linePrefix a String to be prepended to each line of output.
3167 * @return the line prefix
3168 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003169 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3170 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 long totalTimeMicros = 0;
3172 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003173 long max = 0;
3174 long current = 0;
3175 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003177 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003178 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003179 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3180 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003181 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
3183 sb.append(linePrefix);
3184 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3185 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003186 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003188 sb.append(',');
3189 sb.append(current);
3190 sb.append(',');
3191 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003192 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3193 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3194 // totalDuration independent of totalTimeMicros (since they are not pooled).
3195 if (name != null) {
3196 sb.append(',');
3197 sb.append(totalDuration);
3198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 return ",";
3200 }
Bookatz506a8182017-05-01 14:18:42 -07003201
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003202 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3203 String type) {
3204 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3205 pw.print(',');
3206 pw.print(uid);
3207 pw.print(',');
3208 pw.print(category);
3209 pw.print(',');
3210 pw.print(type);
3211 }
3212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 /**
3214 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 * @param pw the PageWriter to dump log to
3217 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3218 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3219 * @param args type-dependent data arguments
3220 */
Bookatzc8c44962017-05-11 12:12:54 -07003221 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003223 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003224 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003225 pw.print(',');
3226 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003228 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003230
3231 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003232 * Dump a given timer stat for terse checkin mode.
3233 *
3234 * @param pw the PageWriter to dump log to
3235 * @param uid the UID to log
3236 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3237 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3238 * @param timer a {@link Timer} to dump stats for
3239 * @param rawRealtime the current elapsed realtime of the system in microseconds
3240 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3241 */
3242 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3243 Timer timer, long rawRealtime, int which) {
3244 if (timer != null) {
3245 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003246 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003247 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003248 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003249 dumpLine(pw, uid, category, type, totalTime, count);
3250 }
3251 }
3252 }
3253
3254 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003255 * Dump a given timer stat to the proto stream.
3256 *
3257 * @param proto the ProtoOutputStream to log to
3258 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3259 * @param timer a {@link Timer} to dump stats for
3260 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3261 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3262 */
3263 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003264 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003265 if (timer == null) {
3266 return;
3267 }
3268 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003269 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003270 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003271 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3272 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3273 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3274 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3275 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003276 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003277 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003278 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003279 // These values will be -1 for timers that don't implement the functionality.
3280 if (maxDurationMs != -1) {
3281 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3282 }
3283 if (curDurationMs != -1) {
3284 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3285 }
3286 if (totalDurationMs != -1) {
3287 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3288 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003289 proto.end(token);
3290 }
3291 }
3292
3293 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003294 * Checks if the ControllerActivityCounter has any data worth dumping.
3295 */
3296 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3297 if (counter == null) {
3298 return false;
3299 }
3300
3301 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3302 || counter.getRxTimeCounter().getCountLocked(which) != 0
3303 || counter.getPowerCounter().getCountLocked(which) != 0) {
3304 return true;
3305 }
3306
3307 for (LongCounter c : counter.getTxTimeCounters()) {
3308 if (c.getCountLocked(which) != 0) {
3309 return true;
3310 }
3311 }
3312 return false;
3313 }
3314
3315 /**
3316 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3317 * The order of the arguments in the final check in line is:
3318 *
3319 * idle, rx, power, tx...
3320 *
3321 * where tx... is one or more transmit level times.
3322 */
3323 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3324 String type,
3325 ControllerActivityCounter counter,
3326 int which) {
3327 if (!controllerActivityHasData(counter, which)) {
3328 return;
3329 }
3330
3331 dumpLineHeader(pw, uid, category, type);
3332 pw.print(",");
3333 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3334 pw.print(",");
3335 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3336 pw.print(",");
3337 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3338 for (LongCounter c : counter.getTxTimeCounters()) {
3339 pw.print(",");
3340 pw.print(c.getCountLocked(which));
3341 }
3342 pw.println();
3343 }
3344
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003345 /**
3346 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3347 */
3348 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3349 ControllerActivityCounter counter,
3350 int which) {
3351 if (!controllerActivityHasData(counter, which)) {
3352 return;
3353 }
3354
3355 final long cToken = proto.start(fieldId);
3356
3357 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3358 counter.getIdleTimeCounter().getCountLocked(which));
3359 proto.write(ControllerActivityProto.RX_DURATION_MS,
3360 counter.getRxTimeCounter().getCountLocked(which));
3361 proto.write(ControllerActivityProto.POWER_MAH,
3362 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3363
3364 long tToken;
3365 LongCounter[] txCounters = counter.getTxTimeCounters();
3366 for (int i = 0; i < txCounters.length; ++i) {
3367 LongCounter c = txCounters[i];
3368 tToken = proto.start(ControllerActivityProto.TX);
3369 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3370 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3371 proto.end(tToken);
3372 }
3373
3374 proto.end(cToken);
3375 }
3376
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003377 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3378 String prefix, String controllerName,
3379 ControllerActivityCounter counter,
3380 int which) {
3381 if (controllerActivityHasData(counter, which)) {
3382 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3383 }
3384 }
3385
3386 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3387 String controllerName,
3388 ControllerActivityCounter counter, int which) {
3389 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3390 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3391 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003392 // Battery real time
3393 final long totalControllerActivityTimeMs
3394 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003395 long totalTxTimeMs = 0;
3396 for (LongCounter txState : counter.getTxTimeCounters()) {
3397 totalTxTimeMs += txState.getCountLocked(which);
3398 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003399
Siddharth Rayb50a6842017-12-14 15:15:28 -08003400 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3401 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3402 sb.setLength(0);
3403 sb.append(prefix);
3404 sb.append(" ");
3405 sb.append(controllerName);
3406 sb.append(" Scan time: ");
3407 formatTimeMs(sb, scanTimeMs);
3408 sb.append("(");
3409 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3410 sb.append(")");
3411 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003412
3413 final long sleepTimeMs
3414 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3415 sb.setLength(0);
3416 sb.append(prefix);
3417 sb.append(" ");
3418 sb.append(controllerName);
3419 sb.append(" Sleep time: ");
3420 formatTimeMs(sb, sleepTimeMs);
3421 sb.append("(");
3422 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3423 sb.append(")");
3424 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003425 }
3426
Siddharth Rayed754702018-02-15 12:44:37 -08003427 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3428 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3429 sb.setLength(0);
3430 sb.append(prefix);
3431 sb.append(" ");
3432 sb.append(controllerName);
3433 sb.append(" Sleep time: ");
3434 formatTimeMs(sb, sleepTimeMs);
3435 sb.append("(");
3436 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3437 sb.append(")");
3438 pw.println(sb.toString());
3439 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003440
3441 sb.setLength(0);
3442 sb.append(prefix);
3443 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003444 sb.append(controllerName);
3445 sb.append(" Idle time: ");
3446 formatTimeMs(sb, idleTimeMs);
3447 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003448 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003449 sb.append(")");
3450 pw.println(sb.toString());
3451
3452 sb.setLength(0);
3453 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003454 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003455 sb.append(controllerName);
3456 sb.append(" Rx time: ");
3457 formatTimeMs(sb, rxTimeMs);
3458 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003459 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003460 sb.append(")");
3461 pw.println(sb.toString());
3462
3463 sb.setLength(0);
3464 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003465 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003466 sb.append(controllerName);
3467 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003468
Siddharth Ray3c648c42017-10-02 17:30:58 -07003469 String [] powerLevel;
3470 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003471 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003472 powerLevel = new String[] {
3473 " less than 0dBm: ",
3474 " 0dBm to 8dBm: ",
3475 " 8dBm to 15dBm: ",
3476 " 15dBm to 20dBm: ",
3477 " above 20dBm: "};
3478 break;
3479 default:
3480 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3481 break;
3482 }
3483 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003484 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003485 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003486 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3487 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3488 sb.setLength(0);
3489 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003490 sb.append(" ");
3491 sb.append(powerLevel[lvl]);
3492 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003493 formatTimeMs(sb, txLvlTimeMs);
3494 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003495 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003496 sb.append(")");
3497 pw.println(sb.toString());
3498 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003499 } else {
3500 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3501 formatTimeMs(sb, txLvlTimeMs);
3502 sb.append("(");
3503 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3504 sb.append(")");
3505 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003506 }
3507
Siddharth Ray3c648c42017-10-02 17:30:58 -07003508 if (powerDrainMaMs > 0) {
3509 sb.setLength(0);
3510 sb.append(prefix);
3511 sb.append(" ");
3512 sb.append(controllerName);
3513 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003514 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003515 sb.append("mAh");
3516 pw.println(sb.toString());
3517 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003518 }
3519
3520 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003521 * Temporary for settings.
3522 */
3523 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3524 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3525 }
3526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 /**
3528 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003529 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003530 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003532 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3533 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003535 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3536 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3539 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003540 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3541 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3542 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 final long totalRealtime = computeRealtime(rawRealtime, which);
3544 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003545 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003546 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003547 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003548 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003549 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3550 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003551 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003552 rawRealtime, which);
3553 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3554 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003555 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003556 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003557 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003559 final long dischargeCount = getUahDischarge(which);
3560 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3561 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003562 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3563 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003564
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003565 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003566
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003567 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003568 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003569
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003570 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003573 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003574 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003575 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003576 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003577 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003578 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003579 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003580 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3581 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003582
Bookatzc8c44962017-05-11 12:12:54 -07003583
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003584 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003585 long fullWakeLockTimeTotal = 0;
3586 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003587
Evan Millar22ac0432009-03-31 11:33:18 -07003588 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003589 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003590
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003591 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3592 = u.getWakelockStats();
3593 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3594 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003595
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003596 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3597 if (fullWakeTimer != null) {
3598 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3599 which);
3600 }
3601
3602 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3603 if (partialWakeTimer != null) {
3604 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3605 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003606 }
3607 }
3608 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003609
3610 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003611 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3612 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3613 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3614 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3615 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3616 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3617 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3618 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003619 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3620 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003621 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3622 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003623 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3624 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003625
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003626 // Dump Modem controller stats
3627 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3628 getModemControllerActivity(), which);
3629
Adam Lesinskie283d332015-04-16 12:29:25 -07003630 // Dump Wifi controller stats
3631 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3632 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003633 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003634 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003635
3636 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3637 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003638
3639 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003640 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3641 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 // Dump misc stats
3644 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003645 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003646 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003647 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003648 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003649 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003650 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3651 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003652 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003653 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3654 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3655 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3656 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003657 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003658
Dianne Hackborn617f8772009-03-31 15:04:46 -07003659 // Dump screen brightness stats
3660 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3661 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003662 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003663 }
3664 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003665
Dianne Hackborn627bba72009-03-24 22:32:56 -07003666 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003667 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3668 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003669 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003670 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003671 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003672 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003673 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003674 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003675 args[i] = getPhoneSignalStrengthCount(i, which);
3676 }
3677 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003678
Dianne Hackborn627bba72009-03-24 22:32:56 -07003679 // Dump network type stats
3680 args = new Object[NUM_DATA_CONNECTION_TYPES];
3681 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003682 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003683 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003684 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3685 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3686 args[i] = getPhoneDataConnectionCount(i, which);
3687 }
3688 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003689
3690 // Dump wifi state stats
3691 args = new Object[NUM_WIFI_STATES];
3692 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003693 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003694 }
3695 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3696 for (int i=0; i<NUM_WIFI_STATES; i++) {
3697 args[i] = getWifiStateCount(i, which);
3698 }
3699 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3700
Dianne Hackborn3251b902014-06-20 14:40:53 -07003701 // Dump wifi suppl state stats
3702 args = new Object[NUM_WIFI_SUPPL_STATES];
3703 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3704 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3705 }
3706 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3707 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3708 args[i] = getWifiSupplStateCount(i, which);
3709 }
3710 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3711
3712 // Dump wifi signal strength stats
3713 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3714 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3715 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3716 }
3717 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3718 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3719 args[i] = getWifiSignalStrengthCount(i, which);
3720 }
3721 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3722
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003723 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003724 final long multicastWakeLockTimeTotalMicros =
3725 getWifiMulticastWakelockTime(rawRealtime, which);
3726 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003727 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3728 multicastWakeLockTimeTotalMicros / 1000,
3729 multicastWakeLockCountTotal);
3730
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003731 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003732 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003733 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003734 }
Bookatzc8c44962017-05-11 12:12:54 -07003735
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003736 if (which == STATS_SINCE_UNPLUGGED) {
3737 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3738 getDischargeStartLevel()-getDischargeCurrentLevel(),
3739 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003740 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003741 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003742 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3743 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003744 } else {
3745 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3746 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003747 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003748 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003749 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003750 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3751 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003752 }
Bookatzc8c44962017-05-11 12:12:54 -07003753
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003754 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003755 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003756 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003757 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003758 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003759 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003760 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3761 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003762 }
Evan Millarc64edde2009-04-18 12:26:32 -07003763 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003764 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003765 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003766 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3767 // Not doing the regular wake lock formatting to remain compatible
3768 // with the old checkin format.
3769 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3770 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003771 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003772 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003773 }
3774 }
Evan Millarc64edde2009-04-18 12:26:32 -07003775 }
Bookatzc8c44962017-05-11 12:12:54 -07003776
Bookatz50df7112017-08-04 14:53:26 -07003777 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3778 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3779 if (rpmStats.size() > 0) {
3780 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3781 sb.setLength(0);
3782 Timer totalTimer = ent.getValue();
3783 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3784 int count = totalTimer.getCountLocked(which);
3785 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3786 long screenOffTimeMs = screenOffTimer != null
3787 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3788 int screenOffCount = screenOffTimer != null
3789 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003790 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3791 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3792 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3793 screenOffCount);
3794 } else {
3795 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3796 "\"" + ent.getKey() + "\"", timeMs, count);
3797 }
Bookatz50df7112017-08-04 14:53:26 -07003798 }
3799 }
3800
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003801 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003802 helper.create(this);
3803 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003804 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003805 if (sippers != null && sippers.size() > 0) {
3806 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3807 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003808 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003809 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3810 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003811 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003812 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003813 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003814 String label;
3815 switch (bs.drainType) {
3816 case IDLE:
3817 label="idle";
3818 break;
3819 case CELL:
3820 label="cell";
3821 break;
3822 case PHONE:
3823 label="phone";
3824 break;
3825 case WIFI:
3826 label="wifi";
3827 break;
3828 case BLUETOOTH:
3829 label="blue";
3830 break;
3831 case SCREEN:
3832 label="scrn";
3833 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003834 case FLASHLIGHT:
3835 label="flashlight";
3836 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003837 case APP:
3838 uid = bs.uidObj.getUid();
3839 label = "uid";
3840 break;
3841 case USER:
3842 uid = UserHandle.getUid(bs.userId, 0);
3843 label = "user";
3844 break;
3845 case UNACCOUNTED:
3846 label = "unacc";
3847 break;
3848 case OVERCOUNTED:
3849 label = "over";
3850 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003851 case CAMERA:
3852 label = "camera";
3853 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003854 case MEMORY:
3855 label = "memory";
3856 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003857 default:
3858 label = "???";
3859 }
3860 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003861 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3862 bs.shouldHide ? 1 : 0,
3863 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3864 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003865 }
3866 }
3867
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003868 final long[] cpuFreqs = getCpuFreqs();
3869 if (cpuFreqs != null) {
3870 sb.setLength(0);
3871 for (int i = 0; i < cpuFreqs.length; ++i) {
3872 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3873 }
3874 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3875 }
3876
Kweku Adams87b19ec2017-10-09 12:40:03 -07003877 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 for (int iu = 0; iu < NU; iu++) {
3879 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003880 if (reqUid >= 0 && uid != reqUid) {
3881 continue;
3882 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003883 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003886 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3887 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3888 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3889 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3890 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3891 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3892 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3893 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003894 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003895 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3896 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003897 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003898 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3899 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003900 // Background data transfers
3901 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3902 which);
3903 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3904 which);
3905 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3906 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3907 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3908 which);
3909 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3910 which);
3911 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3912 which);
3913 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3914 which);
3915
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003916 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3917 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003918 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003919 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3920 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3921 || wifiBytesBgTx > 0
3922 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3923 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003924 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3925 wifiBytesRx, wifiBytesTx,
3926 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003927 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003928 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003929 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3930 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3931 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3932 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003933 }
3934
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003935 // Dump modem controller data, per UID.
3936 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3937 u.getModemControllerActivity(), which);
3938
3939 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003940 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3941 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3942 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003943 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3944 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003945 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3946 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3947 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003948 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003949 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003950 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3951 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003952 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3953 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003954 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003955 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003958 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3959 u.getWifiControllerActivity(), which);
3960
Bookatz867c0d72017-03-07 18:23:42 -08003961 final Timer bleTimer = u.getBluetoothScanTimer();
3962 if (bleTimer != null) {
3963 // Convert from microseconds to milliseconds with rounding
3964 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3965 / 1000;
3966 if (totalTime != 0) {
3967 final int count = bleTimer.getCountLocked(which);
3968 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3969 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003970 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3971 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3972 final long actualTimeBg = bleTimerBg != null ?
3973 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003974 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003975 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3976 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003977 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3978 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3979 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3980 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3981 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3982 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3983 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3984 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3985 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3986 final Timer unoptimizedScanTimerBg =
3987 u.getBluetoothUnoptimizedScanBackgroundTimer();
3988 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3989 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3990 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3991 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3992
Bookatz867c0d72017-03-07 18:23:42 -08003993 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003994 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3995 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3996 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003997 }
3998 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003999
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004000 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4001 u.getBluetoothControllerActivity(), which);
4002
Dianne Hackborn617f8772009-03-31 15:04:46 -07004003 if (u.hasUserActivity()) {
4004 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4005 boolean hasData = false;
4006 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4007 int val = u.getUserActivityCount(i, which);
4008 args[i] = val;
4009 if (val != 0) hasData = true;
4010 }
4011 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004012 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004013 }
4014 }
Bookatzc8c44962017-05-11 12:12:54 -07004015
4016 if (u.getAggregatedPartialWakelockTimer() != null) {
4017 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004018 // Times are since reset (regardless of 'which')
4019 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004020 final Timer bgTimer = timer.getSubTimer();
4021 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004022 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004023 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4024 }
4025
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004026 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4027 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4028 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4029 String linePrefix = "";
4030 sb.setLength(0);
4031 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4032 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004033 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4034 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004035 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004036 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4037 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004038 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4039 rawRealtime, "w", which, linePrefix);
4040
Kweku Adams103351f2017-10-16 14:39:34 -07004041 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004042 if (sb.length() > 0) {
4043 String name = wakelocks.keyAt(iw);
4044 if (name.indexOf(',') >= 0) {
4045 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
Yi Jin02483362017-08-04 11:30:44 -07004047 if (name.indexOf('\n') >= 0) {
4048 name = name.replace('\n', '_');
4049 }
4050 if (name.indexOf('\r') >= 0) {
4051 name = name.replace('\r', '_');
4052 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004053 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004056
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004057 // WiFi Multicast Wakelock Statistics
4058 final Timer mcTimer = u.getMulticastWakelockStats();
4059 if (mcTimer != null) {
4060 final long totalMcWakelockTimeMs =
4061 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4062 final int countMcWakelock = mcTimer.getCountLocked(which);
4063 if(totalMcWakelockTimeMs > 0) {
4064 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4065 totalMcWakelockTimeMs, countMcWakelock);
4066 }
4067 }
4068
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004069 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4070 for (int isy=syncs.size()-1; isy>=0; isy--) {
4071 final Timer timer = syncs.valueAt(isy);
4072 // Convert from microseconds to milliseconds with rounding
4073 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4074 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004075 final Timer bgTimer = timer.getSubTimer();
4076 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004077 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004078 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004079 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004080 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004081 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004082 }
4083 }
4084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004085 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4086 for (int ij=jobs.size()-1; ij>=0; ij--) {
4087 final Timer timer = jobs.valueAt(ij);
4088 // Convert from microseconds to milliseconds with rounding
4089 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4090 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004091 final Timer bgTimer = timer.getSubTimer();
4092 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004093 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004094 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004095 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004096 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004097 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004098 }
4099 }
4100
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004101 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4102 for (int ic=completions.size()-1; ic>=0; ic--) {
4103 SparseIntArray types = completions.valueAt(ic);
4104 if (types != null) {
4105 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4106 "\"" + completions.keyAt(ic) + "\"",
4107 types.get(JobParameters.REASON_CANCELED, 0),
4108 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4109 types.get(JobParameters.REASON_PREEMPT, 0),
4110 types.get(JobParameters.REASON_TIMEOUT, 0),
4111 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4112 }
4113 }
4114
Amith Yamasani977e11f2018-02-16 11:29:54 -08004115 // Dump deferred jobs stats
4116 u.getDeferredJobsCheckinLineLocked(sb, which);
4117 if (sb.length() > 0) {
4118 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4119 }
4120
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004121 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4122 rawRealtime, which);
4123 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4124 rawRealtime, which);
4125 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4126 rawRealtime, which);
4127 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4128 rawRealtime, which);
4129
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004130 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4131 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004132 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004133 final Uid.Sensor se = sensors.valueAt(ise);
4134 final int sensorNumber = sensors.keyAt(ise);
4135 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004136 if (timer != null) {
4137 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004138 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4139 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004140 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004141 final int count = timer.getCountLocked(which);
4142 final Timer bgTimer = se.getSensorBackgroundTime();
4143 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004144 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4145 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4146 final long bgActualTime = bgTimer != null ?
4147 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4148 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4149 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 }
4152 }
4153
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004154 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4155 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004156
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004157 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4158 rawRealtime, which);
4159
4160 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004161 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004162
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004163 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004164 long totalStateTime = 0;
4165 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004166 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4167 totalStateTime += time;
4168 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004169 }
4170 if (totalStateTime > 0) {
4171 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4172 }
4173
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004174 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4175 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004176 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004177 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004178 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004179 }
4180
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004181 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4182 if (cpuFreqs != null) {
4183 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4184 // If total cpuFreqTimes is null, then we don't need to check for
4185 // screenOffCpuFreqTimes.
4186 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4187 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004188 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004189 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004190 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004191 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4192 if (screenOffCpuFreqTimeMs != null) {
4193 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4194 sb.append("," + screenOffCpuFreqTimeMs[i]);
4195 }
4196 } else {
4197 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4198 sb.append(",0");
4199 }
4200 }
4201 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4202 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004203 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004204
4205 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4206 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4207 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4208 sb.setLength(0);
4209 for (int i = 0; i < timesMs.length; ++i) {
4210 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4211 }
4212 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4213 which, procState);
4214 if (screenOffTimesMs != null) {
4215 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4216 sb.append("," + screenOffTimesMs[i]);
4217 }
4218 } else {
4219 for (int i = 0; i < timesMs.length; ++i) {
4220 sb.append(",0");
4221 }
4222 }
4223 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4224 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4225 }
4226 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004227 }
4228
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004229 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4230 = u.getProcessStats();
4231 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4232 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004233
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004234 final long userMillis = ps.getUserTime(which);
4235 final long systemMillis = ps.getSystemTime(which);
4236 final long foregroundMillis = ps.getForegroundTime(which);
4237 final int starts = ps.getStarts(which);
4238 final int numCrashes = ps.getNumCrashes(which);
4239 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004240
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004241 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4242 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004243 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4244 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
4246 }
4247
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004248 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4249 = u.getPackageStats();
4250 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4251 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4252 int wakeups = 0;
4253 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4254 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004255 int count = alarms.valueAt(iwa).getCountLocked(which);
4256 wakeups += count;
4257 String name = alarms.keyAt(iwa).replace(',', '_');
4258 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004259 }
4260 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4261 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4262 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4263 final long startTime = ss.getStartTime(batteryUptime, which);
4264 final int starts = ss.getStarts(which);
4265 final int launches = ss.getLaunches(which);
4266 if (startTime != 0 || starts != 0 || launches != 0) {
4267 dumpLine(pw, uid, category, APK_DATA,
4268 wakeups, // wakeup alarms
4269 packageStats.keyAt(ipkg), // Apk
4270 serviceStats.keyAt(isvc), // service
4271 startTime / 1000, // time spent started, in ms
4272 starts,
4273 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 }
4275 }
4276 }
4277 }
4278 }
4279
Dianne Hackborn81038902012-11-26 17:04:09 -08004280 static final class TimerEntry {
4281 final String mName;
4282 final int mId;
4283 final BatteryStats.Timer mTimer;
4284 final long mTime;
4285 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4286 mName = name;
4287 mId = id;
4288 mTimer = timer;
4289 mTime = time;
4290 }
4291 }
4292
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004293 private void printmAh(PrintWriter printer, double power) {
4294 printer.print(BatteryStatsHelper.makemAh(power));
4295 }
4296
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004297 private void printmAh(StringBuilder sb, double power) {
4298 sb.append(BatteryStatsHelper.makemAh(power));
4299 }
4300
Dianne Hackbornd953c532014-08-16 18:17:38 -07004301 /**
4302 * Temporary for settings.
4303 */
4304 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4305 int reqUid) {
4306 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4307 }
4308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004310 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004311 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4313 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004314 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316
4317 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4318 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4319 final long totalRealtime = computeRealtime(rawRealtime, which);
4320 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004321 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4322 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4323 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004324 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4325 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004326 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004327
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004328 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004329
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004330 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004331 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004333 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4334 if (estimatedBatteryCapacity > 0) {
4335 sb.setLength(0);
4336 sb.append(prefix);
4337 sb.append(" Estimated battery capacity: ");
4338 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4339 sb.append(" mAh");
4340 pw.println(sb.toString());
4341 }
4342
Jocelyn Dangc627d102017-04-14 13:15:14 -07004343 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4344 if (minLearnedBatteryCapacity > 0) {
4345 sb.setLength(0);
4346 sb.append(prefix);
4347 sb.append(" Min learned battery capacity: ");
4348 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4349 sb.append(" mAh");
4350 pw.println(sb.toString());
4351 }
4352 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4353 if (maxLearnedBatteryCapacity > 0) {
4354 sb.setLength(0);
4355 sb.append(prefix);
4356 sb.append(" Max learned battery capacity: ");
4357 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4358 sb.append(" mAh");
4359 pw.println(sb.toString());
4360 }
4361
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004362 sb.setLength(0);
4363 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004364 sb.append(" Time on battery: ");
4365 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4366 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4367 sb.append(") realtime, ");
4368 formatTimeMs(sb, whichBatteryUptime / 1000);
4369 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4370 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004371 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004372
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004373 sb.setLength(0);
4374 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004375 sb.append(" Time on battery screen off: ");
4376 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4377 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4378 sb.append(") realtime, ");
4379 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4380 sb.append("(");
4381 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4382 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004383 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004384
4385 sb.setLength(0);
4386 sb.append(prefix);
4387 sb.append(" Time on battery screen doze: ");
4388 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4389 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4390 sb.append(")");
4391 pw.println(sb.toString());
4392
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004393 sb.setLength(0);
4394 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004395 sb.append(" Total run time: ");
4396 formatTimeMs(sb, totalRealtime / 1000);
4397 sb.append("realtime, ");
4398 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004399 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004400 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004401 if (batteryTimeRemaining >= 0) {
4402 sb.setLength(0);
4403 sb.append(prefix);
4404 sb.append(" Battery time remaining: ");
4405 formatTimeMs(sb, batteryTimeRemaining / 1000);
4406 pw.println(sb.toString());
4407 }
4408 if (chargeTimeRemaining >= 0) {
4409 sb.setLength(0);
4410 sb.append(prefix);
4411 sb.append(" Charge time remaining: ");
4412 formatTimeMs(sb, chargeTimeRemaining / 1000);
4413 pw.println(sb.toString());
4414 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004415
Kweku Adams87b19ec2017-10-09 12:40:03 -07004416 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004417 if (dischargeCount >= 0) {
4418 sb.setLength(0);
4419 sb.append(prefix);
4420 sb.append(" Discharge: ");
4421 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4422 sb.append(" mAh");
4423 pw.println(sb.toString());
4424 }
4425
Kweku Adams87b19ec2017-10-09 12:40:03 -07004426 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004427 if (dischargeScreenOffCount >= 0) {
4428 sb.setLength(0);
4429 sb.append(prefix);
4430 sb.append(" Screen off discharge: ");
4431 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4432 sb.append(" mAh");
4433 pw.println(sb.toString());
4434 }
4435
Kweku Adams87b19ec2017-10-09 12:40:03 -07004436 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004437 if (dischargeScreenDozeCount >= 0) {
4438 sb.setLength(0);
4439 sb.append(prefix);
4440 sb.append(" Screen doze discharge: ");
4441 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4442 sb.append(" mAh");
4443 pw.println(sb.toString());
4444 }
4445
4446 final long dischargeScreenOnCount =
4447 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004448 if (dischargeScreenOnCount >= 0) {
4449 sb.setLength(0);
4450 sb.append(prefix);
4451 sb.append(" Screen on discharge: ");
4452 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4453 sb.append(" mAh");
4454 pw.println(sb.toString());
4455 }
4456
Mike Ma15313c92017-11-15 17:58:21 -08004457 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4458 if (dischargeLightDozeCount >= 0) {
4459 sb.setLength(0);
4460 sb.append(prefix);
4461 sb.append(" Device light doze discharge: ");
4462 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4463 sb.append(" mAh");
4464 pw.println(sb.toString());
4465 }
4466
4467 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4468 if (dischargeDeepDozeCount >= 0) {
4469 sb.setLength(0);
4470 sb.append(prefix);
4471 sb.append(" Device deep doze discharge: ");
4472 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4473 sb.append(" mAh");
4474 pw.println(sb.toString());
4475 }
4476
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004477 pw.print(" Start clock time: ");
4478 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4479
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004480 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004481 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004482 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004483 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4484 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004485 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004486 rawRealtime, which);
4487 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4488 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004489 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004490 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004491 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4492 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4493 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004494 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004495 sb.append(prefix);
4496 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4497 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004498 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004499 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4500 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004501 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004502 pw.println(sb.toString());
4503 sb.setLength(0);
4504 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004505 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004506 boolean didOne = false;
4507 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004508 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004509 if (time == 0) {
4510 continue;
4511 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004512 sb.append("\n ");
4513 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004514 didOne = true;
4515 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4516 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004517 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004518 sb.append("(");
4519 sb.append(formatRatioLocked(time, screenOnTime));
4520 sb.append(")");
4521 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004522 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004523 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004524 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004525 sb.setLength(0);
4526 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004527 sb.append(" Power save mode enabled: ");
4528 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004529 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004530 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004531 sb.append(")");
4532 pw.println(sb.toString());
4533 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004534 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004535 sb.setLength(0);
4536 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004537 sb.append(" Device light idling: ");
4538 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004539 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004540 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4541 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004542 sb.append("x");
4543 pw.println(sb.toString());
4544 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004545 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004546 sb.setLength(0);
4547 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004548 sb.append(" Idle mode light time: ");
4549 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004550 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004551 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4552 sb.append(") ");
4553 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004554 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004555 sb.append(" -- longest ");
4556 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4557 pw.println(sb.toString());
4558 }
4559 if (deviceIdlingTime != 0) {
4560 sb.setLength(0);
4561 sb.append(prefix);
4562 sb.append(" Device full idling: ");
4563 formatTimeMs(sb, deviceIdlingTime / 1000);
4564 sb.append("(");
4565 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004566 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004567 sb.append("x");
4568 pw.println(sb.toString());
4569 }
4570 if (deviceIdleModeFullTime != 0) {
4571 sb.setLength(0);
4572 sb.append(prefix);
4573 sb.append(" Idle mode full time: ");
4574 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4575 sb.append("(");
4576 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4577 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004578 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004579 sb.append("x");
4580 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004581 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004582 pw.println(sb.toString());
4583 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004584 if (phoneOnTime != 0) {
4585 sb.setLength(0);
4586 sb.append(prefix);
4587 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4588 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004589 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004590 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004591 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004592 if (connChanges != 0) {
4593 pw.print(prefix);
4594 pw.print(" Connectivity changes: "); pw.println(connChanges);
4595 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004596
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004597 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004598 long fullWakeLockTimeTotalMicros = 0;
4599 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004600
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004601 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004602
Evan Millar22ac0432009-03-31 11:33:18 -07004603 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004604 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004605
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004606 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4607 = u.getWakelockStats();
4608 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4609 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004610
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004611 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4612 if (fullWakeTimer != null) {
4613 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4614 rawRealtime, which);
4615 }
4616
4617 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4618 if (partialWakeTimer != null) {
4619 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4620 rawRealtime, which);
4621 if (totalTimeMicros > 0) {
4622 if (reqUid < 0) {
4623 // Only show the ordered list of all wake
4624 // locks if the caller is not asking for data
4625 // about a specific uid.
4626 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4627 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004628 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004629 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004630 }
4631 }
4632 }
4633 }
Bookatzc8c44962017-05-11 12:12:54 -07004634
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004635 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4636 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4637 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4638 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4639 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4640 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4641 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4642 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004643 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4644 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004645
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004646 if (fullWakeLockTimeTotalMicros != 0) {
4647 sb.setLength(0);
4648 sb.append(prefix);
4649 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4650 (fullWakeLockTimeTotalMicros + 500) / 1000);
4651 pw.println(sb.toString());
4652 }
4653
4654 if (partialWakeLockTimeTotalMicros != 0) {
4655 sb.setLength(0);
4656 sb.append(prefix);
4657 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4658 (partialWakeLockTimeTotalMicros + 500) / 1000);
4659 pw.println(sb.toString());
4660 }
4661
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004662 final long multicastWakeLockTimeTotalMicros =
4663 getWifiMulticastWakelockTime(rawRealtime, which);
4664 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004665 if (multicastWakeLockTimeTotalMicros != 0) {
4666 sb.setLength(0);
4667 sb.append(prefix);
4668 sb.append(" Total WiFi Multicast wakelock Count: ");
4669 sb.append(multicastWakeLockCountTotal);
4670 pw.println(sb.toString());
4671
4672 sb.setLength(0);
4673 sb.append(prefix);
4674 sb.append(" Total WiFi Multicast wakelock time: ");
4675 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4676 pw.println(sb.toString());
4677 }
4678
Siddharth Ray3c648c42017-10-02 17:30:58 -07004679 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004680 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004681 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004682 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004683 sb.append(" CONNECTIVITY POWER SUMMARY START");
4684 pw.println(sb.toString());
4685
4686 pw.print(prefix);
4687 sb.setLength(0);
4688 sb.append(prefix);
4689 sb.append(" Logging duration for connectivity statistics: ");
4690 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004691 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004692
4693 sb.setLength(0);
4694 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004695 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004696 pw.println(sb.toString());
4697
Siddharth Ray3c648c42017-10-02 17:30:58 -07004698 pw.print(prefix);
4699 sb.setLength(0);
4700 sb.append(prefix);
4701 sb.append(" Cellular kernel active time: ");
4702 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4703 formatTimeMs(sb, mobileActiveTime / 1000);
4704 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4705 sb.append(")");
4706 pw.println(sb.toString());
4707
4708 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4709 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4710 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4711 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4712
Dianne Hackborn627bba72009-03-24 22:32:56 -07004713 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004714 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004715 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004716 didOne = false;
4717 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004718 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004719 if (time == 0) {
4720 continue;
4721 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004722 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004723 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004724 didOne = true;
4725 sb.append(DATA_CONNECTION_NAMES[i]);
4726 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004727 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004728 sb.append("(");
4729 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004730 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004731 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004732 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004733 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004734
4735 sb.setLength(0);
4736 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004737 sb.append(" Cellular Rx signal strength (RSRP):");
4738 final String[] cellularRxSignalStrengthDescription = new String[]{
4739 "very poor (less than -128dBm): ",
4740 "poor (-128dBm to -118dBm): ",
4741 "moderate (-118dBm to -108dBm): ",
4742 "good (-108dBm to -98dBm): ",
4743 "great (greater than -98dBm): "};
4744 didOne = false;
4745 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4746 cellularRxSignalStrengthDescription.length);
4747 for (int i=0; i<numCellularRxBins; i++) {
4748 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4749 if (time == 0) {
4750 continue;
4751 }
4752 sb.append("\n ");
4753 sb.append(prefix);
4754 didOne = true;
4755 sb.append(cellularRxSignalStrengthDescription[i]);
4756 sb.append(" ");
4757 formatTimeMs(sb, time/1000);
4758 sb.append("(");
4759 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4760 sb.append(") ");
4761 }
4762 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004763 pw.println(sb.toString());
4764
Siddharth Rayb50a6842017-12-14 15:15:28 -08004765 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004766 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004767
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004768 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004769 sb.setLength(0);
4770 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004771 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004772 pw.println(sb.toString());
4773
Siddharth Rayb50a6842017-12-14 15:15:28 -08004774 pw.print(prefix);
4775 sb.setLength(0);
4776 sb.append(prefix);
4777 sb.append(" Wifi kernel active time: ");
4778 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4779 formatTimeMs(sb, wifiActiveTime / 1000);
4780 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4781 sb.append(")");
4782 pw.println(sb.toString());
4783
Siddharth Ray3c648c42017-10-02 17:30:58 -07004784 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4785 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4786 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4787 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4788
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004789 sb.setLength(0);
4790 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004791 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004792 didOne = false;
4793 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004794 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004795 if (time == 0) {
4796 continue;
4797 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004798 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004799 didOne = true;
4800 sb.append(WIFI_STATE_NAMES[i]);
4801 sb.append(" ");
4802 formatTimeMs(sb, time/1000);
4803 sb.append("(");
4804 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4805 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004806 }
4807 if (!didOne) sb.append(" (no activity)");
4808 pw.println(sb.toString());
4809
4810 sb.setLength(0);
4811 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004812 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004813 didOne = false;
4814 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4815 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4816 if (time == 0) {
4817 continue;
4818 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004819 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004820 didOne = true;
4821 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4822 sb.append(" ");
4823 formatTimeMs(sb, time/1000);
4824 sb.append("(");
4825 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4826 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004827 }
4828 if (!didOne) sb.append(" (no activity)");
4829 pw.println(sb.toString());
4830
4831 sb.setLength(0);
4832 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004833 sb.append(" Wifi Rx signal strength (RSSI):");
4834 final String[] wifiRxSignalStrengthDescription = new String[]{
4835 "very poor (less than -88.75dBm): ",
4836 "poor (-88.75 to -77.5dBm): ",
4837 "moderate (-77.5dBm to -66.25dBm): ",
4838 "good (-66.25dBm to -55dBm): ",
4839 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004840 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004841 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4842 wifiRxSignalStrengthDescription.length);
4843 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004844 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4845 if (time == 0) {
4846 continue;
4847 }
4848 sb.append("\n ");
4849 sb.append(prefix);
4850 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004851 sb.append(" ");
4852 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004853 formatTimeMs(sb, time/1000);
4854 sb.append("(");
4855 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4856 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004857 }
4858 if (!didOne) sb.append(" (no activity)");
4859 pw.println(sb.toString());
4860
Siddharth Rayb50a6842017-12-14 15:15:28 -08004861 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4862 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004863
Adam Lesinski50e47602015-12-04 17:04:54 -08004864 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004865 sb.setLength(0);
4866 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004867 sb.append(" GPS Statistics:");
4868 pw.println(sb.toString());
4869
4870 sb.setLength(0);
4871 sb.append(prefix);
4872 sb.append(" GPS signal quality (Top 4 Average CN0):");
4873 final String[] gpsSignalQualityDescription = new String[]{
4874 "poor (less than 20 dBHz): ",
4875 "good (greater than 20 dBHz): "};
4876 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4877 gpsSignalQualityDescription.length);
4878 for (int i=0; i<numGpsSignalQualityBins; i++) {
4879 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4880 sb.append("\n ");
4881 sb.append(prefix);
4882 sb.append(" ");
4883 sb.append(gpsSignalQualityDescription[i]);
4884 formatTimeMs(sb, time/1000);
4885 sb.append("(");
4886 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4887 sb.append(") ");
4888 }
4889 pw.println(sb.toString());
4890
4891 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4892 if (gpsBatteryDrainMaMs > 0) {
4893 pw.print(prefix);
4894 sb.setLength(0);
4895 sb.append(prefix);
4896 sb.append(" Battery Drain (mAh): ");
4897 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4898 pw.println(sb.toString());
4899 }
4900
4901 pw.print(prefix);
4902 sb.setLength(0);
4903 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004904 sb.append(" CONNECTIVITY POWER SUMMARY END");
4905 pw.println(sb.toString());
4906 pw.println("");
4907
4908 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004909 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4910 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4911
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004912 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4913 sb.setLength(0);
4914 sb.append(prefix);
4915 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4916 pw.println(sb.toString());
4917
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004918 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4919 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004920
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004921 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004922
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004923 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004924 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004925 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004926 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004927 pw.println(getDischargeStartLevel());
4928 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4929 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004930 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004931 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004932 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004933 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004934 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004935 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004936 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004937 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004938 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004939 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004940 pw.println(getDischargeAmountScreenOff());
4941 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4942 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004943 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004944 } else {
4945 pw.print(prefix); pw.println(" Device battery use since last full charge");
4946 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004947 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004948 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004949 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004950 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004951 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004952 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004953 pw.println(getDischargeAmountScreenOffSinceCharge());
4954 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4955 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004956 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004957 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004958
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004959 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004960 helper.create(this);
4961 helper.refreshStats(which, UserHandle.USER_ALL);
4962 List<BatterySipper> sippers = helper.getUsageList();
4963 if (sippers != null && sippers.size() > 0) {
4964 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4965 pw.print(prefix); pw.print(" Capacity: ");
4966 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004967 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004968 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4969 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4970 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4971 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004972 pw.println();
4973 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004974 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004975 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004976 switch (bs.drainType) {
4977 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004978 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004979 break;
4980 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004981 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004982 break;
4983 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004984 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004985 break;
4986 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004987 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004988 break;
4989 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004990 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004991 break;
4992 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004993 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004994 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004995 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004996 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004997 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004998 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004999 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005000 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005001 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005002 break;
5003 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005004 pw.print(" User "); pw.print(bs.userId);
5005 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005006 break;
5007 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005008 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005009 break;
5010 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005011 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005012 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005013 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005014 pw.print(" Camera: ");
5015 break;
5016 default:
5017 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005018 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005019 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005020 printmAh(pw, bs.totalPowerMah);
5021
Adam Lesinski57123002015-06-12 16:12:07 -07005022 if (bs.usagePowerMah != bs.totalPowerMah) {
5023 // If the usage (generic power) isn't the whole amount, we list out
5024 // what components are involved in the calculation.
5025
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005026 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005027 if (bs.usagePowerMah != 0) {
5028 pw.print(" usage=");
5029 printmAh(pw, bs.usagePowerMah);
5030 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005031 if (bs.cpuPowerMah != 0) {
5032 pw.print(" cpu=");
5033 printmAh(pw, bs.cpuPowerMah);
5034 }
5035 if (bs.wakeLockPowerMah != 0) {
5036 pw.print(" wake=");
5037 printmAh(pw, bs.wakeLockPowerMah);
5038 }
5039 if (bs.mobileRadioPowerMah != 0) {
5040 pw.print(" radio=");
5041 printmAh(pw, bs.mobileRadioPowerMah);
5042 }
5043 if (bs.wifiPowerMah != 0) {
5044 pw.print(" wifi=");
5045 printmAh(pw, bs.wifiPowerMah);
5046 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005047 if (bs.bluetoothPowerMah != 0) {
5048 pw.print(" bt=");
5049 printmAh(pw, bs.bluetoothPowerMah);
5050 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005051 if (bs.gpsPowerMah != 0) {
5052 pw.print(" gps=");
5053 printmAh(pw, bs.gpsPowerMah);
5054 }
5055 if (bs.sensorPowerMah != 0) {
5056 pw.print(" sensor=");
5057 printmAh(pw, bs.sensorPowerMah);
5058 }
5059 if (bs.cameraPowerMah != 0) {
5060 pw.print(" camera=");
5061 printmAh(pw, bs.cameraPowerMah);
5062 }
5063 if (bs.flashlightPowerMah != 0) {
5064 pw.print(" flash=");
5065 printmAh(pw, bs.flashlightPowerMah);
5066 }
5067 pw.print(" )");
5068 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005069
5070 // If there is additional smearing information, include it.
5071 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5072 pw.print(" Including smearing: ");
5073 printmAh(pw, bs.totalSmearedPowerMah);
5074 pw.print(" (");
5075 if (bs.screenPowerMah != 0) {
5076 pw.print(" screen=");
5077 printmAh(pw, bs.screenPowerMah);
5078 }
5079 if (bs.proportionalSmearMah != 0) {
5080 pw.print(" proportional=");
5081 printmAh(pw, bs.proportionalSmearMah);
5082 }
5083 pw.print(" )");
5084 }
5085 if (bs.shouldHide) {
5086 pw.print(" Excluded from smearing");
5087 }
5088
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005089 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005090 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005091 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005092 }
5093
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005094 sippers = helper.getMobilemsppList();
5095 if (sippers != null && sippers.size() > 0) {
5096 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005097 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005098 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005099 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005100 sb.setLength(0);
5101 sb.append(prefix); sb.append(" Uid ");
5102 UserHandle.formatUid(sb, bs.uidObj.getUid());
5103 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5104 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5105 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005106 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005107 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005108 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005109 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005110 sb.setLength(0);
5111 sb.append(prefix);
5112 sb.append(" TOTAL TIME: ");
5113 formatTimeMs(sb, totalTime);
5114 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5115 sb.append(")");
5116 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005117 pw.println();
5118 }
5119
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005120 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5121 @Override
5122 public int compare(TimerEntry lhs, TimerEntry rhs) {
5123 long lhsTime = lhs.mTime;
5124 long rhsTime = rhs.mTime;
5125 if (lhsTime < rhsTime) {
5126 return 1;
5127 }
5128 if (lhsTime > rhsTime) {
5129 return -1;
5130 }
5131 return 0;
5132 }
5133 };
5134
5135 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005136 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5137 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005138 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005139 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5140 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5141 : kernelWakelocks.entrySet()) {
5142 final BatteryStats.Timer timer = ent.getValue();
5143 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005144 if (totalTimeMillis > 0) {
5145 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5146 }
5147 }
5148 if (ktimers.size() > 0) {
5149 Collections.sort(ktimers, timerComparator);
5150 pw.print(prefix); pw.println(" All kernel wake locks:");
5151 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005152 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005153 String linePrefix = ": ";
5154 sb.setLength(0);
5155 sb.append(prefix);
5156 sb.append(" Kernel Wake lock ");
5157 sb.append(timer.mName);
5158 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5159 which, linePrefix);
5160 if (!linePrefix.equals(": ")) {
5161 sb.append(" realtime");
5162 // Only print out wake locks that were held
5163 pw.println(sb.toString());
5164 }
5165 }
5166 pw.println();
5167 }
5168 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005169
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005170 if (timers.size() > 0) {
5171 Collections.sort(timers, timerComparator);
5172 pw.print(prefix); pw.println(" All partial wake locks:");
5173 for (int i=0; i<timers.size(); i++) {
5174 TimerEntry timer = timers.get(i);
5175 sb.setLength(0);
5176 sb.append(" Wake lock ");
5177 UserHandle.formatUid(sb, timer.mId);
5178 sb.append(" ");
5179 sb.append(timer.mName);
5180 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5181 sb.append(" realtime");
5182 pw.println(sb.toString());
5183 }
5184 timers.clear();
5185 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005186 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005188 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005189 if (wakeupReasons.size() > 0) {
5190 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005191 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005192 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005193 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005194 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5195 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005196 }
5197 Collections.sort(reasons, timerComparator);
5198 for (int i=0; i<reasons.size(); i++) {
5199 TimerEntry timer = reasons.get(i);
5200 String linePrefix = ": ";
5201 sb.setLength(0);
5202 sb.append(prefix);
5203 sb.append(" Wakeup reason ");
5204 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005205 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5206 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005207 pw.println(sb.toString());
5208 }
5209 pw.println();
5210 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005211 }
Evan Millar22ac0432009-03-31 11:33:18 -07005212
James Carr2dd7e5e2016-07-20 18:48:39 -07005213 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005214 if (mMemoryStats.size() > 0) {
5215 pw.println(" Memory Stats");
5216 for (int i = 0; i < mMemoryStats.size(); i++) {
5217 sb.setLength(0);
5218 sb.append(" Bandwidth ");
5219 sb.append(mMemoryStats.keyAt(i));
5220 sb.append(" Time ");
5221 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5222 pw.println(sb.toString());
5223 }
5224 pw.println();
5225 }
5226
5227 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5228 if (rpmStats.size() > 0) {
5229 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5230 if (rpmStats.size() > 0) {
5231 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5232 final String timerName = ent.getKey();
5233 final Timer timer = ent.getValue();
5234 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5235 }
5236 }
5237 pw.println();
5238 }
Bookatz82b341172017-09-07 19:06:08 -07005239 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5240 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005241 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005242 pw.print(prefix);
5243 pw.println(" Resource Power Manager Stats for when screen was off");
5244 if (screenOffRpmStats.size() > 0) {
5245 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5246 final String timerName = ent.getKey();
5247 final Timer timer = ent.getValue();
5248 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5249 }
Bookatz50df7112017-08-04 14:53:26 -07005250 }
Bookatz82b341172017-09-07 19:06:08 -07005251 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005252 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005253 }
5254
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005255 final long[] cpuFreqs = getCpuFreqs();
5256 if (cpuFreqs != null) {
5257 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005258 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005259 for (int i = 0; i < cpuFreqs.length; ++i) {
5260 sb.append(" " + cpuFreqs[i]);
5261 }
5262 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005263 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005264 }
5265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 for (int iu=0; iu<NU; iu++) {
5267 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005268 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005269 continue;
5270 }
Bookatzc8c44962017-05-11 12:12:54 -07005271
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005272 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005273
5274 pw.print(prefix);
5275 pw.print(" ");
5276 UserHandle.formatUid(pw, uid);
5277 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005279
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005280 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5281 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5282 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5283 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005284 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5285 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5286
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005287 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5288 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005289 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5290 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005291
5292 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5293 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5294
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005295 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5296 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5297 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005298 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5299 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5300 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5301 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005302 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005303
Adam Lesinski5f056f62016-07-14 16:56:08 -07005304 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5305 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5306
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005307 if (mobileRxBytes > 0 || mobileTxBytes > 0
5308 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005309 pw.print(prefix); pw.print(" Mobile network: ");
5310 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005311 pw.print(formatBytesLocked(mobileTxBytes));
5312 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5313 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005315 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5316 sb.setLength(0);
5317 sb.append(prefix); sb.append(" Mobile radio active: ");
5318 formatTimeMs(sb, uidMobileActiveTime / 1000);
5319 sb.append("(");
5320 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5321 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5322 long packets = mobileRxPackets + mobileTxPackets;
5323 if (packets == 0) {
5324 packets = 1;
5325 }
5326 sb.append(" @ ");
5327 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5328 sb.append(" mspp");
5329 pw.println(sb.toString());
5330 }
5331
Adam Lesinski5f056f62016-07-14 16:56:08 -07005332 if (mobileWakeup > 0) {
5333 sb.setLength(0);
5334 sb.append(prefix);
5335 sb.append(" Mobile radio AP wakeups: ");
5336 sb.append(mobileWakeup);
5337 pw.println(sb.toString());
5338 }
5339
Siddharth Rayb50a6842017-12-14 15:15:28 -08005340 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5341 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005342
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005343 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005344 pw.print(prefix); pw.print(" Wi-Fi network: ");
5345 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005346 pw.print(formatBytesLocked(wifiTxBytes));
5347 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5348 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005349 }
5350
Dianne Hackborn62793e42015-03-09 11:15:41 -07005351 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005352 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005353 || uidWifiRunningTime != 0) {
5354 sb.setLength(0);
5355 sb.append(prefix); sb.append(" Wifi Running: ");
5356 formatTimeMs(sb, uidWifiRunningTime / 1000);
5357 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5358 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005359 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005360 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5361 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5362 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005363 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005364 formatTimeMs(sb, wifiScanTime / 1000);
5365 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005366 whichBatteryRealtime)); sb.append(") ");
5367 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005368 sb.append("x\n");
5369 // actual and background times are unpooled and since reset (regardless of 'which')
5370 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5371 formatTimeMs(sb, wifiScanActualTime / 1000);
5372 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5373 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5374 sb.append(") ");
5375 sb.append(wifiScanCount);
5376 sb.append("x\n");
5377 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5378 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5379 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5380 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5381 sb.append(") ");
5382 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005383 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005384 pw.println(sb.toString());
5385 }
5386
Adam Lesinski5f056f62016-07-14 16:56:08 -07005387 if (wifiWakeup > 0) {
5388 sb.setLength(0);
5389 sb.append(prefix);
5390 sb.append(" WiFi AP wakeups: ");
5391 sb.append(wifiWakeup);
5392 pw.println(sb.toString());
5393 }
5394
Siddharth Rayb50a6842017-12-14 15:15:28 -08005395 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005396 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005397
Adam Lesinski50e47602015-12-04 17:04:54 -08005398 if (btRxBytes > 0 || btTxBytes > 0) {
5399 pw.print(prefix); pw.print(" Bluetooth network: ");
5400 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5401 pw.print(formatBytesLocked(btTxBytes));
5402 pw.println(" sent");
5403 }
5404
Bookatz867c0d72017-03-07 18:23:42 -08005405 final Timer bleTimer = u.getBluetoothScanTimer();
5406 if (bleTimer != null) {
5407 // Convert from microseconds to milliseconds with rounding
5408 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5409 / 1000;
5410 if (totalTimeMs != 0) {
5411 final int count = bleTimer.getCountLocked(which);
5412 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5413 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005414 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5415 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5416 final long actualTimeMsBg = bleTimerBg != null ?
5417 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005418 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005419 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5420 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005421 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5422 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5423 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5424 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5425 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5426 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5427 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5428 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5429 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5430 final Timer unoptimizedScanTimerBg =
5431 u.getBluetoothUnoptimizedScanBackgroundTimer();
5432 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5433 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5434 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5435 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005436
5437 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005438 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005439 sb.append(prefix);
5440 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005441 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005442 sb.append(" (");
5443 sb.append(count);
5444 sb.append(" times)");
5445 if (bleTimer.isRunningLocked()) {
5446 sb.append(" (currently running)");
5447 }
5448 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005449 }
Bookatzb1f04f32017-05-19 13:57:32 -07005450
5451 sb.append(prefix);
5452 sb.append(" Bluetooth Scan (total actual realtime): ");
5453 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5454 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005455 sb.append(count);
5456 sb.append(" times)");
5457 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005458 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005459 }
Bookatzb1f04f32017-05-19 13:57:32 -07005460 sb.append("\n");
5461 if (actualTimeMsBg > 0 || countBg > 0) {
5462 sb.append(prefix);
5463 sb.append(" Bluetooth Scan (background realtime): ");
5464 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5465 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005466 sb.append(countBg);
5467 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005468 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5469 sb.append(" (currently running in background)");
5470 }
5471 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005472 }
Bookatzb1f04f32017-05-19 13:57:32 -07005473
5474 sb.append(prefix);
5475 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005476 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005477 sb.append(" (");
5478 sb.append(resultCountBg);
5479 sb.append(" in background)");
5480
5481 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5482 sb.append("\n");
5483 sb.append(prefix);
5484 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5485 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5486 sb.append(" (max ");
5487 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5488 sb.append(")");
5489 if (unoptimizedScanTimer != null
5490 && unoptimizedScanTimer.isRunningLocked()) {
5491 sb.append(" (currently running unoptimized)");
5492 }
5493 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5494 sb.append("\n");
5495 sb.append(prefix);
5496 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5497 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5498 sb.append(" (max ");
5499 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5500 sb.append(")");
5501 if (unoptimizedScanTimerBg.isRunningLocked()) {
5502 sb.append(" (currently running unoptimized in background)");
5503 }
5504 }
5505 }
Bookatz867c0d72017-03-07 18:23:42 -08005506 pw.println(sb.toString());
5507 uidActivity = true;
5508 }
5509 }
5510
5511
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005512
Dianne Hackborn617f8772009-03-31 15:04:46 -07005513 if (u.hasUserActivity()) {
5514 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005515 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005516 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005517 if (val != 0) {
5518 if (!hasData) {
5519 sb.setLength(0);
5520 sb.append(" User activity: ");
5521 hasData = true;
5522 } else {
5523 sb.append(", ");
5524 }
5525 sb.append(val);
5526 sb.append(" ");
5527 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5528 }
5529 }
5530 if (hasData) {
5531 pw.println(sb.toString());
5532 }
5533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005535 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5536 = u.getWakelockStats();
5537 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005538 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005539 int countWakelock = 0;
5540 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5541 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5542 String linePrefix = ": ";
5543 sb.setLength(0);
5544 sb.append(prefix);
5545 sb.append(" Wake lock ");
5546 sb.append(wakelocks.keyAt(iw));
5547 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5548 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005549 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5550 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005551 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005552 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5553 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005554 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5555 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005556 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5557 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005558 sb.append(" realtime");
5559 pw.println(sb.toString());
5560 uidActivity = true;
5561 countWakelock++;
5562
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005563 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5564 rawRealtime, which);
5565 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5566 rawRealtime, which);
5567 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5568 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005569 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005570 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005571 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005572 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005573 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5574 // pooled and therefore just a lower bound)
5575 long actualTotalPartialWakelock = 0;
5576 long actualBgPartialWakelock = 0;
5577 if (u.getAggregatedPartialWakelockTimer() != null) {
5578 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5579 // Convert from microseconds to milliseconds with rounding
5580 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005581 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005582 final Timer bgAggTimer = aggTimer.getSubTimer();
5583 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005584 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005585 }
5586
5587 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5588 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5589 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005590 sb.setLength(0);
5591 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005592 sb.append(" TOTAL wake: ");
5593 boolean needComma = false;
5594 if (totalFullWakelock != 0) {
5595 needComma = true;
5596 formatTimeMs(sb, totalFullWakelock);
5597 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005598 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005599 if (totalPartialWakelock != 0) {
5600 if (needComma) {
5601 sb.append(", ");
5602 }
5603 needComma = true;
5604 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005605 sb.append("blamed partial");
5606 }
5607 if (actualTotalPartialWakelock != 0) {
5608 if (needComma) {
5609 sb.append(", ");
5610 }
5611 needComma = true;
5612 formatTimeMs(sb, actualTotalPartialWakelock);
5613 sb.append("actual partial");
5614 }
5615 if (actualBgPartialWakelock != 0) {
5616 if (needComma) {
5617 sb.append(", ");
5618 }
5619 needComma = true;
5620 formatTimeMs(sb, actualBgPartialWakelock);
5621 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005622 }
5623 if (totalWindowWakelock != 0) {
5624 if (needComma) {
5625 sb.append(", ");
5626 }
5627 needComma = true;
5628 formatTimeMs(sb, totalWindowWakelock);
5629 sb.append("window");
5630 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005631 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005632 if (needComma) {
5633 sb.append(",");
5634 }
5635 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005636 formatTimeMs(sb, totalDrawWakelock);
5637 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005638 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005639 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005640 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005641 }
5642 }
5643
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005644 // Calculate multicast wakelock stats
5645 final Timer mcTimer = u.getMulticastWakelockStats();
5646 if (mcTimer != null) {
5647 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5648 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5649
5650 if (multicastWakeLockTimeMicros > 0) {
5651 sb.setLength(0);
5652 sb.append(prefix);
5653 sb.append(" WiFi Multicast Wakelock");
5654 sb.append(" count = ");
5655 sb.append(multicastWakeLockCount);
5656 sb.append(" time = ");
5657 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5658 pw.println(sb.toString());
5659 }
5660 }
5661
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005662 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5663 for (int isy=syncs.size()-1; isy>=0; isy--) {
5664 final Timer timer = syncs.valueAt(isy);
5665 // Convert from microseconds to milliseconds with rounding
5666 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5667 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005668 final Timer bgTimer = timer.getSubTimer();
5669 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005670 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005671 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005672 sb.setLength(0);
5673 sb.append(prefix);
5674 sb.append(" Sync ");
5675 sb.append(syncs.keyAt(isy));
5676 sb.append(": ");
5677 if (totalTime != 0) {
5678 formatTimeMs(sb, totalTime);
5679 sb.append("realtime (");
5680 sb.append(count);
5681 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005682 if (bgTime > 0) {
5683 sb.append(", ");
5684 formatTimeMs(sb, bgTime);
5685 sb.append("background (");
5686 sb.append(bgCount);
5687 sb.append(" times)");
5688 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005689 } else {
5690 sb.append("(not used)");
5691 }
5692 pw.println(sb.toString());
5693 uidActivity = true;
5694 }
5695
5696 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5697 for (int ij=jobs.size()-1; ij>=0; ij--) {
5698 final Timer timer = jobs.valueAt(ij);
5699 // Convert from microseconds to milliseconds with rounding
5700 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5701 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005702 final Timer bgTimer = timer.getSubTimer();
5703 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005704 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005705 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005706 sb.setLength(0);
5707 sb.append(prefix);
5708 sb.append(" Job ");
5709 sb.append(jobs.keyAt(ij));
5710 sb.append(": ");
5711 if (totalTime != 0) {
5712 formatTimeMs(sb, totalTime);
5713 sb.append("realtime (");
5714 sb.append(count);
5715 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005716 if (bgTime > 0) {
5717 sb.append(", ");
5718 formatTimeMs(sb, bgTime);
5719 sb.append("background (");
5720 sb.append(bgCount);
5721 sb.append(" times)");
5722 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005723 } else {
5724 sb.append("(not used)");
5725 }
5726 pw.println(sb.toString());
5727 uidActivity = true;
5728 }
5729
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005730 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5731 for (int ic=completions.size()-1; ic>=0; ic--) {
5732 SparseIntArray types = completions.valueAt(ic);
5733 if (types != null) {
5734 pw.print(prefix);
5735 pw.print(" Job Completions ");
5736 pw.print(completions.keyAt(ic));
5737 pw.print(":");
5738 for (int it=0; it<types.size(); it++) {
5739 pw.print(" ");
5740 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5741 pw.print("(");
5742 pw.print(types.valueAt(it));
5743 pw.print("x)");
5744 }
5745 pw.println();
5746 }
5747 }
5748
Amith Yamasani977e11f2018-02-16 11:29:54 -08005749 u.getDeferredJobsLineLocked(sb, which);
5750 if (sb.length() > 0) {
5751 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5752 }
5753
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005754 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5755 prefix, "Flashlight");
5756 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5757 prefix, "Camera");
5758 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5759 prefix, "Video");
5760 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5761 prefix, "Audio");
5762
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005763 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5764 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005765 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005766 final Uid.Sensor se = sensors.valueAt(ise);
5767 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005768 sb.setLength(0);
5769 sb.append(prefix);
5770 sb.append(" Sensor ");
5771 int handle = se.getHandle();
5772 if (handle == Uid.Sensor.GPS) {
5773 sb.append("GPS");
5774 } else {
5775 sb.append(handle);
5776 }
5777 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005779 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005780 if (timer != null) {
5781 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005782 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5783 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005784 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005785 final Timer bgTimer = se.getSensorBackgroundTime();
5786 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005787 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5788 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5789 final long bgActualTime = bgTimer != null ?
5790 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5791
Dianne Hackborn61659e52014-07-09 16:13:01 -07005792 //timer.logState();
5793 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005794 if (actualTime != totalTime) {
5795 formatTimeMs(sb, totalTime);
5796 sb.append("blamed realtime, ");
5797 }
5798
5799 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005800 sb.append("realtime (");
5801 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005802 sb.append(" times)");
5803
5804 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005805 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005806 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5807 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005808 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005809 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 } else {
5812 sb.append("(not used)");
5813 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005814 } else {
5815 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005817
5818 pw.println(sb.toString());
5819 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005822 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5823 "Vibrator");
5824 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5825 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005826 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5827 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005828
Dianne Hackborn61659e52014-07-09 16:13:01 -07005829 long totalStateTime = 0;
5830 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5831 long time = u.getProcessStateTime(ips, rawRealtime, which);
5832 if (time > 0) {
5833 totalStateTime += time;
5834 sb.setLength(0);
5835 sb.append(prefix);
5836 sb.append(" ");
5837 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5838 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005839 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005840 pw.println(sb.toString());
5841 uidActivity = true;
5842 }
5843 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005844 if (totalStateTime > 0) {
5845 sb.setLength(0);
5846 sb.append(prefix);
5847 sb.append(" Total running: ");
5848 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5849 pw.println(sb.toString());
5850 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005851
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005852 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5853 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005854 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005855 sb.setLength(0);
5856 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005857 sb.append(" Total cpu time: u=");
5858 formatTimeMs(sb, userCpuTimeUs / 1000);
5859 sb.append("s=");
5860 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005861 pw.println(sb.toString());
5862 }
5863
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005864 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5865 if (cpuFreqTimes != null) {
5866 sb.setLength(0);
5867 sb.append(" Total cpu time per freq:");
5868 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5869 sb.append(" " + cpuFreqTimes[i]);
5870 }
5871 pw.println(sb.toString());
5872 }
5873 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5874 if (screenOffCpuFreqTimes != null) {
5875 sb.setLength(0);
5876 sb.append(" Total screen-off cpu time per freq:");
5877 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5878 sb.append(" " + screenOffCpuFreqTimes[i]);
5879 }
5880 pw.println(sb.toString());
5881 }
5882
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005883 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5884 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5885 if (cpuTimes != null) {
5886 sb.setLength(0);
5887 sb.append(" Cpu times per freq at state "
5888 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5889 for (int i = 0; i < cpuTimes.length; ++i) {
5890 sb.append(" " + cpuTimes[i]);
5891 }
5892 pw.println(sb.toString());
5893 }
5894
5895 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5896 if (screenOffCpuTimes != null) {
5897 sb.setLength(0);
5898 sb.append(" Screen-off cpu times per freq at state "
5899 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5900 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5901 sb.append(" " + screenOffCpuTimes[i]);
5902 }
5903 pw.println(sb.toString());
5904 }
5905 }
5906
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005907 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5908 = u.getProcessStats();
5909 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5910 final Uid.Proc ps = processStats.valueAt(ipr);
5911 long userTime;
5912 long systemTime;
5913 long foregroundTime;
5914 int starts;
5915 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005917 userTime = ps.getUserTime(which);
5918 systemTime = ps.getSystemTime(which);
5919 foregroundTime = ps.getForegroundTime(which);
5920 starts = ps.getStarts(which);
5921 final int numCrashes = ps.getNumCrashes(which);
5922 final int numAnrs = ps.getNumAnrs(which);
5923 numExcessive = which == STATS_SINCE_CHARGED
5924 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005926 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5927 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5928 sb.setLength(0);
5929 sb.append(prefix); sb.append(" Proc ");
5930 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5931 sb.append(prefix); sb.append(" CPU: ");
5932 formatTimeMs(sb, userTime); sb.append("usr + ");
5933 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5934 formatTimeMs(sb, foregroundTime); sb.append("fg");
5935 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5936 sb.append("\n"); sb.append(prefix); sb.append(" ");
5937 boolean hasOne = false;
5938 if (starts != 0) {
5939 hasOne = true;
5940 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005941 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005942 if (numCrashes != 0) {
5943 if (hasOne) {
5944 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005945 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005946 hasOne = true;
5947 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005948 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005949 if (numAnrs != 0) {
5950 if (hasOne) {
5951 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005953 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 }
5955 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005956 pw.println(sb.toString());
5957 for (int e=0; e<numExcessive; e++) {
5958 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5959 if (ew != null) {
5960 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005961 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005962 pw.print("cpu");
5963 } else {
5964 pw.print("unknown");
5965 }
5966 pw.print(" use: ");
5967 TimeUtils.formatDuration(ew.usedTime, pw);
5968 pw.print(" over ");
5969 TimeUtils.formatDuration(ew.overTime, pw);
5970 if (ew.overTime != 0) {
5971 pw.print(" (");
5972 pw.print((ew.usedTime*100)/ew.overTime);
5973 pw.println("%)");
5974 }
5975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 }
5977 uidActivity = true;
5978 }
5979 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005980
5981 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5982 = u.getPackageStats();
5983 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5984 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5985 pw.println(":");
5986 boolean apkActivity = false;
5987 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5988 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5989 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5990 pw.print(prefix); pw.print(" Wakeup alarm ");
5991 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5992 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5993 pw.println(" times");
5994 apkActivity = true;
5995 }
5996 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5997 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5998 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5999 final long startTime = ss.getStartTime(batteryUptime, which);
6000 final int starts = ss.getStarts(which);
6001 final int launches = ss.getLaunches(which);
6002 if (startTime != 0 || starts != 0 || launches != 0) {
6003 sb.setLength(0);
6004 sb.append(prefix); sb.append(" Service ");
6005 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6006 sb.append(prefix); sb.append(" Created for: ");
6007 formatTimeMs(sb, startTime / 1000);
6008 sb.append("uptime\n");
6009 sb.append(prefix); sb.append(" Starts: ");
6010 sb.append(starts);
6011 sb.append(", launches: "); sb.append(launches);
6012 pw.println(sb.toString());
6013 apkActivity = true;
6014 }
6015 }
6016 if (!apkActivity) {
6017 pw.print(prefix); pw.println(" (nothing executed)");
6018 }
6019 uidActivity = true;
6020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006022 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 }
6024 }
6025 }
6026
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006027 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006028 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006029 int diff = oldval ^ newval;
6030 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006031 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006032 for (int i=0; i<descriptions.length; i++) {
6033 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006034 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006035 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006036 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006037 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006038 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006039 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6040 didWake = true;
6041 pw.print("=");
6042 if (longNames) {
6043 UserHandle.formatUid(pw, wakelockTag.uid);
6044 pw.print(":\"");
6045 pw.print(wakelockTag.string);
6046 pw.print("\"");
6047 } else {
6048 pw.print(wakelockTag.poolIdx);
6049 }
6050 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006051 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006052 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006053 pw.print("=");
6054 int val = (newval&bd.mask)>>bd.shift;
6055 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006056 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006057 } else {
6058 pw.print(val);
6059 }
6060 }
6061 }
6062 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006063 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07006064 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006065 if (longNames) {
6066 UserHandle.formatUid(pw, wakelockTag.uid);
6067 pw.print(":\"");
6068 pw.print(wakelockTag.string);
6069 pw.print("\"");
6070 } else {
6071 pw.print(wakelockTag.poolIdx);
6072 }
6073 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006074 }
Mike Mac2f518a2017-09-19 16:06:03 -07006075
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006076 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006077 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006078 }
6079
6080 public static class HistoryPrinter {
6081 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006082 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006083 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006084 int oldStatus = -1;
6085 int oldHealth = -1;
6086 int oldPlug = -1;
6087 int oldTemp = -1;
6088 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006089 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006090 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006091
Dianne Hackborn3251b902014-06-20 14:40:53 -07006092 void reset() {
6093 oldState = oldState2 = 0;
6094 oldLevel = -1;
6095 oldStatus = -1;
6096 oldHealth = -1;
6097 oldPlug = -1;
6098 oldTemp = -1;
6099 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006100 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006101 }
6102
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006103 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006104 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006105 if (!checkin) {
6106 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006107 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006108 pw.print(" (");
6109 pw.print(rec.numReadInts);
6110 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006111 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006112 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6113 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006114 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006115 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006116 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006117 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006118 }
6119 lastTime = rec.time;
6120 }
6121 if (rec.cmd == HistoryItem.CMD_START) {
6122 if (checkin) {
6123 pw.print(":");
6124 }
6125 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006126 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006127 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6128 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006129 if (checkin) {
6130 pw.print(":");
6131 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006132 if (rec.cmd == HistoryItem.CMD_RESET) {
6133 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006134 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006135 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006136 pw.print("TIME:");
6137 if (checkin) {
6138 pw.println(rec.currentTime);
6139 } else {
6140 pw.print(" ");
6141 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6142 rec.currentTime).toString());
6143 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006144 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6145 if (checkin) {
6146 pw.print(":");
6147 }
6148 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006149 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6150 if (checkin) {
6151 pw.print(":");
6152 }
6153 pw.println("*OVERFLOW*");
6154 } else {
6155 if (!checkin) {
6156 if (rec.batteryLevel < 10) pw.print("00");
6157 else if (rec.batteryLevel < 100) pw.print("0");
6158 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006159 if (verbose) {
6160 pw.print(" ");
6161 if (rec.states < 0) ;
6162 else if (rec.states < 0x10) pw.print("0000000");
6163 else if (rec.states < 0x100) pw.print("000000");
6164 else if (rec.states < 0x1000) pw.print("00000");
6165 else if (rec.states < 0x10000) pw.print("0000");
6166 else if (rec.states < 0x100000) pw.print("000");
6167 else if (rec.states < 0x1000000) pw.print("00");
6168 else if (rec.states < 0x10000000) pw.print("0");
6169 pw.print(Integer.toHexString(rec.states));
6170 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006171 } else {
6172 if (oldLevel != rec.batteryLevel) {
6173 oldLevel = rec.batteryLevel;
6174 pw.print(",Bl="); pw.print(rec.batteryLevel);
6175 }
6176 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006177 if (oldStatus != rec.batteryStatus) {
6178 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006179 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006180 switch (oldStatus) {
6181 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006182 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006183 break;
6184 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006185 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006186 break;
6187 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006188 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006189 break;
6190 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006191 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006192 break;
6193 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006194 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006195 break;
6196 default:
6197 pw.print(oldStatus);
6198 break;
6199 }
6200 }
6201 if (oldHealth != rec.batteryHealth) {
6202 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006203 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006204 switch (oldHealth) {
6205 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006206 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006207 break;
6208 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006209 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006210 break;
6211 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006212 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006213 break;
6214 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006215 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006216 break;
6217 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006218 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006219 break;
6220 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006221 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006222 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006223 case BatteryManager.BATTERY_HEALTH_COLD:
6224 pw.print(checkin ? "c" : "cold");
6225 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006226 default:
6227 pw.print(oldHealth);
6228 break;
6229 }
6230 }
6231 if (oldPlug != rec.batteryPlugType) {
6232 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006233 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006234 switch (oldPlug) {
6235 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006236 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006237 break;
6238 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006239 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006240 break;
6241 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006242 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006243 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006244 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006245 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006246 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006247 default:
6248 pw.print(oldPlug);
6249 break;
6250 }
6251 }
6252 if (oldTemp != rec.batteryTemperature) {
6253 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006254 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 pw.print(oldTemp);
6256 }
6257 if (oldVolt != rec.batteryVoltage) {
6258 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006259 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006260 pw.print(oldVolt);
6261 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006262 final int chargeMAh = rec.batteryChargeUAh / 1000;
6263 if (oldChargeMAh != chargeMAh) {
6264 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006265 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006266 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006267 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006268 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006269 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006270 printBitDescriptions(pw, oldState2, rec.states2, null,
6271 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006272 if (rec.wakeReasonTag != null) {
6273 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006274 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006275 pw.print(rec.wakeReasonTag.poolIdx);
6276 } else {
6277 pw.print(" wake_reason=");
6278 pw.print(rec.wakeReasonTag.uid);
6279 pw.print(":\"");
6280 pw.print(rec.wakeReasonTag.string);
6281 pw.print("\"");
6282 }
6283 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006284 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006285 pw.print(checkin ? "," : " ");
6286 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6287 pw.print("+");
6288 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6289 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006290 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006291 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6292 : HISTORY_EVENT_NAMES;
6293 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6294 | HistoryItem.EVENT_FLAG_FINISH);
6295 if (idx >= 0 && idx < eventNames.length) {
6296 pw.print(eventNames[idx]);
6297 } else {
6298 pw.print(checkin ? "Ev" : "event");
6299 pw.print(idx);
6300 }
6301 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006302 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006303 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006304 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006305 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6306 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006307 pw.print(":\"");
6308 pw.print(rec.eventTag.string);
6309 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006310 }
6311 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006312 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006313 if (rec.stepDetails != null) {
6314 if (!checkin) {
6315 pw.print(" Details: cpu=");
6316 pw.print(rec.stepDetails.userTime);
6317 pw.print("u+");
6318 pw.print(rec.stepDetails.systemTime);
6319 pw.print("s");
6320 if (rec.stepDetails.appCpuUid1 >= 0) {
6321 pw.print(" (");
6322 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6323 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6324 if (rec.stepDetails.appCpuUid2 >= 0) {
6325 pw.print(", ");
6326 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6327 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6328 }
6329 if (rec.stepDetails.appCpuUid3 >= 0) {
6330 pw.print(", ");
6331 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6332 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6333 }
6334 pw.print(')');
6335 }
6336 pw.println();
6337 pw.print(" /proc/stat=");
6338 pw.print(rec.stepDetails.statUserTime);
6339 pw.print(" usr, ");
6340 pw.print(rec.stepDetails.statSystemTime);
6341 pw.print(" sys, ");
6342 pw.print(rec.stepDetails.statIOWaitTime);
6343 pw.print(" io, ");
6344 pw.print(rec.stepDetails.statIrqTime);
6345 pw.print(" irq, ");
6346 pw.print(rec.stepDetails.statSoftIrqTime);
6347 pw.print(" sirq, ");
6348 pw.print(rec.stepDetails.statIdlTime);
6349 pw.print(" idle");
6350 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6351 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6352 + rec.stepDetails.statSoftIrqTime;
6353 int total = totalRun + rec.stepDetails.statIdlTime;
6354 if (total > 0) {
6355 pw.print(" (");
6356 float perc = ((float)totalRun) / ((float)total) * 100;
6357 pw.print(String.format("%.1f%%", perc));
6358 pw.print(" of ");
6359 StringBuilder sb = new StringBuilder(64);
6360 formatTimeMsNoSpace(sb, total*10);
6361 pw.print(sb);
6362 pw.print(")");
6363 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006364 pw.print(", PlatformIdleStat ");
6365 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006366 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006367
6368 pw.print(", SubsystemPowerState ");
6369 pw.print(rec.stepDetails.statSubsystemPowerState);
6370 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006371 } else {
6372 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6373 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6374 pw.print(rec.stepDetails.userTime);
6375 pw.print(":");
6376 pw.print(rec.stepDetails.systemTime);
6377 if (rec.stepDetails.appCpuUid1 >= 0) {
6378 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6379 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6380 if (rec.stepDetails.appCpuUid2 >= 0) {
6381 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6382 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6383 }
6384 if (rec.stepDetails.appCpuUid3 >= 0) {
6385 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6386 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6387 }
6388 }
6389 pw.println();
6390 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6391 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6392 pw.print(rec.stepDetails.statUserTime);
6393 pw.print(',');
6394 pw.print(rec.stepDetails.statSystemTime);
6395 pw.print(',');
6396 pw.print(rec.stepDetails.statIOWaitTime);
6397 pw.print(',');
6398 pw.print(rec.stepDetails.statIrqTime);
6399 pw.print(',');
6400 pw.print(rec.stepDetails.statSoftIrqTime);
6401 pw.print(',');
6402 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006403 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006404 if (rec.stepDetails.statPlatformIdleState != null) {
6405 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006406 if (rec.stepDetails.statSubsystemPowerState != null) {
6407 pw.print(',');
6408 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006409 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006410
6411 if (rec.stepDetails.statSubsystemPowerState != null) {
6412 pw.print(rec.stepDetails.statSubsystemPowerState);
6413 }
6414 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006415 }
6416 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006417 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006418 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006419 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006420 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006421
6422 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6423 UserHandle.formatUid(pw, uid);
6424 pw.print("=");
6425 pw.print(utime);
6426 pw.print("u+");
6427 pw.print(stime);
6428 pw.print("s");
6429 }
6430
6431 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6432 pw.print('/');
6433 pw.print(uid);
6434 pw.print(":");
6435 pw.print(utime);
6436 pw.print(":");
6437 pw.print(stime);
6438 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006439 }
6440
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006441 private void printSizeValue(PrintWriter pw, long size) {
6442 float result = size;
6443 String suffix = "";
6444 if (result >= 10*1024) {
6445 suffix = "KB";
6446 result = result / 1024;
6447 }
6448 if (result >= 10*1024) {
6449 suffix = "MB";
6450 result = result / 1024;
6451 }
6452 if (result >= 10*1024) {
6453 suffix = "GB";
6454 result = result / 1024;
6455 }
6456 if (result >= 10*1024) {
6457 suffix = "TB";
6458 result = result / 1024;
6459 }
6460 if (result >= 10*1024) {
6461 suffix = "PB";
6462 result = result / 1024;
6463 }
6464 pw.print((int)result);
6465 pw.print(suffix);
6466 }
6467
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006468 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6469 String label3, long estimatedTime) {
6470 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006471 return false;
6472 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006473 pw.print(label1);
6474 pw.print(label2);
6475 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006476 StringBuilder sb = new StringBuilder(64);
6477 formatTimeMs(sb, estimatedTime);
6478 pw.print(sb);
6479 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006480 return true;
6481 }
6482
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006483 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6484 LevelStepTracker steps, boolean checkin) {
6485 if (steps == null) {
6486 return false;
6487 }
6488 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006489 if (count <= 0) {
6490 return false;
6491 }
6492 if (!checkin) {
6493 pw.println(header);
6494 }
Kweku Adams030980a2015-04-01 16:07:48 -07006495 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006496 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006497 long duration = steps.getDurationAt(i);
6498 int level = steps.getLevelAt(i);
6499 long initMode = steps.getInitModeAt(i);
6500 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006501 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006502 lineArgs[0] = Long.toString(duration);
6503 lineArgs[1] = Integer.toString(level);
6504 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6505 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6506 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6507 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6508 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6509 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006510 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006511 }
6512 } else {
6513 lineArgs[2] = "";
6514 }
6515 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6516 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6517 } else {
6518 lineArgs[3] = "";
6519 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006520 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006521 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006522 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006523 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006524 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006525 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6526 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006527 pw.print(prefix);
6528 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006529 TimeUtils.formatDuration(duration, pw);
6530 pw.print(" to "); pw.print(level);
6531 boolean haveModes = false;
6532 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6533 pw.print(" (");
6534 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6535 case Display.STATE_OFF: pw.print("screen-off"); break;
6536 case Display.STATE_ON: pw.print("screen-on"); break;
6537 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6538 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006539 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006540 }
6541 haveModes = true;
6542 }
6543 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6544 pw.print(haveModes ? ", " : " (");
6545 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6546 ? "power-save-on" : "power-save-off");
6547 haveModes = true;
6548 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006549 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6550 pw.print(haveModes ? ", " : " (");
6551 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6552 ? "device-idle-on" : "device-idle-off");
6553 haveModes = true;
6554 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006555 if (haveModes) {
6556 pw.print(")");
6557 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006558 pw.println();
6559 }
6560 }
6561 return true;
6562 }
6563
Kweku Adams87b19ec2017-10-09 12:40:03 -07006564 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6565 LevelStepTracker steps) {
6566 if (steps == null) {
6567 return;
6568 }
6569 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006570 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006571 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006572 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6573 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6574
6575 final long initMode = steps.getInitModeAt(i);
6576 final long modMode = steps.getModModeAt(i);
6577
6578 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6579 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6580 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6581 case Display.STATE_OFF:
6582 ds = SystemProto.BatteryLevelStep.DS_OFF;
6583 break;
6584 case Display.STATE_ON:
6585 ds = SystemProto.BatteryLevelStep.DS_ON;
6586 break;
6587 case Display.STATE_DOZE:
6588 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6589 break;
6590 case Display.STATE_DOZE_SUSPEND:
6591 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6592 break;
6593 default:
6594 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6595 break;
6596 }
6597 }
6598 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6599
6600 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6601 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6602 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6603 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6604 }
6605 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6606
6607 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6608 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6609 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6610 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6611 }
6612 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6613
6614 proto.end(token);
6615 }
6616 }
6617
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006618 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006619 public static final int DUMP_DAILY_ONLY = 1<<2;
6620 public static final int DUMP_HISTORY_ONLY = 1<<3;
6621 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6622 public static final int DUMP_VERBOSE = 1<<5;
6623 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006624
Dianne Hackborn37de0982014-05-09 09:32:18 -07006625 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6626 final HistoryPrinter hprinter = new HistoryPrinter();
6627 final HistoryItem rec = new HistoryItem();
6628 long lastTime = -1;
6629 long baseTime = -1;
6630 boolean printed = false;
6631 HistoryEventTracker tracker = null;
6632 while (getNextHistoryLocked(rec)) {
6633 lastTime = rec.time;
6634 if (baseTime < 0) {
6635 baseTime = lastTime;
6636 }
6637 if (rec.time >= histStart) {
6638 if (histStart >= 0 && !printed) {
6639 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006640 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006641 || rec.cmd == HistoryItem.CMD_START
6642 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006643 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006644 hprinter.printNextItem(pw, rec, baseTime, checkin,
6645 (flags&DUMP_VERBOSE) != 0);
6646 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006647 } else if (rec.currentTime != 0) {
6648 printed = true;
6649 byte cmd = rec.cmd;
6650 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006651 hprinter.printNextItem(pw, rec, baseTime, checkin,
6652 (flags&DUMP_VERBOSE) != 0);
6653 rec.cmd = cmd;
6654 }
6655 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006656 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6657 hprinter.printNextItem(pw, rec, baseTime, checkin,
6658 (flags&DUMP_VERBOSE) != 0);
6659 rec.cmd = HistoryItem.CMD_UPDATE;
6660 }
6661 int oldEventCode = rec.eventCode;
6662 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006663 rec.eventTag = new HistoryTag();
6664 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6665 HashMap<String, SparseIntArray> active
6666 = tracker.getStateForEvent(i);
6667 if (active == null) {
6668 continue;
6669 }
6670 for (HashMap.Entry<String, SparseIntArray> ent
6671 : active.entrySet()) {
6672 SparseIntArray uids = ent.getValue();
6673 for (int j=0; j<uids.size(); j++) {
6674 rec.eventCode = i;
6675 rec.eventTag.string = ent.getKey();
6676 rec.eventTag.uid = uids.keyAt(j);
6677 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006678 hprinter.printNextItem(pw, rec, baseTime, checkin,
6679 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006680 rec.wakeReasonTag = null;
6681 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006682 }
6683 }
6684 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006685 rec.eventCode = oldEventCode;
6686 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006687 tracker = null;
6688 }
6689 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006690 hprinter.printNextItem(pw, rec, baseTime, checkin,
6691 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006692 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6693 // This is an attempt to aggregate the previous state and generate
6694 // fake events to reflect that state at the point where we start
6695 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006696 if (tracker == null) {
6697 tracker = new HistoryEventTracker();
6698 }
6699 tracker.updateState(rec.eventCode, rec.eventTag.string,
6700 rec.eventTag.uid, rec.eventTag.poolIdx);
6701 }
6702 }
6703 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006704 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006705 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6706 }
6707 }
6708
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006709 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6710 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6711 if (steps == null) {
6712 return;
6713 }
6714 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6715 if (timeRemaining >= 0) {
6716 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6717 tmpSb.setLength(0);
6718 formatTimeMs(tmpSb, timeRemaining);
6719 pw.print(tmpSb);
6720 pw.print(" (from "); pw.print(tmpOutInt[0]);
6721 pw.println(" steps)");
6722 }
6723 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6724 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6725 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6726 if (estimatedTime > 0) {
6727 pw.print(prefix); pw.print(label); pw.print(" ");
6728 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6729 pw.print(" time: ");
6730 tmpSb.setLength(0);
6731 formatTimeMs(tmpSb, estimatedTime);
6732 pw.print(tmpSb);
6733 pw.print(" (from "); pw.print(tmpOutInt[0]);
6734 pw.println(" steps)");
6735 }
6736 }
6737 }
6738
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006739 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6740 ArrayList<PackageChange> changes) {
6741 if (changes == null) {
6742 return;
6743 }
6744 pw.print(prefix); pw.println("Package changes:");
6745 for (int i=0; i<changes.size(); i++) {
6746 PackageChange pc = changes.get(i);
6747 if (pc.mUpdate) {
6748 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6749 pw.print(" vers="); pw.println(pc.mVersionCode);
6750 } else {
6751 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6752 }
6753 }
6754 }
6755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 /**
6757 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6758 *
6759 * @param pw a Printer to receive the dump output.
6760 */
6761 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006762 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006763 prepareForDumpLocked();
6764
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006765 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006766 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006767
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006768 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006769 final long historyTotalSize = getHistoryTotalSize();
6770 final long historyUsedSize = getHistoryUsedSize();
6771 if (startIteratingHistoryLocked()) {
6772 try {
6773 pw.print("Battery History (");
6774 pw.print((100*historyUsedSize)/historyTotalSize);
6775 pw.print("% used, ");
6776 printSizeValue(pw, historyUsedSize);
6777 pw.print(" used of ");
6778 printSizeValue(pw, historyTotalSize);
6779 pw.print(", ");
6780 pw.print(getHistoryStringPoolSize());
6781 pw.print(" strings using ");
6782 printSizeValue(pw, getHistoryStringPoolBytes());
6783 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006784 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006785 pw.println();
6786 } finally {
6787 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006788 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006789 }
6790
6791 if (startIteratingOldHistoryLocked()) {
6792 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006793 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006794 pw.println("Old battery History:");
6795 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006796 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006797 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006798 if (baseTime < 0) {
6799 baseTime = rec.time;
6800 }
6801 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006802 }
6803 pw.println();
6804 } finally {
6805 finishIteratingOldHistoryLocked();
6806 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006807 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006808 }
6809
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006810 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006811 return;
6812 }
6813
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006814 if (!filtering) {
6815 SparseArray<? extends Uid> uidStats = getUidStats();
6816 final int NU = uidStats.size();
6817 boolean didPid = false;
6818 long nowRealtime = SystemClock.elapsedRealtime();
6819 for (int i=0; i<NU; i++) {
6820 Uid uid = uidStats.valueAt(i);
6821 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6822 if (pids != null) {
6823 for (int j=0; j<pids.size(); j++) {
6824 Uid.Pid pid = pids.valueAt(j);
6825 if (!didPid) {
6826 pw.println("Per-PID Stats:");
6827 didPid = true;
6828 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006829 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6830 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006831 pw.print(" PID "); pw.print(pids.keyAt(j));
6832 pw.print(" wake time: ");
6833 TimeUtils.formatDuration(time, pw);
6834 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006835 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006836 }
6837 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006838 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006839 pw.println();
6840 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006841 }
6842
6843 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006844 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6845 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006846 long timeRemaining = computeBatteryTimeRemaining(
6847 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006848 if (timeRemaining >= 0) {
6849 pw.print(" Estimated discharge time remaining: ");
6850 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6851 pw.println();
6852 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006853 final LevelStepTracker steps = getDischargeLevelStepTracker();
6854 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6855 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6856 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6857 STEP_LEVEL_MODE_VALUES[i], null));
6858 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006859 pw.println();
6860 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006861 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6862 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006863 long timeRemaining = computeChargeTimeRemaining(
6864 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006865 if (timeRemaining >= 0) {
6866 pw.print(" Estimated charge time remaining: ");
6867 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6868 pw.println();
6869 }
6870 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006871 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006872 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006873 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006874 pw.println("Daily stats:");
6875 pw.print(" Current start time: ");
6876 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6877 getCurrentDailyStartTime()).toString());
6878 pw.print(" Next min deadline: ");
6879 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6880 getNextMinDailyDeadline()).toString());
6881 pw.print(" Next max deadline: ");
6882 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6883 getNextMaxDailyDeadline()).toString());
6884 StringBuilder sb = new StringBuilder(64);
6885 int[] outInt = new int[1];
6886 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6887 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006888 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6889 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006890 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006891 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6892 dsteps, false)) {
6893 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6894 sb, outInt);
6895 }
6896 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6897 csteps, false)) {
6898 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6899 sb, outInt);
6900 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006901 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006902 } else {
6903 pw.println(" Current daily steps:");
6904 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6905 sb, outInt);
6906 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6907 sb, outInt);
6908 }
6909 }
6910 DailyItem dit;
6911 int curIndex = 0;
6912 while ((dit=getDailyItemLocked(curIndex)) != null) {
6913 curIndex++;
6914 if ((flags&DUMP_DAILY_ONLY) != 0) {
6915 pw.println();
6916 }
6917 pw.print(" Daily from ");
6918 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6919 pw.print(" to ");
6920 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6921 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006922 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006923 if (dumpDurationSteps(pw, " ",
6924 " Discharge step durations:", dit.mDischargeSteps, false)) {
6925 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6926 sb, outInt);
6927 }
6928 if (dumpDurationSteps(pw, " ",
6929 " Charge step durations:", dit.mChargeSteps, false)) {
6930 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6931 sb, outInt);
6932 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006933 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006934 } else {
6935 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6936 sb, outInt);
6937 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6938 sb, outInt);
6939 }
6940 }
6941 pw.println();
6942 }
6943 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006944 pw.println("Statistics since last charge:");
6945 pw.println(" System starts: " + getStartCount()
6946 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006947 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6948 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006949 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 }
Mike Mac2f518a2017-09-19 16:06:03 -07006952
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006953 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006955 public void dumpCheckinLocked(Context context, PrintWriter pw,
6956 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006957 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006958
6959 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006960 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6961 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006962
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006963 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6964
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006965 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006966 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006967 try {
6968 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6969 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6970 pw.print(HISTORY_STRING_POOL); pw.print(',');
6971 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006972 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006973 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006974 pw.print(",\"");
6975 String str = getHistoryTagPoolString(i);
6976 str = str.replace("\\", "\\\\");
6977 str = str.replace("\"", "\\\"");
6978 pw.print(str);
6979 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006980 pw.println();
6981 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006982 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006983 } finally {
6984 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006985 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006986 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006987 }
6988
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006989 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006990 return;
6991 }
6992
Dianne Hackborne4a59512010-12-07 11:08:07 -08006993 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006994 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006995 for (int i=0; i<apps.size(); i++) {
6996 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006997 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6998 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006999 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007000 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7001 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007002 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007003 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007004 }
7005 SparseArray<? extends Uid> uidStats = getUidStats();
7006 final int NU = uidStats.size();
7007 String[] lineArgs = new String[2];
7008 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007009 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7010 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7011 if (pkgs != null && !pkgs.second.value) {
7012 pkgs.second.value = true;
7013 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007014 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007015 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007016 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7017 (Object[])lineArgs);
7018 }
7019 }
7020 }
7021 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007022 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007023 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007024 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007025 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007026 if (timeRemaining >= 0) {
7027 lineArgs[0] = Long.toString(timeRemaining);
7028 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7029 (Object[])lineArgs);
7030 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007031 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007032 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007033 if (timeRemaining >= 0) {
7034 lineArgs[0] = Long.toString(timeRemaining);
7035 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7036 (Object[])lineArgs);
7037 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007038 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7039 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007042
Kweku Adams87b19ec2017-10-09 12:40:03 -07007043 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007044 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08007045 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007046 final ProtoOutputStream proto = new ProtoOutputStream(fd);
7047 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7048 prepareForDumpLocked();
7049
7050 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7051 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7052 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7053 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7054
Kweku Adams6ccebf22017-12-11 12:30:35 -08007055 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007056
7057 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007058 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7059 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7060 helper.create(this);
7061 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7062
7063 dumpProtoAppsLocked(proto, helper, apps);
7064 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007065 }
7066
7067 proto.end(bToken);
7068 proto.flush();
7069 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007070
Kweku Adams103351f2017-10-16 14:39:34 -07007071 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7072 List<ApplicationInfo> apps) {
7073 final int which = STATS_SINCE_CHARGED;
7074 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7075 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7076 final long rawRealtimeUs = rawRealtimeMs * 1000;
7077 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7078
7079 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7080 if (apps != null) {
7081 for (int i = 0; i < apps.size(); ++i) {
7082 ApplicationInfo ai = apps.get(i);
7083 int aid = UserHandle.getAppId(ai.uid);
7084 ArrayList<String> pkgs = aidToPackages.get(aid);
7085 if (pkgs == null) {
7086 pkgs = new ArrayList<String>();
7087 aidToPackages.put(aid, pkgs);
7088 }
7089 pkgs.add(ai.packageName);
7090 }
7091 }
7092
7093 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7094 final List<BatterySipper> sippers = helper.getUsageList();
7095 if (sippers != null) {
7096 for (int i = 0; i < sippers.size(); ++i) {
7097 final BatterySipper bs = sippers.get(i);
7098 if (bs.drainType != BatterySipper.DrainType.APP) {
7099 // Others are handled by dumpProtoSystemLocked()
7100 continue;
7101 }
7102 uidToSipper.put(bs.uidObj.getUid(), bs);
7103 }
7104 }
7105
7106 SparseArray<? extends Uid> uidStats = getUidStats();
7107 final int n = uidStats.size();
7108 for (int iu = 0; iu < n; ++iu) {
7109 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7110 final Uid u = uidStats.valueAt(iu);
7111
7112 final int uid = uidStats.keyAt(iu);
7113 proto.write(UidProto.UID, uid);
7114
7115 // Print packages and apk stats (UID_DATA & APK_DATA)
7116 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7117 if (pkgs == null) {
7118 pkgs = new ArrayList<String>();
7119 }
7120 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7121 u.getPackageStats();
7122 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7123 String pkg = packageStats.keyAt(ipkg);
7124 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7125 packageStats.valueAt(ipkg).getServiceStats();
7126 if (serviceStats.size() == 0) {
7127 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7128 // example, "android") may be included in the packageStats that aren't part of
7129 // the UID. If they don't have any services, then they shouldn't be listed here.
7130 // These packages won't be a part in the pkgs List.
7131 continue;
7132 }
7133
7134 final long pToken = proto.start(UidProto.PACKAGES);
7135 proto.write(UidProto.Package.NAME, pkg);
7136 // Remove from the packages list since we're logging it here.
7137 pkgs.remove(pkg);
7138
7139 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7140 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007141
7142 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7143 final int starts = ss.getStarts(which);
7144 final int launches = ss.getLaunches(which);
7145 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7146 continue;
7147 }
7148
Kweku Adams103351f2017-10-16 14:39:34 -07007149 long sToken = proto.start(UidProto.Package.SERVICES);
7150
7151 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007152 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7153 proto.write(UidProto.Package.Service.START_COUNT, starts);
7154 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007155
7156 proto.end(sToken);
7157 }
7158 proto.end(pToken);
7159 }
7160 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7161 // from PackageManager data. Packages are only included in packageStats if there was
7162 // specific data tracked for them (services and wakeup alarms, etc.).
7163 for (String p : pkgs) {
7164 final long pToken = proto.start(UidProto.PACKAGES);
7165 proto.write(UidProto.Package.NAME, p);
7166 proto.end(pToken);
7167 }
7168
7169 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7170 if (u.getAggregatedPartialWakelockTimer() != null) {
7171 final Timer timer = u.getAggregatedPartialWakelockTimer();
7172 // Times are since reset (regardless of 'which')
7173 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7174 final Timer bgTimer = timer.getSubTimer();
7175 final long bgTimeMs = bgTimer != null
7176 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7177 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7178 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7179 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7180 proto.end(awToken);
7181 }
7182
7183 // Audio (AUDIO_DATA)
7184 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7185
7186 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7187 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7188 u.getBluetoothControllerActivity(), which);
7189
7190 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7191 final Timer bleTimer = u.getBluetoothScanTimer();
7192 if (bleTimer != null) {
7193 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7194
7195 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7196 rawRealtimeUs, which);
7197 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7198 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7199 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7200 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7201 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7202 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7203 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7204 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7205 // Result counters
7206 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7207 u.getBluetoothScanResultCounter() != null
7208 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7209 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7210 u.getBluetoothScanResultBgCounter() != null
7211 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7212
7213 proto.end(bmToken);
7214 }
7215
7216 // Camera (CAMERA_DATA)
7217 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7218
7219 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7220 final long cpuToken = proto.start(UidProto.CPU);
7221 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7222 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7223
7224 final long[] cpuFreqs = getCpuFreqs();
7225 if (cpuFreqs != null) {
7226 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7227 // If total cpuFreqTimes is null, then we don't need to check for
7228 // screenOffCpuFreqTimes.
7229 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7230 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7231 if (screenOffCpuFreqTimeMs == null) {
7232 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7233 }
7234 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7235 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7236 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7237 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7238 cpuFreqTimeMs[ic]);
7239 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7240 screenOffCpuFreqTimeMs[ic]);
7241 proto.end(cToken);
7242 }
7243 }
7244 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007245
7246 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7247 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7248 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7249 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7250 if (screenOffTimesMs == null) {
7251 screenOffTimesMs = new long[timesMs.length];
7252 }
7253 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7254 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7255 for (int ic = 0; ic < timesMs.length; ++ic) {
7256 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7257 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7258 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7259 timesMs[ic]);
7260 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7261 screenOffTimesMs[ic]);
7262 proto.end(cToken);
7263 }
7264 proto.end(procToken);
7265 }
7266 }
Kweku Adams103351f2017-10-16 14:39:34 -07007267 proto.end(cpuToken);
7268
7269 // Flashlight (FLASHLIGHT_DATA)
7270 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7271 rawRealtimeUs, which);
7272
7273 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7274 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7275 rawRealtimeUs, which);
7276
7277 // Foreground service (FOREGROUND_SERVICE_DATA)
7278 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7279 rawRealtimeUs, which);
7280
7281 // Job completion (JOB_COMPLETION_DATA)
7282 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7283 final int[] reasons = new int[]{
7284 JobParameters.REASON_CANCELED,
7285 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7286 JobParameters.REASON_PREEMPT,
7287 JobParameters.REASON_TIMEOUT,
7288 JobParameters.REASON_DEVICE_IDLE,
7289 };
7290 for (int ic = 0; ic < completions.size(); ++ic) {
7291 SparseIntArray types = completions.valueAt(ic);
7292 if (types != null) {
7293 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7294
7295 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7296
7297 for (int r : reasons) {
7298 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7299 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7300 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7301 proto.end(rToken);
7302 }
7303
7304 proto.end(jcToken);
7305 }
7306 }
7307
7308 // Scheduled jobs (JOB_DATA)
7309 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7310 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7311 final Timer timer = jobs.valueAt(ij);
7312 final Timer bgTimer = timer.getSubTimer();
7313 final long jToken = proto.start(UidProto.JOBS);
7314
7315 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7316 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7317 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7318 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7319
7320 proto.end(jToken);
7321 }
7322
7323 // Modem Controller (MODEM_CONTROLLER_DATA)
7324 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7325 u.getModemControllerActivity(), which);
7326
7327 // Network stats (NETWORK_DATA)
7328 final long nToken = proto.start(UidProto.NETWORK);
7329 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7330 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7331 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7332 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7333 proto.write(UidProto.Network.WIFI_BYTES_RX,
7334 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7335 proto.write(UidProto.Network.WIFI_BYTES_TX,
7336 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7337 proto.write(UidProto.Network.BT_BYTES_RX,
7338 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7339 proto.write(UidProto.Network.BT_BYTES_TX,
7340 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7341 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7342 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7343 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7344 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7345 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7346 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7347 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7348 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7349 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7350 roundUsToMs(u.getMobileRadioActiveTime(which)));
7351 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7352 u.getMobileRadioActiveCount(which));
7353 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7354 u.getMobileRadioApWakeupCount(which));
7355 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7356 u.getWifiRadioApWakeupCount(which));
7357 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7358 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7359 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7360 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7361 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7362 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7363 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7364 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7365 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7366 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7367 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7368 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7369 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7370 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7371 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7372 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7373 proto.end(nToken);
7374
7375 // Power use item (POWER_USE_ITEM_DATA)
7376 BatterySipper bs = uidToSipper.get(uid);
7377 if (bs != null) {
7378 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7379 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7380 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7381 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7382 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7383 bs.proportionalSmearMah);
7384 proto.end(bsToken);
7385 }
7386
7387 // Processes (PROCESS_DATA)
7388 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7389 u.getProcessStats();
7390 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7391 final Uid.Proc ps = processStats.valueAt(ipr);
7392 final long prToken = proto.start(UidProto.PROCESS);
7393
7394 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7395 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7396 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7397 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7398 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7399 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7400 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7401
7402 proto.end(prToken);
7403 }
7404
7405 // Sensors (SENSOR_DATA)
7406 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7407 for (int ise = 0; ise < sensors.size(); ++ise) {
7408 final Uid.Sensor se = sensors.valueAt(ise);
7409 final Timer timer = se.getSensorTime();
7410 if (timer == null) {
7411 continue;
7412 }
7413 final Timer bgTimer = se.getSensorBackgroundTime();
7414 final int sensorNumber = sensors.keyAt(ise);
7415 final long seToken = proto.start(UidProto.SENSORS);
7416
7417 proto.write(UidProto.Sensor.ID, sensorNumber);
7418 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7419 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7420 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7421
7422 proto.end(seToken);
7423 }
7424
7425 // State times (STATE_TIME_DATA)
7426 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7427 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7428 if (durMs == 0) {
7429 continue;
7430 }
7431 final long stToken = proto.start(UidProto.STATES);
7432 proto.write(UidProto.StateTime.STATE, ips);
7433 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7434 proto.end(stToken);
7435 }
7436
7437 // Syncs (SYNC_DATA)
7438 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7439 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7440 final Timer timer = syncs.valueAt(isy);
7441 final Timer bgTimer = timer.getSubTimer();
7442 final long syToken = proto.start(UidProto.SYNCS);
7443
7444 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7445 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7446 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7447 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7448
7449 proto.end(syToken);
7450 }
7451
7452 // User activity (USER_ACTIVITY_DATA)
7453 if (u.hasUserActivity()) {
7454 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7455 int val = u.getUserActivityCount(i, which);
7456 if (val != 0) {
7457 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7458 proto.write(UidProto.UserActivity.NAME, i);
7459 proto.write(UidProto.UserActivity.COUNT, val);
7460 proto.end(uaToken);
7461 }
7462 }
7463 }
7464
7465 // Vibrator (VIBRATOR_DATA)
7466 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7467
7468 // Video (VIDEO_DATA)
7469 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7470
7471 // Wakelocks (WAKELOCK_DATA)
7472 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7473 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7474 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7475 final long wToken = proto.start(UidProto.WAKELOCKS);
7476 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7477 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7478 rawRealtimeUs, which);
7479 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7480 if (pTimer != null) {
7481 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7482 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7483 rawRealtimeUs, which);
7484 }
7485 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7486 rawRealtimeUs, which);
7487 proto.end(wToken);
7488 }
7489
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007490 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7491 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7492 rawRealtimeUs, which);
7493
Kweku Adams103351f2017-10-16 14:39:34 -07007494 // Wakeup alarms (WAKEUP_ALARM_DATA)
7495 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7496 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7497 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7498 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7499 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7500 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7501 proto.write(UidProto.WakeupAlarm.COUNT,
7502 alarms.valueAt(iwa).getCountLocked(which));
7503 proto.end(waToken);
7504 }
7505 }
7506
7507 // Wifi Controller (WIFI_CONTROLLER_DATA)
7508 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7509 u.getWifiControllerActivity(), which);
7510
7511 // Wifi data (WIFI_DATA)
7512 final long wToken = proto.start(UidProto.WIFI);
7513 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7514 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7515 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7516 rawRealtimeUs, which);
7517 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7518 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7519 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7520 rawRealtimeUs, which);
7521 proto.end(wToken);
7522
7523 proto.end(uTkn);
7524 }
7525 }
7526
7527 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007528 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7529 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7530 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7531 final long rawRealtimeUs = rawRealtimeMs * 1000;
7532 final int which = STATS_SINCE_CHARGED;
7533
7534 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007535 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007536 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7537 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7538 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7539 computeRealtime(rawRealtimeUs, which) / 1000);
7540 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7541 computeUptime(rawUptimeUs, which) / 1000);
7542 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7543 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7544 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7545 computeBatteryUptime(rawUptimeUs, which) / 1000);
7546 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7547 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7548 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7549 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7550 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7551 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7552 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7553 getEstimatedBatteryCapacity());
7554 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7555 getMinLearnedBatteryCapacity());
7556 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7557 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007558 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007559
7560 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007561 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007562 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7563 getLowDischargeAmountSinceCharge());
7564 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7565 getHighDischargeAmountSinceCharge());
7566 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7567 getDischargeAmountScreenOnSinceCharge());
7568 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7569 getDischargeAmountScreenOffSinceCharge());
7570 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7571 getDischargeAmountScreenDozeSinceCharge());
7572 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7573 getUahDischarge(which) / 1000);
7574 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7575 getUahDischargeScreenOff(which) / 1000);
7576 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7577 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007578 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7579 getUahDischargeLightDoze(which) / 1000);
7580 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7581 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007582 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007583
7584 // Time remaining
7585 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007586 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007587 if (timeRemainingUs >= 0) {
7588 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7589 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7590 } else {
7591 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7592 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7593 if (timeRemainingUs >= 0) {
7594 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7595 } else {
7596 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7597 }
7598 }
7599
7600 // Charge step (CHARGE_STEP_DATA)
7601 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7602
7603 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7604 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007605 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
7606 boolean isNone = (i == DATA_CONNECTION_NONE);
7607 int telephonyNetworkType = i;
7608 if (i == DATA_CONNECTION_OTHER) {
7609 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7610 }
Kweku Adams103351f2017-10-16 14:39:34 -07007611 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007612 if (isNone) {
7613 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7614 } else {
7615 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7616 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007617 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7618 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007619 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007620 }
7621
7622 // Discharge step (DISCHARGE_STEP_DATA)
7623 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7624
7625 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7626 final long[] cpuFreqs = getCpuFreqs();
7627 if (cpuFreqs != null) {
7628 for (long i : cpuFreqs) {
7629 proto.write(SystemProto.CPU_FREQUENCY, i);
7630 }
7631 }
7632
7633 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7634 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7635 getBluetoothControllerActivity(), which);
7636
7637 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7638 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7639 getModemControllerActivity(), which);
7640
7641 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007642 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007643 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7644 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7645 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7646 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7647 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7648 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7649 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7650 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7651 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7652 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7653 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7654 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7655 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7656 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7657 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7658 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7659 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7660 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7661 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7662 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007663 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007664
7665 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7666 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7667 getWifiControllerActivity(), which);
7668
7669
7670 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007671 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007672 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7673 getWifiOnTime(rawRealtimeUs, which) / 1000);
7674 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7675 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007676 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007677
7678 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7679 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7680 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007681 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007682 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7683 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7684 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007685 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007686 }
7687
7688 // Misc (MISC_DATA)
7689 // Calculate wakelock times across all uids.
7690 long fullWakeLockTimeTotalUs = 0;
7691 long partialWakeLockTimeTotalUs = 0;
7692
7693 final SparseArray<? extends Uid> uidStats = getUidStats();
7694 for (int iu = 0; iu < uidStats.size(); iu++) {
7695 final Uid u = uidStats.valueAt(iu);
7696
7697 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7698 u.getWakelockStats();
7699 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7700 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7701
7702 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7703 if (fullWakeTimer != null) {
7704 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7705 which);
7706 }
7707
7708 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7709 if (partialWakeTimer != null) {
7710 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7711 rawRealtimeUs, which);
7712 }
7713 }
7714 }
Kweku Adams103351f2017-10-16 14:39:34 -07007715 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007716 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7717 getScreenOnTime(rawRealtimeUs, which) / 1000);
7718 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7719 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7720 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7721 fullWakeLockTimeTotalUs / 1000);
7722 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7723 partialWakeLockTimeTotalUs / 1000);
7724 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7725 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7726 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7727 getMobileRadioActiveAdjustedTime(which) / 1000);
7728 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7729 getMobileRadioActiveCount(which));
7730 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7731 getMobileRadioActiveUnknownTime(which) / 1000);
7732 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7733 getInteractiveTime(rawRealtimeUs, which) / 1000);
7734 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7735 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7736 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7737 getNumConnectivityChange(which));
7738 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7739 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7740 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7741 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7742 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7743 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7744 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7745 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7746 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7747 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7748 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7749 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7750 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7751 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7752 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7753 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7754 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7755 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7756 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7757 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007758 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007759
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007760 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007761 final long multicastWakeLockTimeTotalUs =
7762 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7763 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007764 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7765 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7766 multicastWakeLockTimeTotalUs / 1000);
7767 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7768 multicastWakeLockCountTotal);
7769 proto.end(wmctToken);
7770
Kweku Adams87b19ec2017-10-09 12:40:03 -07007771 // Power use item (POWER_USE_ITEM_DATA)
7772 final List<BatterySipper> sippers = helper.getUsageList();
7773 if (sippers != null) {
7774 for (int i = 0; i < sippers.size(); ++i) {
7775 final BatterySipper bs = sippers.get(i);
7776 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7777 int uid = 0;
7778 switch (bs.drainType) {
7779 case IDLE:
7780 n = SystemProto.PowerUseItem.IDLE;
7781 break;
7782 case CELL:
7783 n = SystemProto.PowerUseItem.CELL;
7784 break;
7785 case PHONE:
7786 n = SystemProto.PowerUseItem.PHONE;
7787 break;
7788 case WIFI:
7789 n = SystemProto.PowerUseItem.WIFI;
7790 break;
7791 case BLUETOOTH:
7792 n = SystemProto.PowerUseItem.BLUETOOTH;
7793 break;
7794 case SCREEN:
7795 n = SystemProto.PowerUseItem.SCREEN;
7796 break;
7797 case FLASHLIGHT:
7798 n = SystemProto.PowerUseItem.FLASHLIGHT;
7799 break;
7800 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007801 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007802 continue;
7803 case USER:
7804 n = SystemProto.PowerUseItem.USER;
7805 uid = UserHandle.getUid(bs.userId, 0);
7806 break;
7807 case UNACCOUNTED:
7808 n = SystemProto.PowerUseItem.UNACCOUNTED;
7809 break;
7810 case OVERCOUNTED:
7811 n = SystemProto.PowerUseItem.OVERCOUNTED;
7812 break;
7813 case CAMERA:
7814 n = SystemProto.PowerUseItem.CAMERA;
7815 break;
7816 case MEMORY:
7817 n = SystemProto.PowerUseItem.MEMORY;
7818 break;
7819 }
Kweku Adams103351f2017-10-16 14:39:34 -07007820 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007821 proto.write(SystemProto.PowerUseItem.NAME, n);
7822 proto.write(SystemProto.PowerUseItem.UID, uid);
7823 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7824 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7825 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7826 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7827 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007828 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007829 }
7830 }
7831
7832 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007833 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007834 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7835 helper.getPowerProfile().getBatteryCapacity());
7836 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7837 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7838 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007839 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007840
7841 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7842 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7843 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7844 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007845 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007846 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7847 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7848 ent.getValue(), rawRealtimeUs, which);
7849 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7850 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007851 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007852 }
7853
7854 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7855 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007856 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007857 proto.write(SystemProto.ScreenBrightness.NAME, i);
7858 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7859 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007860 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007861 }
7862
7863 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7864 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7865 which);
7866
7867 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7868 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007869 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007870 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7871 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7872 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007873 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007874 }
7875
7876 // Wakeup reasons (WAKEUP_REASON_DATA)
7877 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7878 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007879 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007880 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7881 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007882 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007883 }
7884
7885 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7886 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007887 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007888 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7889 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7890 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007891 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007892 }
7893
7894 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7895 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007896 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007897 proto.write(SystemProto.WifiState.NAME, i);
7898 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7899 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007900 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007901 }
7902
7903 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7904 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007905 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007906 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7907 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7908 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007909 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007910 }
7911
7912 proto.end(sToken);
7913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914}