blob: 7500707fd22869f841830200e5686b9c348a6f54 [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;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080026import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070027import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070028import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070029import android.util.MutableBoolean;
30import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070033import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070034import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070035import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070036import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080037
Sudheer Shankab2f83c12017-11-13 19:25:01 -080038import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080039import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080040import com.android.internal.os.BatterySipper;
41import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Kweku Adams2f73ecd2017-09-27 16:59:19 -070043import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070044import java.io.PrintWriter;
45import java.util.ArrayList;
46import java.util.Collections;
47import java.util.Comparator;
48import java.util.Formatter;
49import java.util.HashMap;
50import java.util.List;
51import java.util.Map;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
54 * A class providing access to battery usage statistics, including information on
55 * wakelocks, processes, packages, and services. All times are represented in microseconds
56 * except where indicated otherwise.
57 * @hide
58 */
59public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070060 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070063 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
64 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070065
66 /** @hide */
67 public static final String SERVICE_NAME = "batterystats";
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * A constant indicating a partial wake lock timer.
71 */
72 public static final int WAKE_TYPE_PARTIAL = 0;
73
74 /**
75 * A constant indicating a full wake lock timer.
76 */
77 public static final int WAKE_TYPE_FULL = 1;
78
79 /**
80 * A constant indicating a window wake lock timer.
81 */
82 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
87 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070088
The Android Open Source Project10592532009-03-18 17:39:46 -070089 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070090 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070091 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070092 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070093
Dianne Hackborn617f8772009-03-31 15:04:46 -070094 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070095 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070096 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070097 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070098
The Android Open Source Project10592532009-03-18 17:39:46 -070099 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700100 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700101 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700102 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Dianne Hackborn62793e42015-03-09 11:15:41 -0700104 /**
105 * A constant indicating a wifi multicast timer
106 */
107 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700110 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700111 */
112 public static final int VIDEO_TURNED_ON = 8;
113
114 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800115 * A constant indicating a vibrator on timer
116 */
117 public static final int VIBRATOR_ON = 9;
118
119 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700120 * A constant indicating a foreground activity timer
121 */
122 public static final int FOREGROUND_ACTIVITY = 10;
123
124 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700125 * A constant indicating a wifi batched scan is active
126 */
127 public static final int WIFI_BATCHED_SCAN = 11;
128
129 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700130 * A constant indicating a process state timer
131 */
132 public static final int PROCESS_STATE = 12;
133
134 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700135 * A constant indicating a sync timer
136 */
137 public static final int SYNC = 13;
138
139 /**
140 * A constant indicating a job timer
141 */
142 public static final int JOB = 14;
143
144 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800145 * A constant indicating an audio turn on timer
146 */
147 public static final int AUDIO_TURNED_ON = 15;
148
149 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700150 * A constant indicating a flashlight turn on timer
151 */
152 public static final int FLASHLIGHT_TURNED_ON = 16;
153
154 /**
155 * A constant indicating a camera turn on timer
156 */
157 public static final int CAMERA_TURNED_ON = 17;
158
159 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700160 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700161 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700162 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700163
164 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800165 * A constant indicating a bluetooth scan timer.
166 */
167 public static final int BLUETOOTH_SCAN_ON = 19;
168
169 /**
Bookatzc8c44962017-05-11 12:12:54 -0700170 * A constant indicating an aggregated partial wake lock timer.
171 */
172 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
173
174 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700175 * A constant indicating a bluetooth scan timer for unoptimized scans.
176 */
177 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
178
179 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700180 * A constant indicating a foreground service timer
181 */
182 public static final int FOREGROUND_SERVICE = 22;
183
184 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800185 * A constant indicating an aggregate wifi multicast timer
186 */
187 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
188
189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * Include all of the data in the stats, including previously saved data.
191 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700192 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * Include only the current run in the stats.
196 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700197 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 /**
200 * Include only the run since the last time the device was unplugged in the stats.
201 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700202 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700203
204 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700205 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700206 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700207 private static final String[] STAT_NAMES = { "l", "c", "u" };
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700210 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700211 *
212 * New in version 19:
213 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800214 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700215 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700216 * New in version 21:
217 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700218 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700219 * - BLE scan result count
220 * - CPU frequency time per uid
221 * New in version 22:
222 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700223 * - Background partial wakelock time & count
224 * New in version 23:
225 * - Logging smeared power model values
226 * New in version 24:
227 * - Fixed bugs in background timers and BLE scan time
228 * New in version 25:
229 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700230 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700231 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700232 * New in version 27:
233 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800234 * New in version 28:
235 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700236 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800237 * New in version 29:
238 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
239 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800240 * New in version 30:
241 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
242 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800243 * New in version 31:
244 * - New cellular network types.
245 * - Deferred job metrics.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700246 */
Kweku Adamsb78430e2018-02-20 18:06:09 -0800247 static final int CHECKIN_VERSION = 31;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700248
249 /**
250 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700252 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700253
Evan Millar22ac0432009-03-31 11:33:18 -0700254 private static final long BYTES_PER_KB = 1024;
255 private static final long BYTES_PER_MB = 1048576; // 1024^2
256 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700257
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700258 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800259 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700260 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700262 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700263 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700264 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
265 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700266 // rpm line is:
267 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
268 // screen-off time, screen-off count
269 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700270 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800271 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700272 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
273 // fgs line is:
274 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
275 // foreground service time, count
276 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700277 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700278 // wl line is:
279 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700280 // full totalTime, 'f', count, current duration, max duration, total duration,
281 // partial totalTime, 'p', count, current duration, max duration, total duration,
282 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
283 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700284 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700285 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700286 // awl line is:
287 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
288 // cumulative partial wakelock duration, cumulative background partial wakelock duration
289 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700290 private static final String SYNC_DATA = "sy";
291 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700292 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800293
294 /**
295 * jbd line is:
296 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800297 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
298 * 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 -0800299 * <p>
300 * @see #JOB_FRESHNESS_BUCKETS
301 */
302 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700303 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700304 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700305 private static final String NETWORK_DATA = "nt";
306 private static final String USER_ACTIVITY_DATA = "ua";
307 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800308 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700309 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700310 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700311 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800312 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
313 private static final String WIFI_CONTROLLER_DATA = "wfcd";
314 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
315 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700316 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700317 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800318 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800319 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
320 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800321 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700322 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700323 private static final String SCREEN_BRIGHTNESS_DATA = "br";
324 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700325 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700326 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
327 private static final String DATA_CONNECTION_TIME_DATA = "dct";
328 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800329 private static final String WIFI_STATE_TIME_DATA = "wst";
330 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700331 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
332 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
333 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
334 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800335 private static final String POWER_USE_SUMMARY_DATA = "pws";
336 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700337 private static final String DISCHARGE_STEP_DATA = "dsd";
338 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700339 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
340 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700341 private static final String FLASHLIGHT_DATA = "fla";
342 private static final String CAMERA_DATA = "cam";
343 private static final String VIDEO_DATA = "vid";
344 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700345 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
346 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
Adam Lesinski010bf372016-04-11 12:18:18 -0700348 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
349
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700350 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 private final Formatter mFormatter = new Formatter(mFormatBuilder);
352
Siddharth Rayb50a6842017-12-14 15:15:28 -0800353 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
354 private static final String WIFI_CONTROLLER_NAME = "WiFi";
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700357 * Indicates times spent by the uid at each cpu frequency in all process states.
358 *
359 * Other types might include times spent in foreground, background etc.
360 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800361 @VisibleForTesting
362 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700363
364 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800365 * These are the thresholds for bucketing last time since a job was run for an app
366 * 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 -0800367 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800368 * on.
369 */
370 public static final long[] JOB_FRESHNESS_BUCKETS = {
371 1 * 60 * 60 * 1000L,
372 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800373 4 * 60 * 60 * 1000L,
374 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800375 Long.MAX_VALUE
376 };
377
378 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700379 * State for keeping track of counting information.
380 */
381 public static abstract class Counter {
382
383 /**
384 * Returns the count associated with this Counter for the
385 * selected type of statistics.
386 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700387 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700388 */
Evan Millarc64edde2009-04-18 12:26:32 -0700389 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700390
391 /**
392 * Temporary for debugging.
393 */
394 public abstract void logState(Printer pw, String prefix);
395 }
396
397 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700398 * State for keeping track of long counting information.
399 */
400 public static abstract class LongCounter {
401
402 /**
403 * Returns the count associated with this Counter for the
404 * selected type of statistics.
405 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700406 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700407 */
408 public abstract long getCountLocked(int which);
409
410 /**
411 * Temporary for debugging.
412 */
413 public abstract void logState(Printer pw, String prefix);
414 }
415
416 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700417 * State for keeping track of array of long counting information.
418 */
419 public static abstract class LongCounterArray {
420 /**
421 * Returns the counts associated with this Counter for the
422 * selected type of statistics.
423 *
424 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
425 */
426 public abstract long[] getCountsLocked(int which);
427
428 /**
429 * Temporary for debugging.
430 */
431 public abstract void logState(Printer pw, String prefix);
432 }
433
434 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800435 * Container class that aggregates counters for transmit, receive, and idle state of a
436 * radio controller.
437 */
438 public static abstract class ControllerActivityCounter {
439 /**
440 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
441 * idle state.
442 */
443 public abstract LongCounter getIdleTimeCounter();
444
445 /**
446 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800447 * scan state.
448 */
449 public abstract LongCounter getScanTimeCounter();
450
Siddharth Rayed754702018-02-15 12:44:37 -0800451 /**
452 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
453 * sleep state.
454 */
455 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800456
457 /**
458 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800459 * receive state.
460 */
461 public abstract LongCounter getRxTimeCounter();
462
463 /**
464 * An array of {@link LongCounter}, representing various transmit levels, where each level
465 * may draw a different amount of power. The levels themselves are controller-specific.
466 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
467 * various transmit level states.
468 */
469 public abstract LongCounter[] getTxTimeCounters();
470
471 /**
472 * @return a non-null {@link LongCounter} representing the power consumed by the controller
473 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
474 * yield a value of 0 if the device doesn't support power calculations.
475 */
476 public abstract LongCounter getPowerCounter();
477 }
478
479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 * State for keeping track of timing information.
481 */
482 public static abstract class Timer {
483
484 /**
485 * Returns the count associated with this Timer for the
486 * selected type of statistics.
487 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700488 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 */
Evan Millarc64edde2009-04-18 12:26:32 -0700490 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
492 /**
493 * Returns the total time in microseconds associated with this Timer for the
494 * selected type of statistics.
495 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800496 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700497 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 * @return a time in microseconds
499 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800500 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700503 * Returns the total time in microseconds associated with this Timer since the
504 * 'mark' was last set.
505 *
506 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
507 * @return a time in microseconds
508 */
509 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
510
511 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700512 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700513 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700514 */
515 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
516 return -1;
517 }
518
519 /**
520 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700521 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700522 */
523 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
524 return -1;
525 }
526
527 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700528 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800529 *
530 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
531 * been on since reset.
532 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
533 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
534 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
535 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700536 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800537 */
538 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
539 return -1;
540 }
541
542 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700543 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
544 * used, for example, for tracking background usage. Secondary timers are never pooled.
545 *
546 * Not all Timer subclasses have a secondary timer; those that don't return null.
547 */
548 public Timer getSubTimer() {
549 return null;
550 }
551
552 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700553 * Returns whether the timer is currently running. Some types of timers
554 * (e.g. BatchTimers) don't know whether the event is currently active,
555 * and report false.
556 */
557 public boolean isRunningLocked() {
558 return false;
559 }
560
561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * Temporary for debugging.
563 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700564 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
567 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800568 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
569 */
570 public static int mapToInternalProcessState(int procState) {
571 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
572 return ActivityManager.PROCESS_STATE_NONEXISTENT;
573 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
574 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800575 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
576 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800577 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
578 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
579 // Persistent and other foreground states go here.
580 return Uid.PROCESS_STATE_FOREGROUND;
581 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
582 return Uid.PROCESS_STATE_BACKGROUND;
583 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
584 return Uid.PROCESS_STATE_TOP_SLEEPING;
585 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
586 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
587 } else {
588 return Uid.PROCESS_STATE_CACHED;
589 }
590 }
591
592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * The statistics associated with a particular uid.
594 */
595 public static abstract class Uid {
596
597 /**
598 * Returns a mapping containing wakelock statistics.
599 *
600 * @return a Map from Strings to Uid.Wakelock objects.
601 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700602 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603
604 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700605 * Returns the WiFi Multicast Wakelock statistics.
606 *
607 * @return a Timer Object for the per uid Multicast statistics.
608 */
609 public abstract Timer getMulticastWakelockStats();
610
611 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700612 * Returns a mapping containing sync statistics.
613 *
614 * @return a Map from Strings to Timer objects.
615 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700616 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700617
618 /**
619 * Returns a mapping containing scheduled job statistics.
620 *
621 * @return a Map from Strings to Timer objects.
622 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700623 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700624
625 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700626 * Returns statistics about how jobs have completed.
627 *
628 * @return A Map of String job names to completion type -> count mapping.
629 */
630 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
631
632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * The statistics associated with a particular wake lock.
634 */
635 public static abstract class Wakelock {
636 public abstract Timer getWakeTime(int type);
637 }
638
639 /**
Bookatzc8c44962017-05-11 12:12:54 -0700640 * The cumulative time the uid spent holding any partial wakelocks. This will generally
641 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
642 * wakelocks that overlap in time (and therefore over-counts).
643 */
644 public abstract Timer getAggregatedPartialWakelockTimer();
645
646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * Returns a mapping containing sensor statistics.
648 *
649 * @return a Map from Integer sensor ids to Uid.Sensor objects.
650 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700651 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
653 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700654 * Returns a mapping containing active process data.
655 */
656 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700657
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 * Returns a mapping containing process statistics.
660 *
661 * @return a Map from Strings to Uid.Proc objects.
662 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700663 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664
665 /**
666 * Returns a mapping containing package statistics.
667 *
668 * @return a Map from Strings to Uid.Pkg objects.
669 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700670 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700671
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800672 public abstract ControllerActivityCounter getWifiControllerActivity();
673 public abstract ControllerActivityCounter getBluetoothControllerActivity();
674 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800675
676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * {@hide}
678 */
679 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700680
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800681 public abstract void noteWifiRunningLocked(long elapsedRealtime);
682 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
683 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
684 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
685 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
686 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
687 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
688 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
689 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
690 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800691 public abstract void noteActivityResumedLocked(long elapsedRealtime);
692 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800693 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
694 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
695 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700696 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700697 /**
698 * Returns the timer keeping track of wifi scans.
699 */
700 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800701 public abstract int getWifiScanBackgroundCount(int which);
702 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
703 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700704 /**
705 * Returns the timer keeping track of background wifi scans.
706 */
707 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800708 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700709 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800710 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700711 public abstract Timer getAudioTurnedOnTimer();
712 public abstract Timer getVideoTurnedOnTimer();
713 public abstract Timer getFlashlightTurnedOnTimer();
714 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700715 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700716
717 /**
718 * Returns the timer keeping track of Foreground Service time
719 */
720 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800721 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800722 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700723 public abstract Timer getBluetoothUnoptimizedScanTimer();
724 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700725 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700726 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700727
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700728 public abstract long[] getCpuFreqTimes(int which);
729 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700730 /**
731 * Returns cpu active time of an uid.
732 */
733 public abstract long getCpuActiveTime();
734 /**
735 * Returns cpu times of an uid on each cluster
736 */
737 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700738
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800739 /**
740 * Returns cpu times of an uid at a particular process state.
741 */
742 public abstract long[] getCpuFreqTimes(int which, int procState);
743 /**
744 * Returns cpu times of an uid while the screen if off at a particular process state.
745 */
746 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
747
Dianne Hackborna0200e32016-03-30 18:01:41 -0700748 // Note: the following times are disjoint. They can be added together to find the
749 // total time a uid has had any processes running at all.
750
751 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800752 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700753 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800754 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700755 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800756 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700757 * none in the "top" state.
758 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800759 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700760 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700761 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800762 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700763 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800764 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700765 /**
766 * Time this uid has any process in an active background state, but none in the
767 * "foreground" or better state.
768 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800769 public static final int PROCESS_STATE_BACKGROUND = 3;
770 /**
771 * Time this uid has any process that is top while the device is sleeping, but not
772 * active for any other reason. We kind-of consider it a kind of cached process
773 * for execution restrictions.
774 */
775 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
776 /**
777 * Time this uid has any process that is in the background but it has an activity
778 * marked as "can't save state". This is essentially a cached process, though the
779 * system will try much harder than normal to avoid killing it.
780 */
781 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700782 /**
783 * Time this uid has any processes that are sitting around cached, not in one of the
784 * other active states.
785 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800786 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700787 /**
788 * Total number of process states we track.
789 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800790 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700791
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800792 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700793 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800794 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
795 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700796 };
797
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800798 // Used in checkin dump
799 @VisibleForTesting
800 public static final String[] UID_PROCESS_TYPES = {
801 "T", // TOP
802 "FS", // FOREGROUND_SERVICE
803 "F", // FOREGROUND
804 "B", // BACKGROUND
805 "TS", // TOP_SLEEPING
806 "HW", // HEAVY_WEIGHT
807 "C" // CACHED
808 };
809
810 /**
811 * When the process exits one of these states, we need to make sure cpu time in this state
812 * is not attributed to any non-critical process states.
813 */
814 public static final int[] CRITICAL_PROC_STATES = {
815 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
816 };
817
Dianne Hackborn61659e52014-07-09 16:13:01 -0700818 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800819 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700820
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800821 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822
Robert Greenwalta029ea12013-09-25 16:38:12 -0700823 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
824
Dianne Hackborn617f8772009-03-31 15:04:46 -0700825 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700826 * Note that these must match the constants in android.os.PowerManager.
827 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
828 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 */
830 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700831 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700832 };
Bookatzc8c44962017-05-11 12:12:54 -0700833
Phil Weaverda80d672016-03-15 16:25:46 -0700834 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700835
Dianne Hackborn617f8772009-03-31 15:04:46 -0700836 public abstract void noteUserActivityLocked(int type);
837 public abstract boolean hasUserActivity();
838 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700839
840 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800841 public abstract long getNetworkActivityBytes(int type, int which);
842 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800843 public abstract long getMobileRadioActiveTime(int which);
844 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700845
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700846 /**
847 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
848 */
849 public abstract long getUserCpuTimeUs(int which);
850
851 /**
852 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
853 */
854 public abstract long getSystemCpuTimeUs(int which);
855
856 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700857 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700858 * given CPU cluster.
859 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700860 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700861 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700862 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
863 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700864 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700865 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700866
Adam Lesinski5f056f62016-07-14 16:56:08 -0700867 /**
868 * Returns the number of times this UID woke up the Application Processor to
869 * process a mobile radio packet.
870 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
871 */
872 public abstract long getMobileRadioApWakeupCount(int which);
873
874 /**
875 * Returns the number of times this UID woke up the Application Processor to
876 * process a WiFi packet.
877 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
878 */
879 public abstract long getWifiRadioApWakeupCount(int which);
880
Amith Yamasani977e11f2018-02-16 11:29:54 -0800881 /**
882 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
883 * @param sb StringBuilder that can be overwritten with the deferred jobs data
884 * @param which one of STATS_*
885 */
886 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
887
888 /**
889 * Appends the deferred jobs data to the StringBuilder passed in
890 * @param sb StringBuilder that can be overwritten with the deferred jobs data
891 * @param which one of STATS_*
892 */
893 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800896 /*
897 * FIXME: it's not correct to use this magic value because it
898 * could clash with a sensor handle (which are defined by
899 * the sensor HAL, and therefore out of our control
900 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 // Magic sensor number for the GPS.
902 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800907
Bookatz867c0d72017-03-07 18:23:42 -0800908 /** Returns a Timer for sensor usage when app is in the background. */
909 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700912 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800913 public int mWakeNesting;
914 public long mWakeSumMs;
915 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700916 }
917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 /**
919 * The statistics associated with a particular process.
920 */
921 public static abstract class Proc {
922
Dianne Hackborn287952c2010-09-22 22:34:31 -0700923 public static class ExcessivePower {
924 public static final int TYPE_WAKE = 1;
925 public static final int TYPE_CPU = 2;
926
927 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700928 public long overTime;
929 public long usedTime;
930 }
931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800933 * Returns true if this process is still active in the battery stats.
934 */
935 public abstract boolean isActive();
936
937 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700938 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700940 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 */
942 public abstract long getUserTime(int which);
943
944 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700945 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700947 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 */
949 public abstract long getSystemTime(int which);
950
951 /**
952 * Returns the number of times the process has been started.
953 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700954 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 */
956 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700957
958 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800959 * Returns the number of times the process has crashed.
960 *
961 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
962 */
963 public abstract int getNumCrashes(int which);
964
965 /**
966 * Returns the number of times the process has ANRed.
967 *
968 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
969 */
970 public abstract int getNumAnrs(int which);
971
972 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700973 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700974 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700975 * @return foreground cpu time in microseconds
976 */
977 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700978
Dianne Hackborn287952c2010-09-22 22:34:31 -0700979 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700980
Dianne Hackborn287952c2010-09-22 22:34:31 -0700981 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
984 /**
985 * The statistics associated with a particular package.
986 */
987 public static abstract class Pkg {
988
989 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700990 * Returns information about all wakeup alarms that have been triggered for this
991 * package. The mapping keys are tag names for the alarms, the counter contains
992 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700994 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995
996 /**
997 * Returns a mapping containing service statistics.
998 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700999 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000
1001 /**
1002 * The statistics associated with a particular service.
1003 */
Joe Onoratoabded112016-02-08 16:49:39 -08001004 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
1006 /**
1007 * Returns the amount of time spent started.
1008 *
1009 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001010 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 * @return
1012 */
1013 public abstract long getStartTime(long batteryUptime, int which);
1014
1015 /**
1016 * Returns the total number of times startService() has been called.
1017 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001018 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 */
1020 public abstract int getStarts(int which);
1021
1022 /**
1023 * Returns the total number times the service has been launched.
1024 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001025 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public abstract int getLaunches(int which);
1028 }
1029 }
1030 }
1031
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001032 public static final class LevelStepTracker {
1033 public long mLastStepTime = -1;
1034 public int mNumStepDurations;
1035 public final long[] mStepDurations;
1036
1037 public LevelStepTracker(int maxLevelSteps) {
1038 mStepDurations = new long[maxLevelSteps];
1039 }
1040
1041 public LevelStepTracker(int numSteps, long[] steps) {
1042 mNumStepDurations = numSteps;
1043 mStepDurations = new long[numSteps];
1044 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1045 }
1046
1047 public long getDurationAt(int index) {
1048 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1049 }
1050
1051 public int getLevelAt(int index) {
1052 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1053 >> STEP_LEVEL_LEVEL_SHIFT);
1054 }
1055
1056 public int getInitModeAt(int index) {
1057 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1058 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1059 }
1060
1061 public int getModModeAt(int index) {
1062 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1063 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1064 }
1065
1066 private void appendHex(long val, int topOffset, StringBuilder out) {
1067 boolean hasData = false;
1068 while (topOffset >= 0) {
1069 int digit = (int)( (val>>topOffset) & 0xf );
1070 topOffset -= 4;
1071 if (!hasData && digit == 0) {
1072 continue;
1073 }
1074 hasData = true;
1075 if (digit >= 0 && digit <= 9) {
1076 out.append((char)('0' + digit));
1077 } else {
1078 out.append((char)('a' + digit - 10));
1079 }
1080 }
1081 }
1082
1083 public void encodeEntryAt(int index, StringBuilder out) {
1084 long item = mStepDurations[index];
1085 long duration = item & STEP_LEVEL_TIME_MASK;
1086 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1087 >> STEP_LEVEL_LEVEL_SHIFT);
1088 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1089 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1090 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1091 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1092 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1093 case Display.STATE_OFF: out.append('f'); break;
1094 case Display.STATE_ON: out.append('o'); break;
1095 case Display.STATE_DOZE: out.append('d'); break;
1096 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1097 }
1098 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1099 out.append('p');
1100 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001101 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1102 out.append('i');
1103 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001104 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1105 case Display.STATE_OFF: out.append('F'); break;
1106 case Display.STATE_ON: out.append('O'); break;
1107 case Display.STATE_DOZE: out.append('D'); break;
1108 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1109 }
1110 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1111 out.append('P');
1112 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001113 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1114 out.append('I');
1115 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001116 out.append('-');
1117 appendHex(level, 4, out);
1118 out.append('-');
1119 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1120 }
1121
1122 public void decodeEntryAt(int index, String value) {
1123 final int N = value.length();
1124 int i = 0;
1125 char c;
1126 long out = 0;
1127 while (i < N && (c=value.charAt(i)) != '-') {
1128 i++;
1129 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001130 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001131 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001132 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001133 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001134 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001135 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001136 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1137 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1138 break;
1139 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1140 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1141 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001142 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1143 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1144 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001145 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1146 break;
1147 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1148 break;
1149 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1150 break;
1151 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1152 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1153 break;
1154 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1155 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001156 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001157 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1158 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1159 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001160 }
1161 }
1162 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001163 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001164 while (i < N && (c=value.charAt(i)) != '-') {
1165 i++;
1166 level <<= 4;
1167 if (c >= '0' && c <= '9') {
1168 level += c - '0';
1169 } else if (c >= 'a' && c <= 'f') {
1170 level += c - 'a' + 10;
1171 } else if (c >= 'A' && c <= 'F') {
1172 level += c - 'A' + 10;
1173 }
1174 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001175 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001176 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1177 long duration = 0;
1178 while (i < N && (c=value.charAt(i)) != '-') {
1179 i++;
1180 duration <<= 4;
1181 if (c >= '0' && c <= '9') {
1182 duration += c - '0';
1183 } else if (c >= 'a' && c <= 'f') {
1184 duration += c - 'a' + 10;
1185 } else if (c >= 'A' && c <= 'F') {
1186 duration += c - 'A' + 10;
1187 }
1188 }
1189 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1190 }
1191
1192 public void init() {
1193 mLastStepTime = -1;
1194 mNumStepDurations = 0;
1195 }
1196
1197 public void clearTime() {
1198 mLastStepTime = -1;
1199 }
1200
1201 public long computeTimePerLevel() {
1202 final long[] steps = mStepDurations;
1203 final int numSteps = mNumStepDurations;
1204
1205 // For now we'll do a simple average across all steps.
1206 if (numSteps <= 0) {
1207 return -1;
1208 }
1209 long total = 0;
1210 for (int i=0; i<numSteps; i++) {
1211 total += steps[i] & STEP_LEVEL_TIME_MASK;
1212 }
1213 return total / numSteps;
1214 /*
1215 long[] buckets = new long[numSteps];
1216 int numBuckets = 0;
1217 int numToAverage = 4;
1218 int i = 0;
1219 while (i < numSteps) {
1220 long totalTime = 0;
1221 int num = 0;
1222 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1223 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1224 num++;
1225 }
1226 buckets[numBuckets] = totalTime / num;
1227 numBuckets++;
1228 numToAverage *= 2;
1229 i += num;
1230 }
1231 if (numBuckets < 1) {
1232 return -1;
1233 }
1234 long averageTime = buckets[numBuckets-1];
1235 for (i=numBuckets-2; i>=0; i--) {
1236 averageTime = (averageTime + buckets[i]) / 2;
1237 }
1238 return averageTime;
1239 */
1240 }
1241
1242 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1243 int[] outNumOfInterest) {
1244 final long[] steps = mStepDurations;
1245 final int count = mNumStepDurations;
1246 if (count <= 0) {
1247 return -1;
1248 }
1249 long total = 0;
1250 int numOfInterest = 0;
1251 for (int i=0; i<count; i++) {
1252 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1253 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1254 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1255 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1256 // If the modes of interest didn't change during this step period...
1257 if ((modMode&modesOfInterest) == 0) {
1258 // And the mode values during this period match those we are measuring...
1259 if ((initMode&modesOfInterest) == modeValues) {
1260 // Then this can be used to estimate the total time!
1261 numOfInterest++;
1262 total += steps[i] & STEP_LEVEL_TIME_MASK;
1263 }
1264 }
1265 }
1266 if (numOfInterest <= 0) {
1267 return -1;
1268 }
1269
1270 if (outNumOfInterest != null) {
1271 outNumOfInterest[0] = numOfInterest;
1272 }
1273
1274 // The estimated time is the average time we spend in each level, multipled
1275 // by 100 -- the total number of battery levels
1276 return (total / numOfInterest) * 100;
1277 }
1278
1279 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1280 int stepCount = mNumStepDurations;
1281 final long lastStepTime = mLastStepTime;
1282 if (lastStepTime >= 0 && numStepLevels > 0) {
1283 final long[] steps = mStepDurations;
1284 long duration = elapsedRealtime - lastStepTime;
1285 for (int i=0; i<numStepLevels; i++) {
1286 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1287 long thisDuration = duration / (numStepLevels-i);
1288 duration -= thisDuration;
1289 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1290 thisDuration = STEP_LEVEL_TIME_MASK;
1291 }
1292 steps[0] = thisDuration | modeBits;
1293 }
1294 stepCount += numStepLevels;
1295 if (stepCount > steps.length) {
1296 stepCount = steps.length;
1297 }
1298 }
1299 mNumStepDurations = stepCount;
1300 mLastStepTime = elapsedRealtime;
1301 }
1302
1303 public void readFromParcel(Parcel in) {
1304 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001305 if (N > mStepDurations.length) {
1306 throw new ParcelFormatException("more step durations than available: " + N);
1307 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001308 mNumStepDurations = N;
1309 for (int i=0; i<N; i++) {
1310 mStepDurations[i] = in.readLong();
1311 }
1312 }
1313
1314 public void writeToParcel(Parcel out) {
1315 final int N = mNumStepDurations;
1316 out.writeInt(N);
1317 for (int i=0; i<N; i++) {
1318 out.writeLong(mStepDurations[i]);
1319 }
1320 }
1321 }
1322
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001323 public static final class PackageChange {
1324 public String mPackageName;
1325 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001326 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001327 }
1328
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001329 public static final class DailyItem {
1330 public long mStartTime;
1331 public long mEndTime;
1332 public LevelStepTracker mDischargeSteps;
1333 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001334 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001335 }
1336
1337 public abstract DailyItem getDailyItemLocked(int daysAgo);
1338
1339 public abstract long getCurrentDailyStartTime();
1340
1341 public abstract long getNextMinDailyDeadline();
1342
1343 public abstract long getNextMaxDailyDeadline();
1344
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001345 public abstract long[] getCpuFreqs();
1346
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001347 public final static class HistoryTag {
1348 public String string;
1349 public int uid;
1350
1351 public int poolIdx;
1352
1353 public void setTo(HistoryTag o) {
1354 string = o.string;
1355 uid = o.uid;
1356 poolIdx = o.poolIdx;
1357 }
1358
1359 public void setTo(String _string, int _uid) {
1360 string = _string;
1361 uid = _uid;
1362 poolIdx = -1;
1363 }
1364
1365 public void writeToParcel(Parcel dest, int flags) {
1366 dest.writeString(string);
1367 dest.writeInt(uid);
1368 }
1369
1370 public void readFromParcel(Parcel src) {
1371 string = src.readString();
1372 uid = src.readInt();
1373 poolIdx = -1;
1374 }
1375
1376 @Override
1377 public boolean equals(Object o) {
1378 if (this == o) return true;
1379 if (o == null || getClass() != o.getClass()) return false;
1380
1381 HistoryTag that = (HistoryTag) o;
1382
1383 if (uid != that.uid) return false;
1384 if (!string.equals(that.string)) return false;
1385
1386 return true;
1387 }
1388
1389 @Override
1390 public int hashCode() {
1391 int result = string.hashCode();
1392 result = 31 * result + uid;
1393 return result;
1394 }
1395 }
1396
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001397 /**
1398 * Optional detailed information that can go into a history step. This is typically
1399 * generated each time the battery level changes.
1400 */
1401 public final static class HistoryStepDetails {
1402 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1403 public int userTime;
1404 public int systemTime;
1405
1406 // Top three apps using CPU in the last step, with times in 1/100 second.
1407 public int appCpuUid1;
1408 public int appCpuUTime1;
1409 public int appCpuSTime1;
1410 public int appCpuUid2;
1411 public int appCpuUTime2;
1412 public int appCpuSTime2;
1413 public int appCpuUid3;
1414 public int appCpuUTime3;
1415 public int appCpuSTime3;
1416
1417 // Information from /proc/stat
1418 public int statUserTime;
1419 public int statSystemTime;
1420 public int statIOWaitTime;
1421 public int statIrqTime;
1422 public int statSoftIrqTime;
1423 public int statIdlTime;
1424
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001425 // Platform-level low power state stats
1426 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001427 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001428
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001429 public HistoryStepDetails() {
1430 clear();
1431 }
1432
1433 public void clear() {
1434 userTime = systemTime = 0;
1435 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1436 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1437 = appCpuUTime3 = appCpuSTime3 = 0;
1438 }
1439
1440 public void writeToParcel(Parcel out) {
1441 out.writeInt(userTime);
1442 out.writeInt(systemTime);
1443 out.writeInt(appCpuUid1);
1444 out.writeInt(appCpuUTime1);
1445 out.writeInt(appCpuSTime1);
1446 out.writeInt(appCpuUid2);
1447 out.writeInt(appCpuUTime2);
1448 out.writeInt(appCpuSTime2);
1449 out.writeInt(appCpuUid3);
1450 out.writeInt(appCpuUTime3);
1451 out.writeInt(appCpuSTime3);
1452 out.writeInt(statUserTime);
1453 out.writeInt(statSystemTime);
1454 out.writeInt(statIOWaitTime);
1455 out.writeInt(statIrqTime);
1456 out.writeInt(statSoftIrqTime);
1457 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001458 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001459 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001460 }
1461
1462 public void readFromParcel(Parcel in) {
1463 userTime = in.readInt();
1464 systemTime = in.readInt();
1465 appCpuUid1 = in.readInt();
1466 appCpuUTime1 = in.readInt();
1467 appCpuSTime1 = in.readInt();
1468 appCpuUid2 = in.readInt();
1469 appCpuUTime2 = in.readInt();
1470 appCpuSTime2 = in.readInt();
1471 appCpuUid3 = in.readInt();
1472 appCpuUTime3 = in.readInt();
1473 appCpuSTime3 = in.readInt();
1474 statUserTime = in.readInt();
1475 statSystemTime = in.readInt();
1476 statIOWaitTime = in.readInt();
1477 statIrqTime = in.readInt();
1478 statSoftIrqTime = in.readInt();
1479 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001480 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001481 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001482 }
1483 }
1484
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001485 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001486 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001487
1488 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001489 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001490
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001491 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001492 public static final byte CMD_NULL = -1;
1493 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001494 public static final byte CMD_CURRENT_TIME = 5;
1495 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001496 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001497 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001498
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001499 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001500
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001501 /**
1502 * Return whether the command code is a delta data update.
1503 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001504 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001505 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001506 }
1507
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001508 public byte batteryLevel;
1509 public byte batteryStatus;
1510 public byte batteryHealth;
1511 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001512
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001513 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001514 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001515
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001516 // The charge of the battery in micro-Ampere-hours.
1517 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001518
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001519 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001520 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001521 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001522 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001523 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1524 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001525 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526 public static final int STATE_PHONE_STATE_SHIFT = 6;
1527 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001528 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001529 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001530 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001531
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001532 // These states always appear directly in the first int token
1533 // of a delta change; they should be ones that change relatively
1534 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001535 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1536 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001537 public static final int STATE_GPS_ON_FLAG = 1<<29;
1538 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001539 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001540 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001541 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001542 // Do not use, this is used for coulomb delta count.
1543 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001544 // These are on the lower bits used for the command; if they change
1545 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001546 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001547 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001548 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001549 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1550 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001551 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001552 // empty slot
1553 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001554
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001555 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001556 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001557
1558 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001559
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001560 public int states;
1561
Dianne Hackborn3251b902014-06-20 14:40:53 -07001562 // Constants from WIFI_SUPPL_STATE_*
1563 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1564 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1565 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1566 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1567 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1568 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001569 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1570 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1571 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1572 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001573
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001574 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001575 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1576 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1577 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001578 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001579 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1580 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1581 public static final int STATE2_CHARGING_FLAG = 1<<24;
1582 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1583 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1584 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001585 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001586 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001587
1588 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001589 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1590 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001591
1592 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001593
Dianne Hackborn40c87252014-03-19 16:55:40 -07001594 public int states2;
1595
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001596 // The wake lock that was acquired at this point.
1597 public HistoryTag wakelockTag;
1598
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001599 // Kernel wakeup reason at this point.
1600 public HistoryTag wakeReasonTag;
1601
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001602 // Non-null when there is more detailed information at this step.
1603 public HistoryStepDetails stepDetails;
1604
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001605 public static final int EVENT_FLAG_START = 0x8000;
1606 public static final int EVENT_FLAG_FINISH = 0x4000;
1607
1608 // No event in this item.
1609 public static final int EVENT_NONE = 0x0000;
1610 // Event is about a process that is running.
1611 public static final int EVENT_PROC = 0x0001;
1612 // Event is about an application package that is in the foreground.
1613 public static final int EVENT_FOREGROUND = 0x0002;
1614 // Event is about an application package that is at the top of the screen.
1615 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001616 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001617 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001618 // Events for all additional wake locks aquired/release within a wake block.
1619 // These are not generated by default.
1620 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001621 // Event is about an application executing a scheduled job.
1622 public static final int EVENT_JOB = 0x0006;
1623 // Events for users running.
1624 public static final int EVENT_USER_RUNNING = 0x0007;
1625 // Events for foreground user.
1626 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001627 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001628 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001629 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001630 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001631 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001632 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001633 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001634 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001635 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001636 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001637 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001638 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001639 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001640 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001641 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001642 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001643 // Event for a package being on the temporary whitelist.
1644 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001645 // Event for the screen waking up.
1646 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001647 // Event for the UID that woke up the application processor.
1648 // Used for wakeups coming from WiFi, modem, etc.
1649 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001650 // Event for reporting that a specific partial wake lock has been held for a long duration.
1651 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001652
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001653 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001654 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001655 // Mask to extract out only the type part of the event.
1656 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001657
1658 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1659 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1660 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1661 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1662 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1663 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001664 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1665 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001666 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1667 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001668 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1669 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1670 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1671 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1672 public static final int EVENT_USER_FOREGROUND_START =
1673 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1674 public static final int EVENT_USER_FOREGROUND_FINISH =
1675 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001676 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1677 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001678 public static final int EVENT_TEMP_WHITELIST_START =
1679 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1680 public static final int EVENT_TEMP_WHITELIST_FINISH =
1681 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001682 public static final int EVENT_LONG_WAKE_LOCK_START =
1683 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1684 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1685 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001686
1687 // For CMD_EVENT.
1688 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001689 public HistoryTag eventTag;
1690
Dianne Hackborn9a755432014-05-15 17:05:22 -07001691 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001692 public long currentTime;
1693
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001694 // Meta-data when reading.
1695 public int numReadInts;
1696
1697 // Pre-allocated objects.
1698 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001699 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001700 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001701
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001702 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001703 }
Bookatzc8c44962017-05-11 12:12:54 -07001704
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001705 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001706 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001707 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001708 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001709 }
Bookatzc8c44962017-05-11 12:12:54 -07001710
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001711 public int describeContents() {
1712 return 0;
1713 }
1714
1715 public void writeToParcel(Parcel dest, int flags) {
1716 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001717 int bat = (((int)cmd)&0xff)
1718 | ((((int)batteryLevel)<<8)&0xff00)
1719 | ((((int)batteryStatus)<<16)&0xf0000)
1720 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001721 | ((((int)batteryPlugType)<<24)&0xf000000)
1722 | (wakelockTag != null ? 0x10000000 : 0)
1723 | (wakeReasonTag != null ? 0x20000000 : 0)
1724 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001725 dest.writeInt(bat);
1726 bat = (((int)batteryTemperature)&0xffff)
1727 | ((((int)batteryVoltage)<<16)&0xffff0000);
1728 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001729 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001730 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001731 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001732 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001733 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001734 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001735 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001736 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001737 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001738 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001739 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001740 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001741 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001742 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001743 dest.writeLong(currentTime);
1744 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001745 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001746
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001747 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001748 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001749 int bat = src.readInt();
1750 cmd = (byte)(bat&0xff);
1751 batteryLevel = (byte)((bat>>8)&0xff);
1752 batteryStatus = (byte)((bat>>16)&0xf);
1753 batteryHealth = (byte)((bat>>20)&0xf);
1754 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001755 int bat2 = src.readInt();
1756 batteryTemperature = (short)(bat2&0xffff);
1757 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001758 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001759 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001760 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001761 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001762 wakelockTag = localWakelockTag;
1763 wakelockTag.readFromParcel(src);
1764 } else {
1765 wakelockTag = null;
1766 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001767 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001768 wakeReasonTag = localWakeReasonTag;
1769 wakeReasonTag.readFromParcel(src);
1770 } else {
1771 wakeReasonTag = null;
1772 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001773 if ((bat&0x40000000) != 0) {
1774 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001775 eventTag = localEventTag;
1776 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001777 } else {
1778 eventCode = EVENT_NONE;
1779 eventTag = null;
1780 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001781 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001782 currentTime = src.readLong();
1783 } else {
1784 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001785 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001787 }
1788
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001789 public void clear() {
1790 time = 0;
1791 cmd = CMD_NULL;
1792 batteryLevel = 0;
1793 batteryStatus = 0;
1794 batteryHealth = 0;
1795 batteryPlugType = 0;
1796 batteryTemperature = 0;
1797 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001798 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001799 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001800 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001801 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001802 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001804 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001805 }
Bookatzc8c44962017-05-11 12:12:54 -07001806
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001807 public void setTo(HistoryItem o) {
1808 time = o.time;
1809 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001810 setToCommon(o);
1811 }
1812
1813 public void setTo(long time, byte cmd, HistoryItem o) {
1814 this.time = time;
1815 this.cmd = cmd;
1816 setToCommon(o);
1817 }
1818
1819 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001820 batteryLevel = o.batteryLevel;
1821 batteryStatus = o.batteryStatus;
1822 batteryHealth = o.batteryHealth;
1823 batteryPlugType = o.batteryPlugType;
1824 batteryTemperature = o.batteryTemperature;
1825 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001826 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001827 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001828 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001829 if (o.wakelockTag != null) {
1830 wakelockTag = localWakelockTag;
1831 wakelockTag.setTo(o.wakelockTag);
1832 } else {
1833 wakelockTag = null;
1834 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001835 if (o.wakeReasonTag != null) {
1836 wakeReasonTag = localWakeReasonTag;
1837 wakeReasonTag.setTo(o.wakeReasonTag);
1838 } else {
1839 wakeReasonTag = null;
1840 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001841 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001842 if (o.eventTag != null) {
1843 eventTag = localEventTag;
1844 eventTag.setTo(o.eventTag);
1845 } else {
1846 eventTag = null;
1847 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001848 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001849 }
1850
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001851 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001852 return batteryLevel == o.batteryLevel
1853 && batteryStatus == o.batteryStatus
1854 && batteryHealth == o.batteryHealth
1855 && batteryPlugType == o.batteryPlugType
1856 && batteryTemperature == o.batteryTemperature
1857 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001858 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001859 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001860 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001861 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001862 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001863
1864 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001865 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001866 return false;
1867 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001868 if (wakelockTag != o.wakelockTag) {
1869 if (wakelockTag == null || o.wakelockTag == null) {
1870 return false;
1871 }
1872 if (!wakelockTag.equals(o.wakelockTag)) {
1873 return false;
1874 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001875 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001876 if (wakeReasonTag != o.wakeReasonTag) {
1877 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1878 return false;
1879 }
1880 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1881 return false;
1882 }
1883 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001884 if (eventTag != o.eventTag) {
1885 if (eventTag == null || o.eventTag == null) {
1886 return false;
1887 }
1888 if (!eventTag.equals(o.eventTag)) {
1889 return false;
1890 }
1891 }
1892 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001893 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001894 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001895
1896 public final static class HistoryEventTracker {
1897 private final HashMap<String, SparseIntArray>[] mActiveEvents
1898 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1899
1900 public boolean updateState(int code, String name, int uid, int poolIdx) {
1901 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1902 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1903 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1904 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001905 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001906 mActiveEvents[idx] = active;
1907 }
1908 SparseIntArray uids = active.get(name);
1909 if (uids == null) {
1910 uids = new SparseIntArray();
1911 active.put(name, uids);
1912 }
1913 if (uids.indexOfKey(uid) >= 0) {
1914 // Already set, nothing to do!
1915 return false;
1916 }
1917 uids.put(uid, poolIdx);
1918 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1919 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1920 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1921 if (active == null) {
1922 // not currently active, nothing to do.
1923 return false;
1924 }
1925 SparseIntArray uids = active.get(name);
1926 if (uids == null) {
1927 // not currently active, nothing to do.
1928 return false;
1929 }
1930 idx = uids.indexOfKey(uid);
1931 if (idx < 0) {
1932 // not currently active, nothing to do.
1933 return false;
1934 }
1935 uids.removeAt(idx);
1936 if (uids.size() <= 0) {
1937 active.remove(name);
1938 }
1939 }
1940 return true;
1941 }
1942
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001943 public void removeEvents(int code) {
1944 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1945 mActiveEvents[idx] = null;
1946 }
1947
Dianne Hackborn37de0982014-05-09 09:32:18 -07001948 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1949 return mActiveEvents[code];
1950 }
1951 }
1952
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001953 public static final class BitDescription {
1954 public final int mask;
1955 public final int shift;
1956 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001957 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001958 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001959 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001960
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001961 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001962 this.mask = mask;
1963 this.shift = -1;
1964 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001965 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001966 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001967 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001968 }
Bookatzc8c44962017-05-11 12:12:54 -07001969
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001970 public BitDescription(int mask, int shift, String name, String shortName,
1971 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001972 this.mask = mask;
1973 this.shift = shift;
1974 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001975 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001976 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001977 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001978 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001979 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001980
Dianne Hackbornfc064132014-06-02 12:42:12 -07001981 /**
1982 * Don't allow any more batching in to the current history event. This
1983 * is called when printing partial histories, so to ensure that the next
1984 * history event will go in to a new batch after what was printed in the
1985 * last partial history.
1986 */
1987 public abstract void commitCurrentHistoryBatchLocked();
1988
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001989 public abstract int getHistoryTotalSize();
1990
1991 public abstract int getHistoryUsedSize();
1992
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001993 public abstract boolean startIteratingHistoryLocked();
1994
Dianne Hackborn099bc622014-01-22 13:39:16 -08001995 public abstract int getHistoryStringPoolSize();
1996
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001997 public abstract int getHistoryStringPoolBytes();
1998
1999 public abstract String getHistoryTagPoolString(int index);
2000
2001 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002002
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002003 public abstract boolean getNextHistoryLocked(HistoryItem out);
2004
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002005 public abstract void finishIteratingHistoryLocked();
2006
2007 public abstract boolean startIteratingOldHistoryLocked();
2008
2009 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2010
2011 public abstract void finishIteratingOldHistoryLocked();
2012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002014 * Return the base time offset for the battery history.
2015 */
2016 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002017
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 * Returns the number of times the device has been started.
2020 */
2021 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002024 * 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 -08002025 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002026 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 * {@hide}
2028 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002029 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002030
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002031 /**
2032 * Returns the number of times the screen was turned on.
2033 *
2034 * {@hide}
2035 */
2036 public abstract int getScreenOnCount(int which);
2037
Mike Mac2f518a2017-09-19 16:06:03 -07002038 /**
2039 * Returns the time in microseconds that the screen has been dozing while the device was
2040 * running on battery.
2041 *
2042 * {@hide}
2043 */
2044 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2045
2046 /**
2047 * Returns the number of times the screen was turned dozing.
2048 *
2049 * {@hide}
2050 */
2051 public abstract int getScreenDozeCount(int which);
2052
Jeff Browne95c3cd2014-05-02 16:59:26 -07002053 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2054
Dianne Hackborn617f8772009-03-31 15:04:46 -07002055 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2056 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2057 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2058 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2059 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002060
Dianne Hackborn617f8772009-03-31 15:04:46 -07002061 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2062 "dark", "dim", "medium", "light", "bright"
2063 };
Bookatzc8c44962017-05-11 12:12:54 -07002064
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002065 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2066 "0", "1", "2", "3", "4"
2067 };
2068
Dianne Hackborn617f8772009-03-31 15:04:46 -07002069 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002070
Dianne Hackborn617f8772009-03-31 15:04:46 -07002071 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002072 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002073 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002074 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002075 * {@hide}
2076 */
2077 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002078 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002081 * Returns the {@link Timer} object that tracks the given screen brightness.
2082 *
2083 * {@hide}
2084 */
2085 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2086
2087 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002088 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002089 * running on battery.
2090 *
2091 * {@hide}
2092 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002093 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002094
2095 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002096 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002097 *
2098 * {@hide}
2099 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002100 public abstract int getPowerSaveModeEnabledCount(int which);
2101
2102 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002103 * Constant for device idle mode: not active.
2104 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002105 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002106
2107 /**
2108 * Constant for device idle mode: active in lightweight mode.
2109 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002110 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002111
2112 /**
2113 * Constant for device idle mode: active in full mode.
2114 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002115 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002116
2117 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002118 * Returns the time in microseconds that device has been in idle mode while
2119 * running on battery.
2120 *
2121 * {@hide}
2122 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002123 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002124
2125 /**
2126 * Returns the number of times that the devie has gone in to idle mode.
2127 *
2128 * {@hide}
2129 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002130 public abstract int getDeviceIdleModeCount(int mode, int which);
2131
2132 /**
2133 * Return the longest duration we spent in a particular device idle mode (fully in the
2134 * mode, not in idle maintenance etc).
2135 */
2136 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002137
2138 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002139 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002140 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002141 * counts all of the time that we consider the device to be idle, whether or not
2142 * it is currently in the actual device idle mode.
2143 *
2144 * {@hide}
2145 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002146 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002147
2148 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002149 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002150 *
2151 * {@hide}
2152 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002153 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002154
2155 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002156 * Returns the number of times that connectivity state changed.
2157 *
2158 * {@hide}
2159 */
2160 public abstract int getNumConnectivityChange(int which);
2161
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002162
2163 /**
2164 * Returns the time in microseconds that the phone has been running with
2165 * the given GPS signal quality level
2166 *
2167 * {@hide}
2168 */
2169 public abstract long getGpsSignalQualityTime(int strengthBin,
2170 long elapsedRealtimeUs, int which);
2171
2172 /**
2173 * Returns the GPS battery drain in mA-ms
2174 *
2175 * {@hide}
2176 */
2177 public abstract long getGpsBatteryDrainMaMs();
2178
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002179 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002180 * 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 -08002181 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 * {@hide}
2184 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002185 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002186
Dianne Hackborn627bba72009-03-24 22:32:56 -07002187 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002188 * Returns the number of times a phone call was activated.
2189 *
2190 * {@hide}
2191 */
2192 public abstract int getPhoneOnCount(int which);
2193
2194 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002195 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002196 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002197 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002198 * {@hide}
2199 */
2200 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002201 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002202
Dianne Hackborn617f8772009-03-31 15:04:46 -07002203 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002204 * Returns the time in microseconds that the phone has been trying to
2205 * acquire a signal.
2206 *
2207 * {@hide}
2208 */
2209 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002210 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002211
2212 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002213 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2214 * acquire a signal.
2215 *
2216 * {@hide}
2217 */
2218 public abstract Timer getPhoneSignalScanningTimer();
2219
2220 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002221 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002222 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002223 * {@hide}
2224 */
2225 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2226
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002227 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002228 * Return the {@link Timer} object used to track the given signal strength's duration and
2229 * counts.
2230 */
2231 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2232
2233 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002234 * Returns the time in microseconds that the mobile network has been active
2235 * (in a high power state).
2236 *
2237 * {@hide}
2238 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002239 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002240
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002241 /**
2242 * Returns the number of times that the mobile network has transitioned to the
2243 * active state.
2244 *
2245 * {@hide}
2246 */
2247 public abstract int getMobileRadioActiveCount(int which);
2248
2249 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002250 * Returns the time in microseconds that is the difference between the mobile radio
2251 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2252 * from the radio.
2253 *
2254 * {@hide}
2255 */
2256 public abstract long getMobileRadioActiveAdjustedTime(int which);
2257
2258 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002259 * Returns the time in microseconds that the mobile network has been active
2260 * (in a high power state) but not being able to blame on an app.
2261 *
2262 * {@hide}
2263 */
2264 public abstract long getMobileRadioActiveUnknownTime(int which);
2265
2266 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002267 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002268 *
2269 * {@hide}
2270 */
2271 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002272
Dianne Hackborn627bba72009-03-24 22:32:56 -07002273 public static final int DATA_CONNECTION_NONE = 0;
2274 public static final int DATA_CONNECTION_GPRS = 1;
2275 public static final int DATA_CONNECTION_EDGE = 2;
2276 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002277 public static final int DATA_CONNECTION_CDMA = 4;
2278 public static final int DATA_CONNECTION_EVDO_0 = 5;
2279 public static final int DATA_CONNECTION_EVDO_A = 6;
2280 public static final int DATA_CONNECTION_1xRTT = 7;
2281 public static final int DATA_CONNECTION_HSDPA = 8;
2282 public static final int DATA_CONNECTION_HSUPA = 9;
2283 public static final int DATA_CONNECTION_HSPA = 10;
2284 public static final int DATA_CONNECTION_IDEN = 11;
2285 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002286 public static final int DATA_CONNECTION_LTE = 13;
2287 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002288 public static final int DATA_CONNECTION_HSPAP = 15;
Siddharth Rayc72081d2018-02-13 11:31:54 -08002289 public static final int DATA_CONNECTION_GSM = 16;
2290 public static final int DATA_CONNECTION_TD_SCDMA = 17;
2291 public static final int DATA_CONNECTION_IWLAN = 18;
2292 public static final int DATA_CONNECTION_LTE_CA = 19;
2293 public static final int DATA_CONNECTION_OTHER = 20;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002294
Dianne Hackborn627bba72009-03-24 22:32:56 -07002295 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002296 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002297 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Siddharth Rayc72081d2018-02-13 11:31:54 -08002298 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002299 };
Bookatzc8c44962017-05-11 12:12:54 -07002300
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002301 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002302
Dianne Hackborn627bba72009-03-24 22:32:56 -07002303 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002304 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002305 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002306 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002307 * {@hide}
2308 */
2309 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002310 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002313 * Returns the number of times the phone has entered the given data
2314 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002315 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002316 * {@hide}
2317 */
2318 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002319
Kweku Adams87b19ec2017-10-09 12:40:03 -07002320 /**
2321 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2322 */
2323 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2324
Dianne Hackborn3251b902014-06-20 14:40:53 -07002325 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2326 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2327 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2328 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2329 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2330 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2331 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2332 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2333 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2334 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2335 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2336 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2337 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2338
2339 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2340
2341 static final String[] WIFI_SUPPL_STATE_NAMES = {
2342 "invalid", "disconn", "disabled", "inactive", "scanning",
2343 "authenticating", "associating", "associated", "4-way-handshake",
2344 "group-handshake", "completed", "dormant", "uninit"
2345 };
2346
2347 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2348 "inv", "dsc", "dis", "inact", "scan",
2349 "auth", "ascing", "asced", "4-way",
2350 "group", "compl", "dorm", "uninit"
2351 };
2352
Mike Mac2f518a2017-09-19 16:06:03 -07002353 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002354 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002355 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2356 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002357 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002358 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2359 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2360 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002361 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002362 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002363 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002364 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002365 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2366 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002367 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002368 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2369 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2370 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2371 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2372 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2373 new String[] {"in", "out", "emergency", "off"},
2374 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002375 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2376 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2377 SignalStrength.SIGNAL_STRENGTH_NAMES,
2378 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002379 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2380 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2381 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002382 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002383
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002384 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2385 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002386 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002387 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002388 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002389 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002390 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002391 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2392 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2393 new String[] { "off", "light", "full", "???" },
2394 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002395 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2396 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2397 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002398 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2399 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2400 new String[] { "0", "1", "2", "3", "4" },
2401 new String[] { "0", "1", "2", "3", "4" }),
2402 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2403 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2404 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002405 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002406 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002407 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2408 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002409 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2410 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002411 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002412 };
2413
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002414 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002415 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002416 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2417 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002418 };
2419
2420 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002421 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002422 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002423 "Esw", "Ewa", "Elw", "Eec"
2424 };
2425
2426 @FunctionalInterface
2427 public interface IntToString {
2428 String applyAsString(int val);
2429 }
2430
2431 private static final IntToString sUidToString = UserHandle::formatUid;
2432 private static final IntToString sIntToString = Integer::toString;
2433
2434 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2435 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2436 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2437 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2438 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002439 };
2440
Dianne Hackborn617f8772009-03-31 15:04:46 -07002441 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002442 * Returns total time for WiFi Multicast Wakelock timer.
2443 * Note that this may be different from the sum of per uid timer values.
2444 *
2445 * {@hide}
2446 */
2447 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2448
2449 /**
2450 * Returns total time for WiFi Multicast Wakelock timer
2451 * Note that this may be different from the sum of per uid timer values.
2452 *
2453 * {@hide}
2454 */
2455 public abstract int getWifiMulticastWakelockCount(int which);
2456
2457 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002458 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002459 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002460 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002461 * {@hide}
2462 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002463 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002464
2465 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002466 * Returns the time in microseconds that wifi has been active while the device was
2467 * running on battery.
2468 *
2469 * {@hide}
2470 */
2471 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2472
2473 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002474 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002475 * been in the running state while the device was running on battery.
2476 *
2477 * {@hide}
2478 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002479 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002480
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002481 public static final int WIFI_STATE_OFF = 0;
2482 public static final int WIFI_STATE_OFF_SCANNING = 1;
2483 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2484 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2485 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2486 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2487 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2488 public static final int WIFI_STATE_SOFT_AP = 7;
2489
2490 static final String[] WIFI_STATE_NAMES = {
2491 "off", "scanning", "no_net", "disconn",
2492 "sta", "p2p", "sta_p2p", "soft_ap"
2493 };
2494
2495 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2496
2497 /**
2498 * Returns the time in microseconds that WiFi has been running in the given state.
2499 *
2500 * {@hide}
2501 */
2502 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002503 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002504
2505 /**
2506 * Returns the number of times that WiFi has entered the given state.
2507 *
2508 * {@hide}
2509 */
2510 public abstract int getWifiStateCount(int wifiState, int which);
2511
The Android Open Source Project10592532009-03-18 17:39:46 -07002512 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002513 * Returns the {@link Timer} object that tracks the given WiFi state.
2514 *
2515 * {@hide}
2516 */
2517 public abstract Timer getWifiStateTimer(int wifiState);
2518
2519 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002520 * Returns the time in microseconds that the wifi supplicant has been
2521 * in a given state.
2522 *
2523 * {@hide}
2524 */
2525 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2526
2527 /**
2528 * Returns the number of times that the wifi supplicant has transitioned
2529 * to a given state.
2530 *
2531 * {@hide}
2532 */
2533 public abstract int getWifiSupplStateCount(int state, int which);
2534
Kweku Adams87b19ec2017-10-09 12:40:03 -07002535 /**
2536 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2537 *
2538 * {@hide}
2539 */
2540 public abstract Timer getWifiSupplStateTimer(int state);
2541
Dianne Hackborn3251b902014-06-20 14:40:53 -07002542 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2543
2544 /**
2545 * Returns the time in microseconds that WIFI has been running with
2546 * the given signal strength.
2547 *
2548 * {@hide}
2549 */
2550 public abstract long getWifiSignalStrengthTime(int strengthBin,
2551 long elapsedRealtimeUs, int which);
2552
2553 /**
2554 * Returns the number of times WIFI has entered the given signal strength.
2555 *
2556 * {@hide}
2557 */
2558 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2559
2560 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002561 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2562 *
2563 * {@hide}
2564 */
2565 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2566
2567 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002568 * Returns the time in microseconds that the flashlight has been on while the device was
2569 * running on battery.
2570 *
2571 * {@hide}
2572 */
2573 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2574
2575 /**
2576 * Returns the number of times that the flashlight has been turned on while the device was
2577 * running on battery.
2578 *
2579 * {@hide}
2580 */
2581 public abstract long getFlashlightOnCount(int which);
2582
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002583 /**
2584 * Returns the time in microseconds that the camera has been on while the device was
2585 * running on battery.
2586 *
2587 * {@hide}
2588 */
2589 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2590
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002591 /**
2592 * Returns the time in microseconds that bluetooth scans were running while the device was
2593 * on battery.
2594 *
2595 * {@hide}
2596 */
2597 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002598
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002599 public static final int NETWORK_MOBILE_RX_DATA = 0;
2600 public static final int NETWORK_MOBILE_TX_DATA = 1;
2601 public static final int NETWORK_WIFI_RX_DATA = 2;
2602 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002603 public static final int NETWORK_BT_RX_DATA = 4;
2604 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002605 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2606 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2607 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2608 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2609 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002610
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002611 public abstract long getNetworkActivityBytes(int type, int which);
2612 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002613
Adam Lesinskie08af192015-03-25 16:42:59 -07002614 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002615 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002616 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002617 * actual power data.
2618 */
2619 public abstract boolean hasWifiActivityReporting();
2620
2621 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002622 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2623 * in various radio controller states, such as transmit, receive, and idle.
2624 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002625 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002626 public abstract ControllerActivityCounter getWifiControllerActivity();
2627
2628 /**
2629 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2630 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2631 * actual power data.
2632 */
2633 public abstract boolean hasBluetoothActivityReporting();
2634
2635 /**
2636 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2637 * in various radio controller states, such as transmit, receive, and idle.
2638 * @return non-null {@link ControllerActivityCounter}
2639 */
2640 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2641
2642 /**
2643 * Returns true if the BatteryStats object has detailed modem power reports.
2644 * When true, calling {@link #getModemControllerActivity()} will yield the
2645 * actual power data.
2646 */
2647 public abstract boolean hasModemActivityReporting();
2648
2649 /**
2650 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2651 * in various radio controller states, such as transmit, receive, and idle.
2652 * @return non-null {@link ControllerActivityCounter}
2653 */
2654 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002655
The Android Open Source Project10592532009-03-18 17:39:46 -07002656 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002657 * Return the wall clock time when battery stats data collection started.
2658 */
2659 public abstract long getStartClockTime();
2660
2661 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002662 * Return platform version tag that we were running in when the battery stats started.
2663 */
2664 public abstract String getStartPlatformVersion();
2665
2666 /**
2667 * Return platform version tag that we were running in when the battery stats ended.
2668 */
2669 public abstract String getEndPlatformVersion();
2670
2671 /**
2672 * Return the internal version code of the parcelled format.
2673 */
2674 public abstract int getParcelVersion();
2675
2676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 * Return whether we are currently running on battery.
2678 */
2679 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 /**
2682 * Returns a SparseArray containing the statistics for each uid.
2683 */
2684 public abstract SparseArray<? extends Uid> getUidStats();
2685
2686 /**
2687 * Returns the current battery uptime in microseconds.
2688 *
2689 * @param curTime the amount of elapsed realtime in microseconds.
2690 */
2691 public abstract long getBatteryUptime(long curTime);
2692
2693 /**
2694 * Returns the current battery realtime in microseconds.
2695 *
2696 * @param curTime the amount of elapsed realtime in microseconds.
2697 */
2698 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002699
The Android Open Source Project10592532009-03-18 17:39:46 -07002700 /**
Evan Millar633a1742009-04-02 16:36:33 -07002701 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002702 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002703 */
Evan Millar633a1742009-04-02 16:36:33 -07002704 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002705
The Android Open Source Project10592532009-03-18 17:39:46 -07002706 /**
Evan Millar633a1742009-04-02 16:36:33 -07002707 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2708 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002709 */
Evan Millar633a1742009-04-02 16:36:33 -07002710 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711
2712 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002713 * Get the amount the battery has discharged since the stats were
2714 * last reset after charging, as a lower-end approximation.
2715 */
2716 public abstract int getLowDischargeAmountSinceCharge();
2717
2718 /**
2719 * Get the amount the battery has discharged since the stats were
2720 * last reset after charging, as an upper-end approximation.
2721 */
2722 public abstract int getHighDischargeAmountSinceCharge();
2723
2724 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002725 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2726 */
2727 public abstract int getDischargeAmount(int which);
2728
2729 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002730 * Get the amount the battery has discharged while the screen was on,
2731 * since the last time power was unplugged.
2732 */
2733 public abstract int getDischargeAmountScreenOn();
2734
2735 /**
2736 * Get the amount the battery has discharged while the screen was on,
2737 * since the last time the device was charged.
2738 */
2739 public abstract int getDischargeAmountScreenOnSinceCharge();
2740
2741 /**
2742 * Get the amount the battery has discharged while the screen was off,
2743 * since the last time power was unplugged.
2744 */
2745 public abstract int getDischargeAmountScreenOff();
2746
2747 /**
2748 * Get the amount the battery has discharged while the screen was off,
2749 * since the last time the device was charged.
2750 */
2751 public abstract int getDischargeAmountScreenOffSinceCharge();
2752
2753 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002754 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002755 * since the last time power was unplugged.
2756 */
2757 public abstract int getDischargeAmountScreenDoze();
2758
2759 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002760 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002761 * since the last time the device was charged.
2762 */
2763 public abstract int getDischargeAmountScreenDozeSinceCharge();
2764
2765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 * Returns the total, last, or current battery uptime in microseconds.
2767 *
2768 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002769 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 */
2771 public abstract long computeBatteryUptime(long curTime, int which);
2772
2773 /**
2774 * Returns the total, last, or current battery realtime in microseconds.
2775 *
2776 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002777 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 */
2779 public abstract long computeBatteryRealtime(long curTime, int which);
2780
2781 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002782 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002783 *
2784 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002785 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002786 */
2787 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2788
2789 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002790 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002791 *
2792 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002793 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002794 */
2795 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2796
2797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 * Returns the total, last, or current uptime in microseconds.
2799 *
2800 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002801 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 */
2803 public abstract long computeUptime(long curTime, int which);
2804
2805 /**
2806 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002807 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002809 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 */
2811 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002812
2813 /**
2814 * Compute an approximation for how much run time (in microseconds) is remaining on
2815 * the battery. Returns -1 if no time can be computed: either there is not
2816 * enough current data to make a decision, or the battery is currently
2817 * charging.
2818 *
2819 * @param curTime The current elepsed realtime in microseconds.
2820 */
2821 public abstract long computeBatteryTimeRemaining(long curTime);
2822
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002823 // The part of a step duration that is the actual time.
2824 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2825
2826 // Bits in a step duration that are the new battery level we are at.
2827 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002828 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002829
2830 // Bits in a step duration that are the initial mode we were in at that step.
2831 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002832 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002833
2834 // Bits in a step duration that indicate which modes changed during that step.
2835 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002836 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002837
2838 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2839 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2840
Santos Cordone94f0502017-02-24 12:31:20 -08002841 // The largest value for screen state that is tracked in battery states. Any values above
2842 // this should be mapped back to one of the tracked values before being tracked here.
2843 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2844
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002845 // Step duration mode: power save is on.
2846 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2847
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002848 // Step duration mode: device is currently in idle mode.
2849 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2850
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002851 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2852 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002853 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2854 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2856 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2857 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2858 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2859 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002860 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2861 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002862 };
2863 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2864 (Display.STATE_OFF-1),
2865 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002866 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002867 (Display.STATE_ON-1),
2868 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2869 (Display.STATE_DOZE-1),
2870 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2871 (Display.STATE_DOZE_SUSPEND-1),
2872 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002873 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002874 };
2875 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2876 "screen off",
2877 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002878 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002879 "screen on",
2880 "screen on power save",
2881 "screen doze",
2882 "screen doze power save",
2883 "screen doze-suspend",
2884 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002885 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002886 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002887
2888 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002889 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002890 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2891 * a coulomb counter.
2892 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002893 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002894
2895 /**
2896 * Return the amount of battery discharge while the screen was in doze mode, measured in
2897 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2898 * a coulomb counter.
2899 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002900 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002901
2902 /**
2903 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2904 * non-zero only if the device's battery has a coulomb counter.
2905 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002906 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002907
2908 /**
Mike Ma15313c92017-11-15 17:58:21 -08002909 * @return the amount of battery discharge while the device is in light idle mode, measured in
2910 * micro-Ampere-hours.
2911 */
2912 public abstract long getUahDischargeLightDoze(int which);
2913
2914 /**
2915 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2916 * micro-Ampere-hours.
2917 */
2918 public abstract long getUahDischargeDeepDoze(int which);
2919
2920 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002921 * Returns the estimated real battery capacity, which may be less than the capacity
2922 * declared by the PowerProfile.
2923 * @return The estimated battery capacity in mAh.
2924 */
2925 public abstract int getEstimatedBatteryCapacity();
2926
2927 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002928 * @return The minimum learned battery capacity in uAh.
2929 */
2930 public abstract int getMinLearnedBatteryCapacity();
2931
2932 /**
2933 * @return The maximum learned battery capacity in uAh.
2934 */
2935 public abstract int getMaxLearnedBatteryCapacity() ;
2936
2937 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002938 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002939 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002940 public abstract LevelStepTracker getDischargeLevelStepTracker();
2941
2942 /**
2943 * Return the array of daily discharge step durations.
2944 */
2945 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002946
2947 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002948 * Compute an approximation for how much time (in microseconds) remains until the battery
2949 * is fully charged. Returns -1 if no time can be computed: either there is not
2950 * enough current data to make a decision, or the battery is currently
2951 * discharging.
2952 *
2953 * @param curTime The current elepsed realtime in microseconds.
2954 */
2955 public abstract long computeChargeTimeRemaining(long curTime);
2956
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002957 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002958 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002959 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002960 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002961
2962 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002963 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002964 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002965 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002966
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002967 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2968
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002969 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002970
Evan Millarc64edde2009-04-18 12:26:32 -07002971 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972
Bookatz50df7112017-08-04 14:53:26 -07002973 /**
2974 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2975 */
2976 public abstract Map<String, ? extends Timer> getRpmStats();
2977 /**
2978 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2979 */
2980 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2981
2982
James Carr2dd7e5e2016-07-20 18:48:39 -07002983 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2984
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002985 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2986
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002987 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 long days = seconds / (60 * 60 * 24);
2989 if (days != 0) {
2990 out.append(days);
2991 out.append("d ");
2992 }
2993 long used = days * 60 * 60 * 24;
2994
2995 long hours = (seconds - used) / (60 * 60);
2996 if (hours != 0 || used != 0) {
2997 out.append(hours);
2998 out.append("h ");
2999 }
3000 used += hours * 60 * 60;
3001
3002 long mins = (seconds-used) / 60;
3003 if (mins != 0 || used != 0) {
3004 out.append(mins);
3005 out.append("m ");
3006 }
3007 used += mins * 60;
3008
3009 if (seconds != 0 || used != 0) {
3010 out.append(seconds-used);
3011 out.append("s ");
3012 }
3013 }
3014
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003015 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003017 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 sb.append(time - (sec * 1000));
3019 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 }
3021
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003022 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003023 long sec = time / 1000;
3024 formatTimeRaw(sb, sec);
3025 sb.append(time - (sec * 1000));
3026 sb.append("ms");
3027 }
3028
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003029 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003031 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
3033 float perc = ((float)num) / ((float)den) * 100;
3034 mFormatBuilder.setLength(0);
3035 mFormatter.format("%.1f%%", perc);
3036 return mFormatBuilder.toString();
3037 }
3038
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003039 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003040 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003041
Evan Millar22ac0432009-03-31 11:33:18 -07003042 if (bytes < BYTES_PER_KB) {
3043 return bytes + "B";
3044 } else if (bytes < BYTES_PER_MB) {
3045 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3046 return mFormatBuilder.toString();
3047 } else if (bytes < BYTES_PER_GB){
3048 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3049 return mFormatBuilder.toString();
3050 } else {
3051 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3052 return mFormatBuilder.toString();
3053 }
3054 }
3055
Kweku Adams103351f2017-10-16 14:39:34 -07003056 private static long roundUsToMs(long timeUs) {
3057 return (timeUs + 500) / 1000;
3058 }
3059
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003060 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003061 if (timer != null) {
3062 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003063 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003064 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3065 return totalTimeMillis;
3066 }
3067 return 0;
3068 }
3069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 /**
3071 *
3072 * @param sb a StringBuilder object.
3073 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003074 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003076 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 * @param linePrefix a String to be prepended to each line of output.
3078 * @return the line prefix
3079 */
3080 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003081 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003084 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003085
Evan Millarc64edde2009-04-18 12:26:32 -07003086 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 if (totalTimeMillis != 0) {
3088 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003089 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003090 if (name != null) {
3091 sb.append(name);
3092 sb.append(' ');
3093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 sb.append('(');
3095 sb.append(count);
3096 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003097 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3098 if (maxDurationMs >= 0) {
3099 sb.append(" max=");
3100 sb.append(maxDurationMs);
3101 }
Bookatz506a8182017-05-01 14:18:42 -07003102 // Put actual time if it is available and different from totalTimeMillis.
3103 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3104 if (totalDurMs > totalTimeMillis) {
3105 sb.append(" actual=");
3106 sb.append(totalDurMs);
3107 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003108 if (timer.isRunningLocked()) {
3109 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3110 if (currentMs >= 0) {
3111 sb.append(" (running for ");
3112 sb.append(currentMs);
3113 sb.append("ms)");
3114 } else {
3115 sb.append(" (running)");
3116 }
3117 }
3118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 return ", ";
3120 }
3121 }
3122 return linePrefix;
3123 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003124
3125 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003126 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003127 *
3128 * @param pw a PrintWriter object to print to.
3129 * @param sb a StringBuilder object.
3130 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003131 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003132 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3133 * @param prefix a String to be prepended to each line of output.
3134 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003135 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003136 */
3137 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003138 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003139 if (timer != null) {
3140 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003141 final long totalTimeMs = (timer.getTotalTimeLocked(
3142 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003143 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003144 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003145 sb.setLength(0);
3146 sb.append(prefix);
3147 sb.append(" ");
3148 sb.append(type);
3149 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003150 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003151 sb.append("realtime (");
3152 sb.append(count);
3153 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003154 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3155 if (maxDurationMs >= 0) {
3156 sb.append(" max=");
3157 sb.append(maxDurationMs);
3158 }
3159 if (timer.isRunningLocked()) {
3160 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3161 if (currentMs >= 0) {
3162 sb.append(" (running for ");
3163 sb.append(currentMs);
3164 sb.append("ms)");
3165 } else {
3166 sb.append(" (running)");
3167 }
3168 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003169 pw.println(sb.toString());
3170 return true;
3171 }
3172 }
3173 return false;
3174 }
Bookatzc8c44962017-05-11 12:12:54 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 /**
3177 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 * @param sb a StringBuilder object.
3180 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003181 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003183 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 * @param linePrefix a String to be prepended to each line of output.
3185 * @return the line prefix
3186 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003187 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3188 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 long totalTimeMicros = 0;
3190 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003191 long max = 0;
3192 long current = 0;
3193 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003195 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003196 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003197 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3198 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003199 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
3201 sb.append(linePrefix);
3202 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3203 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003204 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003206 sb.append(',');
3207 sb.append(current);
3208 sb.append(',');
3209 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003210 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3211 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3212 // totalDuration independent of totalTimeMicros (since they are not pooled).
3213 if (name != null) {
3214 sb.append(',');
3215 sb.append(totalDuration);
3216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 return ",";
3218 }
Bookatz506a8182017-05-01 14:18:42 -07003219
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003220 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3221 String type) {
3222 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3223 pw.print(',');
3224 pw.print(uid);
3225 pw.print(',');
3226 pw.print(category);
3227 pw.print(',');
3228 pw.print(type);
3229 }
3230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 /**
3232 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 * @param pw the PageWriter to dump log to
3235 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3236 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3237 * @param args type-dependent data arguments
3238 */
Bookatzc8c44962017-05-11 12:12:54 -07003239 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003241 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003242 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003243 pw.print(',');
3244 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003246 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003248
3249 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003250 * Dump a given timer stat for terse checkin mode.
3251 *
3252 * @param pw the PageWriter to dump log to
3253 * @param uid the UID to log
3254 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3255 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3256 * @param timer a {@link Timer} to dump stats for
3257 * @param rawRealtime the current elapsed realtime of the system in microseconds
3258 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3259 */
3260 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3261 Timer timer, long rawRealtime, int which) {
3262 if (timer != null) {
3263 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003264 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003265 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003266 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003267 dumpLine(pw, uid, category, type, totalTime, count);
3268 }
3269 }
3270 }
3271
3272 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003273 * Dump a given timer stat to the proto stream.
3274 *
3275 * @param proto the ProtoOutputStream to log to
3276 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3277 * @param timer a {@link Timer} to dump stats for
3278 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3279 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3280 */
3281 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003282 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003283 if (timer == null) {
3284 return;
3285 }
3286 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003287 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003288 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003289 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3290 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3291 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3292 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3293 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003294 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003295 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003296 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003297 // These values will be -1 for timers that don't implement the functionality.
3298 if (maxDurationMs != -1) {
3299 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3300 }
3301 if (curDurationMs != -1) {
3302 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3303 }
3304 if (totalDurationMs != -1) {
3305 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3306 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003307 proto.end(token);
3308 }
3309 }
3310
3311 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003312 * Checks if the ControllerActivityCounter has any data worth dumping.
3313 */
3314 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3315 if (counter == null) {
3316 return false;
3317 }
3318
3319 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3320 || counter.getRxTimeCounter().getCountLocked(which) != 0
3321 || counter.getPowerCounter().getCountLocked(which) != 0) {
3322 return true;
3323 }
3324
3325 for (LongCounter c : counter.getTxTimeCounters()) {
3326 if (c.getCountLocked(which) != 0) {
3327 return true;
3328 }
3329 }
3330 return false;
3331 }
3332
3333 /**
3334 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3335 * The order of the arguments in the final check in line is:
3336 *
3337 * idle, rx, power, tx...
3338 *
3339 * where tx... is one or more transmit level times.
3340 */
3341 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3342 String type,
3343 ControllerActivityCounter counter,
3344 int which) {
3345 if (!controllerActivityHasData(counter, which)) {
3346 return;
3347 }
3348
3349 dumpLineHeader(pw, uid, category, type);
3350 pw.print(",");
3351 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3352 pw.print(",");
3353 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3354 pw.print(",");
3355 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3356 for (LongCounter c : counter.getTxTimeCounters()) {
3357 pw.print(",");
3358 pw.print(c.getCountLocked(which));
3359 }
3360 pw.println();
3361 }
3362
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003363 /**
3364 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3365 */
3366 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3367 ControllerActivityCounter counter,
3368 int which) {
3369 if (!controllerActivityHasData(counter, which)) {
3370 return;
3371 }
3372
3373 final long cToken = proto.start(fieldId);
3374
3375 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3376 counter.getIdleTimeCounter().getCountLocked(which));
3377 proto.write(ControllerActivityProto.RX_DURATION_MS,
3378 counter.getRxTimeCounter().getCountLocked(which));
3379 proto.write(ControllerActivityProto.POWER_MAH,
3380 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3381
3382 long tToken;
3383 LongCounter[] txCounters = counter.getTxTimeCounters();
3384 for (int i = 0; i < txCounters.length; ++i) {
3385 LongCounter c = txCounters[i];
3386 tToken = proto.start(ControllerActivityProto.TX);
3387 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3388 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3389 proto.end(tToken);
3390 }
3391
3392 proto.end(cToken);
3393 }
3394
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003395 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3396 String prefix, String controllerName,
3397 ControllerActivityCounter counter,
3398 int which) {
3399 if (controllerActivityHasData(counter, which)) {
3400 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3401 }
3402 }
3403
3404 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3405 String controllerName,
3406 ControllerActivityCounter counter, int which) {
3407 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3408 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3409 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003410 // Battery real time
3411 final long totalControllerActivityTimeMs
3412 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003413 long totalTxTimeMs = 0;
3414 for (LongCounter txState : counter.getTxTimeCounters()) {
3415 totalTxTimeMs += txState.getCountLocked(which);
3416 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003417
Siddharth Rayb50a6842017-12-14 15:15:28 -08003418 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3419 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3420 sb.setLength(0);
3421 sb.append(prefix);
3422 sb.append(" ");
3423 sb.append(controllerName);
3424 sb.append(" Scan time: ");
3425 formatTimeMs(sb, scanTimeMs);
3426 sb.append("(");
3427 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3428 sb.append(")");
3429 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003430
3431 final long sleepTimeMs
3432 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3433 sb.setLength(0);
3434 sb.append(prefix);
3435 sb.append(" ");
3436 sb.append(controllerName);
3437 sb.append(" Sleep time: ");
3438 formatTimeMs(sb, sleepTimeMs);
3439 sb.append("(");
3440 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3441 sb.append(")");
3442 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003443 }
3444
Siddharth Rayed754702018-02-15 12:44:37 -08003445 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3446 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3447 sb.setLength(0);
3448 sb.append(prefix);
3449 sb.append(" ");
3450 sb.append(controllerName);
3451 sb.append(" Sleep time: ");
3452 formatTimeMs(sb, sleepTimeMs);
3453 sb.append("(");
3454 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3455 sb.append(")");
3456 pw.println(sb.toString());
3457 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003458
3459 sb.setLength(0);
3460 sb.append(prefix);
3461 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003462 sb.append(controllerName);
3463 sb.append(" Idle time: ");
3464 formatTimeMs(sb, idleTimeMs);
3465 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003466 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003467 sb.append(")");
3468 pw.println(sb.toString());
3469
3470 sb.setLength(0);
3471 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003472 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003473 sb.append(controllerName);
3474 sb.append(" Rx time: ");
3475 formatTimeMs(sb, rxTimeMs);
3476 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003477 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003478 sb.append(")");
3479 pw.println(sb.toString());
3480
3481 sb.setLength(0);
3482 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003483 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003484 sb.append(controllerName);
3485 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003486
Siddharth Ray3c648c42017-10-02 17:30:58 -07003487 String [] powerLevel;
3488 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003489 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003490 powerLevel = new String[] {
3491 " less than 0dBm: ",
3492 " 0dBm to 8dBm: ",
3493 " 8dBm to 15dBm: ",
3494 " 15dBm to 20dBm: ",
3495 " above 20dBm: "};
3496 break;
3497 default:
3498 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3499 break;
3500 }
3501 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003502 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003503 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003504 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3505 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3506 sb.setLength(0);
3507 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003508 sb.append(" ");
3509 sb.append(powerLevel[lvl]);
3510 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003511 formatTimeMs(sb, txLvlTimeMs);
3512 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003513 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003514 sb.append(")");
3515 pw.println(sb.toString());
3516 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003517 } else {
3518 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3519 formatTimeMs(sb, txLvlTimeMs);
3520 sb.append("(");
3521 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3522 sb.append(")");
3523 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003524 }
3525
Siddharth Ray3c648c42017-10-02 17:30:58 -07003526 if (powerDrainMaMs > 0) {
3527 sb.setLength(0);
3528 sb.append(prefix);
3529 sb.append(" ");
3530 sb.append(controllerName);
3531 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003532 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003533 sb.append("mAh");
3534 pw.println(sb.toString());
3535 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003536 }
3537
3538 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003539 * Temporary for settings.
3540 */
3541 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3542 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3543 }
3544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 /**
3546 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003547 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003548 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003550 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3551 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003553 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3554 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3557 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3559 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3560 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 final long totalRealtime = computeRealtime(rawRealtime, which);
3562 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003563 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003564 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003565 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003566 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003567 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3568 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003569 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003570 rawRealtime, which);
3571 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3572 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003573 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003574 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003575 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003576 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003577 final long dischargeCount = getUahDischarge(which);
3578 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3579 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003580 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3581 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003582
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003583 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003584
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003585 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003586 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003587
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003588 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003591 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003592 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003593 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003594 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003595 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003596 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003597 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003598 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3599 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003600
Bookatzc8c44962017-05-11 12:12:54 -07003601
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003602 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003603 long fullWakeLockTimeTotal = 0;
3604 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003605
Evan Millar22ac0432009-03-31 11:33:18 -07003606 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003607 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003608
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003609 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3610 = u.getWakelockStats();
3611 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3612 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003613
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003614 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3615 if (fullWakeTimer != null) {
3616 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3617 which);
3618 }
3619
3620 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3621 if (partialWakeTimer != null) {
3622 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3623 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003624 }
3625 }
3626 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003627
3628 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003629 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3630 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3631 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3632 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3633 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3634 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3635 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3636 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003637 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3638 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003639 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3640 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003641 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3642 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003643
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003644 // Dump Modem controller stats
3645 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3646 getModemControllerActivity(), which);
3647
Adam Lesinskie283d332015-04-16 12:29:25 -07003648 // Dump Wifi controller stats
3649 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3650 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003651 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003652 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003653
3654 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3655 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003656
3657 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003658 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3659 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 // Dump misc stats
3662 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003663 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003664 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003665 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003666 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003667 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003668 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3669 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003670 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003671 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3672 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3673 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3674 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003675 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003676
Dianne Hackborn617f8772009-03-31 15:04:46 -07003677 // Dump screen brightness stats
3678 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3679 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003680 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003681 }
3682 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003683
Dianne Hackborn627bba72009-03-24 22:32:56 -07003684 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003685 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3686 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003687 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003688 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003689 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003690 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003691 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003692 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003693 args[i] = getPhoneSignalStrengthCount(i, which);
3694 }
3695 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003696
Dianne Hackborn627bba72009-03-24 22:32:56 -07003697 // Dump network type stats
3698 args = new Object[NUM_DATA_CONNECTION_TYPES];
3699 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003700 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003701 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003702 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3703 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3704 args[i] = getPhoneDataConnectionCount(i, which);
3705 }
3706 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003707
3708 // Dump wifi state stats
3709 args = new Object[NUM_WIFI_STATES];
3710 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003711 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003712 }
3713 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3714 for (int i=0; i<NUM_WIFI_STATES; i++) {
3715 args[i] = getWifiStateCount(i, which);
3716 }
3717 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3718
Dianne Hackborn3251b902014-06-20 14:40:53 -07003719 // Dump wifi suppl state stats
3720 args = new Object[NUM_WIFI_SUPPL_STATES];
3721 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3722 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3723 }
3724 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3725 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3726 args[i] = getWifiSupplStateCount(i, which);
3727 }
3728 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3729
3730 // Dump wifi signal strength stats
3731 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3732 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3733 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3734 }
3735 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3736 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3737 args[i] = getWifiSignalStrengthCount(i, which);
3738 }
3739 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3740
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003741 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003742 final long multicastWakeLockTimeTotalMicros =
3743 getWifiMulticastWakelockTime(rawRealtime, which);
3744 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003745 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3746 multicastWakeLockTimeTotalMicros / 1000,
3747 multicastWakeLockCountTotal);
3748
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003749 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003750 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003751 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003752 }
Bookatzc8c44962017-05-11 12:12:54 -07003753
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003754 if (which == STATS_SINCE_UNPLUGGED) {
3755 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3756 getDischargeStartLevel()-getDischargeCurrentLevel(),
3757 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003758 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003759 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003760 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3761 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003762 } else {
3763 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3764 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003765 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003766 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003767 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003768 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3769 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003770 }
Bookatzc8c44962017-05-11 12:12:54 -07003771
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003772 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003773 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003774 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003775 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003776 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003777 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003778 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3779 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003780 }
Evan Millarc64edde2009-04-18 12:26:32 -07003781 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003782 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003783 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003784 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3785 // Not doing the regular wake lock formatting to remain compatible
3786 // with the old checkin format.
3787 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3788 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003789 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003790 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003791 }
3792 }
Evan Millarc64edde2009-04-18 12:26:32 -07003793 }
Bookatzc8c44962017-05-11 12:12:54 -07003794
Bookatz50df7112017-08-04 14:53:26 -07003795 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3796 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3797 if (rpmStats.size() > 0) {
3798 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3799 sb.setLength(0);
3800 Timer totalTimer = ent.getValue();
3801 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3802 int count = totalTimer.getCountLocked(which);
3803 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3804 long screenOffTimeMs = screenOffTimer != null
3805 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3806 int screenOffCount = screenOffTimer != null
3807 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003808 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3809 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3810 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3811 screenOffCount);
3812 } else {
3813 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3814 "\"" + ent.getKey() + "\"", timeMs, count);
3815 }
Bookatz50df7112017-08-04 14:53:26 -07003816 }
3817 }
3818
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003819 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003820 helper.create(this);
3821 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003822 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003823 if (sippers != null && sippers.size() > 0) {
3824 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3825 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003826 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003827 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3828 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003829 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003830 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003831 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003832 String label;
3833 switch (bs.drainType) {
3834 case IDLE:
3835 label="idle";
3836 break;
3837 case CELL:
3838 label="cell";
3839 break;
3840 case PHONE:
3841 label="phone";
3842 break;
3843 case WIFI:
3844 label="wifi";
3845 break;
3846 case BLUETOOTH:
3847 label="blue";
3848 break;
3849 case SCREEN:
3850 label="scrn";
3851 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003852 case FLASHLIGHT:
3853 label="flashlight";
3854 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003855 case APP:
3856 uid = bs.uidObj.getUid();
3857 label = "uid";
3858 break;
3859 case USER:
3860 uid = UserHandle.getUid(bs.userId, 0);
3861 label = "user";
3862 break;
3863 case UNACCOUNTED:
3864 label = "unacc";
3865 break;
3866 case OVERCOUNTED:
3867 label = "over";
3868 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003869 case CAMERA:
3870 label = "camera";
3871 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003872 case MEMORY:
3873 label = "memory";
3874 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003875 default:
3876 label = "???";
3877 }
3878 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003879 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3880 bs.shouldHide ? 1 : 0,
3881 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3882 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003883 }
3884 }
3885
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003886 final long[] cpuFreqs = getCpuFreqs();
3887 if (cpuFreqs != null) {
3888 sb.setLength(0);
3889 for (int i = 0; i < cpuFreqs.length; ++i) {
3890 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3891 }
3892 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3893 }
3894
Kweku Adams87b19ec2017-10-09 12:40:03 -07003895 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 for (int iu = 0; iu < NU; iu++) {
3897 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003898 if (reqUid >= 0 && uid != reqUid) {
3899 continue;
3900 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003901 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003904 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3905 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3906 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3907 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3908 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3909 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3910 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3911 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003912 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003913 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3914 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003915 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003916 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3917 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003918 // Background data transfers
3919 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3920 which);
3921 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3922 which);
3923 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3924 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3925 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3926 which);
3927 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3928 which);
3929 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3930 which);
3931 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3932 which);
3933
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003934 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3935 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003936 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003937 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3938 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3939 || wifiBytesBgTx > 0
3940 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3941 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003942 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3943 wifiBytesRx, wifiBytesTx,
3944 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003945 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003946 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003947 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3948 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3949 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3950 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003951 }
3952
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003953 // Dump modem controller data, per UID.
3954 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3955 u.getModemControllerActivity(), which);
3956
3957 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003958 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3959 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3960 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003961 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3962 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003963 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3964 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3965 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003966 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003967 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003968 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3969 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003970 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3971 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003972 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003973 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003976 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3977 u.getWifiControllerActivity(), which);
3978
Bookatz867c0d72017-03-07 18:23:42 -08003979 final Timer bleTimer = u.getBluetoothScanTimer();
3980 if (bleTimer != null) {
3981 // Convert from microseconds to milliseconds with rounding
3982 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3983 / 1000;
3984 if (totalTime != 0) {
3985 final int count = bleTimer.getCountLocked(which);
3986 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3987 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003988 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3989 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3990 final long actualTimeBg = bleTimerBg != null ?
3991 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003992 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003993 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3994 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003995 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3996 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3997 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3998 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3999 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4000 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4001 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4002 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4003 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4004 final Timer unoptimizedScanTimerBg =
4005 u.getBluetoothUnoptimizedScanBackgroundTimer();
4006 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4007 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4008 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4009 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4010
Bookatz867c0d72017-03-07 18:23:42 -08004011 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07004012 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4013 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4014 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004015 }
4016 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004017
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004018 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4019 u.getBluetoothControllerActivity(), which);
4020
Dianne Hackborn617f8772009-03-31 15:04:46 -07004021 if (u.hasUserActivity()) {
4022 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4023 boolean hasData = false;
4024 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4025 int val = u.getUserActivityCount(i, which);
4026 args[i] = val;
4027 if (val != 0) hasData = true;
4028 }
4029 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004030 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004031 }
4032 }
Bookatzc8c44962017-05-11 12:12:54 -07004033
4034 if (u.getAggregatedPartialWakelockTimer() != null) {
4035 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004036 // Times are since reset (regardless of 'which')
4037 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004038 final Timer bgTimer = timer.getSubTimer();
4039 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004040 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004041 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4042 }
4043
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004044 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4045 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4046 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4047 String linePrefix = "";
4048 sb.setLength(0);
4049 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4050 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004051 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4052 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004053 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004054 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4055 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004056 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4057 rawRealtime, "w", which, linePrefix);
4058
Kweku Adams103351f2017-10-16 14:39:34 -07004059 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004060 if (sb.length() > 0) {
4061 String name = wakelocks.keyAt(iw);
4062 if (name.indexOf(',') >= 0) {
4063 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
Yi Jin02483362017-08-04 11:30:44 -07004065 if (name.indexOf('\n') >= 0) {
4066 name = name.replace('\n', '_');
4067 }
4068 if (name.indexOf('\r') >= 0) {
4069 name = name.replace('\r', '_');
4070 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004071 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 }
4073 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004074
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004075 // WiFi Multicast Wakelock Statistics
4076 final Timer mcTimer = u.getMulticastWakelockStats();
4077 if (mcTimer != null) {
4078 final long totalMcWakelockTimeMs =
4079 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4080 final int countMcWakelock = mcTimer.getCountLocked(which);
4081 if(totalMcWakelockTimeMs > 0) {
4082 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4083 totalMcWakelockTimeMs, countMcWakelock);
4084 }
4085 }
4086
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004087 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4088 for (int isy=syncs.size()-1; isy>=0; isy--) {
4089 final Timer timer = syncs.valueAt(isy);
4090 // Convert from microseconds to milliseconds with rounding
4091 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4092 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004093 final Timer bgTimer = timer.getSubTimer();
4094 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004095 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004096 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004097 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004098 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004099 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004100 }
4101 }
4102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004103 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4104 for (int ij=jobs.size()-1; ij>=0; ij--) {
4105 final Timer timer = jobs.valueAt(ij);
4106 // Convert from microseconds to milliseconds with rounding
4107 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4108 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004109 final Timer bgTimer = timer.getSubTimer();
4110 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004111 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004112 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004113 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004114 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004115 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004116 }
4117 }
4118
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004119 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4120 for (int ic=completions.size()-1; ic>=0; ic--) {
4121 SparseIntArray types = completions.valueAt(ic);
4122 if (types != null) {
4123 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4124 "\"" + completions.keyAt(ic) + "\"",
4125 types.get(JobParameters.REASON_CANCELED, 0),
4126 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4127 types.get(JobParameters.REASON_PREEMPT, 0),
4128 types.get(JobParameters.REASON_TIMEOUT, 0),
4129 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4130 }
4131 }
4132
Amith Yamasani977e11f2018-02-16 11:29:54 -08004133 // Dump deferred jobs stats
4134 u.getDeferredJobsCheckinLineLocked(sb, which);
4135 if (sb.length() > 0) {
4136 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4137 }
4138
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004139 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4140 rawRealtime, which);
4141 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4142 rawRealtime, which);
4143 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4144 rawRealtime, which);
4145 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4146 rawRealtime, which);
4147
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004148 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4149 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004150 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004151 final Uid.Sensor se = sensors.valueAt(ise);
4152 final int sensorNumber = sensors.keyAt(ise);
4153 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004154 if (timer != null) {
4155 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004156 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4157 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004158 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004159 final int count = timer.getCountLocked(which);
4160 final Timer bgTimer = se.getSensorBackgroundTime();
4161 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004162 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4163 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4164 final long bgActualTime = bgTimer != null ?
4165 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4166 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4167 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 }
4170 }
4171
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004172 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4173 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004174
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004175 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4176 rawRealtime, which);
4177
4178 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004179 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004180
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004181 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004182 long totalStateTime = 0;
4183 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004184 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4185 totalStateTime += time;
4186 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004187 }
4188 if (totalStateTime > 0) {
4189 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4190 }
4191
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004192 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4193 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004194 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004195 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004196 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004197 }
4198
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004199 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4200 if (cpuFreqs != null) {
4201 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4202 // If total cpuFreqTimes is null, then we don't need to check for
4203 // screenOffCpuFreqTimes.
4204 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4205 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004206 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004207 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004208 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004209 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4210 if (screenOffCpuFreqTimeMs != null) {
4211 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4212 sb.append("," + screenOffCpuFreqTimeMs[i]);
4213 }
4214 } else {
4215 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4216 sb.append(",0");
4217 }
4218 }
4219 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4220 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004221 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004222
4223 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4224 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4225 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4226 sb.setLength(0);
4227 for (int i = 0; i < timesMs.length; ++i) {
4228 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4229 }
4230 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4231 which, procState);
4232 if (screenOffTimesMs != null) {
4233 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4234 sb.append("," + screenOffTimesMs[i]);
4235 }
4236 } else {
4237 for (int i = 0; i < timesMs.length; ++i) {
4238 sb.append(",0");
4239 }
4240 }
4241 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4242 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4243 }
4244 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004245 }
4246
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004247 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4248 = u.getProcessStats();
4249 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4250 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004251
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004252 final long userMillis = ps.getUserTime(which);
4253 final long systemMillis = ps.getSystemTime(which);
4254 final long foregroundMillis = ps.getForegroundTime(which);
4255 final int starts = ps.getStarts(which);
4256 final int numCrashes = ps.getNumCrashes(which);
4257 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004258
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004259 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4260 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004261 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4262 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 }
4264 }
4265
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004266 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4267 = u.getPackageStats();
4268 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4269 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4270 int wakeups = 0;
4271 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4272 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004273 int count = alarms.valueAt(iwa).getCountLocked(which);
4274 wakeups += count;
4275 String name = alarms.keyAt(iwa).replace(',', '_');
4276 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004277 }
4278 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4279 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4280 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4281 final long startTime = ss.getStartTime(batteryUptime, which);
4282 final int starts = ss.getStarts(which);
4283 final int launches = ss.getLaunches(which);
4284 if (startTime != 0 || starts != 0 || launches != 0) {
4285 dumpLine(pw, uid, category, APK_DATA,
4286 wakeups, // wakeup alarms
4287 packageStats.keyAt(ipkg), // Apk
4288 serviceStats.keyAt(isvc), // service
4289 startTime / 1000, // time spent started, in ms
4290 starts,
4291 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 }
4293 }
4294 }
4295 }
4296 }
4297
Dianne Hackborn81038902012-11-26 17:04:09 -08004298 static final class TimerEntry {
4299 final String mName;
4300 final int mId;
4301 final BatteryStats.Timer mTimer;
4302 final long mTime;
4303 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4304 mName = name;
4305 mId = id;
4306 mTimer = timer;
4307 mTime = time;
4308 }
4309 }
4310
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004311 private void printmAh(PrintWriter printer, double power) {
4312 printer.print(BatteryStatsHelper.makemAh(power));
4313 }
4314
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004315 private void printmAh(StringBuilder sb, double power) {
4316 sb.append(BatteryStatsHelper.makemAh(power));
4317 }
4318
Dianne Hackbornd953c532014-08-16 18:17:38 -07004319 /**
4320 * Temporary for settings.
4321 */
4322 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4323 int reqUid) {
4324 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4325 }
4326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004328 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004329 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4331 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004332 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334
4335 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4336 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4337 final long totalRealtime = computeRealtime(rawRealtime, which);
4338 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004339 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4340 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4341 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004342 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4343 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004344 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004345
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004346 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004347
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004348 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004349 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004351 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4352 if (estimatedBatteryCapacity > 0) {
4353 sb.setLength(0);
4354 sb.append(prefix);
4355 sb.append(" Estimated battery capacity: ");
4356 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4357 sb.append(" mAh");
4358 pw.println(sb.toString());
4359 }
4360
Jocelyn Dangc627d102017-04-14 13:15:14 -07004361 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4362 if (minLearnedBatteryCapacity > 0) {
4363 sb.setLength(0);
4364 sb.append(prefix);
4365 sb.append(" Min learned battery capacity: ");
4366 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4367 sb.append(" mAh");
4368 pw.println(sb.toString());
4369 }
4370 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4371 if (maxLearnedBatteryCapacity > 0) {
4372 sb.setLength(0);
4373 sb.append(prefix);
4374 sb.append(" Max learned battery capacity: ");
4375 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4376 sb.append(" mAh");
4377 pw.println(sb.toString());
4378 }
4379
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004380 sb.setLength(0);
4381 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004382 sb.append(" Time on battery: ");
4383 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4384 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4385 sb.append(") realtime, ");
4386 formatTimeMs(sb, whichBatteryUptime / 1000);
4387 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4388 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004389 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004390
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004391 sb.setLength(0);
4392 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004393 sb.append(" Time on battery screen off: ");
4394 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4395 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4396 sb.append(") realtime, ");
4397 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4398 sb.append("(");
4399 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4400 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004401 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004402
4403 sb.setLength(0);
4404 sb.append(prefix);
4405 sb.append(" Time on battery screen doze: ");
4406 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4407 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4408 sb.append(")");
4409 pw.println(sb.toString());
4410
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004411 sb.setLength(0);
4412 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004413 sb.append(" Total run time: ");
4414 formatTimeMs(sb, totalRealtime / 1000);
4415 sb.append("realtime, ");
4416 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004417 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004418 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004419 if (batteryTimeRemaining >= 0) {
4420 sb.setLength(0);
4421 sb.append(prefix);
4422 sb.append(" Battery time remaining: ");
4423 formatTimeMs(sb, batteryTimeRemaining / 1000);
4424 pw.println(sb.toString());
4425 }
4426 if (chargeTimeRemaining >= 0) {
4427 sb.setLength(0);
4428 sb.append(prefix);
4429 sb.append(" Charge time remaining: ");
4430 formatTimeMs(sb, chargeTimeRemaining / 1000);
4431 pw.println(sb.toString());
4432 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004433
Kweku Adams87b19ec2017-10-09 12:40:03 -07004434 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004435 if (dischargeCount >= 0) {
4436 sb.setLength(0);
4437 sb.append(prefix);
4438 sb.append(" Discharge: ");
4439 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4440 sb.append(" mAh");
4441 pw.println(sb.toString());
4442 }
4443
Kweku Adams87b19ec2017-10-09 12:40:03 -07004444 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004445 if (dischargeScreenOffCount >= 0) {
4446 sb.setLength(0);
4447 sb.append(prefix);
4448 sb.append(" Screen off discharge: ");
4449 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4450 sb.append(" mAh");
4451 pw.println(sb.toString());
4452 }
4453
Kweku Adams87b19ec2017-10-09 12:40:03 -07004454 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004455 if (dischargeScreenDozeCount >= 0) {
4456 sb.setLength(0);
4457 sb.append(prefix);
4458 sb.append(" Screen doze discharge: ");
4459 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4460 sb.append(" mAh");
4461 pw.println(sb.toString());
4462 }
4463
4464 final long dischargeScreenOnCount =
4465 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004466 if (dischargeScreenOnCount >= 0) {
4467 sb.setLength(0);
4468 sb.append(prefix);
4469 sb.append(" Screen on discharge: ");
4470 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4471 sb.append(" mAh");
4472 pw.println(sb.toString());
4473 }
4474
Mike Ma15313c92017-11-15 17:58:21 -08004475 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4476 if (dischargeLightDozeCount >= 0) {
4477 sb.setLength(0);
4478 sb.append(prefix);
4479 sb.append(" Device light doze discharge: ");
4480 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4481 sb.append(" mAh");
4482 pw.println(sb.toString());
4483 }
4484
4485 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4486 if (dischargeDeepDozeCount >= 0) {
4487 sb.setLength(0);
4488 sb.append(prefix);
4489 sb.append(" Device deep doze discharge: ");
4490 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4491 sb.append(" mAh");
4492 pw.println(sb.toString());
4493 }
4494
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004495 pw.print(" Start clock time: ");
4496 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4497
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004498 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004499 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004500 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004501 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4502 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004503 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004504 rawRealtime, which);
4505 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4506 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004507 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004508 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004509 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4510 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4511 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004512 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004513 sb.append(prefix);
4514 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4515 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004516 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004517 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4518 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004519 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004520 pw.println(sb.toString());
4521 sb.setLength(0);
4522 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004523 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004524 boolean didOne = false;
4525 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004526 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004527 if (time == 0) {
4528 continue;
4529 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004530 sb.append("\n ");
4531 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004532 didOne = true;
4533 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4534 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004535 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004536 sb.append("(");
4537 sb.append(formatRatioLocked(time, screenOnTime));
4538 sb.append(")");
4539 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004540 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004541 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004542 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004543 sb.setLength(0);
4544 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004545 sb.append(" Power save mode enabled: ");
4546 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004547 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004548 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004549 sb.append(")");
4550 pw.println(sb.toString());
4551 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004552 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004553 sb.setLength(0);
4554 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004555 sb.append(" Device light idling: ");
4556 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004557 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004558 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4559 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004560 sb.append("x");
4561 pw.println(sb.toString());
4562 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004563 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004564 sb.setLength(0);
4565 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004566 sb.append(" Idle mode light time: ");
4567 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004568 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004569 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4570 sb.append(") ");
4571 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004572 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004573 sb.append(" -- longest ");
4574 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4575 pw.println(sb.toString());
4576 }
4577 if (deviceIdlingTime != 0) {
4578 sb.setLength(0);
4579 sb.append(prefix);
4580 sb.append(" Device full idling: ");
4581 formatTimeMs(sb, deviceIdlingTime / 1000);
4582 sb.append("(");
4583 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004584 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004585 sb.append("x");
4586 pw.println(sb.toString());
4587 }
4588 if (deviceIdleModeFullTime != 0) {
4589 sb.setLength(0);
4590 sb.append(prefix);
4591 sb.append(" Idle mode full time: ");
4592 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4593 sb.append("(");
4594 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4595 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004596 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004597 sb.append("x");
4598 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004599 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004600 pw.println(sb.toString());
4601 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004602 if (phoneOnTime != 0) {
4603 sb.setLength(0);
4604 sb.append(prefix);
4605 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4606 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004607 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004608 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004609 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004610 if (connChanges != 0) {
4611 pw.print(prefix);
4612 pw.print(" Connectivity changes: "); pw.println(connChanges);
4613 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004614
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004615 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004616 long fullWakeLockTimeTotalMicros = 0;
4617 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004618
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004619 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004620
Evan Millar22ac0432009-03-31 11:33:18 -07004621 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004622 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004623
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004624 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4625 = u.getWakelockStats();
4626 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4627 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004628
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004629 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4630 if (fullWakeTimer != null) {
4631 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4632 rawRealtime, which);
4633 }
4634
4635 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4636 if (partialWakeTimer != null) {
4637 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4638 rawRealtime, which);
4639 if (totalTimeMicros > 0) {
4640 if (reqUid < 0) {
4641 // Only show the ordered list of all wake
4642 // locks if the caller is not asking for data
4643 // about a specific uid.
4644 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4645 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004646 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004647 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004648 }
4649 }
4650 }
4651 }
Bookatzc8c44962017-05-11 12:12:54 -07004652
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004653 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4654 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4655 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4656 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4657 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4658 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4659 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4660 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004661 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4662 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004663
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004664 if (fullWakeLockTimeTotalMicros != 0) {
4665 sb.setLength(0);
4666 sb.append(prefix);
4667 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4668 (fullWakeLockTimeTotalMicros + 500) / 1000);
4669 pw.println(sb.toString());
4670 }
4671
4672 if (partialWakeLockTimeTotalMicros != 0) {
4673 sb.setLength(0);
4674 sb.append(prefix);
4675 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4676 (partialWakeLockTimeTotalMicros + 500) / 1000);
4677 pw.println(sb.toString());
4678 }
4679
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004680 final long multicastWakeLockTimeTotalMicros =
4681 getWifiMulticastWakelockTime(rawRealtime, which);
4682 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004683 if (multicastWakeLockTimeTotalMicros != 0) {
4684 sb.setLength(0);
4685 sb.append(prefix);
4686 sb.append(" Total WiFi Multicast wakelock Count: ");
4687 sb.append(multicastWakeLockCountTotal);
4688 pw.println(sb.toString());
4689
4690 sb.setLength(0);
4691 sb.append(prefix);
4692 sb.append(" Total WiFi Multicast wakelock time: ");
4693 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4694 pw.println(sb.toString());
4695 }
4696
Siddharth Ray3c648c42017-10-02 17:30:58 -07004697 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004698 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004699 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004700 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004701 sb.append(" CONNECTIVITY POWER SUMMARY START");
4702 pw.println(sb.toString());
4703
4704 pw.print(prefix);
4705 sb.setLength(0);
4706 sb.append(prefix);
4707 sb.append(" Logging duration for connectivity statistics: ");
4708 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004709 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004710
4711 sb.setLength(0);
4712 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004713 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004714 pw.println(sb.toString());
4715
Siddharth Ray3c648c42017-10-02 17:30:58 -07004716 pw.print(prefix);
4717 sb.setLength(0);
4718 sb.append(prefix);
4719 sb.append(" Cellular kernel active time: ");
4720 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4721 formatTimeMs(sb, mobileActiveTime / 1000);
4722 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4723 sb.append(")");
4724 pw.println(sb.toString());
4725
4726 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4727 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4728 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4729 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4730
Dianne Hackborn627bba72009-03-24 22:32:56 -07004731 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004732 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004733 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004734 didOne = false;
4735 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004736 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004737 if (time == 0) {
4738 continue;
4739 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004740 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004741 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004742 didOne = true;
4743 sb.append(DATA_CONNECTION_NAMES[i]);
4744 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004745 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004746 sb.append("(");
4747 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004748 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004749 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004750 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004751 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004752
4753 sb.setLength(0);
4754 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004755 sb.append(" Cellular Rx signal strength (RSRP):");
4756 final String[] cellularRxSignalStrengthDescription = new String[]{
4757 "very poor (less than -128dBm): ",
4758 "poor (-128dBm to -118dBm): ",
4759 "moderate (-118dBm to -108dBm): ",
4760 "good (-108dBm to -98dBm): ",
4761 "great (greater than -98dBm): "};
4762 didOne = false;
4763 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4764 cellularRxSignalStrengthDescription.length);
4765 for (int i=0; i<numCellularRxBins; i++) {
4766 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4767 if (time == 0) {
4768 continue;
4769 }
4770 sb.append("\n ");
4771 sb.append(prefix);
4772 didOne = true;
4773 sb.append(cellularRxSignalStrengthDescription[i]);
4774 sb.append(" ");
4775 formatTimeMs(sb, time/1000);
4776 sb.append("(");
4777 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4778 sb.append(") ");
4779 }
4780 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004781 pw.println(sb.toString());
4782
Siddharth Rayb50a6842017-12-14 15:15:28 -08004783 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004784 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004785
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004786 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004787 sb.setLength(0);
4788 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004789 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004790 pw.println(sb.toString());
4791
Siddharth Rayb50a6842017-12-14 15:15:28 -08004792 pw.print(prefix);
4793 sb.setLength(0);
4794 sb.append(prefix);
4795 sb.append(" Wifi kernel active time: ");
4796 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4797 formatTimeMs(sb, wifiActiveTime / 1000);
4798 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4799 sb.append(")");
4800 pw.println(sb.toString());
4801
Siddharth Ray3c648c42017-10-02 17:30:58 -07004802 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4803 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4804 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4805 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4806
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 sb.setLength(0);
4808 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004809 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004810 didOne = false;
4811 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004812 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004813 if (time == 0) {
4814 continue;
4815 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004816 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004817 didOne = true;
4818 sb.append(WIFI_STATE_NAMES[i]);
4819 sb.append(" ");
4820 formatTimeMs(sb, time/1000);
4821 sb.append("(");
4822 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4823 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004824 }
4825 if (!didOne) sb.append(" (no activity)");
4826 pw.println(sb.toString());
4827
4828 sb.setLength(0);
4829 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004830 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004831 didOne = false;
4832 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4833 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4834 if (time == 0) {
4835 continue;
4836 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004837 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004838 didOne = true;
4839 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4840 sb.append(" ");
4841 formatTimeMs(sb, time/1000);
4842 sb.append("(");
4843 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4844 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004845 }
4846 if (!didOne) sb.append(" (no activity)");
4847 pw.println(sb.toString());
4848
4849 sb.setLength(0);
4850 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004851 sb.append(" Wifi Rx signal strength (RSSI):");
4852 final String[] wifiRxSignalStrengthDescription = new String[]{
4853 "very poor (less than -88.75dBm): ",
4854 "poor (-88.75 to -77.5dBm): ",
4855 "moderate (-77.5dBm to -66.25dBm): ",
4856 "good (-66.25dBm to -55dBm): ",
4857 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004858 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004859 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4860 wifiRxSignalStrengthDescription.length);
4861 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004862 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4863 if (time == 0) {
4864 continue;
4865 }
4866 sb.append("\n ");
4867 sb.append(prefix);
4868 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004869 sb.append(" ");
4870 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004871 formatTimeMs(sb, time/1000);
4872 sb.append("(");
4873 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4874 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004875 }
4876 if (!didOne) sb.append(" (no activity)");
4877 pw.println(sb.toString());
4878
Siddharth Rayb50a6842017-12-14 15:15:28 -08004879 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4880 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004881
Adam Lesinski50e47602015-12-04 17:04:54 -08004882 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004883 sb.setLength(0);
4884 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004885 sb.append(" GPS Statistics:");
4886 pw.println(sb.toString());
4887
4888 sb.setLength(0);
4889 sb.append(prefix);
4890 sb.append(" GPS signal quality (Top 4 Average CN0):");
4891 final String[] gpsSignalQualityDescription = new String[]{
4892 "poor (less than 20 dBHz): ",
4893 "good (greater than 20 dBHz): "};
4894 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4895 gpsSignalQualityDescription.length);
4896 for (int i=0; i<numGpsSignalQualityBins; i++) {
4897 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4898 sb.append("\n ");
4899 sb.append(prefix);
4900 sb.append(" ");
4901 sb.append(gpsSignalQualityDescription[i]);
4902 formatTimeMs(sb, time/1000);
4903 sb.append("(");
4904 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4905 sb.append(") ");
4906 }
4907 pw.println(sb.toString());
4908
4909 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4910 if (gpsBatteryDrainMaMs > 0) {
4911 pw.print(prefix);
4912 sb.setLength(0);
4913 sb.append(prefix);
4914 sb.append(" Battery Drain (mAh): ");
4915 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4916 pw.println(sb.toString());
4917 }
4918
4919 pw.print(prefix);
4920 sb.setLength(0);
4921 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004922 sb.append(" CONNECTIVITY POWER SUMMARY END");
4923 pw.println(sb.toString());
4924 pw.println("");
4925
4926 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004927 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4928 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4929
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004930 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4931 sb.setLength(0);
4932 sb.append(prefix);
4933 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4934 pw.println(sb.toString());
4935
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004936 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4937 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004938
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004939 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004940
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004941 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004942 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004943 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004944 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004945 pw.println(getDischargeStartLevel());
4946 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4947 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004948 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004949 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004950 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004951 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004952 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004953 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004954 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004955 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004956 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004957 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004958 pw.println(getDischargeAmountScreenOff());
4959 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4960 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004961 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004962 } else {
4963 pw.print(prefix); pw.println(" Device battery use since last full charge");
4964 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004965 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004966 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004967 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004968 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004969 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004970 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004971 pw.println(getDischargeAmountScreenOffSinceCharge());
4972 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4973 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004974 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004975 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004976
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004977 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004978 helper.create(this);
4979 helper.refreshStats(which, UserHandle.USER_ALL);
4980 List<BatterySipper> sippers = helper.getUsageList();
4981 if (sippers != null && sippers.size() > 0) {
4982 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4983 pw.print(prefix); pw.print(" Capacity: ");
4984 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004985 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004986 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4987 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4988 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4989 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004990 pw.println();
4991 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004992 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004993 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004994 switch (bs.drainType) {
4995 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004996 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004997 break;
4998 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004999 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005000 break;
5001 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005002 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005003 break;
5004 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005005 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005006 break;
5007 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005008 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005009 break;
5010 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005011 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005012 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005013 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005014 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005015 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005016 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005017 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005018 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005019 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005020 break;
5021 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005022 pw.print(" User "); pw.print(bs.userId);
5023 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005024 break;
5025 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005026 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005027 break;
5028 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005029 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005030 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005031 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005032 pw.print(" Camera: ");
5033 break;
5034 default:
5035 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005036 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005037 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005038 printmAh(pw, bs.totalPowerMah);
5039
Adam Lesinski57123002015-06-12 16:12:07 -07005040 if (bs.usagePowerMah != bs.totalPowerMah) {
5041 // If the usage (generic power) isn't the whole amount, we list out
5042 // what components are involved in the calculation.
5043
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005044 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005045 if (bs.usagePowerMah != 0) {
5046 pw.print(" usage=");
5047 printmAh(pw, bs.usagePowerMah);
5048 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005049 if (bs.cpuPowerMah != 0) {
5050 pw.print(" cpu=");
5051 printmAh(pw, bs.cpuPowerMah);
5052 }
5053 if (bs.wakeLockPowerMah != 0) {
5054 pw.print(" wake=");
5055 printmAh(pw, bs.wakeLockPowerMah);
5056 }
5057 if (bs.mobileRadioPowerMah != 0) {
5058 pw.print(" radio=");
5059 printmAh(pw, bs.mobileRadioPowerMah);
5060 }
5061 if (bs.wifiPowerMah != 0) {
5062 pw.print(" wifi=");
5063 printmAh(pw, bs.wifiPowerMah);
5064 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005065 if (bs.bluetoothPowerMah != 0) {
5066 pw.print(" bt=");
5067 printmAh(pw, bs.bluetoothPowerMah);
5068 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005069 if (bs.gpsPowerMah != 0) {
5070 pw.print(" gps=");
5071 printmAh(pw, bs.gpsPowerMah);
5072 }
5073 if (bs.sensorPowerMah != 0) {
5074 pw.print(" sensor=");
5075 printmAh(pw, bs.sensorPowerMah);
5076 }
5077 if (bs.cameraPowerMah != 0) {
5078 pw.print(" camera=");
5079 printmAh(pw, bs.cameraPowerMah);
5080 }
5081 if (bs.flashlightPowerMah != 0) {
5082 pw.print(" flash=");
5083 printmAh(pw, bs.flashlightPowerMah);
5084 }
5085 pw.print(" )");
5086 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005087
5088 // If there is additional smearing information, include it.
5089 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5090 pw.print(" Including smearing: ");
5091 printmAh(pw, bs.totalSmearedPowerMah);
5092 pw.print(" (");
5093 if (bs.screenPowerMah != 0) {
5094 pw.print(" screen=");
5095 printmAh(pw, bs.screenPowerMah);
5096 }
5097 if (bs.proportionalSmearMah != 0) {
5098 pw.print(" proportional=");
5099 printmAh(pw, bs.proportionalSmearMah);
5100 }
5101 pw.print(" )");
5102 }
5103 if (bs.shouldHide) {
5104 pw.print(" Excluded from smearing");
5105 }
5106
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005107 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005108 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005109 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005110 }
5111
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005112 sippers = helper.getMobilemsppList();
5113 if (sippers != null && sippers.size() > 0) {
5114 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005115 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005116 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005117 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005118 sb.setLength(0);
5119 sb.append(prefix); sb.append(" Uid ");
5120 UserHandle.formatUid(sb, bs.uidObj.getUid());
5121 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5122 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5123 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005124 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005125 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005126 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005127 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005128 sb.setLength(0);
5129 sb.append(prefix);
5130 sb.append(" TOTAL TIME: ");
5131 formatTimeMs(sb, totalTime);
5132 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5133 sb.append(")");
5134 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005135 pw.println();
5136 }
5137
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005138 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5139 @Override
5140 public int compare(TimerEntry lhs, TimerEntry rhs) {
5141 long lhsTime = lhs.mTime;
5142 long rhsTime = rhs.mTime;
5143 if (lhsTime < rhsTime) {
5144 return 1;
5145 }
5146 if (lhsTime > rhsTime) {
5147 return -1;
5148 }
5149 return 0;
5150 }
5151 };
5152
5153 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005154 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5155 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005156 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005157 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5158 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5159 : kernelWakelocks.entrySet()) {
5160 final BatteryStats.Timer timer = ent.getValue();
5161 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005162 if (totalTimeMillis > 0) {
5163 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5164 }
5165 }
5166 if (ktimers.size() > 0) {
5167 Collections.sort(ktimers, timerComparator);
5168 pw.print(prefix); pw.println(" All kernel wake locks:");
5169 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005170 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005171 String linePrefix = ": ";
5172 sb.setLength(0);
5173 sb.append(prefix);
5174 sb.append(" Kernel Wake lock ");
5175 sb.append(timer.mName);
5176 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5177 which, linePrefix);
5178 if (!linePrefix.equals(": ")) {
5179 sb.append(" realtime");
5180 // Only print out wake locks that were held
5181 pw.println(sb.toString());
5182 }
5183 }
5184 pw.println();
5185 }
5186 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005187
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005188 if (timers.size() > 0) {
5189 Collections.sort(timers, timerComparator);
5190 pw.print(prefix); pw.println(" All partial wake locks:");
5191 for (int i=0; i<timers.size(); i++) {
5192 TimerEntry timer = timers.get(i);
5193 sb.setLength(0);
5194 sb.append(" Wake lock ");
5195 UserHandle.formatUid(sb, timer.mId);
5196 sb.append(" ");
5197 sb.append(timer.mName);
5198 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5199 sb.append(" realtime");
5200 pw.println(sb.toString());
5201 }
5202 timers.clear();
5203 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005204 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005205
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005206 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005207 if (wakeupReasons.size() > 0) {
5208 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005209 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005210 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005211 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005212 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5213 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005214 }
5215 Collections.sort(reasons, timerComparator);
5216 for (int i=0; i<reasons.size(); i++) {
5217 TimerEntry timer = reasons.get(i);
5218 String linePrefix = ": ";
5219 sb.setLength(0);
5220 sb.append(prefix);
5221 sb.append(" Wakeup reason ");
5222 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005223 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5224 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005225 pw.println(sb.toString());
5226 }
5227 pw.println();
5228 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005229 }
Evan Millar22ac0432009-03-31 11:33:18 -07005230
James Carr2dd7e5e2016-07-20 18:48:39 -07005231 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005232 if (mMemoryStats.size() > 0) {
5233 pw.println(" Memory Stats");
5234 for (int i = 0; i < mMemoryStats.size(); i++) {
5235 sb.setLength(0);
5236 sb.append(" Bandwidth ");
5237 sb.append(mMemoryStats.keyAt(i));
5238 sb.append(" Time ");
5239 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5240 pw.println(sb.toString());
5241 }
5242 pw.println();
5243 }
5244
5245 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5246 if (rpmStats.size() > 0) {
5247 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5248 if (rpmStats.size() > 0) {
5249 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5250 final String timerName = ent.getKey();
5251 final Timer timer = ent.getValue();
5252 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5253 }
5254 }
5255 pw.println();
5256 }
Bookatz82b341172017-09-07 19:06:08 -07005257 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5258 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005259 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005260 pw.print(prefix);
5261 pw.println(" Resource Power Manager Stats for when screen was off");
5262 if (screenOffRpmStats.size() > 0) {
5263 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5264 final String timerName = ent.getKey();
5265 final Timer timer = ent.getValue();
5266 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5267 }
Bookatz50df7112017-08-04 14:53:26 -07005268 }
Bookatz82b341172017-09-07 19:06:08 -07005269 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005270 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005271 }
5272
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005273 final long[] cpuFreqs = getCpuFreqs();
5274 if (cpuFreqs != null) {
5275 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005276 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005277 for (int i = 0; i < cpuFreqs.length; ++i) {
5278 sb.append(" " + cpuFreqs[i]);
5279 }
5280 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005281 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005282 }
5283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 for (int iu=0; iu<NU; iu++) {
5285 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005286 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005287 continue;
5288 }
Bookatzc8c44962017-05-11 12:12:54 -07005289
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005290 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005291
5292 pw.print(prefix);
5293 pw.print(" ");
5294 UserHandle.formatUid(pw, uid);
5295 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005297
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005298 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5299 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5300 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5301 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005302 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5303 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5304
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005305 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5306 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005307 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5308 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005309
5310 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5311 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5312
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005313 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5314 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5315 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005316 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5317 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5318 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5319 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005320 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005321
Adam Lesinski5f056f62016-07-14 16:56:08 -07005322 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5323 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5324
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005325 if (mobileRxBytes > 0 || mobileTxBytes > 0
5326 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005327 pw.print(prefix); pw.print(" Mobile network: ");
5328 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005329 pw.print(formatBytesLocked(mobileTxBytes));
5330 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5331 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005333 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5334 sb.setLength(0);
5335 sb.append(prefix); sb.append(" Mobile radio active: ");
5336 formatTimeMs(sb, uidMobileActiveTime / 1000);
5337 sb.append("(");
5338 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5339 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5340 long packets = mobileRxPackets + mobileTxPackets;
5341 if (packets == 0) {
5342 packets = 1;
5343 }
5344 sb.append(" @ ");
5345 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5346 sb.append(" mspp");
5347 pw.println(sb.toString());
5348 }
5349
Adam Lesinski5f056f62016-07-14 16:56:08 -07005350 if (mobileWakeup > 0) {
5351 sb.setLength(0);
5352 sb.append(prefix);
5353 sb.append(" Mobile radio AP wakeups: ");
5354 sb.append(mobileWakeup);
5355 pw.println(sb.toString());
5356 }
5357
Siddharth Rayb50a6842017-12-14 15:15:28 -08005358 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5359 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005360
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005361 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005362 pw.print(prefix); pw.print(" Wi-Fi network: ");
5363 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005364 pw.print(formatBytesLocked(wifiTxBytes));
5365 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5366 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005367 }
5368
Dianne Hackborn62793e42015-03-09 11:15:41 -07005369 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005370 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005371 || uidWifiRunningTime != 0) {
5372 sb.setLength(0);
5373 sb.append(prefix); sb.append(" Wifi Running: ");
5374 formatTimeMs(sb, uidWifiRunningTime / 1000);
5375 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5376 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005377 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005378 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5379 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5380 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005381 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005382 formatTimeMs(sb, wifiScanTime / 1000);
5383 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005384 whichBatteryRealtime)); sb.append(") ");
5385 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005386 sb.append("x\n");
5387 // actual and background times are unpooled and since reset (regardless of 'which')
5388 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5389 formatTimeMs(sb, wifiScanActualTime / 1000);
5390 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5391 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5392 sb.append(") ");
5393 sb.append(wifiScanCount);
5394 sb.append("x\n");
5395 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5396 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5397 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5398 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5399 sb.append(") ");
5400 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005401 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005402 pw.println(sb.toString());
5403 }
5404
Adam Lesinski5f056f62016-07-14 16:56:08 -07005405 if (wifiWakeup > 0) {
5406 sb.setLength(0);
5407 sb.append(prefix);
5408 sb.append(" WiFi AP wakeups: ");
5409 sb.append(wifiWakeup);
5410 pw.println(sb.toString());
5411 }
5412
Siddharth Rayb50a6842017-12-14 15:15:28 -08005413 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005414 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005415
Adam Lesinski50e47602015-12-04 17:04:54 -08005416 if (btRxBytes > 0 || btTxBytes > 0) {
5417 pw.print(prefix); pw.print(" Bluetooth network: ");
5418 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5419 pw.print(formatBytesLocked(btTxBytes));
5420 pw.println(" sent");
5421 }
5422
Bookatz867c0d72017-03-07 18:23:42 -08005423 final Timer bleTimer = u.getBluetoothScanTimer();
5424 if (bleTimer != null) {
5425 // Convert from microseconds to milliseconds with rounding
5426 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5427 / 1000;
5428 if (totalTimeMs != 0) {
5429 final int count = bleTimer.getCountLocked(which);
5430 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5431 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005432 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5433 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5434 final long actualTimeMsBg = bleTimerBg != null ?
5435 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005436 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005437 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5438 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005439 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5440 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5441 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5442 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5443 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5444 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5445 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5446 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5447 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5448 final Timer unoptimizedScanTimerBg =
5449 u.getBluetoothUnoptimizedScanBackgroundTimer();
5450 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5451 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5452 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5453 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005454
5455 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005456 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005457 sb.append(prefix);
5458 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005459 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005460 sb.append(" (");
5461 sb.append(count);
5462 sb.append(" times)");
5463 if (bleTimer.isRunningLocked()) {
5464 sb.append(" (currently running)");
5465 }
5466 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005467 }
Bookatzb1f04f32017-05-19 13:57:32 -07005468
5469 sb.append(prefix);
5470 sb.append(" Bluetooth Scan (total actual realtime): ");
5471 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5472 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005473 sb.append(count);
5474 sb.append(" times)");
5475 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005476 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005477 }
Bookatzb1f04f32017-05-19 13:57:32 -07005478 sb.append("\n");
5479 if (actualTimeMsBg > 0 || countBg > 0) {
5480 sb.append(prefix);
5481 sb.append(" Bluetooth Scan (background realtime): ");
5482 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5483 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005484 sb.append(countBg);
5485 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005486 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5487 sb.append(" (currently running in background)");
5488 }
5489 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005490 }
Bookatzb1f04f32017-05-19 13:57:32 -07005491
5492 sb.append(prefix);
5493 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005494 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005495 sb.append(" (");
5496 sb.append(resultCountBg);
5497 sb.append(" in background)");
5498
5499 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5500 sb.append("\n");
5501 sb.append(prefix);
5502 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5503 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5504 sb.append(" (max ");
5505 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5506 sb.append(")");
5507 if (unoptimizedScanTimer != null
5508 && unoptimizedScanTimer.isRunningLocked()) {
5509 sb.append(" (currently running unoptimized)");
5510 }
5511 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5512 sb.append("\n");
5513 sb.append(prefix);
5514 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5515 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5516 sb.append(" (max ");
5517 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5518 sb.append(")");
5519 if (unoptimizedScanTimerBg.isRunningLocked()) {
5520 sb.append(" (currently running unoptimized in background)");
5521 }
5522 }
5523 }
Bookatz867c0d72017-03-07 18:23:42 -08005524 pw.println(sb.toString());
5525 uidActivity = true;
5526 }
5527 }
5528
5529
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005530
Dianne Hackborn617f8772009-03-31 15:04:46 -07005531 if (u.hasUserActivity()) {
5532 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005533 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005534 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005535 if (val != 0) {
5536 if (!hasData) {
5537 sb.setLength(0);
5538 sb.append(" User activity: ");
5539 hasData = true;
5540 } else {
5541 sb.append(", ");
5542 }
5543 sb.append(val);
5544 sb.append(" ");
5545 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5546 }
5547 }
5548 if (hasData) {
5549 pw.println(sb.toString());
5550 }
5551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005553 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5554 = u.getWakelockStats();
5555 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005556 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005557 int countWakelock = 0;
5558 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5559 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5560 String linePrefix = ": ";
5561 sb.setLength(0);
5562 sb.append(prefix);
5563 sb.append(" Wake lock ");
5564 sb.append(wakelocks.keyAt(iw));
5565 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5566 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005567 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5568 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005569 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005570 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5571 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005572 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5573 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005574 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5575 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005576 sb.append(" realtime");
5577 pw.println(sb.toString());
5578 uidActivity = true;
5579 countWakelock++;
5580
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005581 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5582 rawRealtime, which);
5583 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5584 rawRealtime, which);
5585 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5586 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005587 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005588 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005589 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005590 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005591 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5592 // pooled and therefore just a lower bound)
5593 long actualTotalPartialWakelock = 0;
5594 long actualBgPartialWakelock = 0;
5595 if (u.getAggregatedPartialWakelockTimer() != null) {
5596 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5597 // Convert from microseconds to milliseconds with rounding
5598 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005599 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005600 final Timer bgAggTimer = aggTimer.getSubTimer();
5601 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005602 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005603 }
5604
5605 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5606 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5607 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005608 sb.setLength(0);
5609 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005610 sb.append(" TOTAL wake: ");
5611 boolean needComma = false;
5612 if (totalFullWakelock != 0) {
5613 needComma = true;
5614 formatTimeMs(sb, totalFullWakelock);
5615 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005616 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005617 if (totalPartialWakelock != 0) {
5618 if (needComma) {
5619 sb.append(", ");
5620 }
5621 needComma = true;
5622 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005623 sb.append("blamed partial");
5624 }
5625 if (actualTotalPartialWakelock != 0) {
5626 if (needComma) {
5627 sb.append(", ");
5628 }
5629 needComma = true;
5630 formatTimeMs(sb, actualTotalPartialWakelock);
5631 sb.append("actual partial");
5632 }
5633 if (actualBgPartialWakelock != 0) {
5634 if (needComma) {
5635 sb.append(", ");
5636 }
5637 needComma = true;
5638 formatTimeMs(sb, actualBgPartialWakelock);
5639 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005640 }
5641 if (totalWindowWakelock != 0) {
5642 if (needComma) {
5643 sb.append(", ");
5644 }
5645 needComma = true;
5646 formatTimeMs(sb, totalWindowWakelock);
5647 sb.append("window");
5648 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005649 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005650 if (needComma) {
5651 sb.append(",");
5652 }
5653 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005654 formatTimeMs(sb, totalDrawWakelock);
5655 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005656 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005657 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005658 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005659 }
5660 }
5661
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005662 // Calculate multicast wakelock stats
5663 final Timer mcTimer = u.getMulticastWakelockStats();
5664 if (mcTimer != null) {
5665 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5666 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5667
5668 if (multicastWakeLockTimeMicros > 0) {
5669 sb.setLength(0);
5670 sb.append(prefix);
5671 sb.append(" WiFi Multicast Wakelock");
5672 sb.append(" count = ");
5673 sb.append(multicastWakeLockCount);
5674 sb.append(" time = ");
5675 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5676 pw.println(sb.toString());
5677 }
5678 }
5679
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005680 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5681 for (int isy=syncs.size()-1; isy>=0; isy--) {
5682 final Timer timer = syncs.valueAt(isy);
5683 // Convert from microseconds to milliseconds with rounding
5684 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5685 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005686 final Timer bgTimer = timer.getSubTimer();
5687 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005688 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005689 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005690 sb.setLength(0);
5691 sb.append(prefix);
5692 sb.append(" Sync ");
5693 sb.append(syncs.keyAt(isy));
5694 sb.append(": ");
5695 if (totalTime != 0) {
5696 formatTimeMs(sb, totalTime);
5697 sb.append("realtime (");
5698 sb.append(count);
5699 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005700 if (bgTime > 0) {
5701 sb.append(", ");
5702 formatTimeMs(sb, bgTime);
5703 sb.append("background (");
5704 sb.append(bgCount);
5705 sb.append(" times)");
5706 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005707 } else {
5708 sb.append("(not used)");
5709 }
5710 pw.println(sb.toString());
5711 uidActivity = true;
5712 }
5713
5714 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5715 for (int ij=jobs.size()-1; ij>=0; ij--) {
5716 final Timer timer = jobs.valueAt(ij);
5717 // Convert from microseconds to milliseconds with rounding
5718 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5719 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005720 final Timer bgTimer = timer.getSubTimer();
5721 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005722 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005723 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005724 sb.setLength(0);
5725 sb.append(prefix);
5726 sb.append(" Job ");
5727 sb.append(jobs.keyAt(ij));
5728 sb.append(": ");
5729 if (totalTime != 0) {
5730 formatTimeMs(sb, totalTime);
5731 sb.append("realtime (");
5732 sb.append(count);
5733 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005734 if (bgTime > 0) {
5735 sb.append(", ");
5736 formatTimeMs(sb, bgTime);
5737 sb.append("background (");
5738 sb.append(bgCount);
5739 sb.append(" times)");
5740 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005741 } else {
5742 sb.append("(not used)");
5743 }
5744 pw.println(sb.toString());
5745 uidActivity = true;
5746 }
5747
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005748 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5749 for (int ic=completions.size()-1; ic>=0; ic--) {
5750 SparseIntArray types = completions.valueAt(ic);
5751 if (types != null) {
5752 pw.print(prefix);
5753 pw.print(" Job Completions ");
5754 pw.print(completions.keyAt(ic));
5755 pw.print(":");
5756 for (int it=0; it<types.size(); it++) {
5757 pw.print(" ");
5758 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5759 pw.print("(");
5760 pw.print(types.valueAt(it));
5761 pw.print("x)");
5762 }
5763 pw.println();
5764 }
5765 }
5766
Amith Yamasani977e11f2018-02-16 11:29:54 -08005767 u.getDeferredJobsLineLocked(sb, which);
5768 if (sb.length() > 0) {
5769 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5770 }
5771
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005772 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5773 prefix, "Flashlight");
5774 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5775 prefix, "Camera");
5776 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5777 prefix, "Video");
5778 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5779 prefix, "Audio");
5780
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005781 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5782 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005783 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005784 final Uid.Sensor se = sensors.valueAt(ise);
5785 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005786 sb.setLength(0);
5787 sb.append(prefix);
5788 sb.append(" Sensor ");
5789 int handle = se.getHandle();
5790 if (handle == Uid.Sensor.GPS) {
5791 sb.append("GPS");
5792 } else {
5793 sb.append(handle);
5794 }
5795 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005797 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005798 if (timer != null) {
5799 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005800 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5801 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005802 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005803 final Timer bgTimer = se.getSensorBackgroundTime();
5804 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005805 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5806 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5807 final long bgActualTime = bgTimer != null ?
5808 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5809
Dianne Hackborn61659e52014-07-09 16:13:01 -07005810 //timer.logState();
5811 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005812 if (actualTime != totalTime) {
5813 formatTimeMs(sb, totalTime);
5814 sb.append("blamed realtime, ");
5815 }
5816
5817 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005818 sb.append("realtime (");
5819 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005820 sb.append(" times)");
5821
5822 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005823 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005824 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5825 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005826 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005827 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 } else {
5830 sb.append("(not used)");
5831 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005832 } else {
5833 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005835
5836 pw.println(sb.toString());
5837 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 }
5839
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005840 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5841 "Vibrator");
5842 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5843 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005844 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5845 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005846
Dianne Hackborn61659e52014-07-09 16:13:01 -07005847 long totalStateTime = 0;
5848 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5849 long time = u.getProcessStateTime(ips, rawRealtime, which);
5850 if (time > 0) {
5851 totalStateTime += time;
5852 sb.setLength(0);
5853 sb.append(prefix);
5854 sb.append(" ");
5855 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5856 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005857 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005858 pw.println(sb.toString());
5859 uidActivity = true;
5860 }
5861 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005862 if (totalStateTime > 0) {
5863 sb.setLength(0);
5864 sb.append(prefix);
5865 sb.append(" Total running: ");
5866 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5867 pw.println(sb.toString());
5868 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005869
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005870 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5871 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005872 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005873 sb.setLength(0);
5874 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005875 sb.append(" Total cpu time: u=");
5876 formatTimeMs(sb, userCpuTimeUs / 1000);
5877 sb.append("s=");
5878 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005879 pw.println(sb.toString());
5880 }
5881
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005882 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5883 if (cpuFreqTimes != null) {
5884 sb.setLength(0);
5885 sb.append(" Total cpu time per freq:");
5886 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5887 sb.append(" " + cpuFreqTimes[i]);
5888 }
5889 pw.println(sb.toString());
5890 }
5891 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5892 if (screenOffCpuFreqTimes != null) {
5893 sb.setLength(0);
5894 sb.append(" Total screen-off cpu time per freq:");
5895 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5896 sb.append(" " + screenOffCpuFreqTimes[i]);
5897 }
5898 pw.println(sb.toString());
5899 }
5900
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005901 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5902 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5903 if (cpuTimes != null) {
5904 sb.setLength(0);
5905 sb.append(" Cpu times per freq at state "
5906 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5907 for (int i = 0; i < cpuTimes.length; ++i) {
5908 sb.append(" " + cpuTimes[i]);
5909 }
5910 pw.println(sb.toString());
5911 }
5912
5913 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5914 if (screenOffCpuTimes != null) {
5915 sb.setLength(0);
5916 sb.append(" Screen-off cpu times per freq at state "
5917 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5918 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5919 sb.append(" " + screenOffCpuTimes[i]);
5920 }
5921 pw.println(sb.toString());
5922 }
5923 }
5924
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005925 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5926 = u.getProcessStats();
5927 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5928 final Uid.Proc ps = processStats.valueAt(ipr);
5929 long userTime;
5930 long systemTime;
5931 long foregroundTime;
5932 int starts;
5933 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005935 userTime = ps.getUserTime(which);
5936 systemTime = ps.getSystemTime(which);
5937 foregroundTime = ps.getForegroundTime(which);
5938 starts = ps.getStarts(which);
5939 final int numCrashes = ps.getNumCrashes(which);
5940 final int numAnrs = ps.getNumAnrs(which);
5941 numExcessive = which == STATS_SINCE_CHARGED
5942 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005944 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5945 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5946 sb.setLength(0);
5947 sb.append(prefix); sb.append(" Proc ");
5948 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5949 sb.append(prefix); sb.append(" CPU: ");
5950 formatTimeMs(sb, userTime); sb.append("usr + ");
5951 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5952 formatTimeMs(sb, foregroundTime); sb.append("fg");
5953 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5954 sb.append("\n"); sb.append(prefix); sb.append(" ");
5955 boolean hasOne = false;
5956 if (starts != 0) {
5957 hasOne = true;
5958 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005959 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005960 if (numCrashes != 0) {
5961 if (hasOne) {
5962 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005963 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005964 hasOne = true;
5965 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005966 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005967 if (numAnrs != 0) {
5968 if (hasOne) {
5969 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005971 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 }
5973 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005974 pw.println(sb.toString());
5975 for (int e=0; e<numExcessive; e++) {
5976 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5977 if (ew != null) {
5978 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005979 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005980 pw.print("cpu");
5981 } else {
5982 pw.print("unknown");
5983 }
5984 pw.print(" use: ");
5985 TimeUtils.formatDuration(ew.usedTime, pw);
5986 pw.print(" over ");
5987 TimeUtils.formatDuration(ew.overTime, pw);
5988 if (ew.overTime != 0) {
5989 pw.print(" (");
5990 pw.print((ew.usedTime*100)/ew.overTime);
5991 pw.println("%)");
5992 }
5993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 }
5995 uidActivity = true;
5996 }
5997 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005998
5999 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
6000 = u.getPackageStats();
6001 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
6002 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
6003 pw.println(":");
6004 boolean apkActivity = false;
6005 final Uid.Pkg ps = packageStats.valueAt(ipkg);
6006 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
6007 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
6008 pw.print(prefix); pw.print(" Wakeup alarm ");
6009 pw.print(alarms.keyAt(iwa)); pw.print(": ");
6010 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6011 pw.println(" times");
6012 apkActivity = true;
6013 }
6014 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6015 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6016 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6017 final long startTime = ss.getStartTime(batteryUptime, which);
6018 final int starts = ss.getStarts(which);
6019 final int launches = ss.getLaunches(which);
6020 if (startTime != 0 || starts != 0 || launches != 0) {
6021 sb.setLength(0);
6022 sb.append(prefix); sb.append(" Service ");
6023 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6024 sb.append(prefix); sb.append(" Created for: ");
6025 formatTimeMs(sb, startTime / 1000);
6026 sb.append("uptime\n");
6027 sb.append(prefix); sb.append(" Starts: ");
6028 sb.append(starts);
6029 sb.append(", launches: "); sb.append(launches);
6030 pw.println(sb.toString());
6031 apkActivity = true;
6032 }
6033 }
6034 if (!apkActivity) {
6035 pw.print(prefix); pw.println(" (nothing executed)");
6036 }
6037 uidActivity = true;
6038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006040 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
6042 }
6043 }
6044
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006045 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006046 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006047 int diff = oldval ^ newval;
6048 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006049 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006050 for (int i=0; i<descriptions.length; i++) {
6051 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006052 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006053 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006054 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006055 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006056 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006057 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6058 didWake = true;
6059 pw.print("=");
6060 if (longNames) {
6061 UserHandle.formatUid(pw, wakelockTag.uid);
6062 pw.print(":\"");
6063 pw.print(wakelockTag.string);
6064 pw.print("\"");
6065 } else {
6066 pw.print(wakelockTag.poolIdx);
6067 }
6068 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006069 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006070 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006071 pw.print("=");
6072 int val = (newval&bd.mask)>>bd.shift;
6073 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006074 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006075 } else {
6076 pw.print(val);
6077 }
6078 }
6079 }
6080 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006081 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07006082 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006083 if (longNames) {
6084 UserHandle.formatUid(pw, wakelockTag.uid);
6085 pw.print(":\"");
6086 pw.print(wakelockTag.string);
6087 pw.print("\"");
6088 } else {
6089 pw.print(wakelockTag.poolIdx);
6090 }
6091 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006092 }
Mike Mac2f518a2017-09-19 16:06:03 -07006093
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006094 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006095 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006096 }
6097
6098 public static class HistoryPrinter {
6099 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006100 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006101 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006102 int oldStatus = -1;
6103 int oldHealth = -1;
6104 int oldPlug = -1;
6105 int oldTemp = -1;
6106 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006107 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006108 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006109
Dianne Hackborn3251b902014-06-20 14:40:53 -07006110 void reset() {
6111 oldState = oldState2 = 0;
6112 oldLevel = -1;
6113 oldStatus = -1;
6114 oldHealth = -1;
6115 oldPlug = -1;
6116 oldTemp = -1;
6117 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006118 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006119 }
6120
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006121 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006122 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006123 if (!checkin) {
6124 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006125 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006126 pw.print(" (");
6127 pw.print(rec.numReadInts);
6128 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006129 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006130 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6131 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006132 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006133 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006134 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006135 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006136 }
6137 lastTime = rec.time;
6138 }
6139 if (rec.cmd == HistoryItem.CMD_START) {
6140 if (checkin) {
6141 pw.print(":");
6142 }
6143 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006144 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006145 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6146 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006147 if (checkin) {
6148 pw.print(":");
6149 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006150 if (rec.cmd == HistoryItem.CMD_RESET) {
6151 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006152 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006153 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006154 pw.print("TIME:");
6155 if (checkin) {
6156 pw.println(rec.currentTime);
6157 } else {
6158 pw.print(" ");
6159 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6160 rec.currentTime).toString());
6161 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006162 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6163 if (checkin) {
6164 pw.print(":");
6165 }
6166 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006167 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6168 if (checkin) {
6169 pw.print(":");
6170 }
6171 pw.println("*OVERFLOW*");
6172 } else {
6173 if (!checkin) {
6174 if (rec.batteryLevel < 10) pw.print("00");
6175 else if (rec.batteryLevel < 100) pw.print("0");
6176 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006177 if (verbose) {
6178 pw.print(" ");
6179 if (rec.states < 0) ;
6180 else if (rec.states < 0x10) pw.print("0000000");
6181 else if (rec.states < 0x100) pw.print("000000");
6182 else if (rec.states < 0x1000) pw.print("00000");
6183 else if (rec.states < 0x10000) pw.print("0000");
6184 else if (rec.states < 0x100000) pw.print("000");
6185 else if (rec.states < 0x1000000) pw.print("00");
6186 else if (rec.states < 0x10000000) pw.print("0");
6187 pw.print(Integer.toHexString(rec.states));
6188 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006189 } else {
6190 if (oldLevel != rec.batteryLevel) {
6191 oldLevel = rec.batteryLevel;
6192 pw.print(",Bl="); pw.print(rec.batteryLevel);
6193 }
6194 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006195 if (oldStatus != rec.batteryStatus) {
6196 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006197 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006198 switch (oldStatus) {
6199 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006200 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006201 break;
6202 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006203 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006204 break;
6205 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006206 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006207 break;
6208 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006209 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006210 break;
6211 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006212 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006213 break;
6214 default:
6215 pw.print(oldStatus);
6216 break;
6217 }
6218 }
6219 if (oldHealth != rec.batteryHealth) {
6220 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006221 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006222 switch (oldHealth) {
6223 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006224 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006225 break;
6226 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006227 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006228 break;
6229 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006230 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006231 break;
6232 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006233 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006234 break;
6235 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006236 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006237 break;
6238 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006239 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006240 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006241 case BatteryManager.BATTERY_HEALTH_COLD:
6242 pw.print(checkin ? "c" : "cold");
6243 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006244 default:
6245 pw.print(oldHealth);
6246 break;
6247 }
6248 }
6249 if (oldPlug != rec.batteryPlugType) {
6250 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006251 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006252 switch (oldPlug) {
6253 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006254 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 break;
6256 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006257 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006258 break;
6259 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006260 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006261 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006262 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006263 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006264 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006265 default:
6266 pw.print(oldPlug);
6267 break;
6268 }
6269 }
6270 if (oldTemp != rec.batteryTemperature) {
6271 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006272 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006273 pw.print(oldTemp);
6274 }
6275 if (oldVolt != rec.batteryVoltage) {
6276 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006277 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006278 pw.print(oldVolt);
6279 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006280 final int chargeMAh = rec.batteryChargeUAh / 1000;
6281 if (oldChargeMAh != chargeMAh) {
6282 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006283 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006284 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006285 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006286 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006287 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006288 printBitDescriptions(pw, oldState2, rec.states2, null,
6289 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006290 if (rec.wakeReasonTag != null) {
6291 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006292 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006293 pw.print(rec.wakeReasonTag.poolIdx);
6294 } else {
6295 pw.print(" wake_reason=");
6296 pw.print(rec.wakeReasonTag.uid);
6297 pw.print(":\"");
6298 pw.print(rec.wakeReasonTag.string);
6299 pw.print("\"");
6300 }
6301 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006302 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006303 pw.print(checkin ? "," : " ");
6304 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6305 pw.print("+");
6306 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6307 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006308 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006309 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6310 : HISTORY_EVENT_NAMES;
6311 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6312 | HistoryItem.EVENT_FLAG_FINISH);
6313 if (idx >= 0 && idx < eventNames.length) {
6314 pw.print(eventNames[idx]);
6315 } else {
6316 pw.print(checkin ? "Ev" : "event");
6317 pw.print(idx);
6318 }
6319 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006320 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006321 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006322 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006323 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6324 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006325 pw.print(":\"");
6326 pw.print(rec.eventTag.string);
6327 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006328 }
6329 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006330 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006331 if (rec.stepDetails != null) {
6332 if (!checkin) {
6333 pw.print(" Details: cpu=");
6334 pw.print(rec.stepDetails.userTime);
6335 pw.print("u+");
6336 pw.print(rec.stepDetails.systemTime);
6337 pw.print("s");
6338 if (rec.stepDetails.appCpuUid1 >= 0) {
6339 pw.print(" (");
6340 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6341 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6342 if (rec.stepDetails.appCpuUid2 >= 0) {
6343 pw.print(", ");
6344 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6345 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6346 }
6347 if (rec.stepDetails.appCpuUid3 >= 0) {
6348 pw.print(", ");
6349 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6350 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6351 }
6352 pw.print(')');
6353 }
6354 pw.println();
6355 pw.print(" /proc/stat=");
6356 pw.print(rec.stepDetails.statUserTime);
6357 pw.print(" usr, ");
6358 pw.print(rec.stepDetails.statSystemTime);
6359 pw.print(" sys, ");
6360 pw.print(rec.stepDetails.statIOWaitTime);
6361 pw.print(" io, ");
6362 pw.print(rec.stepDetails.statIrqTime);
6363 pw.print(" irq, ");
6364 pw.print(rec.stepDetails.statSoftIrqTime);
6365 pw.print(" sirq, ");
6366 pw.print(rec.stepDetails.statIdlTime);
6367 pw.print(" idle");
6368 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6369 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6370 + rec.stepDetails.statSoftIrqTime;
6371 int total = totalRun + rec.stepDetails.statIdlTime;
6372 if (total > 0) {
6373 pw.print(" (");
6374 float perc = ((float)totalRun) / ((float)total) * 100;
6375 pw.print(String.format("%.1f%%", perc));
6376 pw.print(" of ");
6377 StringBuilder sb = new StringBuilder(64);
6378 formatTimeMsNoSpace(sb, total*10);
6379 pw.print(sb);
6380 pw.print(")");
6381 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006382 pw.print(", PlatformIdleStat ");
6383 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006384 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006385
6386 pw.print(", SubsystemPowerState ");
6387 pw.print(rec.stepDetails.statSubsystemPowerState);
6388 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006389 } else {
6390 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6391 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6392 pw.print(rec.stepDetails.userTime);
6393 pw.print(":");
6394 pw.print(rec.stepDetails.systemTime);
6395 if (rec.stepDetails.appCpuUid1 >= 0) {
6396 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6397 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6398 if (rec.stepDetails.appCpuUid2 >= 0) {
6399 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6400 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6401 }
6402 if (rec.stepDetails.appCpuUid3 >= 0) {
6403 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6404 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6405 }
6406 }
6407 pw.println();
6408 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6409 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6410 pw.print(rec.stepDetails.statUserTime);
6411 pw.print(',');
6412 pw.print(rec.stepDetails.statSystemTime);
6413 pw.print(',');
6414 pw.print(rec.stepDetails.statIOWaitTime);
6415 pw.print(',');
6416 pw.print(rec.stepDetails.statIrqTime);
6417 pw.print(',');
6418 pw.print(rec.stepDetails.statSoftIrqTime);
6419 pw.print(',');
6420 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006421 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006422 if (rec.stepDetails.statPlatformIdleState != null) {
6423 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006424 if (rec.stepDetails.statSubsystemPowerState != null) {
6425 pw.print(',');
6426 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006427 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006428
6429 if (rec.stepDetails.statSubsystemPowerState != null) {
6430 pw.print(rec.stepDetails.statSubsystemPowerState);
6431 }
6432 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006433 }
6434 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006435 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006436 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006437 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006438 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006439
6440 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6441 UserHandle.formatUid(pw, uid);
6442 pw.print("=");
6443 pw.print(utime);
6444 pw.print("u+");
6445 pw.print(stime);
6446 pw.print("s");
6447 }
6448
6449 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6450 pw.print('/');
6451 pw.print(uid);
6452 pw.print(":");
6453 pw.print(utime);
6454 pw.print(":");
6455 pw.print(stime);
6456 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006457 }
6458
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006459 private void printSizeValue(PrintWriter pw, long size) {
6460 float result = size;
6461 String suffix = "";
6462 if (result >= 10*1024) {
6463 suffix = "KB";
6464 result = result / 1024;
6465 }
6466 if (result >= 10*1024) {
6467 suffix = "MB";
6468 result = result / 1024;
6469 }
6470 if (result >= 10*1024) {
6471 suffix = "GB";
6472 result = result / 1024;
6473 }
6474 if (result >= 10*1024) {
6475 suffix = "TB";
6476 result = result / 1024;
6477 }
6478 if (result >= 10*1024) {
6479 suffix = "PB";
6480 result = result / 1024;
6481 }
6482 pw.print((int)result);
6483 pw.print(suffix);
6484 }
6485
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006486 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6487 String label3, long estimatedTime) {
6488 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006489 return false;
6490 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006491 pw.print(label1);
6492 pw.print(label2);
6493 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006494 StringBuilder sb = new StringBuilder(64);
6495 formatTimeMs(sb, estimatedTime);
6496 pw.print(sb);
6497 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006498 return true;
6499 }
6500
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006501 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6502 LevelStepTracker steps, boolean checkin) {
6503 if (steps == null) {
6504 return false;
6505 }
6506 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006507 if (count <= 0) {
6508 return false;
6509 }
6510 if (!checkin) {
6511 pw.println(header);
6512 }
Kweku Adams030980a2015-04-01 16:07:48 -07006513 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006514 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006515 long duration = steps.getDurationAt(i);
6516 int level = steps.getLevelAt(i);
6517 long initMode = steps.getInitModeAt(i);
6518 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006519 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006520 lineArgs[0] = Long.toString(duration);
6521 lineArgs[1] = Integer.toString(level);
6522 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6523 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6524 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6525 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6526 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6527 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006528 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006529 }
6530 } else {
6531 lineArgs[2] = "";
6532 }
6533 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6534 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6535 } else {
6536 lineArgs[3] = "";
6537 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006538 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006539 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006540 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006541 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006542 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006543 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6544 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006545 pw.print(prefix);
6546 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006547 TimeUtils.formatDuration(duration, pw);
6548 pw.print(" to "); pw.print(level);
6549 boolean haveModes = false;
6550 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6551 pw.print(" (");
6552 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6553 case Display.STATE_OFF: pw.print("screen-off"); break;
6554 case Display.STATE_ON: pw.print("screen-on"); break;
6555 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6556 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006557 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006558 }
6559 haveModes = true;
6560 }
6561 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6562 pw.print(haveModes ? ", " : " (");
6563 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6564 ? "power-save-on" : "power-save-off");
6565 haveModes = true;
6566 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006567 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6568 pw.print(haveModes ? ", " : " (");
6569 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6570 ? "device-idle-on" : "device-idle-off");
6571 haveModes = true;
6572 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006573 if (haveModes) {
6574 pw.print(")");
6575 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006576 pw.println();
6577 }
6578 }
6579 return true;
6580 }
6581
Kweku Adams87b19ec2017-10-09 12:40:03 -07006582 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6583 LevelStepTracker steps) {
6584 if (steps == null) {
6585 return;
6586 }
6587 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006588 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006589 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006590 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6591 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6592
6593 final long initMode = steps.getInitModeAt(i);
6594 final long modMode = steps.getModModeAt(i);
6595
6596 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6597 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6598 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6599 case Display.STATE_OFF:
6600 ds = SystemProto.BatteryLevelStep.DS_OFF;
6601 break;
6602 case Display.STATE_ON:
6603 ds = SystemProto.BatteryLevelStep.DS_ON;
6604 break;
6605 case Display.STATE_DOZE:
6606 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6607 break;
6608 case Display.STATE_DOZE_SUSPEND:
6609 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6610 break;
6611 default:
6612 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6613 break;
6614 }
6615 }
6616 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6617
6618 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6619 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6620 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6621 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6622 }
6623 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6624
6625 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6626 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6627 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6628 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6629 }
6630 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6631
6632 proto.end(token);
6633 }
6634 }
6635
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006636 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006637 public static final int DUMP_DAILY_ONLY = 1<<2;
6638 public static final int DUMP_HISTORY_ONLY = 1<<3;
6639 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6640 public static final int DUMP_VERBOSE = 1<<5;
6641 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006642
Dianne Hackborn37de0982014-05-09 09:32:18 -07006643 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6644 final HistoryPrinter hprinter = new HistoryPrinter();
6645 final HistoryItem rec = new HistoryItem();
6646 long lastTime = -1;
6647 long baseTime = -1;
6648 boolean printed = false;
6649 HistoryEventTracker tracker = null;
6650 while (getNextHistoryLocked(rec)) {
6651 lastTime = rec.time;
6652 if (baseTime < 0) {
6653 baseTime = lastTime;
6654 }
6655 if (rec.time >= histStart) {
6656 if (histStart >= 0 && !printed) {
6657 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006658 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006659 || rec.cmd == HistoryItem.CMD_START
6660 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006661 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006662 hprinter.printNextItem(pw, rec, baseTime, checkin,
6663 (flags&DUMP_VERBOSE) != 0);
6664 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006665 } else if (rec.currentTime != 0) {
6666 printed = true;
6667 byte cmd = rec.cmd;
6668 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006669 hprinter.printNextItem(pw, rec, baseTime, checkin,
6670 (flags&DUMP_VERBOSE) != 0);
6671 rec.cmd = cmd;
6672 }
6673 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006674 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6675 hprinter.printNextItem(pw, rec, baseTime, checkin,
6676 (flags&DUMP_VERBOSE) != 0);
6677 rec.cmd = HistoryItem.CMD_UPDATE;
6678 }
6679 int oldEventCode = rec.eventCode;
6680 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006681 rec.eventTag = new HistoryTag();
6682 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6683 HashMap<String, SparseIntArray> active
6684 = tracker.getStateForEvent(i);
6685 if (active == null) {
6686 continue;
6687 }
6688 for (HashMap.Entry<String, SparseIntArray> ent
6689 : active.entrySet()) {
6690 SparseIntArray uids = ent.getValue();
6691 for (int j=0; j<uids.size(); j++) {
6692 rec.eventCode = i;
6693 rec.eventTag.string = ent.getKey();
6694 rec.eventTag.uid = uids.keyAt(j);
6695 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006696 hprinter.printNextItem(pw, rec, baseTime, checkin,
6697 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006698 rec.wakeReasonTag = null;
6699 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006700 }
6701 }
6702 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006703 rec.eventCode = oldEventCode;
6704 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006705 tracker = null;
6706 }
6707 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006708 hprinter.printNextItem(pw, rec, baseTime, checkin,
6709 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006710 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6711 // This is an attempt to aggregate the previous state and generate
6712 // fake events to reflect that state at the point where we start
6713 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006714 if (tracker == null) {
6715 tracker = new HistoryEventTracker();
6716 }
6717 tracker.updateState(rec.eventCode, rec.eventTag.string,
6718 rec.eventTag.uid, rec.eventTag.poolIdx);
6719 }
6720 }
6721 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006722 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006723 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6724 }
6725 }
6726
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006727 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6728 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6729 if (steps == null) {
6730 return;
6731 }
6732 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6733 if (timeRemaining >= 0) {
6734 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6735 tmpSb.setLength(0);
6736 formatTimeMs(tmpSb, timeRemaining);
6737 pw.print(tmpSb);
6738 pw.print(" (from "); pw.print(tmpOutInt[0]);
6739 pw.println(" steps)");
6740 }
6741 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6742 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6743 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6744 if (estimatedTime > 0) {
6745 pw.print(prefix); pw.print(label); pw.print(" ");
6746 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6747 pw.print(" time: ");
6748 tmpSb.setLength(0);
6749 formatTimeMs(tmpSb, estimatedTime);
6750 pw.print(tmpSb);
6751 pw.print(" (from "); pw.print(tmpOutInt[0]);
6752 pw.println(" steps)");
6753 }
6754 }
6755 }
6756
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006757 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6758 ArrayList<PackageChange> changes) {
6759 if (changes == null) {
6760 return;
6761 }
6762 pw.print(prefix); pw.println("Package changes:");
6763 for (int i=0; i<changes.size(); i++) {
6764 PackageChange pc = changes.get(i);
6765 if (pc.mUpdate) {
6766 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6767 pw.print(" vers="); pw.println(pc.mVersionCode);
6768 } else {
6769 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6770 }
6771 }
6772 }
6773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 /**
6775 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6776 *
6777 * @param pw a Printer to receive the dump output.
6778 */
6779 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006780 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006781 prepareForDumpLocked();
6782
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006783 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006784 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006785
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006786 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006787 final long historyTotalSize = getHistoryTotalSize();
6788 final long historyUsedSize = getHistoryUsedSize();
6789 if (startIteratingHistoryLocked()) {
6790 try {
6791 pw.print("Battery History (");
6792 pw.print((100*historyUsedSize)/historyTotalSize);
6793 pw.print("% used, ");
6794 printSizeValue(pw, historyUsedSize);
6795 pw.print(" used of ");
6796 printSizeValue(pw, historyTotalSize);
6797 pw.print(", ");
6798 pw.print(getHistoryStringPoolSize());
6799 pw.print(" strings using ");
6800 printSizeValue(pw, getHistoryStringPoolBytes());
6801 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006802 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006803 pw.println();
6804 } finally {
6805 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006806 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006807 }
6808
6809 if (startIteratingOldHistoryLocked()) {
6810 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006811 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006812 pw.println("Old battery History:");
6813 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006814 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006815 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006816 if (baseTime < 0) {
6817 baseTime = rec.time;
6818 }
6819 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006820 }
6821 pw.println();
6822 } finally {
6823 finishIteratingOldHistoryLocked();
6824 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006825 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006826 }
6827
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006828 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006829 return;
6830 }
6831
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006832 if (!filtering) {
6833 SparseArray<? extends Uid> uidStats = getUidStats();
6834 final int NU = uidStats.size();
6835 boolean didPid = false;
6836 long nowRealtime = SystemClock.elapsedRealtime();
6837 for (int i=0; i<NU; i++) {
6838 Uid uid = uidStats.valueAt(i);
6839 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6840 if (pids != null) {
6841 for (int j=0; j<pids.size(); j++) {
6842 Uid.Pid pid = pids.valueAt(j);
6843 if (!didPid) {
6844 pw.println("Per-PID Stats:");
6845 didPid = true;
6846 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006847 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6848 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006849 pw.print(" PID "); pw.print(pids.keyAt(j));
6850 pw.print(" wake time: ");
6851 TimeUtils.formatDuration(time, pw);
6852 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006853 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006854 }
6855 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006856 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006857 pw.println();
6858 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006859 }
6860
6861 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006862 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6863 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006864 long timeRemaining = computeBatteryTimeRemaining(
6865 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006866 if (timeRemaining >= 0) {
6867 pw.print(" Estimated discharge time remaining: ");
6868 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6869 pw.println();
6870 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006871 final LevelStepTracker steps = getDischargeLevelStepTracker();
6872 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6873 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6874 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6875 STEP_LEVEL_MODE_VALUES[i], null));
6876 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006877 pw.println();
6878 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006879 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6880 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006881 long timeRemaining = computeChargeTimeRemaining(
6882 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006883 if (timeRemaining >= 0) {
6884 pw.print(" Estimated charge time remaining: ");
6885 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6886 pw.println();
6887 }
6888 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006889 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006890 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006891 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006892 pw.println("Daily stats:");
6893 pw.print(" Current start time: ");
6894 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6895 getCurrentDailyStartTime()).toString());
6896 pw.print(" Next min deadline: ");
6897 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6898 getNextMinDailyDeadline()).toString());
6899 pw.print(" Next max deadline: ");
6900 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6901 getNextMaxDailyDeadline()).toString());
6902 StringBuilder sb = new StringBuilder(64);
6903 int[] outInt = new int[1];
6904 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6905 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006906 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6907 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006908 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006909 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6910 dsteps, false)) {
6911 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6912 sb, outInt);
6913 }
6914 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6915 csteps, false)) {
6916 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6917 sb, outInt);
6918 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006919 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006920 } else {
6921 pw.println(" Current daily steps:");
6922 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6923 sb, outInt);
6924 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6925 sb, outInt);
6926 }
6927 }
6928 DailyItem dit;
6929 int curIndex = 0;
6930 while ((dit=getDailyItemLocked(curIndex)) != null) {
6931 curIndex++;
6932 if ((flags&DUMP_DAILY_ONLY) != 0) {
6933 pw.println();
6934 }
6935 pw.print(" Daily from ");
6936 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6937 pw.print(" to ");
6938 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6939 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006940 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006941 if (dumpDurationSteps(pw, " ",
6942 " Discharge step durations:", dit.mDischargeSteps, false)) {
6943 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6944 sb, outInt);
6945 }
6946 if (dumpDurationSteps(pw, " ",
6947 " Charge step durations:", dit.mChargeSteps, false)) {
6948 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6949 sb, outInt);
6950 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006951 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006952 } else {
6953 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6954 sb, outInt);
6955 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6956 sb, outInt);
6957 }
6958 }
6959 pw.println();
6960 }
6961 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006962 pw.println("Statistics since last charge:");
6963 pw.println(" System starts: " + getStartCount()
6964 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006965 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6966 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006967 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 }
Mike Mac2f518a2017-09-19 16:06:03 -07006970
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006971 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006973 public void dumpCheckinLocked(Context context, PrintWriter pw,
6974 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006975 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006976
6977 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006978 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6979 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006980
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006981 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6982
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006983 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006984 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006985 try {
6986 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6987 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6988 pw.print(HISTORY_STRING_POOL); pw.print(',');
6989 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006990 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006991 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006992 pw.print(",\"");
6993 String str = getHistoryTagPoolString(i);
6994 str = str.replace("\\", "\\\\");
6995 str = str.replace("\"", "\\\"");
6996 pw.print(str);
6997 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006998 pw.println();
6999 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007000 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007001 } finally {
7002 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007003 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007004 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007005 }
7006
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007007 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007008 return;
7009 }
7010
Dianne Hackborne4a59512010-12-07 11:08:07 -08007011 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007012 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007013 for (int i=0; i<apps.size(); i++) {
7014 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007015 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7016 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007017 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007018 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7019 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007020 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007021 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007022 }
7023 SparseArray<? extends Uid> uidStats = getUidStats();
7024 final int NU = uidStats.size();
7025 String[] lineArgs = new String[2];
7026 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007027 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7028 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7029 if (pkgs != null && !pkgs.second.value) {
7030 pkgs.second.value = true;
7031 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007032 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007033 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007034 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7035 (Object[])lineArgs);
7036 }
7037 }
7038 }
7039 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007040 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007041 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007042 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007043 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007044 if (timeRemaining >= 0) {
7045 lineArgs[0] = Long.toString(timeRemaining);
7046 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7047 (Object[])lineArgs);
7048 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007049 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007050 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007051 if (timeRemaining >= 0) {
7052 lineArgs[0] = Long.toString(timeRemaining);
7053 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7054 (Object[])lineArgs);
7055 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007056 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7057 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007060
Kweku Adams87b19ec2017-10-09 12:40:03 -07007061 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007062 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08007063 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007064 final ProtoOutputStream proto = new ProtoOutputStream(fd);
7065 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7066 prepareForDumpLocked();
7067
7068 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7069 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7070 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7071 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7072
Kweku Adams6ccebf22017-12-11 12:30:35 -08007073 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007074
7075 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007076 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7077 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7078 helper.create(this);
7079 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7080
7081 dumpProtoAppsLocked(proto, helper, apps);
7082 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007083 }
7084
7085 proto.end(bToken);
7086 proto.flush();
7087 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007088
Kweku Adams103351f2017-10-16 14:39:34 -07007089 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7090 List<ApplicationInfo> apps) {
7091 final int which = STATS_SINCE_CHARGED;
7092 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7093 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7094 final long rawRealtimeUs = rawRealtimeMs * 1000;
7095 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7096
7097 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7098 if (apps != null) {
7099 for (int i = 0; i < apps.size(); ++i) {
7100 ApplicationInfo ai = apps.get(i);
7101 int aid = UserHandle.getAppId(ai.uid);
7102 ArrayList<String> pkgs = aidToPackages.get(aid);
7103 if (pkgs == null) {
7104 pkgs = new ArrayList<String>();
7105 aidToPackages.put(aid, pkgs);
7106 }
7107 pkgs.add(ai.packageName);
7108 }
7109 }
7110
7111 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7112 final List<BatterySipper> sippers = helper.getUsageList();
7113 if (sippers != null) {
7114 for (int i = 0; i < sippers.size(); ++i) {
7115 final BatterySipper bs = sippers.get(i);
7116 if (bs.drainType != BatterySipper.DrainType.APP) {
7117 // Others are handled by dumpProtoSystemLocked()
7118 continue;
7119 }
7120 uidToSipper.put(bs.uidObj.getUid(), bs);
7121 }
7122 }
7123
7124 SparseArray<? extends Uid> uidStats = getUidStats();
7125 final int n = uidStats.size();
7126 for (int iu = 0; iu < n; ++iu) {
7127 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7128 final Uid u = uidStats.valueAt(iu);
7129
7130 final int uid = uidStats.keyAt(iu);
7131 proto.write(UidProto.UID, uid);
7132
7133 // Print packages and apk stats (UID_DATA & APK_DATA)
7134 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7135 if (pkgs == null) {
7136 pkgs = new ArrayList<String>();
7137 }
7138 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7139 u.getPackageStats();
7140 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7141 String pkg = packageStats.keyAt(ipkg);
7142 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7143 packageStats.valueAt(ipkg).getServiceStats();
7144 if (serviceStats.size() == 0) {
7145 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7146 // example, "android") may be included in the packageStats that aren't part of
7147 // the UID. If they don't have any services, then they shouldn't be listed here.
7148 // These packages won't be a part in the pkgs List.
7149 continue;
7150 }
7151
7152 final long pToken = proto.start(UidProto.PACKAGES);
7153 proto.write(UidProto.Package.NAME, pkg);
7154 // Remove from the packages list since we're logging it here.
7155 pkgs.remove(pkg);
7156
7157 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7158 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
7159 long sToken = proto.start(UidProto.Package.SERVICES);
7160
7161 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
7162 proto.write(UidProto.Package.Service.START_DURATION_MS,
7163 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
7164 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
7165 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
7166
7167 proto.end(sToken);
7168 }
7169 proto.end(pToken);
7170 }
7171 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7172 // from PackageManager data. Packages are only included in packageStats if there was
7173 // specific data tracked for them (services and wakeup alarms, etc.).
7174 for (String p : pkgs) {
7175 final long pToken = proto.start(UidProto.PACKAGES);
7176 proto.write(UidProto.Package.NAME, p);
7177 proto.end(pToken);
7178 }
7179
7180 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7181 if (u.getAggregatedPartialWakelockTimer() != null) {
7182 final Timer timer = u.getAggregatedPartialWakelockTimer();
7183 // Times are since reset (regardless of 'which')
7184 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7185 final Timer bgTimer = timer.getSubTimer();
7186 final long bgTimeMs = bgTimer != null
7187 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7188 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7189 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7190 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7191 proto.end(awToken);
7192 }
7193
7194 // Audio (AUDIO_DATA)
7195 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7196
7197 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7198 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7199 u.getBluetoothControllerActivity(), which);
7200
7201 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7202 final Timer bleTimer = u.getBluetoothScanTimer();
7203 if (bleTimer != null) {
7204 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7205
7206 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7207 rawRealtimeUs, which);
7208 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7209 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7210 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7211 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7212 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7213 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7214 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7215 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7216 // Result counters
7217 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7218 u.getBluetoothScanResultCounter() != null
7219 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7220 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7221 u.getBluetoothScanResultBgCounter() != null
7222 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7223
7224 proto.end(bmToken);
7225 }
7226
7227 // Camera (CAMERA_DATA)
7228 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7229
7230 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7231 final long cpuToken = proto.start(UidProto.CPU);
7232 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7233 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7234
7235 final long[] cpuFreqs = getCpuFreqs();
7236 if (cpuFreqs != null) {
7237 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7238 // If total cpuFreqTimes is null, then we don't need to check for
7239 // screenOffCpuFreqTimes.
7240 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7241 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7242 if (screenOffCpuFreqTimeMs == null) {
7243 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7244 }
7245 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7246 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7247 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7248 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7249 cpuFreqTimeMs[ic]);
7250 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7251 screenOffCpuFreqTimeMs[ic]);
7252 proto.end(cToken);
7253 }
7254 }
7255 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007256
7257 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7258 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7259 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7260 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7261 if (screenOffTimesMs == null) {
7262 screenOffTimesMs = new long[timesMs.length];
7263 }
7264 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7265 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7266 for (int ic = 0; ic < timesMs.length; ++ic) {
7267 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7268 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7269 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7270 timesMs[ic]);
7271 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7272 screenOffTimesMs[ic]);
7273 proto.end(cToken);
7274 }
7275 proto.end(procToken);
7276 }
7277 }
Kweku Adams103351f2017-10-16 14:39:34 -07007278 proto.end(cpuToken);
7279
7280 // Flashlight (FLASHLIGHT_DATA)
7281 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7282 rawRealtimeUs, which);
7283
7284 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7285 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7286 rawRealtimeUs, which);
7287
7288 // Foreground service (FOREGROUND_SERVICE_DATA)
7289 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7290 rawRealtimeUs, which);
7291
7292 // Job completion (JOB_COMPLETION_DATA)
7293 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7294 final int[] reasons = new int[]{
7295 JobParameters.REASON_CANCELED,
7296 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7297 JobParameters.REASON_PREEMPT,
7298 JobParameters.REASON_TIMEOUT,
7299 JobParameters.REASON_DEVICE_IDLE,
7300 };
7301 for (int ic = 0; ic < completions.size(); ++ic) {
7302 SparseIntArray types = completions.valueAt(ic);
7303 if (types != null) {
7304 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7305
7306 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7307
7308 for (int r : reasons) {
7309 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7310 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7311 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7312 proto.end(rToken);
7313 }
7314
7315 proto.end(jcToken);
7316 }
7317 }
7318
7319 // Scheduled jobs (JOB_DATA)
7320 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7321 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7322 final Timer timer = jobs.valueAt(ij);
7323 final Timer bgTimer = timer.getSubTimer();
7324 final long jToken = proto.start(UidProto.JOBS);
7325
7326 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7327 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7328 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7329 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7330
7331 proto.end(jToken);
7332 }
7333
7334 // Modem Controller (MODEM_CONTROLLER_DATA)
7335 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7336 u.getModemControllerActivity(), which);
7337
7338 // Network stats (NETWORK_DATA)
7339 final long nToken = proto.start(UidProto.NETWORK);
7340 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7341 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7342 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7343 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7344 proto.write(UidProto.Network.WIFI_BYTES_RX,
7345 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7346 proto.write(UidProto.Network.WIFI_BYTES_TX,
7347 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7348 proto.write(UidProto.Network.BT_BYTES_RX,
7349 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7350 proto.write(UidProto.Network.BT_BYTES_TX,
7351 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7352 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7353 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7354 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7355 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7356 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7357 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7358 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7359 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7360 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7361 roundUsToMs(u.getMobileRadioActiveTime(which)));
7362 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7363 u.getMobileRadioActiveCount(which));
7364 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7365 u.getMobileRadioApWakeupCount(which));
7366 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7367 u.getWifiRadioApWakeupCount(which));
7368 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7369 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7370 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7371 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7372 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7373 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7374 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7375 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7376 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7377 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7378 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7379 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7380 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7381 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7382 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7383 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7384 proto.end(nToken);
7385
7386 // Power use item (POWER_USE_ITEM_DATA)
7387 BatterySipper bs = uidToSipper.get(uid);
7388 if (bs != null) {
7389 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7390 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7391 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7392 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7393 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7394 bs.proportionalSmearMah);
7395 proto.end(bsToken);
7396 }
7397
7398 // Processes (PROCESS_DATA)
7399 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7400 u.getProcessStats();
7401 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7402 final Uid.Proc ps = processStats.valueAt(ipr);
7403 final long prToken = proto.start(UidProto.PROCESS);
7404
7405 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7406 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7407 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7408 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7409 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7410 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7411 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7412
7413 proto.end(prToken);
7414 }
7415
7416 // Sensors (SENSOR_DATA)
7417 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7418 for (int ise = 0; ise < sensors.size(); ++ise) {
7419 final Uid.Sensor se = sensors.valueAt(ise);
7420 final Timer timer = se.getSensorTime();
7421 if (timer == null) {
7422 continue;
7423 }
7424 final Timer bgTimer = se.getSensorBackgroundTime();
7425 final int sensorNumber = sensors.keyAt(ise);
7426 final long seToken = proto.start(UidProto.SENSORS);
7427
7428 proto.write(UidProto.Sensor.ID, sensorNumber);
7429 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7430 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7431 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7432
7433 proto.end(seToken);
7434 }
7435
7436 // State times (STATE_TIME_DATA)
7437 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7438 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7439 if (durMs == 0) {
7440 continue;
7441 }
7442 final long stToken = proto.start(UidProto.STATES);
7443 proto.write(UidProto.StateTime.STATE, ips);
7444 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7445 proto.end(stToken);
7446 }
7447
7448 // Syncs (SYNC_DATA)
7449 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7450 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7451 final Timer timer = syncs.valueAt(isy);
7452 final Timer bgTimer = timer.getSubTimer();
7453 final long syToken = proto.start(UidProto.SYNCS);
7454
7455 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7456 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7457 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7458 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7459
7460 proto.end(syToken);
7461 }
7462
7463 // User activity (USER_ACTIVITY_DATA)
7464 if (u.hasUserActivity()) {
7465 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7466 int val = u.getUserActivityCount(i, which);
7467 if (val != 0) {
7468 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7469 proto.write(UidProto.UserActivity.NAME, i);
7470 proto.write(UidProto.UserActivity.COUNT, val);
7471 proto.end(uaToken);
7472 }
7473 }
7474 }
7475
7476 // Vibrator (VIBRATOR_DATA)
7477 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7478
7479 // Video (VIDEO_DATA)
7480 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7481
7482 // Wakelocks (WAKELOCK_DATA)
7483 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7484 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7485 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7486 final long wToken = proto.start(UidProto.WAKELOCKS);
7487 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7488 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7489 rawRealtimeUs, which);
7490 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7491 if (pTimer != null) {
7492 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7493 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7494 rawRealtimeUs, which);
7495 }
7496 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7497 rawRealtimeUs, which);
7498 proto.end(wToken);
7499 }
7500
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007501 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7502 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7503 rawRealtimeUs, which);
7504
Kweku Adams103351f2017-10-16 14:39:34 -07007505 // Wakeup alarms (WAKEUP_ALARM_DATA)
7506 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7507 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7508 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7509 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7510 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7511 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7512 proto.write(UidProto.WakeupAlarm.COUNT,
7513 alarms.valueAt(iwa).getCountLocked(which));
7514 proto.end(waToken);
7515 }
7516 }
7517
7518 // Wifi Controller (WIFI_CONTROLLER_DATA)
7519 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7520 u.getWifiControllerActivity(), which);
7521
7522 // Wifi data (WIFI_DATA)
7523 final long wToken = proto.start(UidProto.WIFI);
7524 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7525 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7526 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7527 rawRealtimeUs, which);
7528 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7529 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7530 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7531 rawRealtimeUs, which);
7532 proto.end(wToken);
7533
7534 proto.end(uTkn);
7535 }
7536 }
7537
7538 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007539 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7540 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7541 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7542 final long rawRealtimeUs = rawRealtimeMs * 1000;
7543 final int which = STATS_SINCE_CHARGED;
7544
7545 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007546 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007547 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7548 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7549 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7550 computeRealtime(rawRealtimeUs, which) / 1000);
7551 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7552 computeUptime(rawUptimeUs, which) / 1000);
7553 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7554 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7555 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7556 computeBatteryUptime(rawUptimeUs, which) / 1000);
7557 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7558 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7559 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7560 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7561 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7562 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7563 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7564 getEstimatedBatteryCapacity());
7565 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7566 getMinLearnedBatteryCapacity());
7567 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7568 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007569 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007570
7571 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007572 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007573 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7574 getLowDischargeAmountSinceCharge());
7575 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7576 getHighDischargeAmountSinceCharge());
7577 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7578 getDischargeAmountScreenOnSinceCharge());
7579 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7580 getDischargeAmountScreenOffSinceCharge());
7581 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7582 getDischargeAmountScreenDozeSinceCharge());
7583 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7584 getUahDischarge(which) / 1000);
7585 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7586 getUahDischargeScreenOff(which) / 1000);
7587 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7588 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007589 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7590 getUahDischargeLightDoze(which) / 1000);
7591 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7592 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007593 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007594
7595 // Time remaining
7596 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007597 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007598 if (timeRemainingUs >= 0) {
7599 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7600 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7601 } else {
7602 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7603 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7604 if (timeRemainingUs >= 0) {
7605 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7606 } else {
7607 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7608 }
7609 }
7610
7611 // Charge step (CHARGE_STEP_DATA)
7612 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7613
7614 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7615 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007616 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007617 proto.write(SystemProto.DataConnection.NAME, i);
7618 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7619 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007620 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007621 }
7622
7623 // Discharge step (DISCHARGE_STEP_DATA)
7624 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7625
7626 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7627 final long[] cpuFreqs = getCpuFreqs();
7628 if (cpuFreqs != null) {
7629 for (long i : cpuFreqs) {
7630 proto.write(SystemProto.CPU_FREQUENCY, i);
7631 }
7632 }
7633
7634 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7635 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7636 getBluetoothControllerActivity(), which);
7637
7638 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7639 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7640 getModemControllerActivity(), which);
7641
7642 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007643 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007644 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7645 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7646 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7647 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7648 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7649 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7650 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7651 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7652 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7653 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7654 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7655 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7656 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7657 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7658 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7659 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7660 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7661 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7662 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7663 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007664 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007665
7666 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7667 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7668 getWifiControllerActivity(), which);
7669
7670
7671 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007672 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007673 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7674 getWifiOnTime(rawRealtimeUs, which) / 1000);
7675 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7676 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007677 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007678
7679 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7680 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7681 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007682 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007683 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7684 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7685 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007686 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007687 }
7688
7689 // Misc (MISC_DATA)
7690 // Calculate wakelock times across all uids.
7691 long fullWakeLockTimeTotalUs = 0;
7692 long partialWakeLockTimeTotalUs = 0;
7693
7694 final SparseArray<? extends Uid> uidStats = getUidStats();
7695 for (int iu = 0; iu < uidStats.size(); iu++) {
7696 final Uid u = uidStats.valueAt(iu);
7697
7698 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7699 u.getWakelockStats();
7700 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7701 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7702
7703 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7704 if (fullWakeTimer != null) {
7705 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7706 which);
7707 }
7708
7709 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7710 if (partialWakeTimer != null) {
7711 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7712 rawRealtimeUs, which);
7713 }
7714 }
7715 }
Kweku Adams103351f2017-10-16 14:39:34 -07007716 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007717 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7718 getScreenOnTime(rawRealtimeUs, which) / 1000);
7719 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7720 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7721 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7722 fullWakeLockTimeTotalUs / 1000);
7723 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7724 partialWakeLockTimeTotalUs / 1000);
7725 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7726 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7727 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7728 getMobileRadioActiveAdjustedTime(which) / 1000);
7729 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7730 getMobileRadioActiveCount(which));
7731 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7732 getMobileRadioActiveUnknownTime(which) / 1000);
7733 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7734 getInteractiveTime(rawRealtimeUs, which) / 1000);
7735 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7736 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7737 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7738 getNumConnectivityChange(which));
7739 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7740 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7741 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7742 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7743 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7744 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7745 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7746 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7747 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7748 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7749 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7750 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7751 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7752 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7753 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7754 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7755 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7756 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7757 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7758 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007759 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007760
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007761 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007762 final long multicastWakeLockTimeTotalUs =
7763 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7764 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007765 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7766 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7767 multicastWakeLockTimeTotalUs / 1000);
7768 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7769 multicastWakeLockCountTotal);
7770 proto.end(wmctToken);
7771
Kweku Adams87b19ec2017-10-09 12:40:03 -07007772 // Power use item (POWER_USE_ITEM_DATA)
7773 final List<BatterySipper> sippers = helper.getUsageList();
7774 if (sippers != null) {
7775 for (int i = 0; i < sippers.size(); ++i) {
7776 final BatterySipper bs = sippers.get(i);
7777 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7778 int uid = 0;
7779 switch (bs.drainType) {
7780 case IDLE:
7781 n = SystemProto.PowerUseItem.IDLE;
7782 break;
7783 case CELL:
7784 n = SystemProto.PowerUseItem.CELL;
7785 break;
7786 case PHONE:
7787 n = SystemProto.PowerUseItem.PHONE;
7788 break;
7789 case WIFI:
7790 n = SystemProto.PowerUseItem.WIFI;
7791 break;
7792 case BLUETOOTH:
7793 n = SystemProto.PowerUseItem.BLUETOOTH;
7794 break;
7795 case SCREEN:
7796 n = SystemProto.PowerUseItem.SCREEN;
7797 break;
7798 case FLASHLIGHT:
7799 n = SystemProto.PowerUseItem.FLASHLIGHT;
7800 break;
7801 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007802 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007803 continue;
7804 case USER:
7805 n = SystemProto.PowerUseItem.USER;
7806 uid = UserHandle.getUid(bs.userId, 0);
7807 break;
7808 case UNACCOUNTED:
7809 n = SystemProto.PowerUseItem.UNACCOUNTED;
7810 break;
7811 case OVERCOUNTED:
7812 n = SystemProto.PowerUseItem.OVERCOUNTED;
7813 break;
7814 case CAMERA:
7815 n = SystemProto.PowerUseItem.CAMERA;
7816 break;
7817 case MEMORY:
7818 n = SystemProto.PowerUseItem.MEMORY;
7819 break;
7820 }
Kweku Adams103351f2017-10-16 14:39:34 -07007821 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007822 proto.write(SystemProto.PowerUseItem.NAME, n);
7823 proto.write(SystemProto.PowerUseItem.UID, uid);
7824 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7825 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7826 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7827 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7828 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007829 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007830 }
7831 }
7832
7833 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007834 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007835 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7836 helper.getPowerProfile().getBatteryCapacity());
7837 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7838 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7839 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007840 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007841
7842 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7843 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7844 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7845 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007846 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007847 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7848 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7849 ent.getValue(), rawRealtimeUs, which);
7850 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7851 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007852 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007853 }
7854
7855 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7856 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007857 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007858 proto.write(SystemProto.ScreenBrightness.NAME, i);
7859 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7860 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007861 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007862 }
7863
7864 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7865 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7866 which);
7867
7868 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7869 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007870 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007871 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7872 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7873 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007874 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007875 }
7876
7877 // Wakeup reasons (WAKEUP_REASON_DATA)
7878 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7879 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007880 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007881 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7882 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007883 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007884 }
7885
7886 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7887 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007888 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007889 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7890 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7891 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007892 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007893 }
7894
7895 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7896 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007897 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007898 proto.write(SystemProto.WifiState.NAME, i);
7899 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7900 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007901 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007902 }
7903
7904 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7905 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007906 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007907 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7908 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7909 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007910 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007911 }
7912
7913 proto.end(sToken);
7914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915}