blob: d1932cfa6c0d7f9f29ce19936ea1c16590b3b7e0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 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
17package com.android.internal.os;
18
Sudheer Shanka9b735c52017-05-09 18:26:18 -070019import android.annotation.NonNull;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070020import android.annotation.Nullable;
Dianne Hackborn61659e52014-07-09 16:13:01 -070021import android.app.ActivityManager;
Adam Lesinski33dac552015-03-09 15:24:48 -070022import android.bluetooth.BluetoothActivityEnergyInfo;
Adam Lesinski50e47602015-12-04 17:04:54 -080023import android.bluetooth.UidTraffic;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080024import android.content.ContentResolver;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080025import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070026import android.content.Intent;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080027import android.database.ContentObserver;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070028import android.net.ConnectivityManager;
29import android.net.NetworkStats;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080030import android.net.Uri;
Adam Lesinski33dac552015-03-09 15:24:48 -070031import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070032import android.net.wifi.WifiManager;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070033import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070035import android.os.Build;
Siddharth Raya1fd0572017-11-13 14:20:47 -080036import android.os.connectivity.CellularBatteryStats;
Siddharth Rayb50a6842017-12-14 15:15:28 -080037import android.os.connectivity.WifiBatteryStats;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080038import android.os.connectivity.GpsBatteryStats;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070039import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070040import android.os.Handler;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080041import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6f357d32014-01-15 20:40:55 -080042import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070043import android.os.Message;
Bookatz1a1b0462018-01-12 11:47:03 -080044import android.os.OsProtoEnums;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.ParcelFormatException;
47import android.os.Parcelable;
Bookatz1a1b0462018-01-12 11:47:03 -080048import android.os.PowerManager;
Evan Millarc64edde2009-04-18 12:26:32 -070049import android.os.Process;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080050import android.os.RemoteException;
51import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.SystemClock;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070053import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070054import android.os.WorkSource;
Narayan Kamath81822022017-12-08 11:56:01 +000055import android.os.WorkSource.WorkChain;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080056import android.provider.Settings;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070057import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080058import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070059import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070060import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070061import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070062import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070063import android.util.ArrayMap;
Sudheer Shankab2f83c12017-11-13 19:25:01 -080064import android.util.IntArray;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080065import android.util.KeyValueListParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070067import android.util.LogWriter;
James Carr3a226052016-07-01 14:49:52 -070068import android.util.LongSparseArray;
69import android.util.LongSparseLongArray;
Dianne Hackbornd953c532014-08-16 18:17:38 -070070import android.util.MutableInt;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070071import android.util.Pools;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070072import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070074import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080076import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070077import android.util.SparseLongArray;
Bookatzc1a050a2017-10-10 15:49:28 -070078import android.util.StatsLog;
Dianne Hackbornae384452011-06-28 12:33:48 -070079import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080080import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070081import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Adam Lesinski14ae39a2017-05-26 11:50:40 -070083import com.android.internal.annotations.GuardedBy;
Adam Lesinski98f0d462016-04-19 16:46:20 -070084import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080085import com.android.internal.location.gnssmetrics.GnssMetrics;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070086import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080087import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070088import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080089import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070090import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080091import com.android.internal.util.XmlUtils;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070092
Narayan Kamath81822022017-12-08 11:56:01 +000093import java.util.List;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070094import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080095import org.xmlpull.v1.XmlPullParser;
96import org.xmlpull.v1.XmlPullParserException;
97import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070098
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080099import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.io.File;
101import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800102import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.io.FileOutputStream;
104import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700105import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100106import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.util.ArrayList;
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700108import java.util.Arrays;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800109import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -0700111import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.util.Map;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700113import java.util.concurrent.Future;
Christopher Tate4cee7252010-03-19 14:50:40 -0700114import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700115import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117/**
118 * All information we are collecting about things that can happen that impact
119 * battery life. All times are represented in microseconds except where indicated
120 * otherwise.
121 */
Joe Onoratoabded112016-02-08 16:49:39 -0800122public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800124 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700125 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800126 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700127 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700128 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700129 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700130
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700131 // TODO: remove "tcp" from network methods, since we measure total stats.
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700134 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 // Current on-disk Parcel version
Amith Yamasani977e11f2018-02-16 11:29:54 -0800137 private static final int VERSION = 175 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700138
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700139 // Maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100140 private static final int MAX_HISTORY_ITEMS;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700141
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700142 // No, really, THIS is the maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100143 private static final int MAX_MAX_HISTORY_ITEMS;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700144
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800145 // The maximum number of names wakelocks we will keep track of
146 // per uid; once the limit is reached, we batch the remaining wakelocks
147 // in to one common name.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100148 private static final int MAX_WAKELOCKS_PER_UID;
149
150 static final int MAX_HISTORY_BUFFER; // 256KB
151 static final int MAX_MAX_HISTORY_BUFFER; // 320KB
152
153 static {
154 if (ActivityManager.isLowRamDeviceStatic()) {
155 MAX_HISTORY_ITEMS = 800;
156 MAX_MAX_HISTORY_ITEMS = 1200;
157 MAX_WAKELOCKS_PER_UID = 40;
158 MAX_HISTORY_BUFFER = 96*1024; // 96KB
159 MAX_MAX_HISTORY_BUFFER = 128*1024; // 128KB
160 } else {
Makoto Onuki44d64602018-01-18 08:44:28 -0800161 MAX_HISTORY_ITEMS = 4000;
162 MAX_MAX_HISTORY_ITEMS = 6000;
163 MAX_WAKELOCKS_PER_UID = 200;
164 MAX_HISTORY_BUFFER = 512*1024; // 512KB
165 MAX_MAX_HISTORY_BUFFER = 640*1024; // 640KB
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100166 }
167 }
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700168
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800169 // Number of transmit power states the Wifi controller can be in.
170 private static final int NUM_WIFI_TX_LEVELS = 1;
171
172 // Number of transmit power states the Bluetooth controller can be in.
173 private static final int NUM_BT_TX_LEVELS = 1;
174
Sudheer Shanka38383232017-07-25 09:55:03 -0700175 /**
176 * Holding a wakelock costs more than just using the cpu.
177 * Currently, we assign only half the cpu time to an app that is running but
178 * not holding a wakelock. The apps holding wakelocks get the rest of the blame.
179 * If no app is holding a wakelock, then the distribution is normal.
180 */
181 @VisibleForTesting
182 public static final int WAKE_LOCK_WEIGHT = 50;
183
Joe Onoratoabded112016-02-08 16:49:39 -0800184 protected Clocks mClocks;
185
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700186 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700187 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800188 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Sudheer Shankac57729a2018-02-09 15:44:42 -0800190 static final int MSG_REPORT_CPU_UPDATE_NEEDED = 1;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700191 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700192 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700193 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700194
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700195 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
196 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
197
Sudheer Shanka38383232017-07-25 09:55:03 -0700198 @VisibleForTesting
199 protected KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
200 @VisibleForTesting
201 protected KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
202 @VisibleForTesting
203 protected KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700204 new KernelUidCpuFreqTimeReader();
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800205 @VisibleForTesting
Mike Ma3d422c32017-10-25 11:08:57 -0700206 protected KernelUidCpuActiveTimeReader mKernelUidCpuActiveTimeReader =
207 new KernelUidCpuActiveTimeReader();
208 @VisibleForTesting
209 protected KernelUidCpuClusterTimeReader mKernelUidCpuClusterTimeReader =
210 new KernelUidCpuClusterTimeReader();
211 @VisibleForTesting
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800212 protected KernelSingleUidTimeReader mKernelSingleUidTimeReader;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700213
James Carr3a226052016-07-01 14:49:52 -0700214 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
215 = new KernelMemoryBandwidthStats();
216 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
217 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
218 return mKernelMemoryStats;
219 }
220
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800221 @GuardedBy("this")
222 public boolean mPerProcStateCpuTimesAvailable = true;
223
224 /**
225 * Uids for which per-procstate cpu times need to be updated.
226 *
227 * Contains uid -> procState mappings.
228 */
229 @GuardedBy("this")
230 @VisibleForTesting
231 protected final SparseIntArray mPendingUids = new SparseIntArray();
232
Sudheer Shankac20379e2018-02-15 00:06:21 -0800233 @GuardedBy("this")
234 private long mNumCpuTimeReads;
235 @GuardedBy("this")
236 private long mNumBatchedCpuTimeReads;
237 @GuardedBy("this")
238 private long mCpuTimeReadsTrackingStartTime = SystemClock.uptimeMillis();
239
Bookatz0b8a0502017-09-13 11:51:52 -0700240 /** Container for Resource Power Manager stats. Updated by updateRpmStatsLocked. */
Bookatz50df7112017-08-04 14:53:26 -0700241 private final RpmStats mTmpRpmStats = new RpmStats();
Bookatz0b8a0502017-09-13 11:51:52 -0700242 /** The soonest the RPM stats can be updated after it was last updated. */
243 private static final long RPM_STATS_UPDATE_FREQ_MS = 1000;
244 /** Last time that RPM stats were updated by updateRpmStatsLocked. */
245 private long mLastRpmStatsUpdateTimeMs = -RPM_STATS_UPDATE_FREQ_MS;
Bookatz50df7112017-08-04 14:53:26 -0700246
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700247 public interface BatteryCallback {
248 public void batteryNeedsCpuUpdate();
249 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700250 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700251 }
252
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700253 public interface PlatformIdleStateCallback {
Bookatz50df7112017-08-04 14:53:26 -0700254 public void fillLowPowerStats(RpmStats rpmStats);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700255 public String getPlatformLowPowerStats();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000256 public String getSubsystemLowPowerStats();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700257 }
258
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700259 public static abstract class UserInfoProvider {
260 private int[] userIds;
261 protected abstract @Nullable int[] getUserIds();
Sudheer Shanka38383232017-07-25 09:55:03 -0700262 @VisibleForTesting
263 public final void refreshUserIds() {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700264 userIds = getUserIds();
265 }
Sudheer Shanka38383232017-07-25 09:55:03 -0700266 @VisibleForTesting
267 public boolean exists(int userId) {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700268 return userIds != null ? ArrayUtils.contains(userIds, userId) : true;
269 }
270 }
271
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700272 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
273
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700274 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800275 public MyHandler(Looper looper) {
276 super(looper, null, true);
277 }
278
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700279 @Override
280 public void handleMessage(Message msg) {
281 BatteryCallback cb = mCallback;
282 switch (msg.what) {
Sudheer Shankac57729a2018-02-09 15:44:42 -0800283 case MSG_REPORT_CPU_UPDATE_NEEDED:
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700284 if (cb != null) {
285 cb.batteryNeedsCpuUpdate();
286 }
287 break;
288 case MSG_REPORT_POWER_CHANGE:
289 if (cb != null) {
290 cb.batteryPowerChanged(msg.arg1 != 0);
291 }
292 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700293 case MSG_REPORT_CHARGING:
294 if (cb != null) {
295 final String action;
296 synchronized (BatteryStatsImpl.this) {
297 action = mCharging ? BatteryManager.ACTION_CHARGING
298 : BatteryManager.ACTION_DISCHARGING;
299 }
300 Intent intent = new Intent(action);
301 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
302 cb.batterySendBroadcast(intent);
303 }
304 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700305 }
306 }
307 }
308
Sudheer Shankac57729a2018-02-09 15:44:42 -0800309 public void postBatteryNeedsCpuUpdateMsg() {
310 mHandler.sendEmptyMessage(MSG_REPORT_CPU_UPDATE_NEEDED);
311 }
312
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800313 /**
314 * Update per-freq cpu times for all the uids in {@link #mPendingUids}.
315 */
Sudheer Shankae544d162017-12-28 17:06:20 -0800316 public void updateProcStateCpuTimes(boolean onBattery, boolean onBatteryScreenOff) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800317 final SparseIntArray uidStates;
318 synchronized (BatteryStatsImpl.this) {
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800319 if (!mConstants.TRACK_CPU_TIMES_BY_PROC_STATE) {
320 return;
321 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800322 if(!initKernelSingleUidTimeReaderLocked()) {
323 return;
324 }
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800325 // If the KernelSingleUidTimeReader has stale cpu times, then we shouldn't try to
326 // compute deltas since it might result in mis-attributing cpu times to wrong states.
327 if (mKernelSingleUidTimeReader.hasStaleData()) {
328 mPendingUids.clear();
329 return;
330 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800331
332 if (mPendingUids.size() == 0) {
333 return;
334 }
335 uidStates = mPendingUids.clone();
336 mPendingUids.clear();
337 }
338 for (int i = uidStates.size() - 1; i >= 0; --i) {
339 final int uid = uidStates.keyAt(i);
340 final int procState = uidStates.valueAt(i);
341 final int[] isolatedUids;
342 final Uid u;
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800343 synchronized (BatteryStatsImpl.this) {
344 // It's possible that uid no longer exists and any internal references have
345 // already been deleted, so using {@link #getAvailableUidStatsLocked} to avoid
346 // creating an UidStats object if it doesn't already exist.
347 u = getAvailableUidStatsLocked(uid);
348 if (u == null) {
349 continue;
350 }
351 if (u.mChildUids == null) {
352 isolatedUids = null;
353 } else {
354 isolatedUids = u.mChildUids.toArray();
355 for (int j = isolatedUids.length - 1; j >= 0; --j) {
356 isolatedUids[j] = u.mChildUids.get(j);
357 }
358 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800359 }
360 long[] cpuTimesMs = mKernelSingleUidTimeReader.readDeltaMs(uid);
361 if (isolatedUids != null) {
362 for (int j = isolatedUids.length - 1; j >= 0; --j) {
363 cpuTimesMs = addCpuTimes(cpuTimesMs,
364 mKernelSingleUidTimeReader.readDeltaMs(isolatedUids[j]));
365 }
366 }
367 if (onBattery && cpuTimesMs != null) {
368 synchronized (BatteryStatsImpl.this) {
Sudheer Shankae544d162017-12-28 17:06:20 -0800369 u.addProcStateTimesMs(procState, cpuTimesMs, onBattery);
370 u.addProcStateScreenOffTimesMs(procState, cpuTimesMs, onBatteryScreenOff);
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800371 }
372 }
373 }
374 }
375
Sudheer Shankae544d162017-12-28 17:06:20 -0800376 public void copyFromAllUidsCpuTimes() {
377 synchronized (BatteryStatsImpl.this) {
378 copyFromAllUidsCpuTimes(
379 mOnBatteryTimeBase.isRunning(), mOnBatteryScreenOffTimeBase.isRunning());
380 }
381 }
382
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800383 /**
384 * When the battery/screen state changes, we don't attribute the cpu times to any process
385 * but we still need to snapshots of all uids to get correct deltas later on. Since we
386 * already read this data for updating per-freq cpu times, we can use the same data for
387 * per-procstate cpu times.
388 */
Sudheer Shankae544d162017-12-28 17:06:20 -0800389 public void copyFromAllUidsCpuTimes(boolean onBattery, boolean onBatteryScreenOff) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800390 synchronized (BatteryStatsImpl.this) {
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800391 if (!mConstants.TRACK_CPU_TIMES_BY_PROC_STATE) {
392 return;
393 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800394 if(!initKernelSingleUidTimeReaderLocked()) {
395 return;
396 }
397
398 final SparseArray<long[]> allUidCpuFreqTimesMs =
399 mKernelUidCpuFreqTimeReader.getAllUidCpuFreqTimeMs();
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800400 // If the KernelSingleUidTimeReader has stale cpu times, then we shouldn't try to
401 // compute deltas since it might result in mis-attributing cpu times to wrong states.
402 if (mKernelSingleUidTimeReader.hasStaleData()) {
403 mKernelSingleUidTimeReader.setAllUidsCpuTimesMs(allUidCpuFreqTimesMs);
404 mKernelSingleUidTimeReader.markDataAsStale(false);
405 mPendingUids.clear();
406 return;
407 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800408 for (int i = allUidCpuFreqTimesMs.size() - 1; i >= 0; --i) {
409 final int uid = allUidCpuFreqTimesMs.keyAt(i);
410 final Uid u = getAvailableUidStatsLocked(mapUid(uid));
411 if (u == null) {
412 continue;
413 }
414 final long[] cpuTimesMs = allUidCpuFreqTimesMs.valueAt(i);
415 if (cpuTimesMs == null) {
416 continue;
417 }
418 final long[] deltaTimesMs = mKernelSingleUidTimeReader.computeDelta(
419 uid, cpuTimesMs.clone());
Sudheer Shankae544d162017-12-28 17:06:20 -0800420 if (onBattery && deltaTimesMs != null) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800421 final int procState;
422 final int idx = mPendingUids.indexOfKey(uid);
423 if (idx >= 0) {
424 procState = mPendingUids.valueAt(idx);
425 mPendingUids.removeAt(idx);
426 } else {
427 procState = u.mProcessState;
428 }
429 if (procState >= 0 && procState < Uid.NUM_PROCESS_STATE) {
Sudheer Shankae544d162017-12-28 17:06:20 -0800430 u.addProcStateTimesMs(procState, deltaTimesMs, onBattery);
431 u.addProcStateScreenOffTimesMs(procState, deltaTimesMs, onBatteryScreenOff);
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800432 }
433 }
434 }
435 }
436 }
437
438 @VisibleForTesting
439 public long[] addCpuTimes(long[] timesA, long[] timesB) {
440 if (timesA != null && timesB != null) {
441 for (int i = timesA.length - 1; i >= 0; --i) {
442 timesA[i] += timesB[i];
443 }
444 return timesA;
445 }
446 return timesA == null ? (timesB == null ? null : timesB) : timesA;
447 }
448
449 @GuardedBy("this")
450 private boolean initKernelSingleUidTimeReaderLocked() {
451 if (mKernelSingleUidTimeReader == null) {
452 if (mPowerProfile == null) {
453 return false;
454 }
455 if (mCpuFreqs == null) {
456 mCpuFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile);
457 }
458 if (mCpuFreqs != null) {
459 mKernelSingleUidTimeReader = new KernelSingleUidTimeReader(mCpuFreqs.length);
460 } else {
461 mPerProcStateCpuTimesAvailable = mKernelUidCpuFreqTimeReader.allUidTimesAvailable();
462 return false;
463 }
464 }
465 mPerProcStateCpuTimesAvailable = mKernelUidCpuFreqTimeReader.allUidTimesAvailable()
466 && mKernelSingleUidTimeReader.singleUidCpuTimesAvailable();
467 return true;
468 }
469
Joe Onoratoabded112016-02-08 16:49:39 -0800470 public interface Clocks {
471 public long elapsedRealtime();
472 public long uptimeMillis();
473 }
474
475 public static class SystemClocks implements Clocks {
476 public long elapsedRealtime() {
477 return SystemClock.elapsedRealtime();
478 }
479
480 public long uptimeMillis() {
481 return SystemClock.uptimeMillis();
482 }
483 }
484
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700485 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700486 int UPDATE_CPU = 0x01;
487 int UPDATE_WIFI = 0x02;
488 int UPDATE_RADIO = 0x04;
489 int UPDATE_BT = 0x08;
Bookatz50df7112017-08-04 14:53:26 -0700490 int UPDATE_RPM = 0x10; // 16
491 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT | UPDATE_RPM;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800492
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700493 Future<?> scheduleSync(String reason, int flags);
494 Future<?> scheduleCpuSyncDueToRemovedUid(int uid);
Sudheer Shankac20379e2018-02-15 00:06:21 -0800495 Future<?> scheduleReadProcStateCpuTimes(boolean onBattery, boolean onBatteryScreenOff,
496 long delayMillis);
Sudheer Shankae544d162017-12-28 17:06:20 -0800497 Future<?> scheduleCopyFromAllUidsCpuTimes(boolean onBattery, boolean onBatteryScreenOff);
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800498 Future<?> scheduleCpuSyncDueToSettingChange();
Sudheer Shankac57729a2018-02-09 15:44:42 -0800499 Future<?> scheduleCpuSyncDueToScreenStateChange(boolean onBattery,
500 boolean onBatteryScreenOff);
501 Future<?> scheduleCpuSyncDueToWakelockChange(long delayMillis);
502 void cancelCpuSyncDueToWakelockChange();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700503 }
504
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800505 public Handler mHandler;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700506 private ExternalStatsSync mExternalSync = null;
Sudheer Shanka38383232017-07-25 09:55:03 -0700507 @VisibleForTesting
508 protected UserInfoProvider mUserInfoProvider = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700509
510 private BatteryCallback mCallback;
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800513 * Mapping isolated uids to the actual owning app uid.
514 */
515 final SparseIntArray mIsolatedUids = new SparseIntArray();
516
517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 * The statistics we have collected organized by uids.
519 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800520 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
522 // A set of pools of currently active timers. When a timer is queried, we will divide the
523 // elapsed time by the number of active timers to arrive at that timer's share of the time.
524 // In order to do this, we must refresh each timer whenever the number of active timers
525 // changes.
Sudheer Shanka38383232017-07-25 09:55:03 -0700526 @VisibleForTesting
527 protected ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700528 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
529 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700530 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700531 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
532 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
533 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
534 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
535 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
536 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
537 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
538 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700539 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
540 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800541 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700543 // Last partial timers we use for distributing CPU usage.
Sudheer Shanka38383232017-07-25 09:55:03 -0700544 @VisibleForTesting
545 protected ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 // These are the objects that will want to do something when the device
548 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800549 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800550
551 // These are the objects that will want to do something when the device
Mike Mac2f518a2017-09-19 16:06:03 -0700552 // is unplugged from power *and* the screen is off or doze.
553 protected final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800554
555 // Set to true when we want to distribute CPU across wakelocks for the next
556 // CPU update, even if we aren't currently running wake locks.
557 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700558
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700559 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700560
Dianne Hackborn37de0982014-05-09 09:32:18 -0700561 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800562
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700563 long mHistoryBaseTime;
Narayan Kamath695cf722017-12-21 18:32:47 +0000564 protected boolean mHaveBatteryLevel = false;
565 protected boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700566 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700567
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700568 final Parcel mHistoryBuffer = Parcel.obtain();
569 final HistoryItem mHistoryLastWritten = new HistoryItem();
570 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700571 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700572 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700573 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800574 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800575 int[] mReadHistoryUids;
576 int mReadHistoryChars;
577 int mNextHistoryTagIdx = 0;
578 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700579 int mHistoryBufferLastPos = -1;
580 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700581 int mActiveHistoryStates = 0xffffffff;
582 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700583 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700584 long mTrackRunningHistoryElapsedRealtime = 0;
585 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700586
587 final HistoryItem mHistoryCur = new HistoryItem();
588
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700589 HistoryItem mHistory;
590 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700591 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700592 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700593
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800594 // Used by computeHistoryStepDetails
595 HistoryStepDetails mLastHistoryStepDetails = null;
596 byte mLastHistoryStepLevel = 0;
597 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
598 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
599 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700600
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800601 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700602 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800603 */
604 long mLastStepCpuUserTime;
605 long mCurStepCpuUserTime;
606 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700607 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800608 */
609 long mLastStepCpuSystemTime;
610 long mCurStepCpuSystemTime;
611 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700612 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800613 */
614 long mLastStepStatUserTime;
615 long mLastStepStatSystemTime;
616 long mLastStepStatIOWaitTime;
617 long mLastStepStatIrqTime;
618 long mLastStepStatSoftIrqTime;
619 long mLastStepStatIdleTime;
620 long mCurStepStatUserTime;
621 long mCurStepStatSystemTime;
622 long mCurStepStatIOWaitTime;
623 long mCurStepStatIrqTime;
624 long mCurStepStatSoftIrqTime;
625 long mCurStepStatIdleTime;
626
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700627 private HistoryItem mHistoryIterator;
628 private boolean mReadOverflow;
629 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 int mStartCount;
632
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800633 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700634 String mStartPlatformVersion;
635 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 long mUptime;
638 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 long mRealtime;
640 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700641
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800642 int mWakeLockNesting;
643 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700644 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700645 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800646
Mike Mac2f518a2017-09-19 16:06:03 -0700647 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
648 protected int mScreenState = Display.STATE_UNKNOWN;
649 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
650 protected StopwatchTimer mScreenOnTimer;
651 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
652 protected StopwatchTimer mScreenDozeTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700653
Dianne Hackborn617f8772009-03-31 15:04:46 -0700654 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700655 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700656
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800657 boolean mPretendScreenOff;
658
Jeff Browne95c3cd2014-05-02 16:59:26 -0700659 boolean mInteractive;
660 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700661
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700662 boolean mPowerSaveModeEnabled;
663 StopwatchTimer mPowerSaveModeEnabledTimer;
664
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700665 boolean mDeviceIdling;
666 StopwatchTimer mDeviceIdlingTimer;
667
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700668 boolean mDeviceLightIdling;
669 StopwatchTimer mDeviceLightIdlingTimer;
670
671 int mDeviceIdleMode;
672 long mLastIdleTimeStart;
673 long mLongestLightIdleTime;
674 long mLongestFullIdleTime;
675 StopwatchTimer mDeviceIdleModeLightTimer;
676 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700679 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700680
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700681 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700682 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700683
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700684 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700685 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700686
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700687 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700688 StopwatchTimer mFlashlightOnTimer;
689
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700690 int mCameraOnNesting;
691 StopwatchTimer mCameraOnTimer;
692
Siddharth Ray78ccaf52017-12-23 16:16:21 -0800693 int mGpsSignalQualityBin = -1;
Narayan Kamathefa0fe62018-02-05 16:04:43 +0000694 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
695 protected final StopwatchTimer[] mGpsSignalQualityTimer =
Siddharth Ray78ccaf52017-12-23 16:16:21 -0800696 new StopwatchTimer[GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS];
697
Dianne Hackborn627bba72009-03-24 22:32:56 -0700698 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800699 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700700 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800701 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700702
703 StopwatchTimer mPhoneSignalScanningTimer;
704
Dianne Hackborn627bba72009-03-24 22:32:56 -0700705 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700706 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700707 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700708
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800709 final LongSamplingCounter[] mNetworkByteActivityCounters =
710 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
711 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700712 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
713
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800714 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800715 * The WiFi Overall wakelock timer
716 * This timer tracks the actual aggregate time for which MC wakelocks are enabled
717 * since addition of per UID timers would not result in an accurate value due to overlapp of
718 * per uid wakelock timers
719 */
720 StopwatchTimer mWifiMulticastWakelockTimer;
721
722 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800723 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
724 */
725 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700726
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800727 /**
728 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
729 */
730 ControllerActivityCounterImpl mBluetoothActivity;
731
732 /**
733 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
734 */
735 ControllerActivityCounterImpl mModemActivity;
736
737 /**
738 * Whether the device supports WiFi controller energy reporting. This is set to true on
739 * the first WiFi energy report. See {@link #mWifiActivity}.
740 */
741 boolean mHasWifiReporting = false;
742
743 /**
744 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
745 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
746 */
747 boolean mHasBluetoothReporting = false;
748
749 /**
750 * Whether the device supports Modem controller energy reporting. This is set to true on
751 * the first Modem energy report. See {@link #mModemActivity}.
752 */
753 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700754
The Android Open Source Project10592532009-03-18 17:39:46 -0700755 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700756 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700757
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700758 boolean mGlobalWifiRunning;
759 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700760
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800761 int mWifiState = -1;
762 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
763
Dianne Hackborn3251b902014-06-20 14:40:53 -0700764 int mWifiSupplState = -1;
765 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
766
767 int mWifiSignalStrengthBin = -1;
768 final StopwatchTimer[] mWifiSignalStrengthsTimer =
769 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
770
Siddharth Rayb50a6842017-12-14 15:15:28 -0800771 StopwatchTimer mWifiActiveTimer;
772
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800773 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800774 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
775 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800776
Siddharth Rayf5e796a2018-01-22 18:18:17 -0800777 boolean mIsCellularTxPowerHigh = false;
778
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700779 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700780 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800781 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800782 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700783 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800784 LongSamplingCounter mMobileRadioActiveUnknownTime;
785 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800786
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700787 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 /**
790 * These provide time bases that discount the time the device is plugged
791 * in to power.
792 */
793 boolean mOnBattery;
Sudheer Shanka38383232017-07-25 09:55:03 -0700794 @VisibleForTesting
795 protected boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700796
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700797 /**
798 * External reporting of whether the device is actually charging.
799 */
800 boolean mCharging = true;
801 int mLastChargingStateLevel;
802
The Android Open Source Project10592532009-03-18 17:39:46 -0700803 /*
804 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
805 */
Evan Millar633a1742009-04-02 16:36:33 -0700806 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700807 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700808 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700809 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700810 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700811 int mLowDischargeAmountSinceCharge;
812 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800813 int mDischargeScreenOnUnplugLevel;
814 int mDischargeScreenOffUnplugLevel;
Mike Mac2f518a2017-09-19 16:06:03 -0700815 int mDischargeScreenDozeUnplugLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800816 int mDischargeAmountScreenOn;
817 int mDischargeAmountScreenOnSinceCharge;
818 int mDischargeAmountScreenOff;
819 int mDischargeAmountScreenOffSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -0700820 int mDischargeAmountScreenDoze;
821 int mDischargeAmountScreenDozeSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700822
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700823 private LongSamplingCounter mDischargeScreenOffCounter;
Mike Mac2f518a2017-09-19 16:06:03 -0700824 private LongSamplingCounter mDischargeScreenDozeCounter;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700825 private LongSamplingCounter mDischargeCounter;
Mike Ma15313c92017-11-15 17:58:21 -0800826 private LongSamplingCounter mDischargeLightDozeCounter;
827 private LongSamplingCounter mDischargeDeepDozeCounter;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700828
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700829 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700830
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700831 int mInitStepMode = 0;
832 int mCurStepMode = 0;
833 int mModStepMode = 0;
834
Dianne Hackborn260c5022014-04-29 11:23:16 -0700835 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700836 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800837 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
838 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700839 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700840
841 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700842 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800843 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
844 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
845
846 static final int MAX_DAILY_ITEMS = 10;
847
848 long mDailyStartTime = 0;
849 long mNextMinDailyDeadline = 0;
850 long mNextMaxDailyDeadline = 0;
851
852 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700855
Amith Yamasanif37447b2009-10-08 18:28:01 -0700856 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800857 private int mPhoneServiceStateRaw = -1;
858 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700859
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800860 private int mNumConnectivityChange;
861 private int mLoadedNumConnectivityChange;
862 private int mUnpluggedNumConnectivityChange;
863
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700864 private int mEstimatedBatteryCapacity = -1;
865
Jocelyn Dangc627d102017-04-14 13:15:14 -0700866 private int mMinLearnedBatteryCapacity = -1;
867 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800868
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700869 private long[] mCpuFreqs;
870
Sudheer Shanka38383232017-07-25 09:55:03 -0700871 @VisibleForTesting
872 protected PowerProfile mPowerProfile;
Adam Lesinskie08af192015-03-25 16:42:59 -0700873
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800874 @GuardedBy("this")
875 private final Constants mConstants;
876
Evan Millarc64edde2009-04-18 12:26:32 -0700877 /*
Bookatz50df7112017-08-04 14:53:26 -0700878 * Holds a SamplingTimer associated with each Resource Power Manager state and voter,
879 * recording their times when on-battery (regardless of screen state).
880 */
881 private final HashMap<String, SamplingTimer> mRpmStats = new HashMap<>();
882 /** Times for each Resource Power Manager state and voter when screen-off and on-battery. */
883 private final HashMap<String, SamplingTimer> mScreenOffRpmStats = new HashMap<>();
884
885 @Override
886 public Map<String, ? extends Timer> getRpmStats() {
887 return mRpmStats;
888 }
889
Bookatz82b341172017-09-07 19:06:08 -0700890 // TODO: Note: screenOffRpmStats has been disabled via SCREEN_OFF_RPM_STATS_ENABLED.
Bookatz50df7112017-08-04 14:53:26 -0700891 @Override
892 public Map<String, ? extends Timer> getScreenOffRpmStats() {
893 return mScreenOffRpmStats;
894 }
895
896 /*
Evan Millarc64edde2009-04-18 12:26:32 -0700897 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
898 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700899 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700900
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700901 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700902 return mKernelWakelockStats;
903 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700904
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700905 String mLastWakeupReason = null;
906 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700907 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700908
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700909 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700910 return mWakeupReasonStats;
911 }
912
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700913 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700914 public long getUahDischarge(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700915 return mDischargeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700916 }
917
918 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700919 public long getUahDischargeScreenOff(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700920 return mDischargeScreenOffCounter.getCountLocked(which);
921 }
922
923 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700924 public long getUahDischargeScreenDoze(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700925 return mDischargeScreenDozeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700926 }
927
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700928 @Override
Mike Ma15313c92017-11-15 17:58:21 -0800929 public long getUahDischargeLightDoze(int which) {
930 return mDischargeLightDozeCounter.getCountLocked(which);
931 }
932
933 @Override
934 public long getUahDischargeDeepDoze(int which) {
935 return mDischargeDeepDozeCounter.getCountLocked(which);
936 }
937
938 @Override
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700939 public int getEstimatedBatteryCapacity() {
940 return mEstimatedBatteryCapacity;
941 }
942
Jocelyn Dangc627d102017-04-14 13:15:14 -0700943 @Override
944 public int getMinLearnedBatteryCapacity() {
945 return mMinLearnedBatteryCapacity;
946 }
947
948 @Override
949 public int getMaxLearnedBatteryCapacity() {
950 return mMaxLearnedBatteryCapacity;
951 }
952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800954 this(new SystemClocks());
955 }
956
957 public BatteryStatsImpl(Clocks clocks) {
958 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700959 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700960 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800961 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700962 mHandler = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700963 mPlatformIdleStateCallback = null;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700964 mUserInfoProvider = null;
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800965 mConstants = new Constants(mHandler);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700966 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968
Joe Onoratoabded112016-02-08 16:49:39 -0800969 private void init(Clocks clocks) {
970 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800971 }
972
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700973 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800974 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
975 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
976 }
977
Joe Onoratoabded112016-02-08 16:49:39 -0800978 // methods are protected not private to be VisibleForTesting
979 public static class TimeBase {
980 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800981
Joe Onoratoabded112016-02-08 16:49:39 -0800982 protected long mUptime;
983 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800984
Joe Onoratoabded112016-02-08 16:49:39 -0800985 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800986
Joe Onoratoabded112016-02-08 16:49:39 -0800987 protected long mPastUptime;
988 protected long mUptimeStart;
989 protected long mPastRealtime;
990 protected long mRealtimeStart;
991 protected long mUnpluggedUptime;
992 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800993
994 public void dump(PrintWriter pw, String prefix) {
995 StringBuilder sb = new StringBuilder(128);
996 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
997 sb.setLength(0);
998 sb.append(prefix);
999 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -07001000 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001001 pw.println(sb.toString());
1002 sb.setLength(0);
1003 sb.append(prefix);
1004 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -07001005 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001006 pw.println(sb.toString());
1007 sb.setLength(0);
1008 sb.append(prefix);
1009 sb.append("mPastUptime=");
1010 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
1011 formatTimeMs(sb, mUptimeStart / 1000);
1012 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
1013 pw.println(sb.toString());
1014 sb.setLength(0);
1015 sb.append(prefix);
1016 sb.append("mPastRealtime=");
1017 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
1018 formatTimeMs(sb, mRealtimeStart / 1000);
1019 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
1020 pw.println(sb.toString());
1021 }
1022
1023 public void add(TimeBaseObs observer) {
1024 mObservers.add(observer);
1025 }
1026
1027 public void remove(TimeBaseObs observer) {
1028 if (!mObservers.remove(observer)) {
1029 Slog.wtf(TAG, "Removed unknown observer: " + observer);
1030 }
1031 }
1032
Joe Onoratoabded112016-02-08 16:49:39 -08001033 public boolean hasObserver(TimeBaseObs observer) {
1034 return mObservers.contains(observer);
1035 }
1036
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001037 public void init(long uptime, long realtime) {
1038 mRealtime = 0;
1039 mUptime = 0;
1040 mPastUptime = 0;
1041 mPastRealtime = 0;
1042 mUptimeStart = uptime;
1043 mRealtimeStart = realtime;
1044 mUnpluggedUptime = getUptime(mUptimeStart);
1045 mUnpluggedRealtime = getRealtime(mRealtimeStart);
1046 }
1047
1048 public void reset(long uptime, long realtime) {
1049 if (!mRunning) {
1050 mPastUptime = 0;
1051 mPastRealtime = 0;
1052 } else {
1053 mUptimeStart = uptime;
1054 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -08001055 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
1056 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001057 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -08001058 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001059 mUnpluggedRealtime = getRealtime(realtime);
1060 }
1061 }
1062
1063 public long computeUptime(long curTime, int which) {
1064 switch (which) {
1065 case STATS_SINCE_CHARGED:
1066 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001067 case STATS_CURRENT:
1068 return getUptime(curTime);
1069 case STATS_SINCE_UNPLUGGED:
1070 return getUptime(curTime) - mUnpluggedUptime;
1071 }
1072 return 0;
1073 }
1074
1075 public long computeRealtime(long curTime, int which) {
1076 switch (which) {
1077 case STATS_SINCE_CHARGED:
1078 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001079 case STATS_CURRENT:
1080 return getRealtime(curTime);
1081 case STATS_SINCE_UNPLUGGED:
1082 return getRealtime(curTime) - mUnpluggedRealtime;
1083 }
1084 return 0;
1085 }
1086
1087 public long getUptime(long curTime) {
1088 long time = mPastUptime;
1089 if (mRunning) {
1090 time += curTime - mUptimeStart;
1091 }
1092 return time;
1093 }
1094
1095 public long getRealtime(long curTime) {
1096 long time = mPastRealtime;
1097 if (mRunning) {
1098 time += curTime - mRealtimeStart;
1099 }
1100 return time;
1101 }
1102
1103 public long getUptimeStart() {
1104 return mUptimeStart;
1105 }
1106
1107 public long getRealtimeStart() {
1108 return mRealtimeStart;
1109 }
1110
1111 public boolean isRunning() {
1112 return mRunning;
1113 }
1114
1115 public boolean setRunning(boolean running, long uptime, long realtime) {
1116 if (mRunning != running) {
1117 mRunning = running;
1118 if (running) {
1119 mUptimeStart = uptime;
1120 mRealtimeStart = realtime;
1121 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
1122 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
1123
1124 for (int i = mObservers.size() - 1; i >= 0; i--) {
1125 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
1126 }
1127 } else {
1128 mPastUptime += uptime - mUptimeStart;
1129 mPastRealtime += realtime - mRealtimeStart;
1130
1131 long batteryUptime = getUptime(uptime);
1132 long batteryRealtime = getRealtime(realtime);
1133
1134 for (int i = mObservers.size() - 1; i >= 0; i--) {
1135 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
1136 }
1137 }
1138 return true;
1139 }
1140 return false;
1141 }
1142
1143 public void readSummaryFromParcel(Parcel in) {
1144 mUptime = in.readLong();
1145 mRealtime = in.readLong();
1146 }
1147
1148 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
1149 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
1150 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
1151 }
1152
1153 public void readFromParcel(Parcel in) {
1154 mRunning = false;
1155 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001156 mPastUptime = in.readLong();
1157 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -07001158 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001159 mPastRealtime = in.readLong();
1160 mRealtimeStart = in.readLong();
1161 mUnpluggedUptime = in.readLong();
1162 mUnpluggedRealtime = in.readLong();
1163 }
1164
1165 public void writeToParcel(Parcel out, long uptime, long realtime) {
1166 final long runningUptime = getUptime(uptime);
1167 final long runningRealtime = getRealtime(realtime);
1168 out.writeLong(mUptime);
1169 out.writeLong(runningUptime);
1170 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -07001171 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 out.writeLong(runningRealtime);
1173 out.writeLong(mRealtimeStart);
1174 out.writeLong(mUnpluggedUptime);
1175 out.writeLong(mUnpluggedRealtime);
1176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001180 * State for keeping track of counting information.
1181 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001182 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -07001183 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001184 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001185 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001186 int mUnpluggedCount;
1187 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001188
Bookatz8c6f3c52017-05-24 12:00:17 -07001189 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001190 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -07001191 mPluggedCount = in.readInt();
1192 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001193 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001194 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001195 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001196 }
1197
Bookatz8c6f3c52017-05-24 12:00:17 -07001198 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001199 mTimeBase = timeBase;
1200 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001201 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001202
Dianne Hackborn617f8772009-03-31 15:04:46 -07001203 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001204 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001205 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001206 out.writeInt(mUnpluggedCount);
1207 }
1208
Bookatz8c6f3c52017-05-24 12:00:17 -07001209 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001210 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001211 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001212 }
1213
Bookatz8c6f3c52017-05-24 12:00:17 -07001214 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001215 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001216 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001217 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001218
Dianne Hackborn617f8772009-03-31 15:04:46 -07001219 /**
1220 * Writes a possibly null Counter to a Parcel.
1221 *
1222 * @param out the Parcel to be written to.
1223 * @param counter a Counter, or null.
1224 */
Amith Yamasani977e11f2018-02-16 11:29:54 -08001225 public static void writeCounterToParcel(Parcel out, @Nullable Counter counter) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001226 if (counter == null) {
1227 out.writeInt(0); // indicates null
1228 return;
1229 }
1230 out.writeInt(1); // indicates non-null
1231
1232 counter.writeToParcel(out);
1233 }
1234
Amith Yamasani977e11f2018-02-16 11:29:54 -08001235 /**
1236 * Reads a Counter that was written using {@link #writeCounterToParcel(Parcel, Counter)}.
1237 * @param timeBase the timebase to assign to the Counter
1238 * @param in the parcel to read from
1239 * @return the Counter or null.
1240 */
1241 public static @Nullable Counter readCounterFromParcel(TimeBase timeBase, Parcel in) {
1242 if (in.readInt() == 0) {
1243 return null;
1244 }
1245 return new Counter(timeBase, in);
1246 }
1247
Dianne Hackborn617f8772009-03-31 15:04:46 -07001248 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001249 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001250 int val = mCount.get();
1251 if (which == STATS_SINCE_UNPLUGGED) {
1252 val -= mUnpluggedCount;
1253 } else if (which != STATS_SINCE_CHARGED) {
1254 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001255 }
1256
1257 return val;
1258 }
1259
1260 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001261 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -07001262 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -07001263 + " mUnpluggedCount=" + mUnpluggedCount
1264 + " mPluggedCount=" + mPluggedCount);
1265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001266
Bookatz8c6f3c52017-05-24 12:00:17 -07001267 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1268 public void stepAtomic() {
1269 if (mTimeBase.isRunning()) {
1270 mCount.incrementAndGet();
1271 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001272 }
1273
Bookatz4ebc0642017-05-11 12:21:19 -07001274 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001275 if (mTimeBase.isRunning()) {
1276 mCount.addAndGet(delta);
1277 }
Bookatz4ebc0642017-05-11 12:21:19 -07001278 }
1279
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001280 /**
1281 * Clear state of this counter.
1282 */
1283 void reset(boolean detachIfReset) {
1284 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -07001285 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001286 if (detachIfReset) {
1287 detach();
1288 }
1289 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001290
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001291 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001292 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001293 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001294
Bookatz8c6f3c52017-05-24 12:00:17 -07001295 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1296 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001297 int count = mCount.get();
1298 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001299 }
1300
Bookatz8c6f3c52017-05-24 12:00:17 -07001301 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1302 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001303 mLoadedCount = in.readInt();
1304 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -07001305 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001306 }
1307 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07001308
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001309 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001310 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
1311 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001312 public long[] mCounts;
1313 public long[] mLoadedCounts;
1314 public long[] mUnpluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001315
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001316 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001317 mTimeBase = timeBase;
Sudheer Shankae544d162017-12-28 17:06:20 -08001318 mCounts = in.createLongArray();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001319 mLoadedCounts = in.createLongArray();
1320 mUnpluggedCounts = in.createLongArray();
1321 timeBase.add(this);
1322 }
1323
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001324 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001325 mTimeBase = timeBase;
1326 timeBase.add(this);
1327 }
1328
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001329 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001330 out.writeLongArray(mCounts);
1331 out.writeLongArray(mLoadedCounts);
1332 out.writeLongArray(mUnpluggedCounts);
1333 }
1334
1335 @Override
1336 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
Sudheer Shankae544d162017-12-28 17:06:20 -08001337 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001338 }
1339
1340 @Override
1341 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001342 }
1343
1344 @Override
1345 public long[] getCountsLocked(int which) {
Sudheer Shankae544d162017-12-28 17:06:20 -08001346 long[] val = copyArray(mCounts, null);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001347 if (which == STATS_SINCE_UNPLUGGED) {
1348 subtract(val, mUnpluggedCounts);
1349 } else if (which != STATS_SINCE_CHARGED) {
1350 subtract(val, mLoadedCounts);
1351 }
1352 return val;
1353 }
1354
1355 @Override
1356 public void logState(Printer pw, String prefix) {
1357 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1358 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
Sudheer Shankae544d162017-12-28 17:06:20 -08001359 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts));
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001360 }
1361
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001362 public void addCountLocked(long[] counts) {
Sudheer Shankae544d162017-12-28 17:06:20 -08001363 addCountLocked(counts, mTimeBase.isRunning());
1364 }
1365
1366 public void addCountLocked(long[] counts, boolean isRunning) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001367 if (counts == null) {
1368 return;
1369 }
Sudheer Shankae544d162017-12-28 17:06:20 -08001370 if (isRunning) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001371 if (mCounts == null) {
1372 mCounts = new long[counts.length];
1373 }
1374 for (int i = 0; i < counts.length; ++i) {
1375 mCounts[i] += counts[i];
1376 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001377 }
1378 }
1379
Sudheer Shankab8ad5942017-08-08 12:16:09 -07001380 public int getSize() {
1381 return mCounts == null ? 0 : mCounts.length;
1382 }
1383
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001384 /**
1385 * Clear state of this counter.
1386 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001387 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001388 fillArray(mCounts, 0);
1389 fillArray(mLoadedCounts, 0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001390 fillArray(mUnpluggedCounts, 0);
1391 if (detachIfReset) {
1392 detach();
1393 }
1394 }
1395
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001396 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001397 mTimeBase.remove(this);
1398 }
1399
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001400 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001401 out.writeLongArray(mCounts);
1402 }
1403
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001404 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001405 mCounts = in.createLongArray();
1406 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1407 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001408 }
1409
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001410 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1411 if (counterArray != null) {
1412 out.writeInt(1);
1413 counterArray.writeToParcel(out);
1414 } else {
1415 out.writeInt(0);
1416 }
1417 }
1418
1419 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1420 if (in.readInt() != 0) {
1421 return new LongSamplingCounterArray(timeBase, in);
1422 } else {
1423 return null;
1424 }
1425 }
1426
1427 public static void writeSummaryToParcelLocked(Parcel out,
1428 LongSamplingCounterArray counterArray) {
1429 if (counterArray != null) {
1430 out.writeInt(1);
1431 counterArray.writeSummaryToParcelLocked(out);
1432 } else {
1433 out.writeInt(0);
1434 }
1435 }
1436
1437 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1438 TimeBase timeBase) {
1439 if (in.readInt() != 0) {
1440 final LongSamplingCounterArray counterArray
1441 = new LongSamplingCounterArray(timeBase);
1442 counterArray.readSummaryFromParcelLocked(in);
1443 return counterArray;
1444 } else {
1445 return null;
1446 }
1447 }
1448
Bookatz8c6f3c52017-05-24 12:00:17 -07001449 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001450 if (a != null) {
1451 Arrays.fill(a, val);
1452 }
1453 }
1454
Bookatz8c6f3c52017-05-24 12:00:17 -07001455 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001456 if (toSubtract == null) {
1457 return;
1458 }
1459 for (int i = 0; i < val.length; i++) {
1460 val[i] -= toSubtract[i];
1461 }
1462 }
1463
Bookatz8c6f3c52017-05-24 12:00:17 -07001464 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001465 if (src == null) {
1466 return null;
1467 } else {
1468 if (dest == null) {
1469 dest = new long[src.length];
1470 }
1471 System.arraycopy(src, 0, dest, 0, src.length);
1472 return dest;
1473 }
1474 }
1475 }
1476
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001477 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001478 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001479 long mCount;
1480 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001481 long mUnpluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001482
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001483 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1484 mTimeBase = timeBase;
Sudheer Shankac57729a2018-02-09 15:44:42 -08001485 mCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001486 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001487 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001488 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001489 }
1490
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001491 LongSamplingCounter(TimeBase timeBase) {
1492 mTimeBase = timeBase;
1493 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001494 }
1495
1496 public void writeToParcel(Parcel out) {
1497 out.writeLong(mCount);
1498 out.writeLong(mLoadedCount);
1499 out.writeLong(mUnpluggedCount);
1500 }
1501
1502 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001503 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Sudheer Shankac57729a2018-02-09 15:44:42 -08001504 mUnpluggedCount = mCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001505 }
1506
1507 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001508 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001509 }
1510
1511 public long getCountLocked(int which) {
Sudheer Shankac57729a2018-02-09 15:44:42 -08001512 long val = mCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001513 if (which == STATS_SINCE_UNPLUGGED) {
1514 val -= mUnpluggedCount;
1515 } else if (which != STATS_SINCE_CHARGED) {
1516 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001517 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001518 return val;
1519 }
1520
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001521 @Override
1522 public void logState(Printer pw, String prefix) {
1523 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001524 + " mLoadedCount=" + mLoadedCount
Sudheer Shankac57729a2018-02-09 15:44:42 -08001525 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001526 }
1527
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001528 void addCountLocked(long count) {
Sudheer Shankac57729a2018-02-09 15:44:42 -08001529 addCountLocked(count, mTimeBase.isRunning());
1530 }
1531
1532 void addCountLocked(long count, boolean isRunning) {
1533 if (isRunning) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001534 mCount += count;
1535 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001536 }
1537
1538 /**
1539 * Clear state of this counter.
1540 */
1541 void reset(boolean detachIfReset) {
1542 mCount = 0;
Sudheer Shankac57729a2018-02-09 15:44:42 -08001543 mLoadedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001544 if (detachIfReset) {
1545 detach();
1546 }
1547 }
1548
1549 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001550 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001551 }
1552
1553 void writeSummaryFromParcelLocked(Parcel out) {
1554 out.writeLong(mCount);
1555 }
1556
1557 void readSummaryFromParcelLocked(Parcel in) {
1558 mLoadedCount = in.readLong();
1559 mCount = mLoadedCount;
Sudheer Shankac57729a2018-02-09 15:44:42 -08001560 mUnpluggedCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001561 }
1562 }
1563
Dianne Hackborn617f8772009-03-31 15:04:46 -07001564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * State for keeping track of timing information.
1566 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001567 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001568 protected final Clocks mClocks;
1569 protected final int mType;
1570 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001571
Joe Onoratoabded112016-02-08 16:49:39 -08001572 protected int mCount;
1573 protected int mLoadedCount;
1574 protected int mLastCount;
1575 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 // Times are in microseconds for better accuracy when dividing by the
1578 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 /**
1581 * The total time we have accumulated since the start of the original
1582 * boot, to the last time something interesting happened in the
1583 * current run.
1584 */
Joe Onoratoabded112016-02-08 16:49:39 -08001585 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /**
1588 * The total time we loaded for the previous runs. Subtract this from
1589 * mTotalTime to find the time for the current run of the system.
1590 */
Joe Onoratoabded112016-02-08 16:49:39 -08001591 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 /**
1594 * The run time of the last run of the system, as loaded from the
1595 * saved data.
1596 */
Joe Onoratoabded112016-02-08 16:49:39 -08001597 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /**
1600 * The value of mTotalTime when unplug() was last called. Subtract
1601 * this from mTotalTime to find the time since the last unplug from
1602 * power.
1603 */
Joe Onoratoabded112016-02-08 16:49:39 -08001604 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001605
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001606 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001607 * The total time this timer has been running until the latest mark has been set.
1608 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1609 */
Joe Onoratoabded112016-02-08 16:49:39 -08001610 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001611
1612 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001613 * Constructs from a parcel.
1614 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001615 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001616 * @param in
1617 */
Joe Onoratoabded112016-02-08 16:49:39 -08001618 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1619 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001621 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 mCount = in.readInt();
1624 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001625 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 mUnpluggedCount = in.readInt();
1627 mTotalTime = in.readLong();
1628 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001629 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001631 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001632 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001633 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
1635
Joe Onoratoabded112016-02-08 16:49:39 -08001636 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1637 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001639 mTimeBase = timeBase;
1640 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
Evan Millarc64edde2009-04-18 12:26:32 -07001642
1643 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001644
Evan Millarc64edde2009-04-18 12:26:32 -07001645 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001646
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001647 /**
1648 * Clear state of this timer. Returns true if the timer is inactive
1649 * so can be completely dropped.
1650 */
Joe Onoratoabded112016-02-08 16:49:39 -08001651 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001652 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001653 mCount = mLoadedCount = mLastCount = 0;
1654 if (detachIfReset) {
1655 detach();
1656 }
1657 return true;
1658 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001659
Joe Onoratoabded112016-02-08 16:49:39 -08001660 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001661 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001662 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001663
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001664 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001665 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1666 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001667 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001670 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001673 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675
Adam Lesinskie08af192015-03-25 16:42:59 -07001676 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001677 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001679 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 + " old mUnpluggedTime=" + mUnpluggedTime
1681 + " old mUnpluggedCount=" + mUnpluggedCount);
1682 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001683 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001684 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 if (DEBUG && mType < 0) {
1686 Log.v(TAG, "unplug #" + mType
1687 + ": new mUnpluggedTime=" + mUnpluggedTime
1688 + " new mUnpluggedCount=" + mUnpluggedCount);
1689 }
1690 }
1691
Adam Lesinskie08af192015-03-25 16:42:59 -07001692 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001693 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001694 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001695 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001696 + " old mTotalTime=" + mTotalTime);
1697 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001698 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001699 mCount = computeCurrentCountLocked();
1700 if (DEBUG && mType < 0) {
1701 Log.v(TAG, "plug #" + mType
1702 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
1704 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 /**
1707 * Writes a possibly null Timer to a Parcel.
1708 *
1709 * @param out the Parcel to be written to.
1710 * @param timer a Timer, or null.
1711 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001712 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 if (timer == null) {
1714 out.writeInt(0); // indicates null
1715 return;
1716 }
1717 out.writeInt(1); // indicates non-null
1718
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001719 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
1721
1722 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001723 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001724 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1725 if (which == STATS_SINCE_UNPLUGGED) {
1726 val -= mUnpluggedTime;
1727 } else if (which != STATS_SINCE_CHARGED) {
1728 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730
1731 return val;
1732 }
1733
1734 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001735 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001736 int val = computeCurrentCountLocked();
1737 if (which == STATS_SINCE_UNPLUGGED) {
1738 val -= mUnpluggedCount;
1739 } else if (which != STATS_SINCE_CHARGED) {
1740 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742
1743 return val;
1744 }
1745
Adam Lesinskie08af192015-03-25 16:42:59 -07001746 @Override
1747 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1748 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1749 return val - mTimeBeforeMark;
1750 }
1751
1752 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001753 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001754 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1756 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001757 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001759 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001761 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001762
1763
Joe Onoratoabded112016-02-08 16:49:39 -08001764 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001765 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1766 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001767 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001768 }
1769
Joe Onoratoabded112016-02-08 16:49:39 -08001770 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001771 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001772 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001773 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001774 mUnpluggedTime = mTotalTime;
1775 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001776 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001777 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001778
1779 // When reading the summary, we set the mark to be the latest information.
1780 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001781 }
1782 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001783
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001784 /**
1785 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1786 * method. The state of the timer according to its {@link TimeBase} will determine how much
1787 * of the value is recorded.
1788 *
1789 * If the value being recorded resets, {@link #endSample()} can be called in order to
1790 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1791 * between calls, the {@link #endSample()} is automatically called and the new value is
1792 * expected to increase monotonically from that point on.
1793 */
Joe Onoratoabded112016-02-08 16:49:39 -08001794 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001795
Evan Millarc64edde2009-04-18 12:26:32 -07001796 /**
1797 * The most recent reported count from /proc/wakelocks.
1798 */
1799 int mCurrentReportedCount;
1800
1801 /**
1802 * The reported count from /proc/wakelocks when unplug() was last
1803 * called.
1804 */
1805 int mUnpluggedReportedCount;
1806
1807 /**
1808 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001809 */
Evan Millarc64edde2009-04-18 12:26:32 -07001810 long mCurrentReportedTotalTime;
1811
1812
1813 /**
1814 * The reported total_time from /proc/wakelocks when unplug() was last
1815 * called.
1816 */
1817 long mUnpluggedReportedTotalTime;
1818
1819 /**
1820 * Whether we are currently in a discharge cycle.
1821 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001822 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001823
1824 /**
1825 * Whether we are currently recording reported values.
1826 */
1827 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001828
Evan Millarc64edde2009-04-18 12:26:32 -07001829 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001830 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001831 */
1832 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001833
Adam Lesinski98f0d462016-04-19 16:46:20 -07001834 @VisibleForTesting
1835 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001836 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001837 mCurrentReportedCount = in.readInt();
1838 mUnpluggedReportedCount = in.readInt();
1839 mCurrentReportedTotalTime = in.readLong();
1840 mUnpluggedReportedTotalTime = in.readLong();
1841 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001843 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001844
Adam Lesinski98f0d462016-04-19 16:46:20 -07001845 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001846 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001847 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001848 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001849 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001850 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001851
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001852 /**
1853 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1854 * be less than the values used for a previous invocation.
1855 */
1856 public void endSample() {
1857 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1858 mCount = computeCurrentCountLocked();
1859 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1860 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001861 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001862
Evan Millarc64edde2009-04-18 12:26:32 -07001863 public void setUpdateVersion(int version) {
1864 mUpdateVersion = version;
1865 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001866
Evan Millarc64edde2009-04-18 12:26:32 -07001867 public int getUpdateVersion() {
1868 return mUpdateVersion;
1869 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001870
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001871 /**
1872 * Updates the current recorded values. These are meant to be monotonically increasing
1873 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1874 *
1875 * If the values being recorded have been reset, the monotonically increasing requirement
1876 * will be broken. In this case, {@link #endSample()} is automatically called and
1877 * the total value of totalTime and count are recorded, starting a new monotonically
1878 * increasing sample.
1879 *
1880 * @param totalTime total time of sample in microseconds.
1881 * @param count total number of times the event being sampled occurred.
1882 */
1883 public void update(long totalTime, int count) {
1884 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001885 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001886 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001887 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001888 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001889
1890 mTrackingReportedValues = true;
1891
1892 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1893 endSample();
1894 }
1895
1896 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001897 mCurrentReportedCount = count;
1898 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001899
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001900 /**
1901 * Adds deltaTime and deltaCount to the current sample.
1902 *
1903 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1904 * @param deltaCount additional number of times the event being sampled occurred.
1905 */
1906 public void add(long deltaTime, int deltaCount) {
1907 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001908 }
1909
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001910 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001911 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1912 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001913 if (mTrackingReportedValues) {
1914 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1915 mUnpluggedReportedCount = mCurrentReportedCount;
1916 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001917 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001918 }
1919
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001920 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001921 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1922 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1923 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001924 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001925
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001926 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001927 public void logState(Printer pw, String prefix) {
1928 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001929 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001930 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1931 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1932 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1933 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001934
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001935 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001936 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001937 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001938 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1939 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001940
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001941 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001942 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001943 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001944 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1945 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001946
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001947 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001948 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1949 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001950 out.writeInt(mCurrentReportedCount);
1951 out.writeInt(mUnpluggedReportedCount);
1952 out.writeLong(mCurrentReportedTotalTime);
1953 out.writeLong(mUnpluggedReportedTotalTime);
1954 out.writeInt(mTrackingReportedValues ? 1 : 0);
1955 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001956
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001957 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001958 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001959 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001960 mTrackingReportedValues = false;
1961 mUnpluggedReportedTotalTime = 0;
1962 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001963 return true;
1964 }
Evan Millarc64edde2009-04-18 12:26:32 -07001965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001966
Evan Millarc64edde2009-04-18 12:26:32 -07001967 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001968 * A timer that increments in batches. It does not run for durations, but just jumps
1969 * for a pre-determined amount.
1970 */
Joe Onoratoabded112016-02-08 16:49:39 -08001971 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001972 final Uid mUid;
1973
1974 /**
1975 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1976 */
1977 long mLastAddedTime;
1978
1979 /**
1980 * The last duration that we added to the timer. This is in microseconds.
1981 */
1982 long mLastAddedDuration;
1983
1984 /**
1985 * Whether we are currently in a discharge cycle.
1986 */
1987 boolean mInDischarge;
1988
Joe Onoratoabded112016-02-08 16:49:39 -08001989 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1990 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001991 mUid = uid;
1992 mLastAddedTime = in.readLong();
1993 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001994 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001995 }
1996
Joe Onoratoabded112016-02-08 16:49:39 -08001997 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1998 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001999 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002000 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002001 }
2002
2003 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002004 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2005 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002006 out.writeLong(mLastAddedTime);
2007 out.writeLong(mLastAddedDuration);
2008 }
2009
2010 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002011 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08002012 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002013 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002014 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002015 }
2016
2017 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002018 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002019 recomputeLastDuration(elapsedRealtime, false);
2020 mInDischarge = true;
2021 // If we are still within the last added duration, then re-added whatever remains.
2022 if (mLastAddedTime == elapsedRealtime) {
2023 mTotalTime += mLastAddedDuration;
2024 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002025 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002026 }
2027
2028 @Override
2029 public void logState(Printer pw, String prefix) {
2030 super.logState(pw, prefix);
2031 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
2032 + " mLastAddedDuration=" + mLastAddedDuration);
2033 }
2034
2035 private long computeOverage(long curTime) {
2036 if (mLastAddedTime > 0) {
2037 return mLastTime + mLastAddedDuration - curTime;
2038 }
2039 return 0;
2040 }
2041
2042 private void recomputeLastDuration(long curTime, boolean abort) {
2043 final long overage = computeOverage(curTime);
2044 if (overage > 0) {
2045 // Aborting before the duration ran out -- roll back the remaining
2046 // duration. Only do this if currently discharging; otherwise we didn't
2047 // actually add the time.
2048 if (mInDischarge) {
2049 mTotalTime -= overage;
2050 }
2051 if (abort) {
2052 mLastAddedTime = 0;
2053 } else {
2054 mLastAddedTime = curTime;
2055 mLastAddedDuration -= overage;
2056 }
2057 }
2058 }
2059
2060 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08002061 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002062 recomputeLastDuration(now, true);
2063 mLastAddedTime = now;
2064 mLastAddedDuration = durationMillis * 1000;
2065 if (mInDischarge) {
2066 mTotalTime += mLastAddedDuration;
2067 mCount++;
2068 }
2069 }
2070
2071 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08002072 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002073 recomputeLastDuration(now, true);
2074 }
2075
2076 @Override
2077 protected int computeCurrentCountLocked() {
2078 return mCount;
2079 }
2080
2081 @Override
2082 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08002083 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002084 if (overage > 0) {
2085 return mTotalTime = overage;
2086 }
2087 return mTotalTime;
2088 }
2089
2090 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002091 public boolean reset(boolean detachIfReset) {
2092 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002093 recomputeLastDuration(now, true);
2094 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002095 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002096 return !stillActive;
2097 }
2098 }
2099
Joe Onorato92fd23f2016-07-25 11:18:42 -07002100
2101 /**
2102 * A StopwatchTimer that also tracks the total and max individual
2103 * time spent active according to the given timebase. Whereas
2104 * StopwatchTimer apportions the time amongst all in the pool,
2105 * the total and max durations are not apportioned.
2106 */
2107 public static class DurationTimer extends StopwatchTimer {
2108 /**
2109 * The time (in ms) that the timer was last acquired or the time base
2110 * last (re-)started. Increasing the nesting depth does not reset this time.
2111 *
2112 * -1 if the timer is currently not running or the time base is not running.
2113 *
2114 * If written to a parcel, the start time is reset, as is mNesting in the base class
2115 * StopwatchTimer.
2116 */
2117 long mStartTimeMs = -1;
2118
2119 /**
Bookatz867c0d72017-03-07 18:23:42 -08002120 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002121 */
2122 long mMaxDurationMs;
2123
2124 /**
Bookatz867c0d72017-03-07 18:23:42 -08002125 * The time (in ms) that that the timer has been active since most recent
2126 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002127 */
2128 long mCurrentDurationMs;
2129
Bookatz867c0d72017-03-07 18:23:42 -08002130 /**
2131 * The total time (in ms) that that the timer has been active since most recent reset()
2132 * prior to the current startRunningLocked. This is the sum of all past currentDurations
2133 * (but not including the present currentDuration) since reset. Not pooled.
2134 */
2135 long mTotalDurationMs;
2136
Joe Onorato92fd23f2016-07-25 11:18:42 -07002137 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2138 TimeBase timeBase, Parcel in) {
2139 super(clocks, uid, type, timerPool, timeBase, in);
2140 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08002141 mTotalDurationMs = in.readLong();
jackqdyulei610a0a02017-08-09 14:02:01 -07002142 mCurrentDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07002143 }
2144
2145 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2146 TimeBase timeBase) {
2147 super(clocks, uid, type, timerPool, timeBase);
2148 }
2149
2150 @Override
2151 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2152 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08002153 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
jackqdyulei610a0a02017-08-09 14:02:01 -07002154 out.writeLong(mTotalDurationMs);
2155 out.writeLong(getCurrentDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07002156 }
2157
2158 /**
2159 * Write the summary to the parcel.
2160 *
2161 * Since the time base is probably meaningless after we come back, reading
2162 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08002163 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002164 */
2165 @Override
2166 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
2167 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08002168 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08002169 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07002170 }
2171
2172 /**
2173 * Read the summary parcel.
2174 *
2175 * Has the side effect of stopping the timer.
2176 */
2177 @Override
2178 public void readSummaryFromParcelLocked(Parcel in) {
2179 super.readSummaryFromParcelLocked(in);
2180 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08002181 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07002182 mStartTimeMs = -1;
2183 mCurrentDurationMs = 0;
2184 }
2185
2186 /**
2187 * The TimeBase time started (again).
2188 *
2189 * If the timer is also running, store the start time.
2190 */
2191 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
2192 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
2193 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08002194 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002195 }
2196 }
2197
2198 /**
2199 * The TimeBase stopped running.
2200 *
2201 * If the timer is running, add the duration into mCurrentDurationMs.
2202 */
2203 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08002204 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
2205 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002206 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08002207 // baseRealtimeUs has already been converted to the timebase's realtime.
2208 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002209 }
2210 mStartTimeMs = -1;
2211 }
2212
2213 @Override
2214 public void logState(Printer pw, String prefix) {
2215 super.logState(pw, prefix);
2216 }
2217
2218 @Override
2219 public void startRunningLocked(long elapsedRealtimeMs) {
2220 super.startRunningLocked(elapsedRealtimeMs);
2221 if (mNesting == 1 && mTimeBase.isRunning()) {
2222 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08002223 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002224 }
2225 }
2226
2227 /**
2228 * Decrements the mNesting ref-count on this timer.
2229 *
2230 * If it actually stopped (mNesting went to 0), then possibly update
2231 * mMaxDuration if the current duration was the longest ever.
2232 */
2233 @Override
2234 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08002235 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07002236 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002237 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002238 if (durationMs > mMaxDurationMs) {
2239 mMaxDurationMs = durationMs;
2240 }
2241 mStartTimeMs = -1;
2242 mCurrentDurationMs = 0;
2243 }
Kweku Adams47db5a82016-12-09 19:04:50 -08002244 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
2245 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
2246 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002247 }
2248
2249 @Override
2250 public boolean reset(boolean detachIfReset) {
2251 boolean result = super.reset(detachIfReset);
2252 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08002253 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002254 mCurrentDurationMs = 0;
2255 if (mNesting > 0) {
2256 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
2257 } else {
2258 mStartTimeMs = -1;
2259 }
2260 return result;
2261 }
2262
2263 /**
2264 * Returns the max duration that this timer has ever seen.
2265 *
2266 * Note that this time is NOT split between the timers in the timer group that
2267 * this timer is attached to. It is the TOTAL time.
2268 */
2269 @Override
2270 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
2271 if (mNesting > 0) {
2272 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
2273 if (durationMs > mMaxDurationMs) {
2274 return durationMs;
2275 }
2276 }
2277 return mMaxDurationMs;
2278 }
2279
2280 /**
2281 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08002282 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002283 *
2284 * Note that this time is NOT split between the timers in the timer group that
2285 * this timer is attached to. It is the TOTAL time.
jackqdyulei610a0a02017-08-09 14:02:01 -07002286 *
2287 * Note that if running timer is parceled and unparceled, this method will return
2288 * current duration value at the time of parceling even though timer may not be
2289 * currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002290 */
2291 @Override
2292 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
2293 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08002294 if (mNesting > 0 && mTimeBase.isRunning()) {
2295 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
2296 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002297 }
2298 return durationMs;
2299 }
Bookatz867c0d72017-03-07 18:23:42 -08002300
2301 /**
2302 * Returns the total cumulative duration that this timer has been on since reset().
2303 * If mTimerPool == null, this should be the same
2304 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
2305 *
2306 * Note that this time is NOT split between the timers in the timer group that
2307 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
2308 * the result will not be equivalent to getTotalTimeLocked.
2309 */
2310 @Override
2311 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
2312 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
2313 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002314 }
2315
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002316 /**
Evan Millarc64edde2009-04-18 12:26:32 -07002317 * State for keeping track of timing information.
2318 */
Joe Onoratoabded112016-02-08 16:49:39 -08002319 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002320 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07002321 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002322
Evan Millarc64edde2009-04-18 12:26:32 -07002323 int mNesting;
2324
Evan Millarc64edde2009-04-18 12:26:32 -07002325 /**
2326 * The last time at which we updated the timer. If mNesting is > 0,
2327 * subtract this from the current battery time to find the amount of
2328 * time we have been running since we last computed an update.
2329 */
2330 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002331
Evan Millarc64edde2009-04-18 12:26:32 -07002332 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002333 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07002334 * was actually held for an interesting duration. If time base was not running when timer
2335 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07002336 */
Bookatzceebafe2017-04-06 11:59:13 -07002337 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07002338
Amith Yamasanif37447b2009-10-08 18:28:01 -07002339 long mTimeout;
2340
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002341 /**
2342 * For partial wake locks, keep track of whether we are in the list
2343 * to consume CPU cycles.
2344 */
Sudheer Shanka38383232017-07-25 09:55:03 -07002345 @VisibleForTesting
2346 public boolean mInList;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002347
Joe Onoratoabded112016-02-08 16:49:39 -08002348 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002349 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08002350 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002351 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002352 mTimerPool = timerPool;
2353 mUpdateTime = in.readLong();
2354 }
2355
Joe Onoratoabded112016-02-08 16:49:39 -08002356 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002357 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002358 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002359 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002360 mTimerPool = timerPool;
2361 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002362
Joe Onoratoabded112016-02-08 16:49:39 -08002363 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002364 mTimeout = timeout;
2365 }
2366
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002367 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2368 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002369 out.writeLong(mUpdateTime);
2370 }
2371
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002372 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002373 if (mNesting > 0) {
2374 if (DEBUG && mType < 0) {
2375 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2376 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002377 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2378 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002379 if (DEBUG && mType < 0) {
2380 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2381 }
2382 }
2383 }
2384
2385 public void logState(Printer pw, String prefix) {
2386 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002387 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 + " mAcquireTime=" + mAcquireTime);
2389 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002390
Joe Onoratoabded112016-02-08 16:49:39 -08002391 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002393 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002394 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 if (mTimerPool != null) {
2396 // Accumulate time to all currently active timers before adding
2397 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002398 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 // Add this timer to the active pool
2400 mTimerPool.add(this);
2401 }
Bookatzceebafe2017-04-06 11:59:13 -07002402 if (mTimeBase.isRunning()) {
2403 // Increment the count
2404 mCount++;
2405 mAcquireTime = mTotalTime;
2406 } else {
2407 mAcquireTime = -1;
2408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 if (DEBUG && mType < 0) {
2410 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2411 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2412 + " mAcquireTime=" + mAcquireTime);
2413 }
2414 }
2415 }
2416
Joe Onoratoabded112016-02-08 16:49:39 -08002417 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002418 return mNesting > 0;
2419 }
2420
Joe Onoratoabded112016-02-08 16:49:39 -08002421 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 // Ignore attempt to stop a timer that isn't running
2423 if (mNesting == 0) {
2424 return;
2425 }
2426 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002427 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 if (mTimerPool != null) {
2429 // Accumulate time to all active counters, scaled by the total
2430 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002431 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 // Remove this timer from the active pool
2433 mTimerPool.remove(this);
2434 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 mNesting = 1;
2436 mTotalTime = computeRunTimeLocked(batteryRealtime);
2437 mNesting = 0;
2438 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 if (DEBUG && mType < 0) {
2441 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2442 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2443 + " mAcquireTime=" + mAcquireTime);
2444 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002445
Bookatzceebafe2017-04-06 11:59:13 -07002446 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 // If there was no change in the time, then discard this
2448 // count. A somewhat cheezy strategy, but hey.
2449 mCount--;
2450 }
2451 }
2452 }
2453
Joe Onoratoabded112016-02-08 16:49:39 -08002454 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002455 if (mNesting > 0) {
2456 mNesting = 1;
2457 stopRunningLocked(elapsedRealtimeMs);
2458 }
2459 }
2460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 // Update the total time for all other running Timers with the same type as this Timer
2462 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002463 private static long refreshTimersLocked(long batteryRealtime,
2464 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002465 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 final int N = pool.size();
2467 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002468 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 long heldTime = batteryRealtime - t.mUpdateTime;
2470 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002471 final long myTime = heldTime / N;
2472 if (t == self) {
2473 selfTime = myTime;
2474 }
2475 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477 t.mUpdateTime = batteryRealtime;
2478 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002479 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
2481
Evan Millarc64edde2009-04-18 12:26:32 -07002482 @Override
2483 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002484 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2485 curBatteryRealtime = mUpdateTime + mTimeout;
2486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 return mTotalTime + (mNesting > 0
2488 ? (curBatteryRealtime - mUpdateTime)
2489 / (mTimerPool != null ? mTimerPool.size() : 1)
2490 : 0);
2491 }
2492
Evan Millarc64edde2009-04-18 12:26:32 -07002493 @Override
2494 protected int computeCurrentCountLocked() {
2495 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
2497
Adam Lesinskie08af192015-03-25 16:42:59 -07002498 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002499 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002500 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002501 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002502 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002503 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002504 }
Bookatzceebafe2017-04-06 11:59:13 -07002505 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002506 return canDetach;
2507 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002508
Adam Lesinskie08af192015-03-25 16:42:59 -07002509 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002510 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002511 super.detach();
2512 if (mTimerPool != null) {
2513 mTimerPool.remove(this);
2514 }
2515 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002516
Adam Lesinskie08af192015-03-25 16:42:59 -07002517 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002518 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002519 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 mNesting = 0;
2521 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002522
2523 /**
2524 * Set the mark so that we can query later for the total time the timer has
2525 * accumulated since this point. The timer can be running or not.
2526 *
2527 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2528 */
2529 public void setMark(long elapsedRealtimeMs) {
2530 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2531 if (mNesting > 0) {
2532 // We are running.
2533 if (mTimerPool != null) {
2534 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2535 } else {
2536 mTotalTime += batteryRealtime - mUpdateTime;
2537 mUpdateTime = batteryRealtime;
2538 }
2539 }
2540 mTimeBeforeMark = mTotalTime;
2541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002543
Bookatz867c0d72017-03-07 18:23:42 -08002544 /**
2545 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2546 * TimeBase is effectively a subset of the other.
2547 */
Bookatzaa4594a2017-03-24 12:39:56 -07002548 public static class DualTimer extends DurationTimer {
2549 // This class both is a DurationTimer and also holds a second DurationTimer.
2550 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2551 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2552 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002553 // mSubTimer typically tracks only part of the total time, such as background time, as
2554 // determined by a subTimeBase. It is NOT pooled.
2555 private final DurationTimer mSubTimer;
2556
2557 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002558 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2559 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002560 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002561 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002562 */
2563 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2564 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002565 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002566 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2567 }
2568
2569 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002570 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2571 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002572 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002573 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002574 */
2575 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2576 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002577 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002578 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2579 }
2580
Bookatz867c0d72017-03-07 18:23:42 -08002581 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002582 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002583 public DurationTimer getSubTimer() {
2584 return mSubTimer;
2585 }
2586
Bookatzaa4594a2017-03-24 12:39:56 -07002587 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002588 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002589 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002590 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2591 }
2592
Bookatzaa4594a2017-03-24 12:39:56 -07002593 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002594 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002595 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002596 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2597 }
2598
Bookatzaa4594a2017-03-24 12:39:56 -07002599 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002600 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002601 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002602 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2603 }
2604
Bookatzaa4594a2017-03-24 12:39:56 -07002605 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002606 public boolean reset(boolean detachIfReset) {
2607 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002608 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2609 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002610 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002611 return !active;
2612 }
2613
Bookatzaa4594a2017-03-24 12:39:56 -07002614 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002615 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002616 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002617 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002618 }
2619
Bookatzaa4594a2017-03-24 12:39:56 -07002620 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002621 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002622 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002623 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2624 }
2625
Bookatzaa4594a2017-03-24 12:39:56 -07002626 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002627 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002628 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002629 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2630 }
2631
Bookatzaa4594a2017-03-24 12:39:56 -07002632 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002633 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002634 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002635 mSubTimer.readSummaryFromParcelLocked(in);
2636 }
2637 }
2638
2639
Dianne Hackbornd953c532014-08-16 18:17:38 -07002640 public abstract class OverflowArrayMap<T> {
2641 private static final String OVERFLOW_NAME = "*overflow*";
2642
Dianne Hackborn657153b2016-07-29 14:54:14 -07002643 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002644 final ArrayMap<String, T> mMap = new ArrayMap<>();
2645 T mCurOverflow;
2646 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002647 long mLastOverflowTime;
2648 long mLastOverflowFinishTime;
2649 long mLastClearTime;
2650 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002651
Dianne Hackborn657153b2016-07-29 14:54:14 -07002652 public OverflowArrayMap(int uid) {
2653 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002654 }
2655
2656 public ArrayMap<String, T> getMap() {
2657 return mMap;
2658 }
2659
2660 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002661 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002662 mMap.clear();
2663 mCurOverflow = null;
2664 mActiveOverflow = null;
2665 }
2666
2667 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002668 if (name == null) {
2669 name = "";
2670 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002671 mMap.put(name, obj);
2672 if (OVERFLOW_NAME.equals(name)) {
2673 mCurOverflow = obj;
2674 }
2675 }
2676
2677 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002678 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002679 if (mActiveOverflow != null) {
2680 if (mActiveOverflow.size() == 0) {
2681 mActiveOverflow = null;
2682 }
2683 }
2684 if (mActiveOverflow == null) {
2685 // There is no currently active overflow, so we should no longer have
2686 // an overflow entry.
2687 if (mMap.containsKey(OVERFLOW_NAME)) {
2688 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2689 + mMap.get(OVERFLOW_NAME));
2690 mMap.remove(OVERFLOW_NAME);
2691 }
2692 mCurOverflow = null;
2693 } else {
2694 // There is currently active overflow, so we should still have an overflow entry.
2695 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2696 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2697 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2698 }
2699 }
2700 }
2701
2702 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002703 if (name == null) {
2704 name = "";
2705 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002706 T obj = mMap.get(name);
2707 if (obj != null) {
2708 return obj;
2709 }
2710
2711 // No object exists for the given name, but do we currently have it
2712 // running as part of the overflow?
2713 if (mActiveOverflow != null) {
2714 MutableInt over = mActiveOverflow.get(name);
2715 if (over != null) {
2716 // We are already actively counting this name in the overflow object.
2717 obj = mCurOverflow;
2718 if (obj == null) {
2719 // Shouldn't be here, but we'll try to recover.
2720 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2721 obj = mCurOverflow = instantiateObject();
2722 mMap.put(OVERFLOW_NAME, obj);
2723 }
2724 over.value++;
2725 return obj;
2726 }
2727 }
2728
2729 // No object exists for given name nor in the overflow; we need to make
2730 // a new one.
2731 final int N = mMap.size();
2732 if (N >= MAX_WAKELOCKS_PER_UID) {
2733 // Went over the limit on number of objects to track; this one goes
2734 // in to the overflow.
2735 obj = mCurOverflow;
2736 if (obj == null) {
2737 // Need to start overflow now...
2738 obj = mCurOverflow = instantiateObject();
2739 mMap.put(OVERFLOW_NAME, obj);
2740 }
2741 if (mActiveOverflow == null) {
2742 mActiveOverflow = new ArrayMap<>();
2743 }
2744 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002745 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002746 return obj;
2747 }
2748
2749 // Normal case where we just need to make a new object.
2750 obj = instantiateObject();
2751 mMap.put(name, obj);
2752 return obj;
2753 }
2754
2755 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002756 if (name == null) {
2757 name = "";
2758 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002759 T obj = mMap.get(name);
2760 if (obj != null) {
2761 return obj;
2762 }
2763
2764 // No object exists for the given name, but do we currently have it
2765 // running as part of the overflow?
2766 if (mActiveOverflow != null) {
2767 MutableInt over = mActiveOverflow.get(name);
2768 if (over != null) {
2769 // We are already actively counting this name in the overflow object.
2770 obj = mCurOverflow;
2771 if (obj != null) {
2772 over.value--;
2773 if (over.value <= 0) {
2774 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002775 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002776 }
2777 return obj;
2778 }
2779 }
2780 }
2781
2782 // Huh, they are stopping an active operation but we can't find one!
2783 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002784 StringBuilder sb = new StringBuilder();
2785 sb.append("Unable to find object for ");
2786 sb.append(name);
2787 sb.append(" in uid ");
2788 sb.append(mUid);
2789 sb.append(" mapsize=");
2790 sb.append(mMap.size());
2791 sb.append(" activeoverflow=");
2792 sb.append(mActiveOverflow);
2793 sb.append(" curoverflow=");
2794 sb.append(mCurOverflow);
2795 long now = SystemClock.elapsedRealtime();
2796 if (mLastOverflowTime != 0) {
2797 sb.append(" lastOverflowTime=");
2798 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2799 }
2800 if (mLastOverflowFinishTime != 0) {
2801 sb.append(" lastOverflowFinishTime=");
2802 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2803 }
2804 if (mLastClearTime != 0) {
2805 sb.append(" lastClearTime=");
2806 TimeUtils.formatDuration(mLastClearTime-now, sb);
2807 }
2808 if (mLastCleanupTime != 0) {
2809 sb.append(" lastCleanupTime=");
2810 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2811 }
2812 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002813 return null;
2814 }
2815
2816 public abstract T instantiateObject();
2817 }
2818
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002819 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2820 implements Parcelable {
2821 private final LongSamplingCounter mIdleTimeMillis;
Siddharth Rayb50a6842017-12-14 15:15:28 -08002822 private final LongSamplingCounter mScanTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002823 private final LongSamplingCounter mRxTimeMillis;
2824 private final LongSamplingCounter[] mTxTimeMillis;
2825 private final LongSamplingCounter mPowerDrainMaMs;
2826
2827 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2828 mIdleTimeMillis = new LongSamplingCounter(timeBase);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002829 mScanTimeMillis = new LongSamplingCounter(timeBase);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002830 mRxTimeMillis = new LongSamplingCounter(timeBase);
2831 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2832 for (int i = 0; i < numTxStates; i++) {
2833 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2834 }
2835 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2836 }
2837
2838 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2839 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002840 mScanTimeMillis = new LongSamplingCounter(timeBase, in);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002841 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2842 final int recordedTxStates = in.readInt();
2843 if (recordedTxStates != numTxStates) {
2844 throw new ParcelFormatException("inconsistent tx state lengths");
2845 }
2846
2847 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2848 for (int i = 0; i < numTxStates; i++) {
2849 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2850 }
2851 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2852 }
2853
2854 public void readSummaryFromParcel(Parcel in) {
2855 mIdleTimeMillis.readSummaryFromParcelLocked(in);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002856 mScanTimeMillis.readSummaryFromParcelLocked(in);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002857 mRxTimeMillis.readSummaryFromParcelLocked(in);
2858 final int recordedTxStates = in.readInt();
2859 if (recordedTxStates != mTxTimeMillis.length) {
2860 throw new ParcelFormatException("inconsistent tx state lengths");
2861 }
2862 for (LongSamplingCounter counter : mTxTimeMillis) {
2863 counter.readSummaryFromParcelLocked(in);
2864 }
2865 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2866 }
2867
2868 @Override
2869 public int describeContents() {
2870 return 0;
2871 }
2872
2873 public void writeSummaryToParcel(Parcel dest) {
2874 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002875 mScanTimeMillis.writeSummaryFromParcelLocked(dest);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002876 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2877 dest.writeInt(mTxTimeMillis.length);
2878 for (LongSamplingCounter counter : mTxTimeMillis) {
2879 counter.writeSummaryFromParcelLocked(dest);
2880 }
2881 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2882 }
2883
2884 @Override
2885 public void writeToParcel(Parcel dest, int flags) {
2886 mIdleTimeMillis.writeToParcel(dest);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002887 mScanTimeMillis.writeToParcel(dest);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002888 mRxTimeMillis.writeToParcel(dest);
2889 dest.writeInt(mTxTimeMillis.length);
2890 for (LongSamplingCounter counter : mTxTimeMillis) {
2891 counter.writeToParcel(dest);
2892 }
2893 mPowerDrainMaMs.writeToParcel(dest);
2894 }
2895
2896 public void reset(boolean detachIfReset) {
2897 mIdleTimeMillis.reset(detachIfReset);
Siddharth Rayb50a6842017-12-14 15:15:28 -08002898 mScanTimeMillis.reset(detachIfReset);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002899 mRxTimeMillis.reset(detachIfReset);
2900 for (LongSamplingCounter counter : mTxTimeMillis) {
2901 counter.reset(detachIfReset);
2902 }
2903 mPowerDrainMaMs.reset(detachIfReset);
2904 }
2905
2906 public void detach() {
2907 mIdleTimeMillis.detach();
Siddharth Rayb50a6842017-12-14 15:15:28 -08002908 mScanTimeMillis.detach();
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002909 mRxTimeMillis.detach();
2910 for (LongSamplingCounter counter : mTxTimeMillis) {
2911 counter.detach();
2912 }
2913 mPowerDrainMaMs.detach();
2914 }
2915
2916 /**
2917 * @return a LongSamplingCounter, measuring time spent in the idle state in
2918 * milliseconds.
2919 */
2920 @Override
2921 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002922 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002923 }
2924
2925 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002926 * @return a LongSamplingCounter, measuring time spent in the scan state in
2927 * milliseconds.
2928 */
2929 @Override
2930 public LongSamplingCounter getScanTimeCounter() {
2931 return mScanTimeMillis;
2932 }
2933
2934 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002935 * @return a LongSamplingCounter, measuring time spent in the receive state in
2936 * milliseconds.
2937 */
2938 @Override
2939 public LongSamplingCounter getRxTimeCounter() {
2940 return mRxTimeMillis;
2941 }
2942
2943 /**
2944 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2945 * milliseconds.
2946 */
2947 @Override
2948 public LongSamplingCounter[] getTxTimeCounters() {
2949 return mTxTimeMillis;
2950 }
2951
2952 /**
2953 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2954 */
2955 @Override
2956 public LongSamplingCounter getPowerCounter() {
2957 return mPowerDrainMaMs;
2958 }
2959 }
2960
Bookatz50df7112017-08-04 14:53:26 -07002961 /** Get Resource Power Manager stats. Create a new one if it doesn't already exist. */
2962 public SamplingTimer getRpmTimerLocked(String name) {
2963 SamplingTimer rpmt = mRpmStats.get(name);
2964 if (rpmt == null) {
2965 rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2966 mRpmStats.put(name, rpmt);
2967 }
2968 return rpmt;
2969 }
2970
2971 /** Get Screen-off Resource Power Manager stats. Create new one if it doesn't already exist. */
2972 public SamplingTimer getScreenOffRpmTimerLocked(String name) {
2973 SamplingTimer rpmt = mScreenOffRpmStats.get(name);
2974 if (rpmt == null) {
2975 rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
2976 mScreenOffRpmStats.put(name, rpmt);
2977 }
2978 return rpmt;
2979 }
2980
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002981 /*
2982 * Get the wakeup reason counter, and create a new one if one
2983 * doesn't already exist.
2984 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002985 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2986 SamplingTimer timer = mWakeupReasonStats.get(name);
2987 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002988 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002989 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002990 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002991 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002992 }
2993
Evan Millarc64edde2009-04-18 12:26:32 -07002994 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002995 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002996 * doesn't already exist.
2997 */
2998 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2999 SamplingTimer kwlt = mKernelWakelockStats.get(name);
3000 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003001 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07003002 mKernelWakelockStats.put(name, kwlt);
3003 }
3004 return kwlt;
3005 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003006
James Carr3a226052016-07-01 14:49:52 -07003007 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
3008 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
3009 if (kmt == null) {
3010 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
3011 mKernelMemoryStats.put(bucket, kmt);
3012 }
3013 return kmt;
3014 }
3015
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003016 private int writeHistoryTag(HistoryTag tag) {
3017 Integer idxObj = mHistoryTagPool.get(tag);
3018 int idx;
3019 if (idxObj != null) {
3020 idx = idxObj;
3021 } else {
3022 idx = mNextHistoryTagIdx;
3023 HistoryTag key = new HistoryTag();
3024 key.setTo(tag);
3025 tag.poolIdx = idx;
3026 mHistoryTagPool.put(key, idx);
3027 mNextHistoryTagIdx++;
3028 mNumHistoryTagChars += key.string.length() + 1;
3029 }
3030 return idx;
3031 }
3032
3033 private void readHistoryTag(int index, HistoryTag tag) {
3034 tag.string = mReadHistoryStrings[index];
3035 tag.uid = mReadHistoryUids[index];
3036 tag.poolIdx = index;
3037 }
3038
Adam Lesinski926969b2016-04-28 17:31:12 -07003039 /*
3040 The history delta format uses flags to denote further data in subsequent ints in the parcel.
3041
3042 There is always the first token, which may contain the delta time, or an indicator of
3043 the length of the time (int or long) following this token.
3044
3045 First token: always present,
3046 31 23 15 7 0
3047 â–ˆM|L|K|J|I|H|G|Fâ–ˆE|D|C|B|A|T|T|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆ
3048
3049 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
3050 follows containing the time, and 0x7ffff indicates a long immediately follows with the
3051 delta time.
3052 A: battery level changed and an int follows with battery data.
3053 B: state changed and an int follows with state change data.
3054 C: state2 has changed and an int follows with state2 change data.
3055 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
3056 E: event data has changed and an event struct follows.
3057 F: battery charge in coulombs has changed and an int with the charge follows.
3058 G: state flag denoting that the mobile radio was active.
3059 H: state flag denoting that the wifi radio was active.
3060 I: state flag denoting that a wifi scan occurred.
3061 J: state flag denoting that a wifi full lock was held.
3062 K: state flag denoting that the gps was on.
3063 L: state flag denoting that a wakelock was held.
3064 M: state flag denoting that the cpu was running.
3065
3066 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
3067 with the time delta.
3068
3069 Battery level int: if A in the first token is set,
3070 31 23 15 7 0
3071 â–ˆL|L|L|L|L|L|L|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆT|V|V|V|V|V|V|Vâ–ˆV|V|V|V|V|V|V|Dâ–ˆ
3072
3073 D: indicates that extra history details follow.
3074 V: the battery voltage.
3075 T: the battery temperature.
3076 L: the battery level (out of 100).
3077
3078 State change int: if B in the first token is set,
3079 31 23 15 7 0
3080 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
3081
3082 A: wifi multicast was on.
3083 B: battery was plugged in.
3084 C: screen was on.
3085 D: phone was scanning for signal.
3086 E: audio was on.
3087 F: a sensor was active.
3088
3089 State2 change int: if C in the first token is set,
3090 31 23 15 7 0
3091 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
3092
3093 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
3094 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
3095 C: a bluetooth scan was active.
3096 D: the camera was active.
3097 E: bluetooth was on.
3098 F: a phone call was active.
3099 G: the device was charging.
3100 H: 2 bits indicating the device-idle (doze) state: off, light, full
3101 I: the flashlight was on.
3102 J: wifi was on.
3103 K: wifi was running.
3104 L: video was playing.
3105 M: power save mode was on.
3106
3107 Wakelock/wakereason struct: if D in the first token is set,
3108 TODO(adamlesinski): describe wakelock/wakereason struct.
3109
3110 Event struct: if E in the first token is set,
3111 TODO(adamlesinski): describe the event struct.
3112
3113 History step details struct: if D in the battery level int is set,
3114 TODO(adamlesinski): describe the history step details struct.
3115
3116 Battery charge int: if F in the first token is set, an int representing the battery charge
3117 in coulombs follows.
3118 */
3119
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003120 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003121 static final int DELTA_TIME_MASK = 0x7ffff;
3122 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
3123 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
3124 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003125 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07003126 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003127 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07003128 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003129 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07003130 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003131 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07003132 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003133 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07003134 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003135 // Flag in delta int: contains the battery charge count in uAh.
3136 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003137 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07003138 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003139
3140 // These are the pieces of battery state that are packed in to the upper bits of
3141 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07003142 // in STATE_BATTERY_MASK.
3143 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003144 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
3145 static final int STATE_BATTERY_STATUS_SHIFT = 29;
3146 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
3147 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
3148 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
3149 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003150
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003151 // We use the low bit of the battery state int to indicate that we have full details
3152 // from a battery level change.
3153 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
3154
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003155 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003156 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003157 dest.writeInt(DELTA_TIME_ABS);
3158 cur.writeToParcel(dest, 0);
3159 return;
3160 }
3161
3162 final long deltaTime = cur.time - last.time;
3163 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
3164 final int lastStateInt = buildStateInt(last);
3165
3166 int deltaTimeToken;
3167 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
3168 deltaTimeToken = DELTA_TIME_LONG;
3169 } else if (deltaTime >= DELTA_TIME_ABS) {
3170 deltaTimeToken = DELTA_TIME_INT;
3171 } else {
3172 deltaTimeToken = (int)deltaTime;
3173 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003174 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003175 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
3176 ? BATTERY_DELTA_LEVEL_FLAG : 0;
3177 final boolean computeStepDetails = includeStepDetails != 0
3178 || mLastHistoryStepDetails == null;
3179 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003180 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
3181 if (batteryLevelIntChanged) {
3182 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
3183 }
3184 final int stateInt = buildStateInt(cur);
3185 final boolean stateIntChanged = stateInt != lastStateInt;
3186 if (stateIntChanged) {
3187 firstToken |= DELTA_STATE_FLAG;
3188 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003189 final boolean state2IntChanged = cur.states2 != last.states2;
3190 if (state2IntChanged) {
3191 firstToken |= DELTA_STATE2_FLAG;
3192 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003193 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003194 firstToken |= DELTA_WAKELOCK_FLAG;
3195 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003196 if (cur.eventCode != HistoryItem.EVENT_NONE) {
3197 firstToken |= DELTA_EVENT_FLAG;
3198 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003199
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003200 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
3201 if (batteryChargeChanged) {
3202 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07003203 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003204 dest.writeInt(firstToken);
3205 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3206 + " deltaTime=" + deltaTime);
3207
3208 if (deltaTimeToken >= DELTA_TIME_INT) {
3209 if (deltaTimeToken == DELTA_TIME_INT) {
3210 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
3211 dest.writeInt((int)deltaTime);
3212 } else {
3213 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
3214 dest.writeLong(deltaTime);
3215 }
3216 }
3217 if (batteryLevelIntChanged) {
3218 dest.writeInt(batteryLevelInt);
3219 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
3220 + Integer.toHexString(batteryLevelInt)
3221 + " batteryLevel=" + cur.batteryLevel
3222 + " batteryTemp=" + cur.batteryTemperature
3223 + " batteryVolt=" + (int)cur.batteryVoltage);
3224 }
3225 if (stateIntChanged) {
3226 dest.writeInt(stateInt);
3227 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
3228 + Integer.toHexString(stateInt)
3229 + " batteryStatus=" + cur.batteryStatus
3230 + " batteryHealth=" + cur.batteryHealth
3231 + " batteryPlugType=" + cur.batteryPlugType
3232 + " states=0x" + Integer.toHexString(cur.states));
3233 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003234 if (state2IntChanged) {
3235 dest.writeInt(cur.states2);
3236 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
3237 + Integer.toHexString(cur.states2));
3238 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003239 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
3240 int wakeLockIndex;
3241 int wakeReasonIndex;
3242 if (cur.wakelockTag != null) {
3243 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
3244 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3245 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3246 } else {
3247 wakeLockIndex = 0xffff;
3248 }
3249 if (cur.wakeReasonTag != null) {
3250 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
3251 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3252 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3253 } else {
3254 wakeReasonIndex = 0xffff;
3255 }
3256 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003257 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003258 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003259 int index = writeHistoryTag(cur.eventTag);
3260 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003261 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003262 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
3263 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3264 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003265 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003266 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07003267 if (mPlatformIdleStateCallback != null) {
3268 mCurHistoryStepDetails.statPlatformIdleState =
3269 mPlatformIdleStateCallback.getPlatformLowPowerStats();
3270 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
3271 mCurHistoryStepDetails.statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00003272
3273 mCurHistoryStepDetails.statSubsystemPowerState =
3274 mPlatformIdleStateCallback.getSubsystemLowPowerStats();
3275 if (DEBUG) Slog.i(TAG, "WRITE SubsystemPowerState:" +
3276 mCurHistoryStepDetails.statSubsystemPowerState);
3277
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07003278 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003279 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
3280 if (includeStepDetails != 0) {
3281 mCurHistoryStepDetails.writeToParcel(dest);
3282 }
3283 cur.stepDetails = mCurHistoryStepDetails;
3284 mLastHistoryStepDetails = mCurHistoryStepDetails;
3285 } else {
3286 cur.stepDetails = null;
3287 }
3288 if (mLastHistoryStepLevel < cur.batteryLevel) {
3289 mLastHistoryStepDetails = null;
3290 }
3291 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07003292
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003293 if (batteryChargeChanged) {
3294 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
3295 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07003296 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003297 }
3298
3299 private int buildBatteryLevelInt(HistoryItem h) {
3300 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08003301 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
3302 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
3303 }
3304
3305 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
3306 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
3307 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
3308 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003309 }
3310
3311 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003312 int plugType = 0;
3313 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
3314 plugType = 1;
3315 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
3316 plugType = 2;
3317 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
3318 plugType = 3;
3319 }
3320 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
3321 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
3322 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07003323 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003324 }
3325
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003326 private void computeHistoryStepDetails(final HistoryStepDetails out,
3327 final HistoryStepDetails last) {
3328 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
3329
3330 // Perform a CPU update right after we do this collection, so we have started
3331 // collecting good data for the next step.
3332 requestImmediateCpuUpdate();
3333
3334 if (last == null) {
3335 // We are not generating a delta, so all we need to do is reset the stats
3336 // we will later be doing a delta from.
3337 final int NU = mUidStats.size();
3338 for (int i=0; i<NU; i++) {
3339 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3340 uid.mLastStepUserTime = uid.mCurStepUserTime;
3341 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3342 }
3343 mLastStepCpuUserTime = mCurStepCpuUserTime;
3344 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3345 mLastStepStatUserTime = mCurStepStatUserTime;
3346 mLastStepStatSystemTime = mCurStepStatSystemTime;
3347 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3348 mLastStepStatIrqTime = mCurStepStatIrqTime;
3349 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3350 mLastStepStatIdleTime = mCurStepStatIdleTime;
3351 tmp.clear();
3352 return;
3353 }
3354 if (DEBUG) {
3355 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
3356 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
3357 + " irq=" + mLastStepStatIrqTime + " sirq="
3358 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
3359 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
3360 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
3361 + " irq=" + mCurStepStatIrqTime + " sirq="
3362 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
3363 }
3364 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
3365 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
3366 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
3367 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
3368 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
3369 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
3370 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
3371 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
3372 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
3373 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
3374 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
3375 final int NU = mUidStats.size();
3376 for (int i=0; i<NU; i++) {
3377 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3378 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
3379 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
3380 final int totalTime = totalUTime + totalSTime;
3381 uid.mLastStepUserTime = uid.mCurStepUserTime;
3382 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3383 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
3384 continue;
3385 }
3386 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
3387 out.appCpuUid3 = uid.mUid;
3388 out.appCpuUTime3 = totalUTime;
3389 out.appCpuSTime3 = totalSTime;
3390 } else {
3391 out.appCpuUid3 = out.appCpuUid2;
3392 out.appCpuUTime3 = out.appCpuUTime2;
3393 out.appCpuSTime3 = out.appCpuSTime2;
3394 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
3395 out.appCpuUid2 = uid.mUid;
3396 out.appCpuUTime2 = totalUTime;
3397 out.appCpuSTime2 = totalSTime;
3398 } else {
3399 out.appCpuUid2 = out.appCpuUid1;
3400 out.appCpuUTime2 = out.appCpuUTime1;
3401 out.appCpuSTime2 = out.appCpuSTime1;
3402 out.appCpuUid1 = uid.mUid;
3403 out.appCpuUTime1 = totalUTime;
3404 out.appCpuSTime1 = totalSTime;
3405 }
3406 }
3407 }
3408 mLastStepCpuUserTime = mCurStepCpuUserTime;
3409 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3410 mLastStepStatUserTime = mCurStepStatUserTime;
3411 mLastStepStatSystemTime = mCurStepStatSystemTime;
3412 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3413 mLastStepStatIrqTime = mCurStepStatIrqTime;
3414 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3415 mLastStepStatIdleTime = mCurStepStatIdleTime;
3416 }
3417
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003418 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3419 int firstToken = src.readInt();
3420 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003421 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003422 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003423 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3424 + " deltaTimeToken=" + deltaTimeToken);
3425
3426 if (deltaTimeToken < DELTA_TIME_ABS) {
3427 cur.time += deltaTimeToken;
3428 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3429 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003430 cur.numReadInts += 2;
3431 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003432 cur.readFromParcel(src);
3433 return;
3434 } else if (deltaTimeToken == DELTA_TIME_INT) {
3435 int delta = src.readInt();
3436 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003437 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003438 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3439 } else {
3440 long delta = src.readLong();
3441 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3442 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003443 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003444 }
3445
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003446 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003447 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003448 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003449 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003450 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003451 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
3452 + Integer.toHexString(batteryLevelInt)
3453 + " batteryLevel=" + cur.batteryLevel
3454 + " batteryTemp=" + cur.batteryTemperature
3455 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003456 } else {
3457 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003458 }
3459
3460 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3461 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003462 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003463 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3464 & STATE_BATTERY_STATUS_MASK);
3465 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3466 & STATE_BATTERY_HEALTH_MASK);
3467 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3468 & STATE_BATTERY_PLUG_MASK);
3469 switch (cur.batteryPlugType) {
3470 case 1:
3471 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3472 break;
3473 case 2:
3474 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3475 break;
3476 case 3:
3477 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3478 break;
3479 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003480 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003481 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3482 + Integer.toHexString(stateInt)
3483 + " batteryStatus=" + cur.batteryStatus
3484 + " batteryHealth=" + cur.batteryHealth
3485 + " batteryPlugType=" + cur.batteryPlugType
3486 + " states=0x" + Integer.toHexString(cur.states));
3487 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003488 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003489 }
3490
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003491 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3492 cur.states2 = src.readInt();
3493 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3494 + Integer.toHexString(cur.states2));
3495 }
3496
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003497 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003498 int indexes = src.readInt();
3499 int wakeLockIndex = indexes&0xffff;
3500 int wakeReasonIndex = (indexes>>16)&0xffff;
3501 if (wakeLockIndex != 0xffff) {
3502 cur.wakelockTag = cur.localWakelockTag;
3503 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3504 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3505 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3506 } else {
3507 cur.wakelockTag = null;
3508 }
3509 if (wakeReasonIndex != 0xffff) {
3510 cur.wakeReasonTag = cur.localWakeReasonTag;
3511 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3512 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3513 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3514 } else {
3515 cur.wakeReasonTag = null;
3516 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003517 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003518 } else {
3519 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003520 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003521 }
3522
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003523 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003524 cur.eventTag = cur.localEventTag;
3525 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003526 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003527 final int index = ((codeAndIndex>>16)&0xffff);
3528 readHistoryTag(index, cur.eventTag);
3529 cur.numReadInts += 1;
3530 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3531 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3532 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003533 } else {
3534 cur.eventCode = HistoryItem.EVENT_NONE;
3535 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003536
3537 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3538 cur.stepDetails = mReadHistoryStepDetails;
3539 cur.stepDetails.readFromParcel(src);
3540 } else {
3541 cur.stepDetails = null;
3542 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003543
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003544 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3545 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003546 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003547 }
3548
Dianne Hackbornfc064132014-06-02 12:42:12 -07003549 @Override
3550 public void commitCurrentHistoryBatchLocked() {
3551 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3552 }
3553
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003554 void addHistoryBufferLocked(long elapsedRealtimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003555 if (!mHaveBatteryLevel || !mRecordingHistory) {
3556 return;
3557 }
3558
Dianne Hackborn40c87252014-03-19 16:55:40 -07003559 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003560 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3561 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003562 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003563 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003564 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3565 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003566 + Integer.toHexString(lastDiffStates) + " diff2="
3567 + Integer.toHexString(diffStates2) + " lastDiff2="
3568 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003569 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003570 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003571 && (diffStates2&lastDiffStates2) == 0
3572 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3573 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003574 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003575 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003576 || cur.eventCode == HistoryItem.EVENT_NONE)
3577 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3578 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3579 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3580 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3581 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3582 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003583 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003584 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003585 // as long as no bit has changed both between now and the last entry, as
3586 // well as the last entry and the one before it (so we capture any toggles).
3587 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003588 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3589 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3590 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003591 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003592 // If the last written history had a wakelock tag, we need to retain it.
3593 // Note that the condition above made sure that we aren't in a case where
3594 // both it and the current history item have a wakelock tag.
3595 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003596 cur.wakelockTag = cur.localWakelockTag;
3597 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003598 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003599 // If the last written history had a wake reason tag, we need to retain it.
3600 // Note that the condition above made sure that we aren't in a case where
3601 // both it and the current history item have a wakelock tag.
3602 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003603 cur.wakeReasonTag = cur.localWakeReasonTag;
3604 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003605 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003606 // If the last written history had an event, we need to retain it.
3607 // Note that the condition above made sure that we aren't in a case where
3608 // both it and the current history item have an event.
3609 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003610 cur.eventCode = mHistoryLastWritten.eventCode;
3611 cur.eventTag = cur.localEventTag;
3612 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003613 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003614 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003615 }
3616
Adam Lesinski45489782016-12-15 23:45:17 -08003617 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003618 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003619 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3620 // Clients can't deal with history buffers this large. This only
3621 // really happens when the device is on charger and interacted with
3622 // for long periods of time, like in retail mode. Since the device is
3623 // most likely charged, when unplugged, stats would have reset anyways.
3624 // Reset the stats and mark that we overflowed.
3625 // b/32540341
3626 resetAllStatsLocked();
3627
3628 // Mark that we want to set *OVERFLOW* event and the RESET:START
3629 // events.
3630 recordResetDueToOverflow = true;
3631
3632 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003633 if (!mHistoryOverflow) {
3634 mHistoryOverflow = true;
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003635 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, cur);
3636 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003637 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003638 }
3639
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003640 // After overflow, we allow various bit-wise states to settle to 0.
3641 boolean writeAnyway = false;
3642 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3643 & mActiveHistoryStates;
3644 if (mHistoryLastWritten.states != curStates) {
3645 // mActiveHistoryStates keeps track of which bits in .states are now being
3646 // forced to 0.
3647 int old = mActiveHistoryStates;
3648 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3649 writeAnyway |= old != mActiveHistoryStates;
3650 }
3651 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3652 & mActiveHistoryStates2;
3653 if (mHistoryLastWritten.states2 != curStates2) {
3654 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3655 // forced to 0.
3656 int old = mActiveHistoryStates2;
3657 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3658 writeAnyway |= old != mActiveHistoryStates2;
3659 }
3660
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003661 // Once we've reached the maximum number of items, we only
3662 // record changes to the battery level and the most interesting states.
3663 // Once we've reached the maximum maximum number of items, we only
3664 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003665 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003666 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003667 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003668 & HistoryItem.MOST_INTERESTING_STATES) == 0
3669 || ((mHistoryLastWritten.states2^cur.states2)
3670 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003671 return;
3672 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003673
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003674 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003675 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003676 }
3677
Adam Lesinski45489782016-12-15 23:45:17 -08003678 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003679 // The history is currently empty; we need it to start with a time stamp.
3680 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003681 if (recordResetDueToOverflow) {
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003682 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW, cur);
Adam Lesinski45489782016-12-15 23:45:17 -08003683 }
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003684 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_RESET, cur);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003685 }
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003686 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003687 }
3688
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003689 private void addHistoryBufferLocked(long elapsedRealtimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003690 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003691 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003692 }
3693 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3694 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003695 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003696 mHistoryLastWritten.states &= mActiveHistoryStates;
3697 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003698 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003699 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003700 cur.wakelockTag = null;
3701 cur.wakeReasonTag = null;
3702 cur.eventCode = HistoryItem.EVENT_NONE;
3703 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003704 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3705 + " now " + mHistoryBuffer.dataPosition()
3706 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003707 }
3708
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003709 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003710 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003711
Dianne Hackborn40c87252014-03-19 16:55:40 -07003712 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003713 if (mTrackRunningHistoryElapsedRealtime != 0) {
3714 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3715 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3716 if (diffUptime < (diffElapsed-20)) {
3717 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3718 mHistoryAddTmp.setTo(mHistoryLastWritten);
3719 mHistoryAddTmp.wakelockTag = null;
3720 mHistoryAddTmp.wakeReasonTag = null;
3721 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3722 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003723 addHistoryRecordInnerLocked(wakeElapsedTime, mHistoryAddTmp);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003724 }
3725 }
3726 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3727 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3728 mTrackRunningHistoryUptime = uptimeMs;
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003729 addHistoryRecordInnerLocked(elapsedRealtimeMs, mHistoryCur);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003730 }
3731
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003732 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, HistoryItem cur) {
3733 addHistoryBufferLocked(elapsedRealtimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003734
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003735 if (!USE_OLD_HISTORY) {
3736 return;
3737 }
3738
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003739 if (!mHaveBatteryLevel || !mRecordingHistory) {
3740 return;
3741 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003742
3743 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003744 // and no states have since the last recorded entry changed and
3745 // are now resetting back to their original value, then just collapse
3746 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003747 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003748 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003749 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3750 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003751 // If the current is the same as the one before, then we no
3752 // longer need the entry.
3753 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003754 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003755 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003756 mHistoryLastEnd.next = null;
3757 mHistoryEnd.next = mHistoryCache;
3758 mHistoryCache = mHistoryEnd;
3759 mHistoryEnd = mHistoryLastEnd;
3760 mHistoryLastEnd = null;
3761 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003762 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3763 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003764 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003765 }
3766 return;
3767 }
3768
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003769 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003770 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003771
3772 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3773 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003774 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003775 }
3776
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003777 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3778 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003779 // record changes to the battery level and the most interesting states.
3780 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003781 // record changes to the battery level.
3782 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003783 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003784 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003785 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003786 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003787 return;
3788 }
3789 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003790
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +00003791 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003792 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003793
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003794 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003795 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003796 mHistoryCur.eventCode = code;
3797 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3798 mHistoryCur.eventTag.string = name;
3799 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003800 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003801 }
3802
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003803 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003804 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003805 if (rec != null) {
3806 mHistoryCache = rec.next;
3807 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003808 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003809 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003810 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003811
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003812 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003813 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003814
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003815 void addHistoryRecordLocked(HistoryItem rec) {
3816 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003817 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003818 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003819 if (mHistoryEnd != null) {
3820 mHistoryEnd.next = rec;
3821 mHistoryEnd = rec;
3822 } else {
3823 mHistory = mHistoryEnd = rec;
3824 }
3825 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003826
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003827 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003828 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003829 if (USE_OLD_HISTORY) {
3830 if (mHistory != null) {
3831 mHistoryEnd.next = mHistoryCache;
3832 mHistoryCache = mHistory;
3833 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3834 }
3835 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003836 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003837
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003838 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003839 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003840 mTrackRunningHistoryElapsedRealtime = 0;
3841 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003842
3843 mHistoryBuffer.setDataSize(0);
3844 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003845 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003846 mHistoryLastLastWritten.clear();
3847 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003848 mHistoryTagPool.clear();
3849 mNextHistoryTagIdx = 0;
3850 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003851 mHistoryBufferLastPos = -1;
3852 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003853 mActiveHistoryStates = 0xffffffff;
3854 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003855 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003856
Andreas Gampe3f24e692018-02-05 13:24:28 -08003857 @GuardedBy("this")
Mike Mac2f518a2017-09-19 16:06:03 -07003858 public void updateTimeBasesLocked(boolean unplugged, int screenState, long uptime,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003859 long realtime) {
Mike Maa7724752017-10-10 15:29:25 -07003860 final boolean screenOff = !isScreenOn(screenState);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003861 final boolean updateOnBatteryTimeBase = unplugged != mOnBatteryTimeBase.isRunning();
3862 final boolean updateOnBatteryScreenOffTimeBase =
3863 (unplugged && screenOff) != mOnBatteryScreenOffTimeBase.isRunning();
Bookatz867c0d72017-03-07 18:23:42 -08003864
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003865 if (updateOnBatteryScreenOffTimeBase || updateOnBatteryTimeBase) {
3866 if (updateOnBatteryScreenOffTimeBase) {
3867 updateKernelWakelocksLocked();
3868 updateBatteryPropertiesLocked();
Bookatz867c0d72017-03-07 18:23:42 -08003869 }
Bookatz82b341172017-09-07 19:06:08 -07003870 // This if{} is only necessary due to SCREEN_OFF_RPM_STATS_ENABLED, which exists because
3871 // updateRpmStatsLocked is too slow to run each screen change. When the speed is
3872 // improved, remove the surrounding if{}.
3873 if (SCREEN_OFF_RPM_STATS_ENABLED || updateOnBatteryTimeBase) {
3874 updateRpmStatsLocked(); // if either OnBattery or OnBatteryScreenOff timebase changes.
3875 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003876 if (DEBUG_ENERGY_CPU) {
Mike Mac2f518a2017-09-19 16:06:03 -07003877 Slog.d(TAG, "Updating cpu time because screen is now "
3878 + Display.stateToString(screenState)
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003879 + " and battery is " + (unplugged ? "on" : "off"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003880 }
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003881
3882 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Mike Mac2f518a2017-09-19 16:06:03 -07003883 if (updateOnBatteryTimeBase) {
3884 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3885 mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003886 }
Mike Mac2f518a2017-09-19 16:06:03 -07003887 }
3888 if (updateOnBatteryScreenOffTimeBase) {
3889 mOnBatteryScreenOffTimeBase.setRunning(unplugged && screenOff, uptime, realtime);
3890 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3891 mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003892 }
Bookatzc8c44962017-05-11 12:12:54 -07003893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003896
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003897 private void updateBatteryPropertiesLocked() {
3898 try {
3899 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3900 ServiceManager.getService("batteryproperties"));
3901 registrar.scheduleUpdate();
3902 } catch (RemoteException e) {
3903 // Ignore.
3904 }
3905 }
3906
Dianne Hackborn099bc622014-01-22 13:39:16 -08003907 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3908 mIsolatedUids.put(isolatedUid, appUid);
Bookatz90867622018-01-31 15:05:57 -08003909 StatsLog.write(StatsLog.ISOLATED_UID_CHANGED, appUid, isolatedUid,
3910 StatsLog.ISOLATED_UID_CHANGED__EVENT__CREATED);
Sudheer Shankab2f83c12017-11-13 19:25:01 -08003911 final Uid u = getUidStatsLocked(appUid);
3912 u.addIsolatedUid(isolatedUid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003913 }
3914
Adam Lesinski61db88f2015-07-01 15:05:07 -07003915 /**
3916 * Schedules a read of the latest cpu times before removing the isolated UID.
3917 * @see #removeIsolatedUidLocked(int)
3918 */
3919 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003920 int curUid = mIsolatedUids.get(isolatedUid, -1);
3921 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003922 if (mExternalSync != null) {
3923 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3924 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003925 }
3926 }
3927
Adam Lesinski61db88f2015-07-01 15:05:07 -07003928 /**
3929 * This should only be called after the cpu times have been read.
3930 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3931 */
Andreas Gampe3f24e692018-02-05 13:24:28 -08003932 @GuardedBy("this")
Adam Lesinski61db88f2015-07-01 15:05:07 -07003933 public void removeIsolatedUidLocked(int isolatedUid) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08003934 StatsLog.write(
Bookatz90867622018-01-31 15:05:57 -08003935 StatsLog.ISOLATED_UID_CHANGED, mIsolatedUids.get(isolatedUid, -1),
3936 isolatedUid, StatsLog.ISOLATED_UID_CHANGED__EVENT__REMOVED);
Sudheer Shankab2f83c12017-11-13 19:25:01 -08003937 final int idx = mIsolatedUids.indexOfKey(isolatedUid);
3938 if (idx >= 0) {
3939 final int ownerUid = mIsolatedUids.valueAt(idx);
3940 final Uid u = getUidStatsLocked(ownerUid);
3941 u.removeIsolatedUid(isolatedUid);
3942 mIsolatedUids.removeAt(idx);
3943 }
3944 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3945 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Mike Mafae87da2018-01-19 20:07:20 -08003946 if (mConstants.TRACK_CPU_ACTIVE_CLUSTER_TIME) {
3947 mKernelUidCpuActiveTimeReader.removeUid(isolatedUid);
3948 mKernelUidCpuClusterTimeReader.removeUid(isolatedUid);
3949 }
Adam Lesinski61db88f2015-07-01 15:05:07 -07003950 }
3951
Dianne Hackborn099bc622014-01-22 13:39:16 -08003952 public int mapUid(int uid) {
3953 int isolated = mIsolatedUids.get(uid, -1);
3954 return isolated > 0 ? isolated : uid;
3955 }
3956
3957 public void noteEventLocked(int code, String name, int uid) {
3958 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003959 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3960 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003961 }
Joe Onoratoabded112016-02-08 16:49:39 -08003962 final long elapsedRealtime = mClocks.elapsedRealtime();
3963 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003964 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003965 }
3966
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003967 boolean ensureStartClockTime(final long currentTime) {
3968 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3969 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3970 // If the start clock time has changed by more than a year, then presumably
3971 // the previous time was completely bogus. So we are going to figure out a
3972 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003973 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003974 return true;
3975 }
3976 return false;
3977 }
3978
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003979 public void noteCurrentTimeChangedLocked() {
3980 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003981 final long elapsedRealtime = mClocks.elapsedRealtime();
3982 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003983 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003984 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003985 }
3986
Dianne Hackborn61659e52014-07-09 16:13:01 -07003987 public void noteProcessStartLocked(String name, int uid) {
3988 uid = mapUid(uid);
3989 if (isOnBattery()) {
3990 Uid u = getUidStatsLocked(uid);
3991 u.getProcessStatsLocked(name).incStartsLocked();
3992 }
3993 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3994 return;
3995 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003996 if (!mRecordAllHistory) {
3997 return;
3998 }
Joe Onoratoabded112016-02-08 16:49:39 -08003999 final long elapsedRealtime = mClocks.elapsedRealtime();
4000 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004001 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
4002 }
4003
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004004 public void noteProcessCrashLocked(String name, int uid) {
4005 uid = mapUid(uid);
4006 if (isOnBattery()) {
4007 Uid u = getUidStatsLocked(uid);
4008 u.getProcessStatsLocked(name).incNumCrashesLocked();
4009 }
4010 }
4011
4012 public void noteProcessAnrLocked(String name, int uid) {
4013 uid = mapUid(uid);
4014 if (isOnBattery()) {
4015 Uid u = getUidStatsLocked(uid);
4016 u.getProcessStatsLocked(name).incNumAnrsLocked();
4017 }
4018 }
4019
Dianne Hackborna8d10942015-11-19 17:55:19 -08004020 public void noteUidProcessStateLocked(int uid, int state) {
Amith Yamasanifd3caf62017-07-26 11:48:35 -07004021 int parentUid = mapUid(uid);
4022 if (uid != parentUid) {
4023 // Isolated UIDs process state is already rolled up into parent, so no need to track
4024 // Otherwise the parent's process state will get downgraded incorrectly
4025 return;
4026 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004027 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004028 }
4029
4030 public void noteProcessFinishLocked(String name, int uid) {
4031 uid = mapUid(uid);
4032 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
4033 return;
4034 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004035 if (!mRecordAllHistory) {
4036 return;
4037 }
Joe Onoratoabded112016-02-08 16:49:39 -08004038 final long elapsedRealtime = mClocks.elapsedRealtime();
4039 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004040 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004041 }
4042
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004043 public void noteSyncStartLocked(String name, int uid) {
4044 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004045 final long elapsedRealtime = mClocks.elapsedRealtime();
4046 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004047 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
4048 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
4049 return;
4050 }
4051 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
4052 }
4053
4054 public void noteSyncFinishLocked(String name, int uid) {
4055 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004056 final long elapsedRealtime = mClocks.elapsedRealtime();
4057 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004058 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
4059 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
4060 return;
4061 }
4062 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
4063 }
4064
4065 public void noteJobStartLocked(String name, int uid) {
4066 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004067 final long elapsedRealtime = mClocks.elapsedRealtime();
4068 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004069 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
4070 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
4071 return;
4072 }
4073 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
4074 }
4075
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004076 public void noteJobFinishLocked(String name, int uid, int stopReason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004077 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004078 final long elapsedRealtime = mClocks.elapsedRealtime();
4079 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004080 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime, stopReason);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004081 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
4082 return;
4083 }
4084 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
4085 }
4086
Amith Yamasani977e11f2018-02-16 11:29:54 -08004087 public void noteJobsDeferredLocked(int uid, int numDeferred, long sinceLast) {
4088 uid = mapUid(uid);
4089 getUidStatsLocked(uid).noteJobsDeferredLocked(numDeferred, sinceLast);
4090 }
4091
Narayan Kamath695cf722017-12-21 18:32:47 +00004092 public void noteAlarmStartLocked(String name, WorkSource workSource, int uid) {
4093 noteAlarmStartOrFinishLocked(HistoryItem.EVENT_ALARM_START, name, workSource, uid);
Dianne Hackborn1e383822015-04-10 14:02:33 -07004094 }
4095
Narayan Kamath695cf722017-12-21 18:32:47 +00004096 public void noteAlarmFinishLocked(String name, WorkSource workSource, int uid) {
4097 noteAlarmStartOrFinishLocked(HistoryItem.EVENT_ALARM_FINISH, name, workSource, uid);
4098 }
4099
4100 private void noteAlarmStartOrFinishLocked(int historyItem, String name, WorkSource workSource,
4101 int uid) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07004102 if (!mRecordAllHistory) {
4103 return;
4104 }
Narayan Kamath695cf722017-12-21 18:32:47 +00004105
Joe Onoratoabded112016-02-08 16:49:39 -08004106 final long elapsedRealtime = mClocks.elapsedRealtime();
4107 final long uptime = mClocks.uptimeMillis();
Narayan Kamath695cf722017-12-21 18:32:47 +00004108
4109 if (workSource != null) {
4110 for (int i = 0; i < workSource.size(); ++i) {
4111 uid = mapUid(workSource.get(i));
4112 if (mActiveEvents.updateState(historyItem, name, uid, 0)) {
4113 addHistoryEventLocked(elapsedRealtime, uptime, historyItem, name, uid);
4114 }
4115 }
4116
4117 List<WorkChain> workChains = workSource.getWorkChains();
4118 if (workChains != null) {
4119 for (int i = 0; i < workChains.size(); ++i) {
4120 uid = mapUid(workChains.get(i).getAttributionUid());
4121 if (mActiveEvents.updateState(historyItem, name, uid, 0)) {
4122 addHistoryEventLocked(elapsedRealtime, uptime, historyItem, name, uid);
4123 }
4124 }
4125 }
4126 } else {
4127 uid = mapUid(uid);
4128
4129 if (mActiveEvents.updateState(historyItem, name, uid, 0)) {
4130 addHistoryEventLocked(elapsedRealtime, uptime, historyItem, name, uid);
4131 }
4132 }
4133 }
4134
4135 public void noteWakupAlarmLocked(String packageName, int uid, WorkSource workSource,
4136 String tag) {
Narayan Kamath695cf722017-12-21 18:32:47 +00004137 if (workSource != null) {
4138 for (int i = 0; i < workSource.size(); ++i) {
4139 uid = workSource.get(i);
4140 final String workSourceName = workSource.getName(i);
4141
Tej Singh6f724c42018-01-03 20:02:03 -08004142 if (isOnBattery()) {
4143 BatteryStatsImpl.Uid.Pkg pkg = getPackageStatsLocked(uid,
4144 workSourceName != null ? workSourceName : packageName);
4145 pkg.noteWakeupAlarmLocked(tag);
4146 }
Yangster-mac2f5daec2018-01-16 10:23:15 -08004147 StatsLog.write_non_chained(StatsLog.WAKEUP_ALARM_OCCURRED, workSource.get(i),
4148 workSource.getName(i), tag);
Narayan Kamath695cf722017-12-21 18:32:47 +00004149 }
4150
4151 ArrayList<WorkChain> workChains = workSource.getWorkChains();
4152 if (workChains != null) {
4153 for (int i = 0; i < workChains.size(); ++i) {
4154 final WorkChain wc = workChains.get(i);
4155 uid = wc.getAttributionUid();
4156
Tej Singh6f724c42018-01-03 20:02:03 -08004157 if (isOnBattery()) {
4158 BatteryStatsImpl.Uid.Pkg pkg = getPackageStatsLocked(uid, packageName);
4159 pkg.noteWakeupAlarmLocked(tag);
4160 }
Yangster-macafad8c62018-01-05 22:30:49 -08004161 StatsLog.write(StatsLog.WAKEUP_ALARM_OCCURRED, wc.getUids(), wc.getTags(), tag);
Narayan Kamath695cf722017-12-21 18:32:47 +00004162 }
4163 }
4164 } else {
Tej Singh6f724c42018-01-03 20:02:03 -08004165 if (isOnBattery()) {
4166 BatteryStatsImpl.Uid.Pkg pkg = getPackageStatsLocked(uid, packageName);
4167 pkg.noteWakeupAlarmLocked(tag);
4168 }
Yangster-mac2f5daec2018-01-16 10:23:15 -08004169 StatsLog.write_non_chained(StatsLog.WAKEUP_ALARM_OCCURRED, uid, null, tag);
Narayan Kamath695cf722017-12-21 18:32:47 +00004170 }
Dianne Hackborn1e383822015-04-10 14:02:33 -07004171 }
4172
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004173 private void requestWakelockCpuUpdate() {
Sudheer Shankac57729a2018-02-09 15:44:42 -08004174 mExternalSync.scheduleCpuSyncDueToWakelockChange(DELAY_UPDATE_WAKELOCKS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004175 }
4176
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004177 private void requestImmediateCpuUpdate() {
Sudheer Shankac57729a2018-02-09 15:44:42 -08004178 mExternalSync.scheduleCpuSyncDueToWakelockChange(0 /* delayMillis */);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004179 }
4180
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004181 public void setRecordAllHistoryLocked(boolean enabled) {
4182 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004183 if (!enabled) {
4184 // Clear out any existing state.
4185 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07004186 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004187 // Record the currently running processes as stopping, now that we are no
4188 // longer tracking them.
4189 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
4190 HistoryItem.EVENT_PROC);
4191 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004192 long mSecRealtime = mClocks.elapsedRealtime();
4193 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004194 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
4195 SparseIntArray uids = ent.getValue();
4196 for (int j=0; j<uids.size(); j++) {
4197 addHistoryEventLocked(mSecRealtime, mSecUptime,
4198 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
4199 }
4200 }
4201 }
4202 } else {
4203 // Record the currently running processes as starting, now that we are tracking them.
4204 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
4205 HistoryItem.EVENT_PROC);
4206 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004207 long mSecRealtime = mClocks.elapsedRealtime();
4208 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004209 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
4210 SparseIntArray uids = ent.getValue();
4211 for (int j=0; j<uids.size(); j++) {
4212 addHistoryEventLocked(mSecRealtime, mSecUptime,
4213 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
4214 }
4215 }
4216 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004217 }
4218 }
4219
Dianne Hackborn9a755432014-05-15 17:05:22 -07004220 public void setNoAutoReset(boolean enabled) {
4221 mNoAutoReset = enabled;
4222 }
4223
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004224 public void setPretendScreenOff(boolean pretendScreenOff) {
Mike Mac2f518a2017-09-19 16:06:03 -07004225 if (mPretendScreenOff != pretendScreenOff) {
4226 mPretendScreenOff = pretendScreenOff;
4227 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
4228 }
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004229 }
4230
Dianne Hackborn9a755432014-05-15 17:05:22 -07004231 private String mInitialAcquireWakeName;
4232 private int mInitialAcquireWakeUid = -1;
4233
Narayan Kamath81822022017-12-08 11:56:01 +00004234 public void noteStartWakeLocked(int uid, int pid, WorkChain wc, String name, String historyName,
4235 int type, boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004236 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004237 if (type == WAKE_TYPE_PARTIAL) {
4238 // Only care about partial wake locks, since full wake locks
4239 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004240 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07004241 if (historyName == null) {
4242 historyName = name;
4243 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004244 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004245 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
4246 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07004247 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07004248 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004249 }
4250 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004251 if (mWakeLockNesting == 0) {
4252 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
4253 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
4254 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004255 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004256 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004257 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08004258 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004259 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07004260 } else if (!mWakeLockImportant && !unimportantForLogging
4261 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08004262 if (mHistoryLastWritten.wakelockTag != null) {
4263 // We'll try to update the last tag.
4264 mHistoryLastWritten.wakelockTag = null;
4265 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004266 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004267 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004268 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08004269 }
4270 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004271 }
4272 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004273 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004274 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07004275 if (mOnBatteryScreenOffTimeBase.isRunning()) {
4276 // We only update the cpu time when a wake lock is acquired if the screen is off.
4277 // If the screen is on, we don't distribute the power amongst partial wakelocks.
4278 if (DEBUG_ENERGY_CPU) {
4279 Slog.d(TAG, "Updating cpu time because of +wake_lock");
4280 }
4281 requestWakelockCpuUpdate();
4282 }
Narayan Kamath81822022017-12-08 11:56:01 +00004283
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004284 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Narayan Kamath81822022017-12-08 11:56:01 +00004285
Yangster-mac20877162017-12-22 17:19:39 -08004286 if (wc != null) {
Bookatz1a1b0462018-01-12 11:47:03 -08004287 StatsLog.write(StatsLog.WAKELOCK_STATE_CHANGED, wc.getUids(), wc.getTags(),
Bookatz90867622018-01-31 15:05:57 -08004288 getPowerManagerWakeLockLevel(type), name,
4289 StatsLog.WAKELOCK_STATE_CHANGED__STATE__ACQUIRE);
Yangster-macafad8c62018-01-05 22:30:49 -08004290 } else {
Bookatz1a1b0462018-01-12 11:47:03 -08004291 StatsLog.write_non_chained(StatsLog.WAKELOCK_STATE_CHANGED, uid, null,
Bookatz90867622018-01-31 15:05:57 -08004292 getPowerManagerWakeLockLevel(type), name,
4293 StatsLog.WAKELOCK_STATE_CHANGED__STATE__ACQUIRE);
Narayan Kamath81822022017-12-08 11:56:01 +00004294 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004295 }
4296 }
4297
Narayan Kamath81822022017-12-08 11:56:01 +00004298 public void noteStopWakeLocked(int uid, int pid, WorkChain wc, String name, String historyName,
4299 int type, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004300 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004301 if (type == WAKE_TYPE_PARTIAL) {
4302 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004303 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07004304 if (historyName == null) {
4305 historyName = name;
4306 }
4307 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
4308 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07004309 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07004310 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07004311 }
4312 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004313 if (mWakeLockNesting == 0) {
4314 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
4315 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
4316 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07004317 mInitialAcquireWakeName = null;
4318 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004319 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07004320 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004321 }
4322 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07004323 if (mOnBatteryScreenOffTimeBase.isRunning()) {
4324 if (DEBUG_ENERGY_CPU) {
4325 Slog.d(TAG, "Updating cpu time because of -wake_lock");
4326 }
4327 requestWakelockCpuUpdate();
4328 }
Narayan Kamath81822022017-12-08 11:56:01 +00004329
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004330 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Yangster-mac20877162017-12-22 17:19:39 -08004331 if (wc != null) {
Bookatz1a1b0462018-01-12 11:47:03 -08004332 StatsLog.write(StatsLog.WAKELOCK_STATE_CHANGED, wc.getUids(), wc.getTags(),
Bookatz90867622018-01-31 15:05:57 -08004333 getPowerManagerWakeLockLevel(type), name,
4334 StatsLog.WAKELOCK_STATE_CHANGED__STATE__RELEASE);
Yangster-macafad8c62018-01-05 22:30:49 -08004335 } else {
Bookatz1a1b0462018-01-12 11:47:03 -08004336 StatsLog.write_non_chained(StatsLog.WAKELOCK_STATE_CHANGED, uid, null,
Bookatz90867622018-01-31 15:05:57 -08004337 getPowerManagerWakeLockLevel(type), name,
4338 StatsLog.WAKELOCK_STATE_CHANGED__STATE__RELEASE);
Narayan Kamath81822022017-12-08 11:56:01 +00004339 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004340 }
4341 }
4342
Bookatz1a1b0462018-01-12 11:47:03 -08004343 /**
4344 * Converts BatteryStats wakelock types back into PowerManager wakelock levels.
4345 * This is the inverse map of Notifier.getBatteryStatsWakeLockMonitorType().
4346 * These are estimations, since batterystats loses some of the original data.
4347 * TODO: Delete this. Instead, StatsLog.write should be called from PowerManager's Notifier.
4348 */
4349 private int getPowerManagerWakeLockLevel(int battertStatsWakelockType) {
4350 switch (battertStatsWakelockType) {
4351 // PowerManager.PARTIAL_WAKE_LOCK or PROXIMITY_SCREEN_OFF_WAKE_LOCK
4352 case BatteryStats.WAKE_TYPE_PARTIAL:
4353 return PowerManager.PARTIAL_WAKE_LOCK;
4354
4355 // PowerManager.SCREEN_DIM_WAKE_LOCK or SCREEN_BRIGHT_WAKE_LOCK
4356 case BatteryStats.WAKE_TYPE_FULL:
4357 return PowerManager.FULL_WAKE_LOCK;
4358
4359 case BatteryStats.WAKE_TYPE_DRAW:
4360 return PowerManager.DRAW_WAKE_LOCK;
4361
4362 // It appears that nothing can ever make a Window and PowerManager lacks an equivalent.
4363 case BatteryStats.WAKE_TYPE_WINDOW:
4364 Slog.e(TAG, "Illegal window wakelock type observed in batterystats.");
4365 return -1;
4366
4367 default:
4368 Slog.e(TAG, "Illegal wakelock type in batterystats: " + battertStatsWakelockType);
4369 return -1;
4370 }
4371 }
4372
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004373 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
4374 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08004375 final long elapsedRealtime = mClocks.elapsedRealtime();
4376 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004377 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004378 for (int i=0; i<N; i++) {
Narayan Kamath81822022017-12-08 11:56:01 +00004379 noteStartWakeLocked(ws.get(i), pid, null, name, historyName, type,
4380 unimportantForLogging, elapsedRealtime, uptime);
4381 }
4382
4383 List<WorkChain> wcs = ws.getWorkChains();
4384 if (wcs != null) {
4385 for (int i = 0; i < wcs.size(); ++i) {
4386 final WorkChain wc = wcs.get(i);
4387 noteStartWakeLocked(wc.getAttributionUid(), pid, wc, name, historyName, type,
4388 unimportantForLogging, elapsedRealtime, uptime);
4389 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004390 }
4391 }
4392
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004393 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
4394 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004395 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08004396 final long elapsedRealtime = mClocks.elapsedRealtime();
4397 final long uptime = mClocks.uptimeMillis();
Narayan Kamath81822022017-12-08 11:56:01 +00004398
4399 List<WorkChain>[] wcs = WorkSource.diffChains(ws, newWs);
4400
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004401 // For correct semantics, we start the need worksources first, so that we won't
4402 // make inappropriate history items as if all wake locks went away and new ones
4403 // appeared. This is okay because tracking of wake locks allows nesting.
Narayan Kamath81822022017-12-08 11:56:01 +00004404 //
4405 // First the starts :
Dianne Hackborn40c87252014-03-19 16:55:40 -07004406 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004407 for (int i=0; i<NN; i++) {
Narayan Kamath81822022017-12-08 11:56:01 +00004408 noteStartWakeLocked(newWs.get(i), newPid, null, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07004409 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004410 }
Narayan Kamath81822022017-12-08 11:56:01 +00004411 if (wcs != null) {
4412 List<WorkChain> newChains = wcs[0];
4413 if (newChains != null) {
4414 for (int i = 0; i < newChains.size(); ++i) {
4415 final WorkChain newChain = newChains.get(i);
4416 noteStartWakeLocked(newChain.getAttributionUid(), newPid, newChain, newName,
4417 newHistoryName, newType, newUnimportantForLogging, elapsedRealtime,
4418 uptime);
4419 }
4420 }
4421 }
4422
4423 // Then the stops :
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004424 final int NO = ws.size();
4425 for (int i=0; i<NO; i++) {
Narayan Kamath81822022017-12-08 11:56:01 +00004426 noteStopWakeLocked(ws.get(i), pid, null, name, historyName, type, elapsedRealtime,
4427 uptime);
4428 }
4429 if (wcs != null) {
4430 List<WorkChain> goneChains = wcs[1];
4431 if (goneChains != null) {
4432 for (int i = 0; i < goneChains.size(); ++i) {
4433 final WorkChain goneChain = goneChains.get(i);
4434 noteStopWakeLocked(goneChain.getAttributionUid(), pid, goneChain, name,
4435 historyName, type, elapsedRealtime, uptime);
4436 }
4437 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004438 }
4439 }
4440
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004441 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
4442 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08004443 final long elapsedRealtime = mClocks.elapsedRealtime();
4444 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004445 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004446 for (int i=0; i<N; i++) {
Narayan Kamath81822022017-12-08 11:56:01 +00004447 noteStopWakeLocked(ws.get(i), pid, null, name, historyName, type, elapsedRealtime,
4448 uptime);
4449 }
4450
4451 List<WorkChain> wcs = ws.getWorkChains();
4452 if (wcs != null) {
4453 for (int i = 0; i < wcs.size(); ++i) {
4454 final WorkChain wc = wcs.get(i);
4455 noteStopWakeLocked(wc.getAttributionUid(), pid, wc, name, historyName, type,
4456 elapsedRealtime, uptime);
4457 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004458 }
4459 }
4460
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004461 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
Yangster-mac2f5daec2018-01-16 10:23:15 -08004462 StatsLog.write_non_chained(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004463 uid, null, name, historyName,
4464 StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__ON);
Yangster-macafad8c62018-01-05 22:30:49 -08004465
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004466 uid = mapUid(uid);
Narayan Kamath96a92562018-01-02 18:57:17 +00004467 noteLongPartialWakeLockStartInternal(name, historyName, uid);
Narayan Kamath96a92562018-01-02 18:57:17 +00004468 }
4469
4470 public void noteLongPartialWakelockStartFromSource(String name, String historyName,
4471 WorkSource workSource) {
4472 final int N = workSource.size();
4473 for (int i = 0; i < N; ++i) {
4474 final int uid = mapUid(workSource.get(i));
4475 noteLongPartialWakeLockStartInternal(name, historyName, uid);
Yangster-mac2f5daec2018-01-16 10:23:15 -08004476 StatsLog.write_non_chained(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004477 workSource.get(i), workSource.getName(i), name, historyName,
4478 StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__ON);
Narayan Kamath96a92562018-01-02 18:57:17 +00004479 }
4480
4481 final ArrayList<WorkChain> workChains = workSource.getWorkChains();
4482 if (workChains != null) {
4483 for (int i = 0; i < workChains.size(); ++i) {
4484 final WorkChain workChain = workChains.get(i);
4485 final int uid = workChain.getAttributionUid();
4486 noteLongPartialWakeLockStartInternal(name, historyName, uid);
4487
Yangster-macafad8c62018-01-05 22:30:49 -08004488 StatsLog.write(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004489 workChain.getUids(), workChain.getTags(), name, historyName,
4490 StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__ON);
Narayan Kamath96a92562018-01-02 18:57:17 +00004491 }
4492 }
4493 }
4494
4495 private void noteLongPartialWakeLockStartInternal(String name, String historyName, int uid) {
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004496 final long elapsedRealtime = mClocks.elapsedRealtime();
4497 final long uptime = mClocks.uptimeMillis();
4498 if (historyName == null) {
4499 historyName = name;
4500 }
4501 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
4502 0)) {
4503 return;
4504 }
4505 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
4506 historyName, uid);
4507 }
4508
4509 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
Bookatz90867622018-01-31 15:05:57 -08004510 StatsLog.write_non_chained(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED, uid, null,
4511 name, historyName, StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__OFF);
Yangster-macafad8c62018-01-05 22:30:49 -08004512
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004513 uid = mapUid(uid);
Narayan Kamath96a92562018-01-02 18:57:17 +00004514 noteLongPartialWakeLockFinishInternal(name, historyName, uid);
Narayan Kamath96a92562018-01-02 18:57:17 +00004515 }
4516
4517 public void noteLongPartialWakelockFinishFromSource(String name, String historyName,
4518 WorkSource workSource) {
4519 final int N = workSource.size();
4520 for (int i = 0; i < N; ++i) {
4521 final int uid = mapUid(workSource.get(i));
4522 noteLongPartialWakeLockFinishInternal(name, historyName, uid);
Yangster-mac2f5daec2018-01-16 10:23:15 -08004523 StatsLog.write_non_chained(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004524 workSource.get(i), workSource.getName(i), name, historyName,
4525 StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__OFF);
Narayan Kamath96a92562018-01-02 18:57:17 +00004526 }
4527
4528 final ArrayList<WorkChain> workChains = workSource.getWorkChains();
4529 if (workChains != null) {
4530 for (int i = 0; i < workChains.size(); ++i) {
4531 final WorkChain workChain = workChains.get(i);
4532 final int uid = workChain.getAttributionUid();
Narayan Kamath96a92562018-01-02 18:57:17 +00004533 noteLongPartialWakeLockFinishInternal(name, historyName, uid);
Yangster-macafad8c62018-01-05 22:30:49 -08004534 StatsLog.write(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004535 workChain.getUids(), workChain.getTags(), name, historyName,
4536 StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED__STATE__OFF);
Narayan Kamath96a92562018-01-02 18:57:17 +00004537 }
4538 }
4539 }
4540
4541 private void noteLongPartialWakeLockFinishInternal(String name, String historyName, int uid) {
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004542 final long elapsedRealtime = mClocks.elapsedRealtime();
4543 final long uptime = mClocks.uptimeMillis();
4544 if (historyName == null) {
4545 historyName = name;
4546 }
4547 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
4548 0)) {
4549 return;
4550 }
4551 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
4552 historyName, uid);
4553 }
4554
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004555 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
4556 if (mLastWakeupReason != null) {
4557 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004558 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07004559 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Bookatz90867622018-01-31 15:05:57 -08004560 StatsLog.write(StatsLog.KERNEL_WAKEUP_REPORTED, mLastWakeupReason,
4561 /* duration_usec */ deltaUptime * 1000);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004562 mLastWakeupReason = null;
4563 }
4564 }
4565
4566 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08004567 final long elapsedRealtime = mClocks.elapsedRealtime();
4568 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004569 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004570 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004571 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004572 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
4573 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004574 mHistoryCur.wakeReasonTag.uid = 0;
4575 mLastWakeupReason = reason;
4576 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004577 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004578 }
4579
Adam Lesinski72478f02015-06-17 15:39:43 -07004580 public boolean startAddingCpuLocked() {
Sudheer Shankac57729a2018-02-09 15:44:42 -08004581 mExternalSync.cancelCpuSyncDueToWakelockChange();
Adam Lesinski72478f02015-06-17 15:39:43 -07004582 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004583 }
4584
Adam Lesinski72478f02015-06-17 15:39:43 -07004585 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
4586 int statSystemTime, int statIOWaitTime, int statIrqTime,
4587 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004588 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
4589 + " user=" + statUserTime + " sys=" + statSystemTime
4590 + " io=" + statIOWaitTime + " irq=" + statIrqTime
4591 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
4592 mCurStepCpuUserTime += totalUTime;
4593 mCurStepCpuSystemTime += totalSTime;
4594 mCurStepStatUserTime += statUserTime;
4595 mCurStepStatSystemTime += statSystemTime;
4596 mCurStepStatIOWaitTime += statIOWaitTime;
4597 mCurStepStatIrqTime += statIrqTime;
4598 mCurStepStatSoftIrqTime += statSoftIrqTime;
4599 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004600 }
4601
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004602 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004603 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004604 Uid u = mUidStats.get(uid);
4605 if (u != null) {
4606 u.mPids.remove(pid);
4607 }
4608 }
4609
4610 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004611 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004612 Uid u = mUidStats.get(uid);
4613 if (u != null) {
4614 Uid.Pid p = u.mPids.get(pid);
4615 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004616 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004617 }
4618 }
4619 return 0;
4620 }
4621
Dianne Hackborn287952c2010-09-22 22:34:31 -07004622 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004623 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004624 Uid u = mUidStats.get(uid);
4625 if (u != null) {
4626 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
4627 }
4628 }
4629
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004630 int mSensorNesting;
4631
4632 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004633 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004634 final long elapsedRealtime = mClocks.elapsedRealtime();
4635 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004636 if (mSensorNesting == 0) {
4637 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
4638 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
4639 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004640 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004641 }
4642 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004643 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004644 }
4645
4646 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004647 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004648 final long elapsedRealtime = mClocks.elapsedRealtime();
4649 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004650 mSensorNesting--;
4651 if (mSensorNesting == 0) {
4652 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4653 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4654 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004655 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004656 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004657 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004658 }
4659
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004660 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004661
Narayan Kamath32684dd2018-01-08 17:32:51 +00004662 public void noteGpsChangedLocked(WorkSource oldWs, WorkSource newWs) {
4663 for (int i = 0; i < newWs.size(); ++i) {
4664 noteStartGpsLocked(newWs.get(i), null);
4665 }
4666
4667 for (int i = 0; i < oldWs.size(); ++i) {
4668 noteStopGpsLocked((oldWs.get(i)), null);
4669 }
4670
4671 List<WorkChain>[] wcs = WorkSource.diffChains(oldWs, newWs);
4672 if (wcs != null) {
4673 if (wcs[0] != null) {
4674 final List<WorkChain> newChains = wcs[0];
4675 for (int i = 0; i < newChains.size(); ++i) {
4676 noteStartGpsLocked(-1, newChains.get(i));
4677 }
4678 }
4679
4680 if (wcs[1] != null) {
4681 final List<WorkChain> goneChains = wcs[1];
4682 for (int i = 0; i < goneChains.size(); ++i) {
4683 noteStopGpsLocked(-1, goneChains.get(i));
4684 }
4685 }
4686 }
4687 }
4688
4689 private void noteStartGpsLocked(int uid, WorkChain workChain) {
4690 uid = getAttributionUid(uid, workChain);
Joe Onoratoabded112016-02-08 16:49:39 -08004691 final long elapsedRealtime = mClocks.elapsedRealtime();
4692 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004693 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004694 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004695 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4696 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004697 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004698 }
4699 mGpsNesting++;
Narayan Kamath32684dd2018-01-08 17:32:51 +00004700
4701 if (workChain == null) {
Bookatz90867622018-01-31 15:05:57 -08004702 StatsLog.write_non_chained(StatsLog.GPS_SCAN_STATE_CHANGED, uid, null,
4703 StatsLog.GPS_SCAN_STATE_CHANGED__STATE__ON);
Narayan Kamath32684dd2018-01-08 17:32:51 +00004704 } else {
4705 StatsLog.write(StatsLog.GPS_SCAN_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08004706 workChain.getUids(), workChain.getTags(),
4707 StatsLog.GPS_SCAN_STATE_CHANGED__STATE__ON);
Narayan Kamath32684dd2018-01-08 17:32:51 +00004708 }
4709
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004710 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004712
Narayan Kamath32684dd2018-01-08 17:32:51 +00004713 private void noteStopGpsLocked(int uid, WorkChain workChain) {
4714 uid = getAttributionUid(uid, workChain);
Joe Onoratoabded112016-02-08 16:49:39 -08004715 final long elapsedRealtime = mClocks.elapsedRealtime();
4716 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004717 mGpsNesting--;
4718 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004719 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004720 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4721 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004722 addHistoryRecordLocked(elapsedRealtime, uptime);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004723 stopAllGpsSignalQualityTimersLocked(-1);
4724 mGpsSignalQualityBin = -1;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004725 }
Narayan Kamath32684dd2018-01-08 17:32:51 +00004726
4727 if (workChain == null) {
Bookatz90867622018-01-31 15:05:57 -08004728 StatsLog.write_non_chained(StatsLog.GPS_SCAN_STATE_CHANGED, uid, null,
4729 StatsLog.GPS_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath32684dd2018-01-08 17:32:51 +00004730 } else {
4731 StatsLog.write(StatsLog.GPS_SCAN_STATE_CHANGED, workChain.getUids(),
Bookatz90867622018-01-31 15:05:57 -08004732 workChain.getTags(), StatsLog.GPS_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath32684dd2018-01-08 17:32:51 +00004733 }
4734
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004735 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004736 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004737
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004738 public void noteGpsSignalQualityLocked(int signalLevel) {
4739 if (mGpsNesting == 0) {
4740 return;
4741 }
4742 if (signalLevel < 0 || signalLevel >= GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS) {
4743 stopAllGpsSignalQualityTimersLocked(-1);
4744 return;
4745 }
4746 final long elapsedRealtime = mClocks.elapsedRealtime();
4747 final long uptime = mClocks.uptimeMillis();
4748 if (mGpsSignalQualityBin != signalLevel) {
4749 if (mGpsSignalQualityBin >= 0) {
4750 mGpsSignalQualityTimer[mGpsSignalQualityBin].stopRunningLocked(elapsedRealtime);
4751 }
4752 if(!mGpsSignalQualityTimer[signalLevel].isRunningLocked()) {
4753 mGpsSignalQualityTimer[signalLevel].startRunningLocked(elapsedRealtime);
4754 }
4755 mHistoryCur.states2 = (mHistoryCur.states2&~HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK)
4756 | (signalLevel << HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT);
4757 addHistoryRecordLocked(elapsedRealtime, uptime);
4758 mGpsSignalQualityBin = signalLevel;
4759 }
4760 return;
4761 }
4762
Andreas Gampe3f24e692018-02-05 13:24:28 -08004763 @GuardedBy("this")
Jeff Browne95c3cd2014-05-02 16:59:26 -07004764 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004765 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004766
4767 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4768 // original 4 are mapped to one of the originals.
4769 if (state > MAX_TRACKED_SCREEN_STATE) {
4770 switch (state) {
4771 case Display.STATE_VR:
4772 state = Display.STATE_ON;
4773 break;
4774 default:
4775 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4776 break;
4777 }
4778 }
4779
Jeff Browne95c3cd2014-05-02 16:59:26 -07004780 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004781 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004782 final int oldState = mScreenState;
4783 mScreenState = state;
4784 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4785 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004786
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004787 if (state != Display.STATE_UNKNOWN) {
4788 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004789 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4790 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4791 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004792 } else {
4793 Slog.wtf(TAG, "Unexpected screen state: " + state);
4794 }
4795 }
4796
Mike Mac2f518a2017-09-19 16:06:03 -07004797 final long elapsedRealtime = mClocks.elapsedRealtime();
4798 final long uptime = mClocks.uptimeMillis();
4799
4800 boolean updateHistory = false;
4801 if (isScreenDoze(state)) {
4802 mHistoryCur.states |= HistoryItem.STATE_SCREEN_DOZE_FLAG;
4803 mScreenDozeTimer.startRunningLocked(elapsedRealtime);
4804 updateHistory = true;
4805 } else if (isScreenDoze(oldState)) {
4806 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_DOZE_FLAG;
4807 mScreenDozeTimer.stopRunningLocked(elapsedRealtime);
4808 updateHistory = true;
4809 }
4810 if (isScreenOn(state)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004811 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4812 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4813 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004814 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4815 if (mScreenBrightnessBin >= 0) {
4816 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4817 }
Mike Mac2f518a2017-09-19 16:06:03 -07004818 updateHistory = true;
4819 } else if (isScreenOn(oldState)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004820 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4821 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4822 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004823 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4824 if (mScreenBrightnessBin >= 0) {
4825 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4826 }
Mike Mac2f518a2017-09-19 16:06:03 -07004827 updateHistory = true;
4828 }
4829 if (updateHistory) {
4830 if (DEBUG_HISTORY) Slog.v(TAG, "Screen state to: "
4831 + Display.stateToString(state));
4832 addHistoryRecordLocked(elapsedRealtime, uptime);
4833 }
Sudheer Shankac57729a2018-02-09 15:44:42 -08004834 mExternalSync.scheduleCpuSyncDueToScreenStateChange(
4835 mOnBatteryTimeBase.isRunning(), mOnBatteryScreenOffTimeBase.isRunning());
Mike Mac2f518a2017-09-19 16:06:03 -07004836 if (isScreenOn(state)) {
4837 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
4838 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
4839 // Fake a wake lock, so we consider the device waked as long as the screen is on.
Narayan Kamath81822022017-12-08 11:56:01 +00004840 noteStartWakeLocked(-1, -1, null, "screen", null, WAKE_TYPE_PARTIAL, false,
Mike Mac2f518a2017-09-19 16:06:03 -07004841 elapsedRealtime, uptime);
4842 } else if (isScreenOn(oldState)) {
Narayan Kamath81822022017-12-08 11:56:01 +00004843 noteStopWakeLocked(-1, -1, null, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004844 elapsedRealtime, uptime);
Mike Mac2f518a2017-09-19 16:06:03 -07004845 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
Joe Onoratoabded112016-02-08 16:49:39 -08004846 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Mike Mac2f518a2017-09-19 16:06:03 -07004847 }
4848 // Update discharge amounts.
4849 if (mOnBatteryInternal) {
4850 updateDischargeScreenLevelsLocked(oldState, state);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004851 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004852 }
4853 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004854
Dianne Hackborn617f8772009-03-31 15:04:46 -07004855 public void noteScreenBrightnessLocked(int brightness) {
4856 // Bin the brightness.
4857 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4858 if (bin < 0) bin = 0;
4859 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4860 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004861 final long elapsedRealtime = mClocks.elapsedRealtime();
4862 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004863 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4864 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004865 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4866 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004867 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004868 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004869 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004870 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004871 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004872 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004873 }
4874 mScreenBrightnessBin = bin;
4875 }
4876 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004877
Dianne Hackborn617f8772009-03-31 15:04:46 -07004878 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004879 if (mOnBatteryInternal) {
4880 uid = mapUid(uid);
4881 getUidStatsLocked(uid).noteUserActivityLocked(event);
4882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004884
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004885 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004886 final long elapsedRealtime = mClocks.elapsedRealtime();
4887 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004888 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4889 reason, reasonUid);
4890 }
4891
Jeff Browne95c3cd2014-05-02 16:59:26 -07004892 public void noteInteractiveLocked(boolean interactive) {
4893 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004894 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004895 mInteractive = interactive;
4896 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4897 if (interactive) {
4898 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4899 } else {
4900 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4901 }
4902 }
4903 }
4904
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004905 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004906 final long elapsedRealtime = mClocks.elapsedRealtime();
4907 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004908 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4909 extra, type);
4910 mNumConnectivityChange++;
4911 }
4912
Adam Lesinski5f056f62016-07-14 16:56:08 -07004913 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4914 final long uptimeMillis, int uid) {
4915 uid = mapUid(uid);
4916 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4917 uid);
4918 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4919 }
4920
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004921 /**
4922 * Updates the radio power state and returns true if an external stats collection should occur.
4923 */
4924 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004925 final long elapsedRealtime = mClocks.elapsedRealtime();
4926 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004927 if (mMobileRadioPowerState != powerState) {
4928 long realElapsedRealtimeMs;
4929 final boolean active =
4930 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4931 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4932 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004933 if (uid > 0) {
4934 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4935 }
4936
Adam Lesinski9acfd812016-04-19 18:29:50 -07004937 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004938 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4939 } else {
4940 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004941 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004942 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4943 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4944 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004945 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004946 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4947 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4948 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004949 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004950 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4951 }
4952 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4953 + Integer.toHexString(mHistoryCur.states));
4954 addHistoryRecordLocked(elapsedRealtime, uptime);
4955 mMobileRadioPowerState = powerState;
Bookatzddccf0a2017-11-28 16:48:14 -08004956 StatsLog.write(StatsLog.MOBILE_RADIO_POWER_STATE_CHANGED, uid, powerState);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004957 if (active) {
4958 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4959 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4960 } else {
4961 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004962 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004963 // Tell the caller to collect radio network/power stats.
4964 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004965 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004966 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004967 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004968 }
4969
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004970 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004971 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004972 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4973 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4974 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004975 final long elapsedRealtime = mClocks.elapsedRealtime();
4976 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004977 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004978 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004979 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4980 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004981 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004982 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004983 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004984 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4985 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004986 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004987 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004988 }
4989 addHistoryRecordLocked(elapsedRealtime, uptime);
Bookatz90867622018-01-31 15:05:57 -08004990 StatsLog.write(StatsLog.BATTERY_SAVER_MODE_STATE_CHANGED, enabled ?
4991 StatsLog.BATTERY_SAVER_MODE_STATE_CHANGED__STATE__ON :
4992 StatsLog.BATTERY_SAVER_MODE_STATE_CHANGED__STATE__OFF);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004993 }
4994 }
4995
Bookatzddccf0a2017-11-28 16:48:14 -08004996 public void noteDeviceIdleModeLocked(final int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004997 final long elapsedRealtime = mClocks.elapsedRealtime();
4998 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004999 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005000 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005001 // We don't go out of general idling mode until explicitly taken out of
5002 // device idle through going active or significant motion.
5003 nowIdling = true;
5004 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005005 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
5006 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
5007 // We don't go out of general light idling mode until explicitly taken out of
5008 // device idle through going active or significant motion.
5009 nowLightIdling = true;
5010 }
5011 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
5012 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
5013 activeReason, activeUid);
5014 }
Bookatzddccf0a2017-11-28 16:48:14 -08005015 if (mDeviceIdling != nowIdling || mDeviceLightIdling != nowLightIdling) {
5016 int statsmode;
5017 if (nowIdling) statsmode = DEVICE_IDLE_MODE_DEEP;
5018 else if (nowLightIdling) statsmode = DEVICE_IDLE_MODE_LIGHT;
5019 else statsmode = DEVICE_IDLE_MODE_OFF;
5020 StatsLog.write(StatsLog.DEVICE_IDLING_MODE_STATE_CHANGED, statsmode);
5021 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005022 if (mDeviceIdling != nowIdling) {
5023 mDeviceIdling = nowIdling;
5024 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
5025 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
5026 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005027 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005028 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
5029 } else {
5030 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
5031 }
5032 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005033 if (mDeviceLightIdling != nowLightIdling) {
5034 mDeviceLightIdling = nowLightIdling;
5035 if (nowLightIdling) {
5036 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005037 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005038 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005039 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005040 }
5041 if (mDeviceIdleMode != mode) {
5042 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
5043 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
5044 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
5045 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005046 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005047 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
5048 mLastIdleTimeStart = elapsedRealtime;
5049 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
5050 if (lastDuration > mLongestLightIdleTime) {
5051 mLongestLightIdleTime = lastDuration;
5052 }
5053 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005054 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005055 if (lastDuration > mLongestFullIdleTime) {
5056 mLongestFullIdleTime = lastDuration;
5057 }
5058 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
5059 }
5060 if (mode == DEVICE_IDLE_MODE_LIGHT) {
5061 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005062 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005063 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
5064 }
5065 mDeviceIdleMode = mode;
Bookatzddccf0a2017-11-28 16:48:14 -08005066 StatsLog.write(StatsLog.DEVICE_IDLE_MODE_STATE_CHANGED, mode);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005067 }
5068 }
5069
Dianne Hackborn3accca02013-09-20 09:32:11 -07005070 public void notePackageInstalledLocked(String pkgName, long versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08005071 final long elapsedRealtime = mClocks.elapsedRealtime();
5072 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3accca02013-09-20 09:32:11 -07005073 // XXX need to figure out what to do with long version codes.
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005074 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
Dianne Hackborn3accca02013-09-20 09:32:11 -07005075 pkgName, (int)versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005076 PackageChange pc = new PackageChange();
5077 pc.mPackageName = pkgName;
5078 pc.mUpdate = true;
5079 pc.mVersionCode = versionCode;
5080 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005081 }
5082
5083 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08005084 final long elapsedRealtime = mClocks.elapsedRealtime();
5085 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005086 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
5087 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005088 PackageChange pc = new PackageChange();
5089 pc.mPackageName = pkgName;
5090 pc.mUpdate = true;
5091 addPackageChange(pc);
5092 }
5093
5094 private void addPackageChange(PackageChange pc) {
5095 if (mDailyPackageChanges == null) {
5096 mDailyPackageChanges = new ArrayList<>();
5097 }
5098 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005099 }
5100
Siddharth Ray78ccaf52017-12-23 16:16:21 -08005101 void stopAllGpsSignalQualityTimersLocked(int except) {
5102 final long elapsedRealtime = mClocks.elapsedRealtime();
5103 for (int i = 0; i < GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
5104 if (i == except) {
5105 continue;
5106 }
5107 while (mGpsSignalQualityTimer[i].isRunningLocked()) {
5108 mGpsSignalQualityTimer[i].stopRunningLocked(elapsedRealtime);
5109 }
5110 }
5111 }
5112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 public void notePhoneOnLocked() {
5114 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08005115 final long elapsedRealtime = mClocks.elapsedRealtime();
5116 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005117 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005118 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
5119 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005120 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005122 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 }
5124 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 public void notePhoneOffLocked() {
5127 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08005128 final long elapsedRealtime = mClocks.elapsedRealtime();
5129 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005130 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005131 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
5132 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005133 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005135 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 }
5137 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07005138
Dianne Hackborn3251b902014-06-20 14:40:53 -07005139 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005140 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08005141 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005142 if (i == except) {
5143 continue;
5144 }
5145 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005146 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005147 }
5148 }
5149 }
5150
Dianne Hackborne4a59512010-12-07 11:08:07 -08005151 private int fixPhoneServiceState(int state, int signalBin) {
5152 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
5153 // In this case we will always be STATE_OUT_OF_SERVICE, so need
5154 // to infer that we are scanning from other data.
5155 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08005156 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005157 state = ServiceState.STATE_IN_SERVICE;
5158 }
5159 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005160
Dianne Hackborne4a59512010-12-07 11:08:07 -08005161 return state;
5162 }
5163
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005164 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005165 boolean scanning = false;
5166 boolean newHistory = false;
5167
5168 mPhoneServiceStateRaw = state;
5169 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005170 mPhoneSignalStrengthBinRaw = strengthBin;
5171
Joe Onoratoabded112016-02-08 16:49:39 -08005172 final long elapsedRealtime = mClocks.elapsedRealtime();
5173 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005174
5175 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
5176 // In this case we will always be STATE_OUT_OF_SERVICE, so need
5177 // to infer that we are scanning from other data.
5178 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005179 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005180 state = ServiceState.STATE_IN_SERVICE;
5181 }
5182 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005183
5184 // If the phone is powered off, stop all timers.
5185 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005186 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07005187
Dianne Hackborne4a59512010-12-07 11:08:07 -08005188 // If we are in service, make sure the correct signal string timer is running.
5189 } else if (state == ServiceState.STATE_IN_SERVICE) {
5190 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005191
5192 // If we're out of service, we are in the lowest signal strength
5193 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07005194 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005195 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005196 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07005197 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005198 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08005199 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005200 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
5201 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005202 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005203 }
5204 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005205
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005206 if (!scanning) {
5207 // If we are no longer scanning, then stop the scanning timer.
5208 if (mPhoneSignalScanningTimer.isRunningLocked()) {
5209 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
5210 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
5211 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005212 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005213 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005214 }
5215 }
5216
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005217 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005218 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
5219 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005220 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005221 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005222 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005223 mPhoneServiceState = state;
5224 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08005225
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005226 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005227 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005228 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005229 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005230 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005231 if (strengthBin >= 0) {
5232 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005233 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005234 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07005235 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
5236 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005237 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08005238 + Integer.toHexString(mHistoryCur.states));
5239 newHistory = true;
Bookatze5885242017-10-24 20:10:31 -07005240 StatsLog.write(StatsLog.PHONE_SIGNAL_STRENGTH_CHANGED, strengthBin);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005241 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07005242 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005243 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005244 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08005245 }
5246
5247 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07005248 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005249 }
5250 }
5251
5252 /**
5253 * Telephony stack updates the phone state.
5254 * @param state phone state from ServiceState.getState()
5255 */
5256 public void notePhoneStateLocked(int state, int simState) {
5257 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07005258 }
5259
Wink Savillee9b06d72009-05-18 21:47:50 -07005260 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07005261 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08005262 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005263 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005264 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005265
Dianne Hackborn627bba72009-03-24 22:32:56 -07005266 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
5267 int bin = DATA_CONNECTION_NONE;
5268 if (hasData) {
5269 switch (dataType) {
5270 case TelephonyManager.NETWORK_TYPE_EDGE:
5271 bin = DATA_CONNECTION_EDGE;
5272 break;
5273 case TelephonyManager.NETWORK_TYPE_GPRS:
5274 bin = DATA_CONNECTION_GPRS;
5275 break;
5276 case TelephonyManager.NETWORK_TYPE_UMTS:
5277 bin = DATA_CONNECTION_UMTS;
5278 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005279 case TelephonyManager.NETWORK_TYPE_CDMA:
5280 bin = DATA_CONNECTION_CDMA;
5281 break;
5282 case TelephonyManager.NETWORK_TYPE_EVDO_0:
5283 bin = DATA_CONNECTION_EVDO_0;
5284 break;
5285 case TelephonyManager.NETWORK_TYPE_EVDO_A:
5286 bin = DATA_CONNECTION_EVDO_A;
5287 break;
5288 case TelephonyManager.NETWORK_TYPE_1xRTT:
5289 bin = DATA_CONNECTION_1xRTT;
5290 break;
5291 case TelephonyManager.NETWORK_TYPE_HSDPA:
5292 bin = DATA_CONNECTION_HSDPA;
5293 break;
5294 case TelephonyManager.NETWORK_TYPE_HSUPA:
5295 bin = DATA_CONNECTION_HSUPA;
5296 break;
5297 case TelephonyManager.NETWORK_TYPE_HSPA:
5298 bin = DATA_CONNECTION_HSPA;
5299 break;
5300 case TelephonyManager.NETWORK_TYPE_IDEN:
5301 bin = DATA_CONNECTION_IDEN;
5302 break;
5303 case TelephonyManager.NETWORK_TYPE_EVDO_B:
5304 bin = DATA_CONNECTION_EVDO_B;
5305 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07005306 case TelephonyManager.NETWORK_TYPE_LTE:
5307 bin = DATA_CONNECTION_LTE;
5308 break;
5309 case TelephonyManager.NETWORK_TYPE_EHRPD:
5310 bin = DATA_CONNECTION_EHRPD;
5311 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08005312 case TelephonyManager.NETWORK_TYPE_HSPAP:
5313 bin = DATA_CONNECTION_HSPAP;
5314 break;
Siddharth Rayc72081d2018-02-13 11:31:54 -08005315 case TelephonyManager.NETWORK_TYPE_GSM:
5316 bin = DATA_CONNECTION_GSM;
5317 break;
5318 case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
5319 bin = DATA_CONNECTION_TD_SCDMA;
5320 break;
5321 case TelephonyManager.NETWORK_TYPE_IWLAN:
5322 bin = DATA_CONNECTION_IWLAN;
5323 break;
5324 case TelephonyManager.NETWORK_TYPE_LTE_CA:
5325 bin = DATA_CONNECTION_LTE_CA;
5326 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07005327 default:
5328 bin = DATA_CONNECTION_OTHER;
5329 break;
5330 }
5331 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07005332 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005333 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005334 final long elapsedRealtime = mClocks.elapsedRealtime();
5335 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005336 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
5337 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005338 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
5339 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005340 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005341 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005342 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005343 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005344 }
5345 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005346 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005347 }
5348 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005349
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005350 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07005351 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08005352 final long elapsedRealtime = mClocks.elapsedRealtime();
5353 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005354 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005355 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
5356 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005357 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005358 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005359 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005360 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07005361 }
5362 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005363
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005364 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08005365 final long elapsedRealtime = mClocks.elapsedRealtime();
5366 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07005367 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07005368 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005369 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
5370 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005371 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005372 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005373 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005374 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07005375 }
5376 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005377
5378 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005379 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005380 final long elapsedRealtime = mClocks.elapsedRealtime();
5381 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005382 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005383 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005384 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
5385 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005386 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005387 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005388 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005389 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005390 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005391 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005392
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005393 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005394 if (mAudioOnNesting == 0) {
5395 return;
5396 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005397 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005398 final long elapsedRealtime = mClocks.elapsedRealtime();
5399 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005400 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005401 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005402 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
5403 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005404 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005405 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005406 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005407 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005408 }
5409
5410 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005411 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005412 final long elapsedRealtime = mClocks.elapsedRealtime();
5413 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005414 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005415 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005416 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
5417 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005418 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005419 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005420 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005421 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005422 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005423 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005424
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005425 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005426 if (mVideoOnNesting == 0) {
5427 return;
5428 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005429 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005430 final long elapsedRealtime = mClocks.elapsedRealtime();
5431 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005432 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005433 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005434 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
5435 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005436 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005437 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005438 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005439 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005440 }
5441
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005442 public void noteResetAudioLocked() {
5443 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005444 final long elapsedRealtime = mClocks.elapsedRealtime();
5445 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005446 mAudioOnNesting = 0;
5447 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
5448 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
5449 + Integer.toHexString(mHistoryCur.states));
5450 addHistoryRecordLocked(elapsedRealtime, uptime);
5451 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
5452 for (int i=0; i<mUidStats.size(); i++) {
5453 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5454 uid.noteResetAudioLocked(elapsedRealtime);
5455 }
5456 }
5457 }
5458
5459 public void noteResetVideoLocked() {
5460 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005461 final long elapsedRealtime = mClocks.elapsedRealtime();
5462 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005463 mAudioOnNesting = 0;
5464 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
5465 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
5466 + Integer.toHexString(mHistoryCur.states));
5467 addHistoryRecordLocked(elapsedRealtime, uptime);
5468 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
5469 for (int i=0; i<mUidStats.size(); i++) {
5470 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5471 uid.noteResetVideoLocked(elapsedRealtime);
5472 }
5473 }
5474 }
5475
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005476 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005477 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005478 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005479 }
5480
5481 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005482 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005483 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005484 }
5485
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005486 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005487 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005488 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
5489 }
5490
5491 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005492 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005493 getUidStatsLocked(uid).noteVibratorOffLocked();
5494 }
5495
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005496 public void noteFlashlightOnLocked(int uid) {
5497 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005498 final long elapsedRealtime = mClocks.elapsedRealtime();
5499 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005500 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005501 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
5502 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005503 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005504 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005505 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
5506 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005507 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
5508 }
5509
5510 public void noteFlashlightOffLocked(int uid) {
5511 if (mFlashlightOnNesting == 0) {
5512 return;
5513 }
5514 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005515 final long elapsedRealtime = mClocks.elapsedRealtime();
5516 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005517 if (--mFlashlightOnNesting == 0) {
5518 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
5519 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
5520 + Integer.toHexString(mHistoryCur.states2));
5521 addHistoryRecordLocked(elapsedRealtime, uptime);
5522 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
5523 }
5524 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
5525 }
5526
5527 public void noteCameraOnLocked(int uid) {
5528 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005529 final long elapsedRealtime = mClocks.elapsedRealtime();
5530 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005531 if (mCameraOnNesting++ == 0) {
5532 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
5533 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
5534 + Integer.toHexString(mHistoryCur.states2));
5535 addHistoryRecordLocked(elapsedRealtime, uptime);
5536 mCameraOnTimer.startRunningLocked(elapsedRealtime);
5537 }
5538 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
5539 }
5540
5541 public void noteCameraOffLocked(int uid) {
5542 if (mCameraOnNesting == 0) {
5543 return;
5544 }
5545 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005546 final long elapsedRealtime = mClocks.elapsedRealtime();
5547 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005548 if (--mCameraOnNesting == 0) {
5549 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
5550 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
5551 + Integer.toHexString(mHistoryCur.states2));
5552 addHistoryRecordLocked(elapsedRealtime, uptime);
5553 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
5554 }
5555 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
5556 }
5557
5558 public void noteResetCameraLocked() {
5559 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005560 final long elapsedRealtime = mClocks.elapsedRealtime();
5561 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005562 mCameraOnNesting = 0;
5563 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
5564 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
5565 + Integer.toHexString(mHistoryCur.states2));
5566 addHistoryRecordLocked(elapsedRealtime, uptime);
5567 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
5568 for (int i=0; i<mUidStats.size(); i++) {
5569 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5570 uid.noteResetCameraLocked(elapsedRealtime);
5571 }
5572 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005573 }
5574
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005575 public void noteResetFlashlightLocked() {
5576 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005577 final long elapsedRealtime = mClocks.elapsedRealtime();
5578 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005579 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005580 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
5581 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005582 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005583 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005584 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
5585 for (int i=0; i<mUidStats.size(); i++) {
5586 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5587 uid.noteResetFlashlightLocked(elapsedRealtime);
5588 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005589 }
5590 }
5591
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005592 private void noteBluetoothScanStartedLocked(WorkChain workChain, int uid,
5593 boolean isUnoptimized) {
5594 uid = getAttributionUid(uid, workChain);
Bookatz867c0d72017-03-07 18:23:42 -08005595 final long elapsedRealtime = mClocks.elapsedRealtime();
5596 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005597 if (mBluetoothScanNesting == 0) {
5598 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5599 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
5600 + Integer.toHexString(mHistoryCur.states2));
5601 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07005602 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005603 }
5604 mBluetoothScanNesting++;
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005605
Yangster-macafad8c62018-01-05 22:30:49 -08005606 if (workChain != null) {
5607 StatsLog.write(StatsLog.BLE_SCAN_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08005608 workChain.getUids(), workChain.getTags(),
5609 StatsLog.BLE_SCAN_STATE_CHANGED__STATE__ON);
Yangster-macafad8c62018-01-05 22:30:49 -08005610 if (isUnoptimized) {
5611 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08005612 workChain.getUids(), workChain.getTags(),
5613 StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED__STATE__ON);
Yangster-macafad8c62018-01-05 22:30:49 -08005614 }
5615 } else {
Bookatz90867622018-01-31 15:05:57 -08005616 StatsLog.write_non_chained(StatsLog.BLE_SCAN_STATE_CHANGED, uid, null,
5617 StatsLog.BLE_SCAN_STATE_CHANGED__STATE__ON);
Yangster-macafad8c62018-01-05 22:30:49 -08005618 if (isUnoptimized) {
Yangster-mac2f5daec2018-01-16 10:23:15 -08005619 StatsLog.write_non_chained(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED, uid, null,
Bookatz90867622018-01-31 15:05:57 -08005620 StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED__STATE__ON);
Yangster-macafad8c62018-01-05 22:30:49 -08005621 }
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005622 }
5623
Bookatzb1f04f32017-05-19 13:57:32 -07005624 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005625 if (workChain != null) {
5626 getUidStatsLocked(uid).addBluetoothWorkChain(workChain, isUnoptimized);
5627 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005628 }
5629
Bookatzb1f04f32017-05-19 13:57:32 -07005630 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005631 final int N = ws.size();
5632 for (int i = 0; i < N; i++) {
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005633 noteBluetoothScanStartedLocked(null, ws.get(i), isUnoptimized);
5634 }
5635
5636 final List<WorkChain> workChains = ws.getWorkChains();
5637 if (workChains != null) {
5638 for (int i = 0; i < workChains.size(); ++i) {
5639 noteBluetoothScanStartedLocked(workChains.get(i), -1, isUnoptimized);
5640 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005641 }
5642 }
5643
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005644 private void noteBluetoothScanStoppedLocked(WorkChain workChain, int uid,
5645 boolean isUnoptimized) {
5646 uid = getAttributionUid(uid, workChain);
Bookatz867c0d72017-03-07 18:23:42 -08005647 final long elapsedRealtime = mClocks.elapsedRealtime();
5648 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005649 mBluetoothScanNesting--;
5650 if (mBluetoothScanNesting == 0) {
5651 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5652 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
5653 + Integer.toHexString(mHistoryCur.states2));
5654 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07005655 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005656 }
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005657
Yangster-macafad8c62018-01-05 22:30:49 -08005658 if (workChain != null) {
5659 StatsLog.write(
Bookatz90867622018-01-31 15:05:57 -08005660 StatsLog.BLE_SCAN_STATE_CHANGED, workChain.getUids(), workChain.getTags(),
5661 StatsLog.BLE_SCAN_STATE_CHANGED__STATE__OFF);
Yangster-macafad8c62018-01-05 22:30:49 -08005662 if (isUnoptimized) {
5663 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08005664 workChain.getUids(), workChain.getTags(),
5665 StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED__STATE__OFF);
Yangster-macafad8c62018-01-05 22:30:49 -08005666 }
5667 } else {
Bookatz90867622018-01-31 15:05:57 -08005668 StatsLog.write_non_chained(StatsLog.BLE_SCAN_STATE_CHANGED, uid, null,
5669 StatsLog.BLE_SCAN_STATE_CHANGED__STATE__OFF);
Yangster-macafad8c62018-01-05 22:30:49 -08005670 if (isUnoptimized) {
Yangster-mac2f5daec2018-01-16 10:23:15 -08005671 StatsLog.write_non_chained(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED, uid, null,
Bookatz90867622018-01-31 15:05:57 -08005672 StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED__STATE__OFF);
Yangster-macafad8c62018-01-05 22:30:49 -08005673 }
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005674 }
5675
Bookatz94c5a312017-07-11 16:49:17 -07005676 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005677 if (workChain != null) {
5678 getUidStatsLocked(uid).removeBluetoothWorkChain(workChain, isUnoptimized);
5679 }
5680 }
5681
5682 private int getAttributionUid(int uid, WorkChain workChain) {
5683 if (workChain != null) {
5684 return mapUid(workChain.getAttributionUid());
5685 }
5686
5687 return mapUid(uid);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005688 }
5689
Bookatz94c5a312017-07-11 16:49:17 -07005690 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005691 final int N = ws.size();
5692 for (int i = 0; i < N; i++) {
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005693 noteBluetoothScanStoppedLocked(null, ws.get(i), isUnoptimized);
5694 }
5695
5696 final List<WorkChain> workChains = ws.getWorkChains();
5697 if (workChains != null) {
5698 for (int i = 0; i < workChains.size(); ++i) {
5699 noteBluetoothScanStoppedLocked(workChains.get(i), -1, isUnoptimized);
5700 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005701 }
5702 }
5703
5704 public void noteResetBluetoothScanLocked() {
5705 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005706 final long elapsedRealtime = mClocks.elapsedRealtime();
5707 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005708 mBluetoothScanNesting = 0;
5709 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5710 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
5711 + Integer.toHexString(mHistoryCur.states2));
5712 addHistoryRecordLocked(elapsedRealtime, uptime);
5713 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005714
5715
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005716 for (int i=0; i<mUidStats.size(); i++) {
5717 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5718 uid.noteResetBluetoothScanLocked(elapsedRealtime);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005719
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005720 List<WorkChain> allWorkChains = uid.getAllBluetoothWorkChains();
5721 if (allWorkChains != null) {
5722 for (int j = 0; j < allWorkChains.size(); ++j) {
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005723 StatsLog.write(StatsLog.BLE_SCAN_STATE_CHANGED,
Yangster-macafad8c62018-01-05 22:30:49 -08005724 allWorkChains.get(j).getUids(),
Bookatz90867622018-01-31 15:05:57 -08005725 allWorkChains.get(j).getTags(),
5726 StatsLog.BLE_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005727 }
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005728 allWorkChains.clear();
5729 }
5730
5731 List<WorkChain> unoptimizedWorkChains = uid.getUnoptimizedBluetoothWorkChains();
5732 if (unoptimizedWorkChains != null) {
5733 for (int j = 0; j < unoptimizedWorkChains.size(); ++j) {
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005734 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED,
Yangster-macafad8c62018-01-05 22:30:49 -08005735 unoptimizedWorkChains.get(j).getUids(),
Bookatz90867622018-01-31 15:05:57 -08005736 unoptimizedWorkChains.get(j).getTags(),
5737 StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005738 }
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005739 unoptimizedWorkChains.clear();
5740 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005741 }
5742 }
5743 }
5744
Bookatz4ebc0642017-05-11 12:21:19 -07005745 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07005746 final int N = ws.size();
5747 for (int i = 0; i < N; i++) {
5748 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07005749 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Yangster-mac2f5daec2018-01-16 10:23:15 -08005750 StatsLog.write_non_chained(StatsLog.BLE_SCAN_RESULT_RECEIVED, ws.get(i), ws.getName(i),
5751 numNewResults);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005752 }
5753
5754 final List<WorkChain> workChains = ws.getWorkChains();
5755 if (workChains != null) {
5756 for (int i = 0; i < workChains.size(); ++i) {
5757 final WorkChain wc = workChains.get(i);
5758 int uid = mapUid(wc.getAttributionUid());
5759 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Yangster-macafad8c62018-01-05 22:30:49 -08005760 StatsLog.write(StatsLog.BLE_SCAN_RESULT_RECEIVED,
5761 wc.getUids(), wc.getTags(), numNewResults);
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00005762 }
Bookatz956f36bf2017-04-28 09:48:17 -07005763 }
5764 }
5765
Adam Lesinski5f056f62016-07-14 16:56:08 -07005766 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
5767 final long uptimeMillis, int uid) {
5768 uid = mapUid(uid);
5769 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
5770 uid);
5771 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
5772 }
5773
5774 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005775 final long elapsedRealtime = mClocks.elapsedRealtime();
5776 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005777 if (mWifiRadioPowerState != powerState) {
5778 final boolean active =
5779 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
5780 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
5781 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07005782 if (uid > 0) {
5783 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
5784 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005785 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
Siddharth Rayb50a6842017-12-14 15:15:28 -08005786 mWifiActiveTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005787 } else {
5788 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
Siddharth Rayb50a6842017-12-14 15:15:28 -08005789 mWifiActiveTimer.stopRunningLocked(
5790 timestampNs / (1000 * 1000));
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005791 }
5792 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
5793 + Integer.toHexString(mHistoryCur.states));
5794 addHistoryRecordLocked(elapsedRealtime, uptime);
5795 mWifiRadioPowerState = powerState;
Bookatzddccf0a2017-11-28 16:48:14 -08005796 StatsLog.write(StatsLog.WIFI_RADIO_POWER_STATE_CHANGED, uid, powerState);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005797 }
5798 }
5799
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005800 public void noteWifiRunningLocked(WorkSource ws) {
5801 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005802 final long elapsedRealtime = mClocks.elapsedRealtime();
5803 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005804 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005805 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
5806 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005807 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005808 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005809 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005810 int N = ws.size();
5811 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005812 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005813 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005814 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00005815
5816 List<WorkChain> workChains = ws.getWorkChains();
5817 if (workChains != null) {
5818 for (int i = 0; i < workChains.size(); ++i) {
5819 int uid = mapUid(workChains.get(i).getAttributionUid());
5820 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
5821 }
5822 }
5823
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005824 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005825 } else {
5826 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005827 }
5828 }
5829
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005830 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
5831 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005832 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005833 int N = oldWs.size();
5834 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005835 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005836 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005837 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00005838
5839 List<WorkChain> workChains = oldWs.getWorkChains();
5840 if (workChains != null) {
5841 for (int i = 0; i < workChains.size(); ++i) {
5842 int uid = mapUid(workChains.get(i).getAttributionUid());
5843 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
5844 }
5845 }
5846
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005847 N = newWs.size();
5848 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005849 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005850 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005851 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00005852
5853 workChains = newWs.getWorkChains();
5854 if (workChains != null) {
5855 for (int i = 0; i < workChains.size(); ++i) {
5856 int uid = mapUid(workChains.get(i).getAttributionUid());
5857 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
5858 }
5859 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005860 } else {
5861 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
5862 }
5863 }
5864
5865 public void noteWifiStoppedLocked(WorkSource ws) {
5866 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005867 final long elapsedRealtime = mClocks.elapsedRealtime();
5868 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005869 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005870 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
5871 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005872 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005873 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005874 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005875 int N = ws.size();
5876 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005877 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005878 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005879 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00005880
5881 List<WorkChain> workChains = ws.getWorkChains();
5882 if (workChains != null) {
5883 for (int i = 0; i < workChains.size(); ++i) {
5884 int uid = mapUid(workChains.get(i).getAttributionUid());
5885 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
5886 }
5887 }
5888
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005889 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005890 } else {
5891 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005892 }
5893 }
5894
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005895 public void noteWifiStateLocked(int wifiState, String accessPoint) {
5896 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
5897 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005898 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005899 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005900 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005901 }
5902 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005903 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005904 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005905 }
5906 }
5907
Dianne Hackborn3251b902014-06-20 14:40:53 -07005908 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
5909 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5910 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005911 final long elapsedRealtime = mClocks.elapsedRealtime();
5912 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005913 if (mWifiSupplState >= 0) {
5914 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5915 }
5916 mWifiSupplState = supplState;
5917 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5918 mHistoryCur.states2 =
5919 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5920 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5921 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5922 + Integer.toHexString(mHistoryCur.states2));
5923 addHistoryRecordLocked(elapsedRealtime, uptime);
5924 }
5925 }
5926
5927 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005928 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005929 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5930 if (i == except) {
5931 continue;
5932 }
5933 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5934 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5935 }
5936 }
5937 }
5938
5939 public void noteWifiRssiChangedLocked(int newRssi) {
5940 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5941 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5942 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005943 final long elapsedRealtime = mClocks.elapsedRealtime();
5944 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005945 if (mWifiSignalStrengthBin >= 0) {
5946 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5947 elapsedRealtime);
5948 }
5949 if (strengthBin >= 0) {
5950 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5951 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Bookatze5885242017-10-24 20:10:31 -07005952 StatsLog.write(StatsLog.WIFI_SIGNAL_STRENGTH_CHANGED, strengthBin);
Dianne Hackborn3251b902014-06-20 14:40:53 -07005953 }
5954 mHistoryCur.states2 =
5955 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5956 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5957 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5958 + Integer.toHexString(mHistoryCur.states2));
5959 addHistoryRecordLocked(elapsedRealtime, uptime);
5960 } else {
5961 stopAllWifiSignalStrengthTimersLocked(-1);
5962 }
5963 mWifiSignalStrengthBin = strengthBin;
5964 }
5965 }
5966
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005967 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005968
The Android Open Source Project10592532009-03-18 17:39:46 -07005969 public void noteFullWifiLockAcquiredLocked(int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005970 final long elapsedRealtime = mClocks.elapsedRealtime();
5971 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005972 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005973 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005974 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5975 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005976 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005977 }
5978 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005979 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005980 }
5981
5982 public void noteFullWifiLockReleasedLocked(int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005983 final long elapsedRealtime = mClocks.elapsedRealtime();
5984 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005985 mWifiFullLockNesting--;
5986 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005987 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005988 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5989 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005990 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005991 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005992 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005993 }
5994
Nick Pelly6ccaa542012-06-15 15:22:47 -07005995 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005996
Nick Pelly6ccaa542012-06-15 15:22:47 -07005997 public void noteWifiScanStartedLocked(int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005998 final long elapsedRealtime = mClocks.elapsedRealtime();
5999 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07006000 if (mWifiScanNesting == 0) {
6001 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
6002 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006003 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006004 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006005 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006006 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006007 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006008 }
6009
Nick Pelly6ccaa542012-06-15 15:22:47 -07006010 public void noteWifiScanStoppedLocked(int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08006011 final long elapsedRealtime = mClocks.elapsedRealtime();
6012 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07006013 mWifiScanNesting--;
6014 if (mWifiScanNesting == 0) {
6015 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
6016 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006017 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006018 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006019 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006020 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006021 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006022
Robert Greenwalta029ea12013-09-25 16:38:12 -07006023 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006024 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08006025 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006026 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006027 }
6028
6029 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006030 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08006031 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006032 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006033 }
6034
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006035 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006036
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006037 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006038 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08006039 final long elapsedRealtime = mClocks.elapsedRealtime();
6040 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006041 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006042 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006043 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
6044 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006045 addHistoryRecordLocked(elapsedRealtime, uptime);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08006046
6047 // Start Wifi Multicast overall timer
6048 if (!mWifiMulticastWakelockTimer.isRunningLocked()) {
6049 if (DEBUG_HISTORY) Slog.v(TAG, "WiFi Multicast Overall Timer Started");
6050 mWifiMulticastWakelockTimer.startRunningLocked(elapsedRealtime);
6051 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006052 }
6053 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006054 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006055 }
6056
6057 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006058 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08006059 final long elapsedRealtime = mClocks.elapsedRealtime();
6060 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006061 mWifiMulticastNesting--;
6062 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006063 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006064 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
6065 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006066 addHistoryRecordLocked(elapsedRealtime, uptime);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08006067
6068 // Stop Wifi Multicast overall timer
6069 if (mWifiMulticastWakelockTimer.isRunningLocked()) {
6070 if (DEBUG_HISTORY) Slog.v(TAG, "Multicast Overall Timer Stopped");
6071 mWifiMulticastWakelockTimer.stopRunningLocked(elapsedRealtime);
6072 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006073 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006074 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006075 }
6076
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006077 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
6078 int N = ws.size();
6079 for (int i=0; i<N; i++) {
Narayan Kamath728c8a02017-12-28 15:48:07 +00006080 final int uid = mapUid(ws.get(i));
6081 noteFullWifiLockAcquiredLocked(uid);
Bookatz90867622018-01-31 15:05:57 -08006082 StatsLog.write_non_chained(StatsLog.WIFI_LOCK_STATE_CHANGED, ws.get(i), ws.getName(i),
6083 StatsLog.WIFI_LOCK_STATE_CHANGED__STATE__ON);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006084 }
6085
6086 final List<WorkChain> workChains = ws.getWorkChains();
6087 if (workChains != null) {
6088 for (int i = 0; i < workChains.size(); ++i) {
6089 final WorkChain workChain = workChains.get(i);
6090 final int uid = mapUid(workChain.getAttributionUid());
6091 noteFullWifiLockAcquiredLocked(uid);
Yangster-macafad8c62018-01-05 22:30:49 -08006092 StatsLog.write(StatsLog.WIFI_LOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08006093 workChain.getUids(), workChain.getTags(),
6094 StatsLog.WIFI_LOCK_STATE_CHANGED__STATE__ON);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006095 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006096 }
6097 }
6098
6099 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
6100 int N = ws.size();
6101 for (int i=0; i<N; i++) {
Narayan Kamath728c8a02017-12-28 15:48:07 +00006102 final int uid = mapUid(ws.get(i));
6103 noteFullWifiLockReleasedLocked(uid);
Bookatz90867622018-01-31 15:05:57 -08006104 StatsLog.write_non_chained(StatsLog.WIFI_LOCK_STATE_CHANGED, ws.get(i), ws.getName(i),
6105 StatsLog.WIFI_LOCK_STATE_CHANGED__STATE__OFF);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006106 }
6107
6108 final List<WorkChain> workChains = ws.getWorkChains();
6109 if (workChains != null) {
6110 for (int i = 0; i < workChains.size(); ++i) {
6111 final WorkChain workChain = workChains.get(i);
6112 final int uid = mapUid(workChain.getAttributionUid());
6113 noteFullWifiLockReleasedLocked(uid);
Yangster-macafad8c62018-01-05 22:30:49 -08006114 StatsLog.write(StatsLog.WIFI_LOCK_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08006115 workChain.getUids(), workChain.getTags(),
6116 StatsLog.WIFI_LOCK_STATE_CHANGED__STATE__OFF);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006117 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006118 }
6119 }
6120
Nick Pelly6ccaa542012-06-15 15:22:47 -07006121 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006122 int N = ws.size();
6123 for (int i=0; i<N; i++) {
Narayan Kamath728c8a02017-12-28 15:48:07 +00006124 final int uid = mapUid(ws.get(i));
6125 noteWifiScanStartedLocked(uid);
Yangster-mac2f5daec2018-01-16 10:23:15 -08006126 StatsLog.write_non_chained(StatsLog.WIFI_SCAN_STATE_CHANGED, ws.get(i), ws.getName(i),
Bookatz90867622018-01-31 15:05:57 -08006127 StatsLog.WIFI_SCAN_STATE_CHANGED__STATE__ON);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006128 }
6129
6130 final List<WorkChain> workChains = ws.getWorkChains();
6131 if (workChains != null) {
6132 for (int i = 0; i < workChains.size(); ++i) {
6133 final WorkChain workChain = workChains.get(i);
6134 final int uid = mapUid(workChain.getAttributionUid());
6135 noteWifiScanStartedLocked(uid);
Yangster-macafad8c62018-01-05 22:30:49 -08006136 StatsLog.write(StatsLog.WIFI_SCAN_STATE_CHANGED, workChain.getUids(),
Bookatz90867622018-01-31 15:05:57 -08006137 workChain.getTags(), StatsLog.WIFI_SCAN_STATE_CHANGED__STATE__ON);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006138 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006139 }
6140 }
6141
Nick Pelly6ccaa542012-06-15 15:22:47 -07006142 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006143 int N = ws.size();
6144 for (int i=0; i<N; i++) {
Narayan Kamath728c8a02017-12-28 15:48:07 +00006145 final int uid = mapUid(ws.get(i));
6146 noteWifiScanStoppedLocked(uid);
Yangster-mac2f5daec2018-01-16 10:23:15 -08006147 StatsLog.write_non_chained(StatsLog.WIFI_SCAN_STATE_CHANGED, ws.get(i), ws.getName(i),
Bookatz90867622018-01-31 15:05:57 -08006148 StatsLog.WIFI_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006149 }
6150
6151 final List<WorkChain> workChains = ws.getWorkChains();
6152 if (workChains != null) {
6153 for (int i = 0; i < workChains.size(); ++i) {
6154 final WorkChain workChain = workChains.get(i);
6155 final int uid = mapUid(workChain.getAttributionUid());
6156 noteWifiScanStoppedLocked(uid);
Yangster-macafad8c62018-01-05 22:30:49 -08006157 StatsLog.write(StatsLog.WIFI_SCAN_STATE_CHANGED,
Bookatz90867622018-01-31 15:05:57 -08006158 workChain.getUids(), workChain.getTags(),
6159 StatsLog.WIFI_SCAN_STATE_CHANGED__STATE__OFF);
Narayan Kamath728c8a02017-12-28 15:48:07 +00006160 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006161 }
6162 }
6163
Robert Greenwalta029ea12013-09-25 16:38:12 -07006164 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
6165 int N = ws.size();
6166 for (int i=0; i<N; i++) {
6167 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
6168 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00006169
6170 final List<WorkChain> workChains = ws.getWorkChains();
6171 if (workChains != null) {
6172 for (int i = 0; i < workChains.size(); ++i) {
6173 noteWifiBatchedScanStartedLocked(workChains.get(i).getAttributionUid(), csph);
6174 }
6175 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006176 }
6177
6178 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
6179 int N = ws.size();
6180 for (int i=0; i<N; i++) {
6181 noteWifiBatchedScanStoppedLocked(ws.get(i));
6182 }
Narayan Kamath728c8a02017-12-28 15:48:07 +00006183
6184 final List<WorkChain> workChains = ws.getWorkChains();
6185 if (workChains != null) {
6186 for (int i = 0; i < workChains.size(); ++i) {
6187 noteWifiBatchedScanStoppedLocked(workChains.get(i).getAttributionUid());
6188 }
6189 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006190 }
6191
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006192 private static String[] includeInStringArray(String[] array, String str) {
6193 if (ArrayUtils.indexOf(array, str) >= 0) {
6194 return array;
6195 }
6196 String[] newArray = new String[array.length+1];
6197 System.arraycopy(array, 0, newArray, 0, array.length);
6198 newArray[array.length] = str;
6199 return newArray;
6200 }
6201
6202 private static String[] excludeFromStringArray(String[] array, String str) {
6203 int index = ArrayUtils.indexOf(array, str);
6204 if (index >= 0) {
6205 String[] newArray = new String[array.length-1];
6206 if (index > 0) {
6207 System.arraycopy(array, 0, newArray, 0, index);
6208 }
6209 if (index < array.length-1) {
6210 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
6211 }
6212 return newArray;
6213 }
6214 return array;
6215 }
6216
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07006217 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07006218 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006219
Adam Lesinski14ae39a2017-05-26 11:50:40 -07006220 synchronized (mModemNetworkLock) {
6221 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
6222 mModemIfaces = includeInStringArray(mModemIfaces, iface);
6223 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
6224 } else {
6225 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
6226 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
6227 }
6228 }
6229
6230 synchronized (mWifiNetworkLock) {
6231 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
6232 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
6233 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
6234 } else {
6235 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
6236 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
6237 }
6238 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07006239 }
6240
David Chenc8a43242017-10-17 16:23:28 -07006241 public String[] getWifiIfaces() {
6242 synchronized (mWifiNetworkLock) {
6243 return mWifiIfaces;
6244 }
6245 }
6246
6247 public String[] getMobileIfaces() {
6248 synchronized (mModemNetworkLock) {
6249 return mModemIfaces;
6250 }
6251 }
6252
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006253 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
6254 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006256
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006257 @Override public int getScreenOnCount(int which) {
6258 return mScreenOnTimer.getCountLocked(which);
6259 }
6260
Mike Mac2f518a2017-09-19 16:06:03 -07006261 @Override public long getScreenDozeTime(long elapsedRealtimeUs, int which) {
6262 return mScreenDozeTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6263 }
6264
6265 @Override public int getScreenDozeCount(int which) {
6266 return mScreenDozeTimer.getCountLocked(which);
6267 }
6268
Dianne Hackborn617f8772009-03-31 15:04:46 -07006269 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006270 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07006271 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006272 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006273 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006274
Kweku Adams87b19ec2017-10-09 12:40:03 -07006275 @Override public Timer getScreenBrightnessTimer(int brightnessBin) {
6276 return mScreenBrightnessTimer[brightnessBin];
6277 }
6278
Jeff Browne95c3cd2014-05-02 16:59:26 -07006279 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
6280 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006281 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006282
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07006283 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
6284 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006285 }
6286
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07006287 @Override public int getPowerSaveModeEnabledCount(int which) {
6288 return mPowerSaveModeEnabledTimer.getCountLocked(which);
6289 }
6290
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006291 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
6292 int which) {
6293 switch (mode) {
6294 case DEVICE_IDLE_MODE_LIGHT:
6295 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07006296 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006297 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6298 }
6299 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07006300 }
6301
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006302 @Override public int getDeviceIdleModeCount(int mode, int which) {
6303 switch (mode) {
6304 case DEVICE_IDLE_MODE_LIGHT:
6305 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07006306 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006307 return mDeviceIdleModeFullTimer.getCountLocked(which);
6308 }
6309 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006310 }
6311
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006312 @Override public long getLongestDeviceIdleModeTime(int mode) {
6313 switch (mode) {
6314 case DEVICE_IDLE_MODE_LIGHT:
6315 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07006316 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006317 return mLongestFullIdleTime;
6318 }
6319 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006320 }
6321
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006322 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
6323 switch (mode) {
6324 case DEVICE_IDLE_MODE_LIGHT:
6325 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07006326 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006327 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6328 }
6329 return 0;
6330 }
6331
6332 @Override public int getDeviceIdlingCount(int mode, int which) {
6333 switch (mode) {
6334 case DEVICE_IDLE_MODE_LIGHT:
6335 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07006336 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07006337 return mDeviceIdlingTimer.getCountLocked(which);
6338 }
6339 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006340 }
6341
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006342 @Override public int getNumConnectivityChange(int which) {
6343 int val = mNumConnectivityChange;
6344 if (which == STATS_CURRENT) {
6345 val -= mLoadedNumConnectivityChange;
6346 } else if (which == STATS_SINCE_UNPLUGGED) {
6347 val -= mUnpluggedNumConnectivityChange;
6348 }
6349 return val;
6350 }
6351
Siddharth Ray78ccaf52017-12-23 16:16:21 -08006352 @Override public long getGpsSignalQualityTime(int strengthBin,
6353 long elapsedRealtimeUs, int which) {
6354 if (strengthBin < 0 || strengthBin >= GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS) {
6355 return 0;
6356 }
6357 return mGpsSignalQualityTimer[strengthBin].getTotalTimeLocked(
6358 elapsedRealtimeUs, which);
6359 }
6360
6361 @Override public long getGpsBatteryDrainMaMs() {
Siddharth Ray0ed2e952018-01-22 11:32:14 -08006362 final double opVolt = mPowerProfile.getAveragePower(
6363 PowerProfile.POWER_GPS_OPERATING_VOLTAGE) / 1000.0;
6364 if (opVolt == 0) {
6365 return 0;
6366 }
6367 double energyUsedMaMs = 0.0;
6368 final int which = STATS_SINCE_CHARGED;
6369 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
6370 for(int i=0; i < GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
6371 energyUsedMaMs
6372 += mPowerProfile.getAveragePower(PowerProfile.POWER_GPS_SIGNAL_QUALITY_BASED, i)
6373 * (getGpsSignalQualityTime(i, rawRealtime, which) / 1000);
6374 }
6375 return (long) energyUsedMaMs;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08006376 }
6377
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006378 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
6379 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006381
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006382 @Override public int getPhoneOnCount(int which) {
6383 return mPhoneOnTimer.getCountLocked(which);
6384 }
6385
Dianne Hackborn627bba72009-03-24 22:32:56 -07006386 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006387 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07006388 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006389 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006390 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07006391
6392 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006393 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07006394 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006395 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07006396 }
6397
Kweku Adams87b19ec2017-10-09 12:40:03 -07006398 @Override public Timer getPhoneSignalScanningTimer() {
6399 return mPhoneSignalScanningTimer;
6400 }
6401
Catherine Liufb900812012-07-17 14:12:56 -05006402 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
6403 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006404 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006405
Kweku Adams87b19ec2017-10-09 12:40:03 -07006406 @Override public Timer getPhoneSignalStrengthTimer(int strengthBin) {
6407 return mPhoneSignalStrengthsTimer[strengthBin];
6408 }
6409
Dianne Hackborn627bba72009-03-24 22:32:56 -07006410 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006411 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07006412 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006413 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006414 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006415
Dianne Hackborn617f8772009-03-31 15:04:46 -07006416 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07006417 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006418 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006419
Kweku Adams87b19ec2017-10-09 12:40:03 -07006420 @Override public Timer getPhoneDataConnectionTimer(int dataType) {
6421 return mPhoneDataConnectionsTimer[dataType];
6422 }
6423
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006424 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
6425 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006426 }
6427
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006428 @Override public int getMobileRadioActiveCount(int which) {
6429 return mMobileRadioActiveTimer.getCountLocked(which);
6430 }
6431
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006432 @Override public long getMobileRadioActiveAdjustedTime(int which) {
6433 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
6434 }
6435
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006436 @Override public long getMobileRadioActiveUnknownTime(int which) {
6437 return mMobileRadioActiveUnknownTime.getCountLocked(which);
6438 }
6439
6440 @Override public int getMobileRadioActiveUnknownCount(int which) {
6441 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
6442 }
6443
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08006444 @Override public long getWifiMulticastWakelockTime(
6445 long elapsedRealtimeUs, int which) {
6446 return mWifiMulticastWakelockTimer.getTotalTimeLocked(
6447 elapsedRealtimeUs, which);
6448 }
6449
6450 @Override public int getWifiMulticastWakelockCount(int which) {
6451 return mWifiMulticastWakelockTimer.getCountLocked(which);
6452 }
6453
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006454 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
6455 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006456 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006457
Siddharth Rayb50a6842017-12-14 15:15:28 -08006458 @Override public long getWifiActiveTime(long elapsedRealtimeUs, int which) {
6459 return mWifiActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6460 }
6461
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006462 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
6463 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07006464 }
6465
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006466 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006467 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006468 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006469 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006470 }
6471
6472 @Override public int getWifiStateCount(int wifiState, int which) {
6473 return mWifiStateTimer[wifiState].getCountLocked(which);
6474 }
6475
Kweku Adams87b19ec2017-10-09 12:40:03 -07006476 @Override public Timer getWifiStateTimer(int wifiState) {
6477 return mWifiStateTimer[wifiState];
6478 }
6479
Dianne Hackborn3251b902014-06-20 14:40:53 -07006480 @Override public long getWifiSupplStateTime(int state,
6481 long elapsedRealtimeUs, int which) {
6482 return mWifiSupplStateTimer[state].getTotalTimeLocked(
6483 elapsedRealtimeUs, which);
6484 }
6485
6486 @Override public int getWifiSupplStateCount(int state, int which) {
6487 return mWifiSupplStateTimer[state].getCountLocked(which);
6488 }
6489
Kweku Adams87b19ec2017-10-09 12:40:03 -07006490 @Override public Timer getWifiSupplStateTimer(int state) {
6491 return mWifiSupplStateTimer[state];
6492 }
6493
Dianne Hackborn3251b902014-06-20 14:40:53 -07006494 @Override public long getWifiSignalStrengthTime(int strengthBin,
6495 long elapsedRealtimeUs, int which) {
6496 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
6497 elapsedRealtimeUs, which);
6498 }
6499
6500 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
6501 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
6502 }
6503
Kweku Adams87b19ec2017-10-09 12:40:03 -07006504 @Override public Timer getWifiSignalStrengthTimer(int strengthBin) {
6505 return mWifiSignalStrengthsTimer[strengthBin];
6506 }
6507
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006508 @Override
6509 public ControllerActivityCounter getBluetoothControllerActivity() {
6510 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07006511 }
6512
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006513 @Override
6514 public ControllerActivityCounter getWifiControllerActivity() {
6515 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07006516 }
6517
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006518 @Override
6519 public ControllerActivityCounter getModemControllerActivity() {
6520 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07006521 }
6522
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006523 @Override
6524 public boolean hasBluetoothActivityReporting() {
6525 return mHasBluetoothReporting;
6526 }
6527
6528 @Override
6529 public boolean hasWifiActivityReporting() {
6530 return mHasWifiReporting;
6531 }
6532
6533 @Override
6534 public boolean hasModemActivityReporting() {
6535 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07006536 }
6537
Ruben Brunk5b1308f2015-06-03 18:49:27 -07006538 @Override
6539 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006540 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6541 }
6542
Ruben Brunk5b1308f2015-06-03 18:49:27 -07006543 @Override
6544 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006545 return mFlashlightOnTimer.getCountLocked(which);
6546 }
6547
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006548 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07006549 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
6550 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6551 }
6552
6553 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006554 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
6555 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
6556 }
6557
6558 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006559 public long getNetworkActivityBytes(int type, int which) {
6560 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
6561 return mNetworkByteActivityCounters[type].getCountLocked(which);
6562 } else {
6563 return 0;
6564 }
6565 }
6566
6567 @Override
6568 public long getNetworkActivityPackets(int type, int which) {
6569 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
6570 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006571 } else {
6572 return 0;
6573 }
6574 }
6575
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08006576 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07006577 final long currentTime = System.currentTimeMillis();
6578 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08006579 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
6580 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07006581 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08006582 return mStartClockTime;
6583 }
6584
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006585 @Override public String getStartPlatformVersion() {
6586 return mStartPlatformVersion;
6587 }
6588
6589 @Override public String getEndPlatformVersion() {
6590 return mEndPlatformVersion;
6591 }
6592
6593 @Override public int getParcelVersion() {
6594 return VERSION;
6595 }
6596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 @Override public boolean getIsOnBattery() {
6598 return mOnBattery;
6599 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
6602 return mUidStats;
6603 }
6604
Adam Lesinski5f056f62016-07-14 16:56:08 -07006605 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
6606 if (timer != null) {
6607 timer.detach();
6608 }
6609 }
6610
6611 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
6612 boolean detachIfReset) {
6613 if (timer != null) {
6614 return timer.reset(detachIfReset);
6615 }
6616 return true;
6617 }
6618
Bookatz867c0d72017-03-07 18:23:42 -08006619 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
6620 if (timer != null) {
6621 return timer.reset(detachIfReset);
6622 }
6623 return true;
6624 }
6625
Adam Lesinski5f056f62016-07-14 16:56:08 -07006626 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
6627 if (counter != null) {
6628 counter.detach();
6629 }
6630 }
6631
6632 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
6633 boolean detachIfReset) {
6634 if (counter != null) {
6635 counter.reset(detachIfReset);
6636 }
6637 }
6638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 /**
6640 * The statistics associated with a particular uid.
6641 */
Joe Onoratoabded112016-02-08 16:49:39 -08006642 public static class Uid extends BatteryStats.Uid {
6643 /**
6644 * BatteryStatsImpl that we are associated with.
6645 */
6646 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006649
Bookatz867c0d72017-03-07 18:23:42 -08006650 /** TimeBase for when uid is in background and device is on battery. */
6651 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6652 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07006653 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6654 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08006655
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006656 boolean mWifiRunning;
6657 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006658
The Android Open Source Project10592532009-03-18 17:39:46 -07006659 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07006660 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006661
Nick Pelly6ccaa542012-06-15 15:22:47 -07006662 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08006663 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006664
Dianne Hackborn61659e52014-07-09 16:13:01 -07006665 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07006666 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6667 StopwatchTimer[] mWifiBatchedScanTimer;
6668
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006669 boolean mWifiMulticastEnabled;
6670 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006671
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006672 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006673 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006674 StopwatchTimer mFlashlightTurnedOnTimer;
6675 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006676 StopwatchTimer mForegroundActivityTimer;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006677 StopwatchTimer mForegroundServiceTimer;
Bookatzc8c44962017-05-11 12:12:54 -07006678 /** Total time spent by the uid holding any partial wakelocks. */
6679 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006680 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07006681 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07006682 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07006683 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006684
Dianne Hackborna8d10942015-11-19 17:55:19 -08006685 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006686 StopwatchTimer[] mProcessStateTimer;
6687
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006688 boolean mInForegroundService = false;
6689
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006690 BatchTimer mVibratorOnTimer;
6691
Dianne Hackborn617f8772009-03-31 15:04:46 -07006692 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006693
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006694 LongSamplingCounter[] mNetworkByteActivityCounters;
6695 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006696 LongSamplingCounter mMobileRadioActiveTime;
6697 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07006700 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
6701 */
6702 private LongSamplingCounter mMobileRadioApWakeupCount;
6703
6704 /**
6705 * How many times this UID woke up the Application Processor due to a Wifi packet.
6706 */
6707 private LongSamplingCounter mWifiRadioApWakeupCount;
6708
6709 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07006710 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006711 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07006712 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006713 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07006714
6715 /**
6716 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006717 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07006718 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006719 private ControllerActivityCounterImpl mBluetoothControllerActivity;
6720
6721 /**
6722 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
6723 * Can be null if the UID has had no such activity.
6724 */
6725 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07006726
6727 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006728 * The CPU times we had at the last history details update.
6729 */
6730 long mLastStepUserTime;
6731 long mLastStepSystemTime;
6732 long mCurStepUserTime;
6733 long mCurStepSystemTime;
6734
Joe Onoratoabded112016-02-08 16:49:39 -08006735 LongSamplingCounter mUserCpuTime;
6736 LongSamplingCounter mSystemCpuTime;
Sudheer Shankaaf857412017-07-21 00:14:24 -07006737 LongSamplingCounter[][] mCpuClusterSpeedTimesUs;
Mike Ma3d422c32017-10-25 11:08:57 -07006738 LongSamplingCounter mCpuActiveTimeMs;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006739
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006740 LongSamplingCounterArray mCpuFreqTimeMs;
6741 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
Mike Ma3d422c32017-10-25 11:08:57 -07006742 LongSamplingCounterArray mCpuClusterTimesMs;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006743
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006744 LongSamplingCounterArray[] mProcStateTimeMs;
6745 LongSamplingCounterArray[] mProcStateScreenOffTimeMs;
6746
6747 IntArray mChildUids;
6748
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 * The statistics we have collected for this uid's wake locks.
6751 */
Joe Onoratoabded112016-02-08 16:49:39 -08006752 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753
6754 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006755 * The statistics we have collected for this uid's syncs.
6756 */
Bookatz2bffb5b2017-04-13 11:59:33 -07006757 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006758
6759 /**
6760 * The statistics we have collected for this uid's jobs.
6761 */
Bookatzaa4594a2017-03-24 12:39:56 -07006762 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006763
6764 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006765 * Count of the jobs that have completed and the reasons why they completed.
6766 */
6767 final ArrayMap<String, SparseIntArray> mJobCompletions = new ArrayMap<>();
6768
6769 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -08006770 * Count of app launch events that had associated deferred job counts or info about
6771 * last time a job was run.
6772 */
6773 Counter mJobsDeferredEventCount;
6774
6775 /**
6776 * Count of deferred jobs that were pending when the app was launched or brought to
6777 * the foreground through a user interaction.
6778 */
6779 Counter mJobsDeferredCount;
6780
6781 /**
6782 * Sum of time since the last time a job was run for this app before it was launched.
6783 */
6784 LongSamplingCounter mJobsFreshnessTimeMs;
6785
6786 /**
6787 * Array of counts of instances where the time since the last job was run for the app
6788 * fell within one of the thresholds in {@link #JOB_FRESHNESS_BUCKETS}.
6789 */
6790 final Counter[] mJobsFreshnessBuckets;
6791
6792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 * The statistics we have collected for this uid's sensor activations.
6794 */
Adam Lesinskie08af192015-03-25 16:42:59 -07006795 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796
6797 /**
6798 * The statistics we have collected for this uid's processes.
6799 */
Adam Lesinskie08af192015-03-25 16:42:59 -07006800 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801
6802 /**
6803 * The statistics we have collected for this uid's processes.
6804 */
Adam Lesinskie08af192015-03-25 16:42:59 -07006805 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006806
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006807 /**
6808 * The transient wake stats we have collected for this uid's pids.
6809 */
Adam Lesinskie08af192015-03-25 16:42:59 -07006810 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006811
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00006812 /**
6813 * The list of WorkChains associated with active bluetooth scans.
6814 *
6815 * NOTE: This is a hack and it only needs to exist because there's a "reset" API that is
6816 * supposed to stop and log all WorkChains that were currently active.
6817 */
6818 ArrayList<WorkChain> mAllBluetoothChains = null;
6819 ArrayList<WorkChain> mUnoptimizedBluetoothChains = null;
6820
Joe Onoratoabded112016-02-08 16:49:39 -08006821 public Uid(BatteryStatsImpl bsi, int uid) {
6822 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08006824
Bookatz867c0d72017-03-07 18:23:42 -08006825 mOnBatteryBackgroundTimeBase = new TimeBase();
6826 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
6827 mBsi.mClocks.elapsedRealtime() * 1000);
6828
Bookatzc8c44962017-05-11 12:12:54 -07006829 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
6830 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
6831 mBsi.mClocks.elapsedRealtime() * 1000);
6832
Joe Onoratoabded112016-02-08 16:49:39 -08006833 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6834 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Mike Ma3d422c32017-10-25 11:08:57 -07006835 mCpuActiveTimeMs = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6836 mCpuClusterTimesMs = new LongSamplingCounterArray(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08006837
Dianne Hackborn657153b2016-07-29 14:54:14 -07006838 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08006839 @Override public Wakelock instantiateObject() {
6840 return new Wakelock(mBsi, Uid.this);
6841 }
6842 };
Bookatz2bffb5b2017-04-13 11:59:33 -07006843 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
6844 @Override public DualTimer instantiateObject() {
6845 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
6846 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08006847 }
6848 };
Bookatzaa4594a2017-03-24 12:39:56 -07006849 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
6850 @Override public DualTimer instantiateObject() {
6851 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
6852 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08006853 }
6854 };
6855
6856 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
6857 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
6858 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
6859 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08006860 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
6861 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006862 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08006863 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
6864 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006865 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
Amith Yamasani977e11f2018-02-16 11:29:54 -08006866 mJobsDeferredEventCount = new Counter(mBsi.mOnBatteryTimeBase);
6867 mJobsDeferredCount = new Counter(mBsi.mOnBatteryTimeBase);
6868 mJobsFreshnessTimeMs = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6869 mJobsFreshnessBuckets = new Counter[JOB_FRESHNESS_BUCKETS.length];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 }
6871
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006872 @VisibleForTesting
6873 public void setProcessStateForTest(int procState) {
6874 mProcessState = procState;
6875 }
6876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006878 public long[] getCpuFreqTimes(int which) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006879 return nullIfAllZeros(mCpuFreqTimeMs, which);
6880 }
6881
6882 @Override
6883 public long[] getScreenOffCpuFreqTimes(int which) {
6884 return nullIfAllZeros(mScreenOffCpuFreqTimeMs, which);
6885 }
6886
6887 @Override
Mike Ma3d422c32017-10-25 11:08:57 -07006888 public long getCpuActiveTime() {
6889 return mCpuActiveTimeMs.getCountLocked(STATS_SINCE_CHARGED);
6890 }
6891
6892 @Override
6893 public long[] getCpuClusterTimes() {
6894 return nullIfAllZeros(mCpuClusterTimesMs, STATS_SINCE_CHARGED);
6895 }
6896
6897
6898 @Override
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006899 public long[] getCpuFreqTimes(int which, int procState) {
6900 if (which < 0 || which >= NUM_PROCESS_STATE) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006901 return null;
6902 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006903 if (mProcStateTimeMs == null) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006904 return null;
6905 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006906 if (!mBsi.mPerProcStateCpuTimesAvailable) {
6907 mProcStateTimeMs = null;
6908 return null;
6909 }
6910 return nullIfAllZeros(mProcStateTimeMs[procState], which);
6911 }
6912
6913 @Override
6914 public long[] getScreenOffCpuFreqTimes(int which, int procState) {
6915 if (which < 0 || which >= NUM_PROCESS_STATE) {
6916 return null;
6917 }
6918 if (mProcStateScreenOffTimeMs == null) {
6919 return null;
6920 }
6921 if (!mBsi.mPerProcStateCpuTimesAvailable) {
6922 mProcStateScreenOffTimeMs = null;
6923 return null;
6924 }
6925 return nullIfAllZeros(mProcStateScreenOffTimeMs[procState], which);
6926 }
6927
6928 public void addIsolatedUid(int isolatedUid) {
6929 if (mChildUids == null) {
6930 mChildUids = new IntArray();
6931 } else if (mChildUids.indexOf(isolatedUid) >= 0) {
6932 return;
6933 }
6934 mChildUids.add(isolatedUid);
6935 }
6936
6937 public void removeIsolatedUid(int isolatedUid) {
6938 final int idx = mChildUids == null ? -1 : mChildUids.indexOf(isolatedUid);
6939 if (idx < 0) {
6940 return;
6941 }
6942 mChildUids.remove(idx);
6943 }
6944
6945 private long[] nullIfAllZeros(LongSamplingCounterArray cpuTimesMs, int which) {
6946 if (cpuTimesMs == null) {
6947 return null;
6948 }
6949 final long[] counts = cpuTimesMs.getCountsLocked(which);
6950 if (counts == null) {
6951 return null;
6952 }
6953 // Return counts only if at least one of the elements is non-zero.
6954 for (int i = counts.length - 1; i >= 0; --i) {
6955 if (counts[i] != 0) {
6956 return counts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006957 }
6958 }
6959 return null;
6960 }
6961
Sudheer Shankae544d162017-12-28 17:06:20 -08006962 private void addProcStateTimesMs(int procState, long[] cpuTimesMs, boolean onBattery) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006963 if (mProcStateTimeMs == null) {
6964 mProcStateTimeMs = new LongSamplingCounterArray[NUM_PROCESS_STATE];
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006965 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006966 if (mProcStateTimeMs[procState] == null
6967 || mProcStateTimeMs[procState].getSize() != cpuTimesMs.length) {
6968 mProcStateTimeMs[procState] = new LongSamplingCounterArray(
6969 mBsi.mOnBatteryTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006970 }
Sudheer Shankae544d162017-12-28 17:06:20 -08006971 mProcStateTimeMs[procState].addCountLocked(cpuTimesMs, onBattery);
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006972 }
6973
Sudheer Shankae544d162017-12-28 17:06:20 -08006974 private void addProcStateScreenOffTimesMs(int procState, long[] cpuTimesMs,
6975 boolean onBatteryScreenOff) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006976 if (mProcStateScreenOffTimeMs == null) {
6977 mProcStateScreenOffTimeMs = new LongSamplingCounterArray[NUM_PROCESS_STATE];
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006978 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006979 if (mProcStateScreenOffTimeMs[procState] == null
6980 || mProcStateScreenOffTimeMs[procState].getSize() != cpuTimesMs.length) {
6981 mProcStateScreenOffTimeMs[procState] = new LongSamplingCounterArray(
6982 mBsi.mOnBatteryScreenOffTimeBase);
6983 }
Sudheer Shankae544d162017-12-28 17:06:20 -08006984 mProcStateScreenOffTimeMs[procState].addCountLocked(cpuTimesMs, onBatteryScreenOff);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006985 }
6986
6987 @Override
Bookatzc8c44962017-05-11 12:12:54 -07006988 public Timer getAggregatedPartialWakelockTimer() {
6989 return mAggregatedPartialWakelockTimer;
6990 }
6991
6992 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006993 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006994 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 }
6996
6997 @Override
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07006998 public Timer getMulticastWakelockStats() {
6999 return mWifiMulticastTimer;
7000 }
7001
7002 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007003 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007004 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007005 }
7006
7007 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007008 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007009 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007010 }
7011
7012 @Override
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007013 public ArrayMap<String, SparseIntArray> getJobCompletionStats() {
7014 return mJobCompletions;
7015 }
7016
7017 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07007018 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 return mSensorStats;
7020 }
7021
7022 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007023 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 return mProcessStats;
7025 }
7026
7027 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007028 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 return mPackageStats;
7030 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007031
7032 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 public int getUid() {
7034 return mUid;
7035 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007036
7037 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007038 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007039 if (!mWifiRunning) {
7040 mWifiRunning = true;
7041 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007042 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7043 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007044 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007045 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007046 }
7047 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007048
Dianne Hackborn617f8772009-03-31 15:04:46 -07007049 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007050 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007051 if (mWifiRunning) {
7052 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007053 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007054 }
7055 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007056
Dianne Hackborn617f8772009-03-31 15:04:46 -07007057 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007058 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07007059 if (!mFullWifiLockOut) {
7060 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007061 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007062 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7063 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007064 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007065 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07007066 }
7067 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007068
The Android Open Source Project10592532009-03-18 17:39:46 -07007069 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007070 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07007071 if (mFullWifiLockOut) {
7072 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007073 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07007074 }
7075 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007076
The Android Open Source Project10592532009-03-18 17:39:46 -07007077 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007078 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007079 if (!mWifiScanStarted) {
7080 mWifiScanStarted = true;
7081 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08007082 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7083 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
7084 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007085 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007086 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07007087 }
7088 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007089
The Android Open Source Project10592532009-03-18 17:39:46 -07007090 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007091 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007092 if (mWifiScanStarted) {
7093 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007094 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07007095 }
7096 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007097
7098 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007099 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07007100 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07007101 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07007102 csph = csph >> 3;
7103 bin++;
7104 }
7105
7106 if (mWifiBatchedScanBinStarted == bin) return;
7107
7108 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
7109 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007110 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007111 }
7112 mWifiBatchedScanBinStarted = bin;
7113 if (mWifiBatchedScanTimer[bin] == null) {
7114 makeWifiBatchedScanBin(bin, null);
7115 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007116 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007117 }
7118
7119 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007120 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07007121 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
7122 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007123 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007124 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7125 }
7126 }
7127
7128 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007129 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007130 if (!mWifiMulticastEnabled) {
7131 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007132 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007133 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7134 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007135 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007136 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Tej Singh4503e102018-01-04 14:35:01 -08007137 StatsLog.write_non_chained(
Bookatz90867622018-01-31 15:05:57 -08007138 StatsLog.WIFI_MULTICAST_LOCK_STATE_CHANGED, getUid(), null,
7139 StatsLog.WIFI_MULTICAST_LOCK_STATE_CHANGED__STATE__ON);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007140 }
7141 }
7142
7143 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007144 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007145 if (mWifiMulticastEnabled) {
7146 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007147 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Tej Singh4503e102018-01-04 14:35:01 -08007148 StatsLog.write_non_chained(
Bookatz90867622018-01-31 15:05:57 -08007149 StatsLog.WIFI_MULTICAST_LOCK_STATE_CHANGED, getUid(), null,
7150 StatsLog.WIFI_MULTICAST_LOCK_STATE_CHANGED__STATE__OFF);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007151 }
7152 }
7153
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007154 @Override
7155 public ControllerActivityCounter getWifiControllerActivity() {
7156 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07007157 }
7158
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007159 @Override
7160 public ControllerActivityCounter getBluetoothControllerActivity() {
7161 return mBluetoothControllerActivity;
7162 }
7163
7164 @Override
7165 public ControllerActivityCounter getModemControllerActivity() {
7166 return mModemControllerActivity;
7167 }
7168
7169 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
7170 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007171 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007172 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08007173 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007174 return mWifiControllerActivity;
7175 }
7176
7177 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
7178 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007179 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007180 NUM_BT_TX_LEVELS);
7181 }
7182 return mBluetoothControllerActivity;
7183 }
7184
7185 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
7186 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007187 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007188 ModemActivityInfo.TX_POWER_LEVELS);
7189 }
7190 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08007191 }
7192
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007193 public StopwatchTimer createAudioTurnedOnTimerLocked() {
7194 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007195 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7196 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007197 }
7198 return mAudioTurnedOnTimer;
7199 }
7200
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007201 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007202 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007203 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, getUid(), null,
7204 StatsLog.AUDIO_STATE_CHANGED__STATE__ON);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007205 }
7206
7207 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
7208 if (mAudioTurnedOnTimer != null) {
7209 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07007210 if (!mAudioTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
Bookatz90867622018-01-31 15:05:57 -08007211 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, getUid(), null,
7212 StatsLog.AUDIO_STATE_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -07007213 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007214 }
7215 }
7216
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007217 public void noteResetAudioLocked(long elapsedRealtimeMs) {
7218 if (mAudioTurnedOnTimer != null) {
7219 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007220 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, getUid(), null,
7221 StatsLog.AUDIO_STATE_CHANGED__STATE__OFF);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007222 }
7223 }
7224
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007225 public StopwatchTimer createVideoTurnedOnTimerLocked() {
7226 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007227 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7228 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007229 }
7230 return mVideoTurnedOnTimer;
7231 }
7232
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007233 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007234 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007235 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(), null,
7236 StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED__STATE__ON);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007237 }
7238
7239 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
7240 if (mVideoTurnedOnTimer != null) {
7241 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07007242 if (!mVideoTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
Yangster-mac2f5daec2018-01-16 10:23:15 -08007243 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(),
Bookatz90867622018-01-31 15:05:57 -08007244 null, StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -07007245 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007246 }
7247 }
7248
Dianne Hackborn10eaa852014-07-22 22:54:55 -07007249 public void noteResetVideoLocked(long elapsedRealtimeMs) {
7250 if (mVideoTurnedOnTimer != null) {
7251 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Yangster-mac2f5daec2018-01-16 10:23:15 -08007252 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(), null,
Bookatz90867622018-01-31 15:05:57 -08007253 StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED__STATE__OFF);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007254 }
7255 }
7256
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007257 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
7258 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007259 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7260 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007261 }
7262 return mFlashlightTurnedOnTimer;
7263 }
7264
7265 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
7266 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007267 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), null,
7268 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__ON);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007269 }
7270
7271 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
7272 if (mFlashlightTurnedOnTimer != null) {
7273 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07007274 if (!mFlashlightTurnedOnTimer.isRunningLocked()) {
Yangster-mac2f5daec2018-01-16 10:23:15 -08007275 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), null,
Bookatz90867622018-01-31 15:05:57 -08007276 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -07007277 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007278 }
7279 }
7280
7281 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
7282 if (mFlashlightTurnedOnTimer != null) {
7283 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007284 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), null,
7285 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__OFF);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007286 }
7287 }
7288
7289 public StopwatchTimer createCameraTurnedOnTimerLocked() {
7290 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007291 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7292 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007293 }
7294 return mCameraTurnedOnTimer;
7295 }
7296
7297 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
7298 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007299 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, getUid(), null,
7300 StatsLog.CAMERA_STATE_CHANGED__STATE__ON);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007301 }
7302
7303 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
7304 if (mCameraTurnedOnTimer != null) {
7305 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07007306 if (!mCameraTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
Bookatz90867622018-01-31 15:05:57 -08007307 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, getUid(), null,
7308 StatsLog.CAMERA_STATE_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -07007309 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007310 }
7311 }
7312
7313 public void noteResetCameraLocked(long elapsedRealtimeMs) {
7314 if (mCameraTurnedOnTimer != null) {
7315 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08007316 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, getUid(), null,
7317 StatsLog.CAMERA_STATE_CHANGED__STATE__OFF);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007318 }
7319 }
7320
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007321 public StopwatchTimer createForegroundActivityTimerLocked() {
7322 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007323 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7324 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007325 }
7326 return mForegroundActivityTimer;
7327 }
7328
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007329 public StopwatchTimer createForegroundServiceTimerLocked() {
7330 if (mForegroundServiceTimer == null) {
7331 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7332 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase);
7333 }
7334 return mForegroundServiceTimer;
7335 }
7336
Bookatzc8c44962017-05-11 12:12:54 -07007337 public DualTimer createAggregatedPartialWakelockTimerLocked() {
7338 if (mAggregatedPartialWakelockTimer == null) {
7339 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7340 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7341 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
7342 }
7343 return mAggregatedPartialWakelockTimer;
7344 }
7345
Bookatz867c0d72017-03-07 18:23:42 -08007346 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007347 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08007348 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7349 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7350 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007351 }
7352 return mBluetoothScanTimer;
7353 }
7354
Bookatzb1f04f32017-05-19 13:57:32 -07007355 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
7356 if (mBluetoothUnoptimizedScanTimer == null) {
7357 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7358 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7359 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
7360 }
7361 return mBluetoothUnoptimizedScanTimer;
7362 }
7363
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00007364 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs,
7365 boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007366 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07007367 if (isUnoptimized) {
7368 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
7369 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007370 }
7371
Bookatz94c5a312017-07-11 16:49:17 -07007372 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007373 if (mBluetoothScanTimer != null) {
7374 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
7375 }
Bookatz94c5a312017-07-11 16:49:17 -07007376 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07007377 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
7378 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007379 }
7380
Narayan Kamath0ebc96c2017-12-29 13:32:38 +00007381 public void addBluetoothWorkChain(WorkChain workChain, boolean isUnoptimized) {
7382 if (mAllBluetoothChains == null) {
7383 mAllBluetoothChains = new ArrayList<WorkChain>(4);
7384 }
7385
7386 if (isUnoptimized && mUnoptimizedBluetoothChains == null) {
7387 mUnoptimizedBluetoothChains = new ArrayList<WorkChain>(4);
7388 }
7389
7390 mAllBluetoothChains.add(workChain);
7391 if (isUnoptimized) {
7392 mUnoptimizedBluetoothChains.add(workChain);
7393 }
7394 }
7395
7396 public void removeBluetoothWorkChain(WorkChain workChain, boolean isUnoptimized) {
7397 if (mAllBluetoothChains != null) {
7398 mAllBluetoothChains.remove(workChain);
7399 }
7400
7401 if (isUnoptimized && mUnoptimizedBluetoothChains != null) {
7402 mUnoptimizedBluetoothChains.remove(workChain);
7403 }
7404 }
7405
7406 public List<WorkChain> getAllBluetoothWorkChains() {
7407 return mAllBluetoothChains;
7408 }
7409
7410 public List<WorkChain> getUnoptimizedBluetoothWorkChains() {
7411 return mUnoptimizedBluetoothChains;
7412 }
7413
7414
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007415 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
7416 if (mBluetoothScanTimer != null) {
7417 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
7418 }
Bookatzb1f04f32017-05-19 13:57:32 -07007419 if (mBluetoothUnoptimizedScanTimer != null) {
7420 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
7421 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007422 }
7423
Bookatz956f36bf2017-04-28 09:48:17 -07007424 public Counter createBluetoothScanResultCounterLocked() {
7425 if (mBluetoothScanResultCounter == null) {
7426 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
7427 }
7428 return mBluetoothScanResultCounter;
7429 }
7430
Bookatzb1f04f32017-05-19 13:57:32 -07007431 public Counter createBluetoothScanResultBgCounterLocked() {
7432 if (mBluetoothScanResultBgCounter == null) {
7433 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
7434 }
7435 return mBluetoothScanResultBgCounter;
7436 }
7437
Bookatz4ebc0642017-05-11 12:21:19 -07007438 public void noteBluetoothScanResultsLocked(int numNewResults) {
7439 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07007440 // Uses background timebase, so the count will only be incremented if uid in background.
7441 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07007442 }
7443
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007444 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007445 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007446 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007447 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007448 }
7449
7450 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007451 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007452 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007453 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007454 }
7455 }
7456
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007457 public void noteForegroundServiceResumedLocked(long elapsedRealtimeMs) {
7458 createForegroundServiceTimerLocked().startRunningLocked(elapsedRealtimeMs);
7459 }
7460
7461 public void noteForegroundServicePausedLocked(long elapsedRealtimeMs) {
7462 if (mForegroundServiceTimer != null) {
7463 mForegroundServiceTimer.stopRunningLocked(elapsedRealtimeMs);
7464 }
7465 }
7466
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007467 public BatchTimer createVibratorOnTimerLocked() {
7468 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007469 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7470 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007471 }
7472 return mVibratorOnTimer;
7473 }
7474
7475 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08007476 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007477 }
7478
7479 public void noteVibratorOffLocked() {
7480 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007481 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007482 }
7483 }
7484
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007485 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007486 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007487 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007488 return 0;
7489 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007490 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007491 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007492
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007493 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007494 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007495 if (mFullWifiLockTimer == null) {
7496 return 0;
7497 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007498 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07007499 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007500
7501 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007502 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007503 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007504 return 0;
7505 }
Bookatzaa4594a2017-03-24 12:39:56 -07007506 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07007507 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007508
7509 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07007510 public int getWifiScanCount(int which) {
7511 if (mWifiScanTimer == null) {
7512 return 0;
7513 }
Bookatzaa4594a2017-03-24 12:39:56 -07007514 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08007515 }
7516
7517 @Override
Kweku Adams103351f2017-10-16 14:39:34 -07007518 public Timer getWifiScanTimer() {
7519 return mWifiScanTimer;
7520 }
7521
7522 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007523 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07007524 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08007525 return 0;
7526 }
7527 return mWifiScanTimer.getSubTimer().getCountLocked(which);
7528 }
7529
7530 @Override
7531 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
7532 if (mWifiScanTimer == null) {
7533 return 0;
7534 }
7535 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07007536 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08007537 }
7538
7539 @Override
7540 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007541 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08007542 return 0;
7543 }
7544 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
7545 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07007546 }
7547
7548 @Override
Kweku Adams103351f2017-10-16 14:39:34 -07007549 public Timer getWifiScanBackgroundTimer() {
7550 if (mWifiScanTimer == null) {
7551 return null;
7552 }
7553 return mWifiScanTimer.getSubTimer();
7554 }
7555
7556 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007557 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07007558 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
7559 if (mWifiBatchedScanTimer[csphBin] == null) {
7560 return 0;
7561 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007562 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007563 }
7564
7565 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07007566 public int getWifiBatchedScanCount(int csphBin, int which) {
7567 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
7568 if (mWifiBatchedScanTimer[csphBin] == null) {
7569 return 0;
7570 }
7571 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
7572 }
7573
7574 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007575 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007576 if (mWifiMulticastTimer == null) {
7577 return 0;
7578 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007579 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007580 }
7581
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007582 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007583 public Timer getAudioTurnedOnTimer() {
7584 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007585 }
7586
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007587 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007588 public Timer getVideoTurnedOnTimer() {
7589 return mVideoTurnedOnTimer;
7590 }
7591
7592 @Override
7593 public Timer getFlashlightTurnedOnTimer() {
7594 return mFlashlightTurnedOnTimer;
7595 }
7596
7597 @Override
7598 public Timer getCameraTurnedOnTimer() {
7599 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007600 }
7601
Dianne Hackborn617f8772009-03-31 15:04:46 -07007602 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007603 public Timer getForegroundActivityTimer() {
7604 return mForegroundActivityTimer;
7605 }
7606
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007607 @Override
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007608 public Timer getForegroundServiceTimer() {
7609 return mForegroundServiceTimer;
7610 }
7611
7612 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007613 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07007614 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08007615 }
7616
7617 @Override
7618 public Timer getBluetoothScanBackgroundTimer() {
7619 if (mBluetoothScanTimer == null) {
7620 return null;
7621 }
7622 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007623 }
7624
Bookatz956f36bf2017-04-28 09:48:17 -07007625 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07007626 public Timer getBluetoothUnoptimizedScanTimer() {
7627 return mBluetoothUnoptimizedScanTimer;
7628 }
7629
7630 @Override
7631 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
7632 if (mBluetoothUnoptimizedScanTimer == null) {
7633 return null;
7634 }
7635 return mBluetoothUnoptimizedScanTimer.getSubTimer();
7636 }
7637
7638 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07007639 public Counter getBluetoothScanResultCounter() {
7640 return mBluetoothScanResultCounter;
7641 }
7642
Bookatzb1f04f32017-05-19 13:57:32 -07007643 @Override
7644 public Counter getBluetoothScanResultBgCounter() {
7645 return mBluetoothScanResultBgCounter;
7646 }
7647
Dianne Hackborn61659e52014-07-09 16:13:01 -07007648 void makeProcessState(int i, Parcel in) {
7649 if (i < 0 || i >= NUM_PROCESS_STATE) return;
7650
7651 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007652 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
7653 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007654 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08007655 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
7656 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007657 }
7658 }
7659
7660 @Override
7661 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
7662 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
7663 if (mProcessStateTimer[state] == null) {
7664 return 0;
7665 }
7666 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
7667 }
7668
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007669 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08007670 public Timer getProcessStateTimer(int state) {
7671 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
7672 return mProcessStateTimer[state];
7673 }
7674
7675 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007676 public Timer getVibratorOnTimer() {
7677 return mVibratorOnTimer;
7678 }
7679
7680 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07007681 public void noteUserActivityLocked(int type) {
7682 if (mUserActivityCounters == null) {
7683 initUserActivityLocked();
7684 }
Jeff Browndf693de2012-07-27 12:03:38 -07007685 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
7686 mUserActivityCounters[type].stepAtomic();
7687 } else {
7688 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
7689 new Throwable());
7690 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07007691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007692
Dianne Hackborn617f8772009-03-31 15:04:46 -07007693 @Override
7694 public boolean hasUserActivity() {
7695 return mUserActivityCounters != null;
7696 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007697
Dianne Hackborn617f8772009-03-31 15:04:46 -07007698 @Override
7699 public int getUserActivityCount(int type, int which) {
7700 if (mUserActivityCounters == null) {
7701 return 0;
7702 }
Evan Millarc64edde2009-04-18 12:26:32 -07007703 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007704 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007705
Robert Greenwalta029ea12013-09-25 16:38:12 -07007706 void makeWifiBatchedScanBin(int i, Parcel in) {
7707 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
7708
Joe Onoratoabded112016-02-08 16:49:39 -08007709 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007710 if (collected == null) {
7711 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007712 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007713 }
7714 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007715 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
7716 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007717 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08007718 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
7719 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007720 }
7721 }
7722
7723
Dianne Hackborn617f8772009-03-31 15:04:46 -07007724 void initUserActivityLocked() {
7725 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7726 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007727 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007728 }
7729 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007730
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007731 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
7732 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007733 initNetworkActivityLocked();
7734 }
7735 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007736 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
7737 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007738 } else {
7739 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
7740 new Throwable());
7741 }
7742 }
7743
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007744 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
7745 if (mNetworkByteActivityCounters == null) {
7746 initNetworkActivityLocked();
7747 }
7748 mMobileRadioActiveTime.addCountLocked(batteryUptime);
7749 mMobileRadioActiveCount.addCountLocked(1);
7750 }
7751
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007752 @Override
7753 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007754 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007755 }
7756
7757 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007758 public long getNetworkActivityBytes(int type, int which) {
7759 if (mNetworkByteActivityCounters != null && type >= 0
7760 && type < mNetworkByteActivityCounters.length) {
7761 return mNetworkByteActivityCounters[type].getCountLocked(which);
7762 } else {
7763 return 0;
7764 }
7765 }
7766
7767 @Override
7768 public long getNetworkActivityPackets(int type, int which) {
7769 if (mNetworkPacketActivityCounters != null && type >= 0
7770 && type < mNetworkPacketActivityCounters.length) {
7771 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007772 } else {
7773 return 0;
7774 }
7775 }
7776
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007777 @Override
7778 public long getMobileRadioActiveTime(int which) {
7779 return mMobileRadioActiveTime != null
7780 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
7781 }
7782
7783 @Override
7784 public int getMobileRadioActiveCount(int which) {
7785 return mMobileRadioActiveCount != null
7786 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
7787 }
7788
Adam Lesinskie08af192015-03-25 16:42:59 -07007789 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007790 public long getUserCpuTimeUs(int which) {
7791 return mUserCpuTime.getCountLocked(which);
7792 }
7793
7794 @Override
7795 public long getSystemCpuTimeUs(int which) {
7796 return mSystemCpuTime.getCountLocked(which);
7797 }
7798
7799 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07007800 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007801 if (mCpuClusterSpeedTimesUs != null) {
7802 if (cluster >= 0 && cluster < mCpuClusterSpeedTimesUs.length) {
7803 final LongSamplingCounter[] cpuSpeedTimesUs = mCpuClusterSpeedTimesUs[cluster];
7804 if (cpuSpeedTimesUs != null) {
7805 if (step >= 0 && step < cpuSpeedTimesUs.length) {
7806 final LongSamplingCounter c = cpuSpeedTimesUs[step];
Adam Lesinski6832f392015-09-05 18:05:40 -07007807 if (c != null) {
7808 return c.getCountLocked(which);
7809 }
7810 }
7811 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007812 }
7813 }
7814 return 0;
7815 }
7816
Adam Lesinski5f056f62016-07-14 16:56:08 -07007817 public void noteMobileRadioApWakeupLocked() {
7818 if (mMobileRadioApWakeupCount == null) {
7819 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
7820 }
7821 mMobileRadioApWakeupCount.addCountLocked(1);
7822 }
7823
7824 @Override
7825 public long getMobileRadioApWakeupCount(int which) {
7826 if (mMobileRadioApWakeupCount != null) {
7827 return mMobileRadioApWakeupCount.getCountLocked(which);
7828 }
7829 return 0;
7830 }
7831
7832 public void noteWifiRadioApWakeupLocked() {
7833 if (mWifiRadioApWakeupCount == null) {
7834 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
7835 }
7836 mWifiRadioApWakeupCount.addCountLocked(1);
7837 }
7838
7839 @Override
7840 public long getWifiRadioApWakeupCount(int which) {
7841 if (mWifiRadioApWakeupCount != null) {
7842 return mWifiRadioApWakeupCount.getCountLocked(which);
7843 }
7844 return 0;
7845 }
7846
Amith Yamasani977e11f2018-02-16 11:29:54 -08007847 @Override
7848 public void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which) {
7849 sb.setLength(0);
7850 final int deferredEventCount = mJobsDeferredEventCount.getCountLocked(which);
7851 if (deferredEventCount == 0) {
7852 return;
7853 }
7854 final int deferredCount = mJobsDeferredCount.getCountLocked(which);
7855 final long averageLatency = deferredEventCount != 0
7856 ? mJobsFreshnessTimeMs.getCountLocked(which) / deferredEventCount
7857 : 0L;
7858 sb.append(deferredEventCount); sb.append(',');
7859 sb.append(deferredCount); sb.append(',');
7860 sb.append(averageLatency);
7861 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
7862 if (mJobsFreshnessBuckets[i] == null) {
7863 sb.append(",0");
7864 } else {
7865 sb.append(",");
7866 sb.append(mJobsFreshnessBuckets[i].getCountLocked(which));
7867 }
7868 }
7869 }
7870
7871 @Override
7872 public void getDeferredJobsLineLocked(StringBuilder sb, int which) {
7873 sb.setLength(0);
7874 final int deferredEventCount = mJobsDeferredEventCount.getCountLocked(which);
7875 if (deferredEventCount == 0) {
7876 return;
7877 }
7878 final int deferredCount = mJobsDeferredCount.getCountLocked(which);
7879 final long averageLatency = deferredEventCount != 0
7880 ? mJobsFreshnessTimeMs.getCountLocked(which) / deferredEventCount
7881 : 0L;
7882 sb.append("times="); sb.append(deferredEventCount); sb.append(", ");
7883 sb.append("count="); sb.append(deferredCount); sb.append(", ");
7884 sb.append("avgLatency="); sb.append(averageLatency); sb.append(", ");
7885 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
7886 sb.append("<"); sb.append(JOB_FRESHNESS_BUCKETS[i]); sb.append("ms=");
7887 if (mJobsFreshnessBuckets[i] == null) {
7888 sb.append("0");
7889 } else {
7890 sb.append(mJobsFreshnessBuckets[i].getCountLocked(which));
7891 }
7892 sb.append(" ");
7893 }
7894 }
7895
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007896 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007897 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7898 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007899 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007900 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
7901 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007902 }
Joe Onoratoabded112016-02-08 16:49:39 -08007903 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
7904 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007906
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007907 /**
7908 * Clear all stats for this uid. Returns true if the uid is completely
7909 * inactive so can be dropped.
7910 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07007911 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Bookatz993a0be2017-07-21 09:03:23 -07007912 public boolean reset(long uptime, long realtime) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007913 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007914
Bookatz993a0be2017-07-21 09:03:23 -07007915 mOnBatteryBackgroundTimeBase.init(uptime, realtime);
7916 mOnBatteryScreenOffBackgroundTimeBase.init(uptime, realtime);
7917
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007918 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007919 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007920 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007921 }
7922 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007923 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007924 active |= mFullWifiLockOut;
7925 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007926 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007927 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07007928 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007929 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007930 if (mWifiBatchedScanTimer != null) {
7931 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7932 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007933 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007934 }
7935 }
7936 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
7937 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007938 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007939 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007940 active |= mWifiMulticastEnabled;
7941 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007942
7943 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
7944 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
7945 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
7946 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
7947 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007948 active |= !resetTimerIfNotNull(mForegroundServiceTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07007949 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07007950 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07007951 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07007952 if (mBluetoothScanResultCounter != null) {
7953 mBluetoothScanResultCounter.reset(false);
7954 }
Bookatzb1f04f32017-05-19 13:57:32 -07007955 if (mBluetoothScanResultBgCounter != null) {
7956 mBluetoothScanResultBgCounter.reset(false);
7957 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007958
Dianne Hackborn61659e52014-07-09 16:13:01 -07007959 if (mProcessStateTimer != null) {
7960 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7961 if (mProcessStateTimer[i] != null) {
7962 active |= !mProcessStateTimer[i].reset(false);
7963 }
7964 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007965 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007966 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007967 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007968 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007969 mVibratorOnTimer.detach();
7970 mVibratorOnTimer = null;
7971 } else {
7972 active = true;
7973 }
7974 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007975
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007976 if (mUserActivityCounters != null) {
7977 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7978 mUserActivityCounters[i].reset(false);
7979 }
7980 }
7981
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007982 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007983 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007984 mNetworkByteActivityCounters[i].reset(false);
7985 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007986 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007987 mMobileRadioActiveTime.reset(false);
7988 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007989 }
7990
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007991 if (mWifiControllerActivity != null) {
7992 mWifiControllerActivity.reset(false);
7993 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007994
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07007995 if (mBluetoothControllerActivity != null) {
7996 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007997 }
7998
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07007999 if (mModemControllerActivity != null) {
8000 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07008001 }
8002
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008003 mUserCpuTime.reset(false);
8004 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07008005
Sudheer Shankaaf857412017-07-21 00:14:24 -07008006 if (mCpuClusterSpeedTimesUs != null) {
8007 for (LongSamplingCounter[] speeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008008 if (speeds != null) {
8009 for (LongSamplingCounter speed : speeds) {
8010 if (speed != null) {
8011 speed.reset(false);
8012 }
8013 }
8014 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008015 }
8016 }
8017
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008018 if (mCpuFreqTimeMs != null) {
8019 mCpuFreqTimeMs.reset(false);
8020 }
8021 if (mScreenOffCpuFreqTimeMs != null) {
8022 mScreenOffCpuFreqTimeMs.reset(false);
8023 }
8024
Mike Ma3d422c32017-10-25 11:08:57 -07008025 mCpuActiveTimeMs.reset(false);
8026 mCpuClusterTimesMs.reset(false);
8027
Sudheer Shankab2f83c12017-11-13 19:25:01 -08008028 if (mProcStateTimeMs != null) {
8029 for (LongSamplingCounterArray counters : mProcStateTimeMs) {
8030 if (counters != null) {
8031 counters.reset(false);
8032 }
8033 }
8034 }
8035 if (mProcStateScreenOffTimeMs != null) {
8036 for (LongSamplingCounterArray counters : mProcStateScreenOffTimeMs) {
8037 if (counters != null) {
8038 counters.reset(false);
8039 }
8040 }
8041 }
8042
Adam Lesinski5f056f62016-07-14 16:56:08 -07008043 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
8044 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
8045
Dianne Hackbornd953c532014-08-16 18:17:38 -07008046 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
8047 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
8048 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008049 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008050 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008051 } else {
8052 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008053 }
8054 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008055 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07008056 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008057 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008058 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008059 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008060 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008061 timer.detach();
8062 } else {
8063 active = true;
8064 }
8065 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008066 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07008067 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008068 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07008069 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008070 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008071 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008072 timer.detach();
8073 } else {
8074 active = true;
8075 }
8076 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008077 mJobStats.cleanup();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008078 mJobCompletions.clear();
Amith Yamasani977e11f2018-02-16 11:29:54 -08008079
8080 mJobsDeferredEventCount.reset(false);
8081 mJobsDeferredCount.reset(false);
8082 mJobsFreshnessTimeMs.reset(false);
8083 for (int ij = 0; ij < JOB_FRESHNESS_BUCKETS.length; ij++) {
8084 if (mJobsFreshnessBuckets[ij] != null) {
8085 mJobsFreshnessBuckets[ij].reset(false);
8086 }
8087 }
8088
Dianne Hackborn61659e52014-07-09 16:13:01 -07008089 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
8090 Sensor s = mSensorStats.valueAt(ise);
8091 if (s.reset()) {
8092 mSensorStats.removeAt(ise);
8093 } else {
8094 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008095 }
8096 }
Amith Yamasani977e11f2018-02-16 11:29:54 -08008097
Dianne Hackborn61659e52014-07-09 16:13:01 -07008098 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
8099 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008100 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008101 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08008102 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008103 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008104 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008105 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008106 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008107 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008108 } else {
8109 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008110 }
8111 }
8112 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008113 if (mPackageStats.size() > 0) {
8114 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
8115 while (it.hasNext()) {
8116 Map.Entry<String, Pkg> pkgEntry = it.next();
8117 Pkg p = pkgEntry.getValue();
8118 p.detach();
8119 if (p.mServiceStats.size() > 0) {
8120 Iterator<Map.Entry<String, Pkg.Serv>> it2
8121 = p.mServiceStats.entrySet().iterator();
8122 while (it2.hasNext()) {
8123 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
8124 servEntry.getValue().detach();
8125 }
8126 }
8127 }
8128 mPackageStats.clear();
8129 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008130
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008131 mLastStepUserTime = mLastStepSystemTime = 0;
8132 mCurStepUserTime = mCurStepSystemTime = 0;
8133
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008134 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008135 if (mWifiRunningTimer != null) {
8136 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008137 }
8138 if (mFullWifiLockTimer != null) {
8139 mFullWifiLockTimer.detach();
8140 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07008141 if (mWifiScanTimer != null) {
8142 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008143 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07008144 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
8145 if (mWifiBatchedScanTimer[i] != null) {
8146 mWifiBatchedScanTimer[i].detach();
8147 }
8148 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008149 if (mWifiMulticastTimer != null) {
8150 mWifiMulticastTimer.detach();
8151 }
8152 if (mAudioTurnedOnTimer != null) {
8153 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008154 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008155 }
8156 if (mVideoTurnedOnTimer != null) {
8157 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008158 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008159 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008160 if (mFlashlightTurnedOnTimer != null) {
8161 mFlashlightTurnedOnTimer.detach();
8162 mFlashlightTurnedOnTimer = null;
8163 }
8164 if (mCameraTurnedOnTimer != null) {
8165 mCameraTurnedOnTimer.detach();
8166 mCameraTurnedOnTimer = null;
8167 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008168 if (mForegroundActivityTimer != null) {
8169 mForegroundActivityTimer.detach();
8170 mForegroundActivityTimer = null;
8171 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008172 if (mForegroundServiceTimer != null) {
8173 mForegroundServiceTimer.detach();
8174 mForegroundServiceTimer = null;
8175 }
Bookatzc8c44962017-05-11 12:12:54 -07008176 if (mAggregatedPartialWakelockTimer != null) {
8177 mAggregatedPartialWakelockTimer.detach();
8178 mAggregatedPartialWakelockTimer = null;
8179 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008180 if (mBluetoothScanTimer != null) {
8181 mBluetoothScanTimer.detach();
8182 mBluetoothScanTimer = null;
8183 }
Bookatzb1f04f32017-05-19 13:57:32 -07008184 if (mBluetoothUnoptimizedScanTimer != null) {
8185 mBluetoothUnoptimizedScanTimer.detach();
8186 mBluetoothUnoptimizedScanTimer = null;
8187 }
Bookatz956f36bf2017-04-28 09:48:17 -07008188 if (mBluetoothScanResultCounter != null) {
8189 mBluetoothScanResultCounter.detach();
8190 mBluetoothScanResultCounter = null;
8191 }
Bookatzb1f04f32017-05-19 13:57:32 -07008192 if (mBluetoothScanResultBgCounter != null) {
8193 mBluetoothScanResultBgCounter.detach();
8194 mBluetoothScanResultBgCounter = null;
8195 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008196 if (mUserActivityCounters != null) {
8197 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
8198 mUserActivityCounters[i].detach();
8199 }
8200 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008201 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008202 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008203 mNetworkByteActivityCounters[i].detach();
8204 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008205 }
8206 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008207
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008208 if (mWifiControllerActivity != null) {
8209 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07008210 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008211
8212 if (mBluetoothControllerActivity != null) {
8213 mBluetoothControllerActivity.detach();
8214 }
8215
8216 if (mModemControllerActivity != null) {
8217 mModemControllerActivity.detach();
8218 }
8219
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008220 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008221
8222 mUserCpuTime.detach();
8223 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07008224
Sudheer Shankaaf857412017-07-21 00:14:24 -07008225 if (mCpuClusterSpeedTimesUs != null) {
8226 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008227 if (cpuSpeeds != null) {
8228 for (LongSamplingCounter c : cpuSpeeds) {
8229 if (c != null) {
8230 c.detach();
8231 }
8232 }
8233 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008234 }
8235 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07008236
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008237 if (mCpuFreqTimeMs != null) {
8238 mCpuFreqTimeMs.detach();
8239 }
8240 if (mScreenOffCpuFreqTimeMs != null) {
8241 mScreenOffCpuFreqTimeMs.detach();
8242 }
Mike Ma3d422c32017-10-25 11:08:57 -07008243 mCpuActiveTimeMs.detach();
8244 mCpuClusterTimesMs.detach();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008245
Sudheer Shankab2f83c12017-11-13 19:25:01 -08008246 if (mProcStateTimeMs != null) {
8247 for (LongSamplingCounterArray counters : mProcStateTimeMs) {
8248 if (counters != null) {
8249 counters.detach();
8250 }
8251 }
8252 }
8253 if (mProcStateScreenOffTimeMs != null) {
8254 for (LongSamplingCounterArray counters : mProcStateScreenOffTimeMs) {
8255 if (counters != null) {
8256 counters.detach();
8257 }
8258 }
8259 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07008260 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
8261 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008262 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008263
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008264 return !active;
8265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008266
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008267 void writeJobCompletionsToParcelLocked(Parcel out) {
8268 int NJC = mJobCompletions.size();
8269 out.writeInt(NJC);
8270 for (int ijc=0; ijc<NJC; ijc++) {
8271 out.writeString(mJobCompletions.keyAt(ijc));
8272 SparseIntArray types = mJobCompletions.valueAt(ijc);
8273 int NT = types.size();
8274 out.writeInt(NT);
8275 for (int it=0; it<NT; it++) {
8276 out.writeInt(types.keyAt(it));
8277 out.writeInt(types.valueAt(it));
8278 }
8279 }
8280 }
8281
Bookatz867c0d72017-03-07 18:23:42 -08008282 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
8283 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07008284 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08008285
Dianne Hackbornd953c532014-08-16 18:17:38 -07008286 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
8287 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07008288 out.writeInt(NW);
8289 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008290 out.writeString(wakeStats.keyAt(iw));
8291 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008292 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 }
8294
Bookatz2bffb5b2017-04-13 11:59:33 -07008295 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008296 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008297 out.writeInt(NS);
8298 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008299 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07008300 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008301 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
8302 }
8303
Bookatzaa4594a2017-03-24 12:39:56 -07008304 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008305 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008306 out.writeInt(NJ);
8307 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008308 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07008309 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008310 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
8311 }
8312
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008313 writeJobCompletionsToParcelLocked(out);
8314
Amith Yamasani977e11f2018-02-16 11:29:54 -08008315 mJobsDeferredEventCount.writeToParcel(out);
8316 mJobsDeferredCount.writeToParcel(out);
8317 mJobsFreshnessTimeMs.writeToParcel(out);
8318 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
8319 Counter.writeCounterToParcel(out, mJobsFreshnessBuckets[i]);
8320 }
8321
Dianne Hackborn61659e52014-07-09 16:13:01 -07008322 int NSE = mSensorStats.size();
8323 out.writeInt(NSE);
8324 for (int ise=0; ise<NSE; ise++) {
8325 out.writeInt(mSensorStats.keyAt(ise));
8326 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008327 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 }
8329
Dianne Hackborn61659e52014-07-09 16:13:01 -07008330 int NP = mProcessStats.size();
8331 out.writeInt(NP);
8332 for (int ip=0; ip<NP; ip++) {
8333 out.writeString(mProcessStats.keyAt(ip));
8334 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 proc.writeToParcelLocked(out);
8336 }
8337
8338 out.writeInt(mPackageStats.size());
8339 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
8340 out.writeString(pkgEntry.getKey());
8341 Uid.Pkg pkg = pkgEntry.getValue();
8342 pkg.writeToParcelLocked(out);
8343 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008344
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008345 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008346 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008347 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008348 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008349 out.writeInt(0);
8350 }
8351 if (mFullWifiLockTimer != null) {
8352 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008353 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008354 } else {
8355 out.writeInt(0);
8356 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07008357 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008358 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008359 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008360 } else {
8361 out.writeInt(0);
8362 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07008363 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
8364 if (mWifiBatchedScanTimer[i] != null) {
8365 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008366 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07008367 } else {
8368 out.writeInt(0);
8369 }
8370 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008371 if (mWifiMulticastTimer != null) {
8372 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008373 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008374 } else {
8375 out.writeInt(0);
8376 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008377
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008378 if (mAudioTurnedOnTimer != null) {
8379 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008380 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008381 } else {
8382 out.writeInt(0);
8383 }
8384 if (mVideoTurnedOnTimer != null) {
8385 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008386 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008387 } else {
8388 out.writeInt(0);
8389 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008390 if (mFlashlightTurnedOnTimer != null) {
8391 out.writeInt(1);
8392 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
8393 } else {
8394 out.writeInt(0);
8395 }
8396 if (mCameraTurnedOnTimer != null) {
8397 out.writeInt(1);
8398 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
8399 } else {
8400 out.writeInt(0);
8401 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008402 if (mForegroundActivityTimer != null) {
8403 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008404 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008405 } else {
8406 out.writeInt(0);
8407 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008408 if (mForegroundServiceTimer != null) {
8409 out.writeInt(1);
8410 mForegroundServiceTimer.writeToParcel(out, elapsedRealtimeUs);
8411 } else {
8412 out.writeInt(0);
8413 }
Bookatzc8c44962017-05-11 12:12:54 -07008414 if (mAggregatedPartialWakelockTimer != null) {
8415 out.writeInt(1);
8416 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
8417 } else {
8418 out.writeInt(0);
8419 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008420 if (mBluetoothScanTimer != null) {
8421 out.writeInt(1);
8422 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
8423 } else {
8424 out.writeInt(0);
8425 }
Bookatzb1f04f32017-05-19 13:57:32 -07008426 if (mBluetoothUnoptimizedScanTimer != null) {
8427 out.writeInt(1);
8428 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
8429 } else {
8430 out.writeInt(0);
8431 }
Bookatz956f36bf2017-04-28 09:48:17 -07008432 if (mBluetoothScanResultCounter != null) {
8433 out.writeInt(1);
8434 mBluetoothScanResultCounter.writeToParcel(out);
8435 } else {
8436 out.writeInt(0);
8437 }
Bookatzb1f04f32017-05-19 13:57:32 -07008438 if (mBluetoothScanResultBgCounter != null) {
8439 out.writeInt(1);
8440 mBluetoothScanResultBgCounter.writeToParcel(out);
8441 } else {
8442 out.writeInt(0);
8443 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008444 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
8445 if (mProcessStateTimer[i] != null) {
8446 out.writeInt(1);
8447 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
8448 } else {
8449 out.writeInt(0);
8450 }
8451 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008452 if (mVibratorOnTimer != null) {
8453 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008454 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008455 } else {
8456 out.writeInt(0);
8457 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008458 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07008459 out.writeInt(1);
8460 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
8461 mUserActivityCounters[i].writeToParcel(out);
8462 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008463 } else {
8464 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008465 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008466 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008467 out.writeInt(1);
8468 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008469 mNetworkByteActivityCounters[i].writeToParcel(out);
8470 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008471 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008472 mMobileRadioActiveTime.writeToParcel(out);
8473 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008474 } else {
8475 out.writeInt(0);
8476 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008477
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008478 if (mWifiControllerActivity != null) {
8479 out.writeInt(1);
8480 mWifiControllerActivity.writeToParcel(out, 0);
8481 } else {
8482 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07008483 }
8484
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008485 if (mBluetoothControllerActivity != null) {
8486 out.writeInt(1);
8487 mBluetoothControllerActivity.writeToParcel(out, 0);
8488 } else {
8489 out.writeInt(0);
8490 }
8491
8492 if (mModemControllerActivity != null) {
8493 out.writeInt(1);
8494 mModemControllerActivity.writeToParcel(out, 0);
8495 } else {
8496 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07008497 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008498
8499 mUserCpuTime.writeToParcel(out);
8500 mSystemCpuTime.writeToParcel(out);
8501
Sudheer Shankaaf857412017-07-21 00:14:24 -07008502 if (mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008503 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -07008504 out.writeInt(mCpuClusterSpeedTimesUs.length);
8505 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008506 if (cpuSpeeds != null) {
8507 out.writeInt(1);
8508 out.writeInt(cpuSpeeds.length);
8509 for (LongSamplingCounter c : cpuSpeeds) {
8510 if (c != null) {
8511 out.writeInt(1);
8512 c.writeToParcel(out);
8513 } else {
8514 out.writeInt(0);
8515 }
8516 }
8517 } else {
8518 out.writeInt(0);
8519 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008520 }
Adam Lesinski6832f392015-09-05 18:05:40 -07008521 } else {
8522 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008523 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07008524
Sudheer Shanka59f5c002017-05-15 10:57:15 -07008525 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
8526 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Mike Ma3d422c32017-10-25 11:08:57 -07008527
8528 mCpuActiveTimeMs.writeToParcel(out);
8529 mCpuClusterTimesMs.writeToParcel(out);
8530
Sudheer Shankab2f83c12017-11-13 19:25:01 -08008531 if (mProcStateTimeMs != null) {
8532 out.writeInt(mProcStateTimeMs.length);
8533 for (LongSamplingCounterArray counters : mProcStateTimeMs) {
8534 LongSamplingCounterArray.writeToParcel(out, counters);
8535 }
8536 } else {
8537 out.writeInt(0);
8538 }
8539 if (mProcStateScreenOffTimeMs != null) {
8540 out.writeInt(mProcStateScreenOffTimeMs.length);
8541 for (LongSamplingCounterArray counters : mProcStateScreenOffTimeMs) {
8542 LongSamplingCounterArray.writeToParcel(out, counters);
8543 }
8544 } else {
8545 out.writeInt(0);
8546 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008547
Adam Lesinski5f056f62016-07-14 16:56:08 -07008548 if (mMobileRadioApWakeupCount != null) {
8549 out.writeInt(1);
8550 mMobileRadioApWakeupCount.writeToParcel(out);
8551 } else {
8552 out.writeInt(0);
8553 }
8554
8555 if (mWifiRadioApWakeupCount != null) {
8556 out.writeInt(1);
8557 mWifiRadioApWakeupCount.writeToParcel(out);
8558 } else {
8559 out.writeInt(0);
8560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 }
8562
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008563 void readJobCompletionsFromParcelLocked(Parcel in) {
8564 int numJobCompletions = in.readInt();
8565 mJobCompletions.clear();
8566 for (int j = 0; j < numJobCompletions; j++) {
8567 String jobName = in.readString();
8568 int numTypes = in.readInt();
8569 if (numTypes > 0) {
8570 SparseIntArray types = new SparseIntArray();
8571 for (int k = 0; k < numTypes; k++) {
8572 int type = in.readInt();
8573 int count = in.readInt();
8574 types.put(type, count);
8575 }
8576 mJobCompletions.put(jobName, types);
8577 }
8578 }
8579 }
8580
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008581 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08008582 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07008583 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08008584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 int numWakelocks = in.readInt();
8586 mWakelockStats.clear();
8587 for (int j = 0; j < numWakelocks; j++) {
8588 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008589 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07008590 wakelock.readFromParcelLocked(
8591 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008592 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 }
8594
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008595 int numSyncs = in.readInt();
8596 mSyncStats.clear();
8597 for (int j = 0; j < numSyncs; j++) {
8598 String syncName = in.readString();
8599 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008600 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
8601 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008602 }
8603 }
8604
8605 int numJobs = in.readInt();
8606 mJobStats.clear();
8607 for (int j = 0; j < numJobs; j++) {
8608 String jobName = in.readString();
8609 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07008610 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
8611 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008612 }
8613 }
8614
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008615 readJobCompletionsFromParcelLocked(in);
8616
Amith Yamasani977e11f2018-02-16 11:29:54 -08008617 mJobsDeferredEventCount = new Counter(mBsi.mOnBatteryTimeBase, in);
8618 mJobsDeferredCount = new Counter(mBsi.mOnBatteryTimeBase, in);
8619 mJobsFreshnessTimeMs = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8620 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
8621 mJobsFreshnessBuckets[i] = Counter.readCounterFromParcel(mBsi.mOnBatteryTimeBase,
8622 in);
8623 }
8624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 int numSensors = in.readInt();
8626 mSensorStats.clear();
8627 for (int k = 0; k < numSensors; k++) {
8628 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08008629 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08008630 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
8631 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 mSensorStats.put(sensorNumber, sensor);
8633 }
8634
8635 int numProcs = in.readInt();
8636 mProcessStats.clear();
8637 for (int k = 0; k < numProcs; k++) {
8638 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008639 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 proc.readFromParcelLocked(in);
8641 mProcessStats.put(processName, proc);
8642 }
8643
8644 int numPkgs = in.readInt();
8645 mPackageStats.clear();
8646 for (int l = 0; l < numPkgs; l++) {
8647 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008648 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649 pkg.readFromParcelLocked(in);
8650 mPackageStats.put(packageName, pkg);
8651 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008652
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008653 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008654 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008655 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
8656 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008657 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008658 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008659 }
8660 mFullWifiLockOut = false;
8661 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008662 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
8663 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008664 } else {
8665 mFullWifiLockTimer = null;
8666 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07008667 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008668 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08008669 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
8670 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
8671 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008672 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07008673 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008674 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07008675 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
8676 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
8677 if (in.readInt() != 0) {
8678 makeWifiBatchedScanBin(i, in);
8679 } else {
8680 mWifiBatchedScanTimer[i] = null;
8681 }
8682 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008683 mWifiMulticastEnabled = false;
8684 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008685 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
8686 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008687 } else {
8688 mWifiMulticastTimer = null;
8689 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008690 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008691 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
8692 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008693 } else {
8694 mAudioTurnedOnTimer = null;
8695 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008696 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008697 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
8698 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008699 } else {
8700 mVideoTurnedOnTimer = null;
8701 }
8702 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008703 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
8704 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008705 } else {
8706 mFlashlightTurnedOnTimer = null;
8707 }
8708 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008709 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
8710 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008711 } else {
8712 mCameraTurnedOnTimer = null;
8713 }
8714 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008715 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
8716 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008717 } else {
8718 mForegroundActivityTimer = null;
8719 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008720 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008721 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
8722 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase, in);
8723 } else {
8724 mForegroundServiceTimer = null;
8725 }
8726 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07008727 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
8728 AGGREGATED_WAKE_TYPE_PARTIAL, null,
8729 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
8730 in);
8731 } else {
8732 mAggregatedPartialWakelockTimer = null;
8733 }
8734 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08008735 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
8736 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
8737 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008738 } else {
8739 mBluetoothScanTimer = null;
8740 }
Bookatz956f36bf2017-04-28 09:48:17 -07008741 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07008742 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
8743 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
8744 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
8745 } else {
8746 mBluetoothUnoptimizedScanTimer = null;
8747 }
8748 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07008749 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
8750 } else {
8751 mBluetoothScanResultCounter = null;
8752 }
Bookatzb1f04f32017-05-19 13:57:32 -07008753 if (in.readInt() != 0) {
8754 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
8755 } else {
8756 mBluetoothScanResultBgCounter = null;
8757 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08008758 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008759 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
8760 if (in.readInt() != 0) {
8761 makeProcessState(i, in);
8762 } else {
8763 mProcessStateTimer[i] = null;
8764 }
8765 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008766 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008767 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
8768 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008769 } else {
8770 mVibratorOnTimer = null;
8771 }
8772 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07008773 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
8774 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008775 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008776 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008777 } else {
8778 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07008779 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008780 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008781 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
8782 mNetworkPacketActivityCounters
8783 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008784 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008785 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08008786 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008787 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08008788 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008789 }
Joe Onoratoabded112016-02-08 16:49:39 -08008790 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8791 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008792 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008793 mNetworkByteActivityCounters = null;
8794 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008795 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008796
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008797 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008798 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008799 NUM_WIFI_TX_LEVELS, in);
8800 } else {
8801 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07008802 }
8803
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008804 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008805 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008806 NUM_BT_TX_LEVELS, in);
8807 } else {
8808 mBluetoothControllerActivity = null;
8809 }
8810
8811 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08008812 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008813 ModemActivityInfo.TX_POWER_LEVELS, in);
8814 } else {
8815 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07008816 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008817
Joe Onoratoabded112016-02-08 16:49:39 -08008818 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8819 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008820
Adam Lesinski6832f392015-09-05 18:05:40 -07008821 if (in.readInt() != 0) {
8822 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08008823 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008824 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008825 }
Adam Lesinski6832f392015-09-05 18:05:40 -07008826
Sudheer Shankaaf857412017-07-21 00:14:24 -07008827 mCpuClusterSpeedTimesUs = new LongSamplingCounter[numCpuClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07008828 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
8829 if (in.readInt() != 0) {
8830 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08008831 if (mBsi.mPowerProfile != null &&
8832 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008833 throw new ParcelFormatException("Incompatible number of cpu speeds");
8834 }
8835
8836 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
Sudheer Shankaaf857412017-07-21 00:14:24 -07008837 mCpuClusterSpeedTimesUs[cluster] = cpuSpeeds;
Adam Lesinski6832f392015-09-05 18:05:40 -07008838 for (int speed = 0; speed < numSpeeds; speed++) {
8839 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07008840 cpuSpeeds[speed] = new LongSamplingCounter(
8841 mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07008842 }
8843 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07008844 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07008845 mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07008846 }
8847 }
8848 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07008849 mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008850 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07008851
Sudheer Shanka59f5c002017-05-15 10:57:15 -07008852 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
8853 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
8854 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008855
Mike Ma3d422c32017-10-25 11:08:57 -07008856 mCpuActiveTimeMs = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8857 mCpuClusterTimesMs = new LongSamplingCounterArray(mBsi.mOnBatteryTimeBase, in);
8858
Sudheer Shankab2f83c12017-11-13 19:25:01 -08008859 int length = in.readInt();
8860 if (length == NUM_PROCESS_STATE) {
8861 mProcStateTimeMs = new LongSamplingCounterArray[length];
8862 for (int procState = 0; procState < length; ++procState) {
8863 mProcStateTimeMs[procState] = LongSamplingCounterArray.readFromParcel(
8864 in, mBsi.mOnBatteryTimeBase);
8865 }
8866 } else {
8867 mProcStateTimeMs = null;
8868 }
8869 length = in.readInt();
8870 if (length == NUM_PROCESS_STATE) {
8871 mProcStateScreenOffTimeMs = new LongSamplingCounterArray[length];
8872 for (int procState = 0; procState < length; ++procState) {
8873 mProcStateScreenOffTimeMs[procState] = LongSamplingCounterArray.readFromParcel(
8874 in, mBsi.mOnBatteryScreenOffTimeBase);
8875 }
8876 } else {
8877 mProcStateScreenOffTimeMs = null;
8878 }
8879
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008880 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07008881 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8882 } else {
8883 mMobileRadioApWakeupCount = null;
8884 }
8885
8886 if (in.readInt() != 0) {
8887 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
8888 } else {
8889 mWifiRadioApWakeupCount = null;
8890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 }
8892
Amith Yamasani977e11f2018-02-16 11:29:54 -08008893 public void noteJobsDeferredLocked(int numDeferred, long sinceLast) {
8894 mJobsDeferredEventCount.addAtomic(1);
8895 mJobsDeferredCount.addAtomic(numDeferred);
8896 if (sinceLast != 0) {
8897 // Add the total time, which can be divided by the event count to get an average
8898 mJobsFreshnessTimeMs.addCountLocked(sinceLast);
8899 // Also keep track of how many times there were in these different buckets.
8900 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
8901 if (sinceLast < JOB_FRESHNESS_BUCKETS[i]) {
8902 if (mJobsFreshnessBuckets[i] == null) {
8903 mJobsFreshnessBuckets[i] = new Counter(
8904 mBsi.mOnBatteryTimeBase);
8905 }
8906 mJobsFreshnessBuckets[i].addAtomic(1);
8907 break;
8908 }
8909 }
8910 }
8911 }
8912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 /**
8914 * The statistics associated with a particular wake lock.
8915 */
Joe Onoratoabded112016-02-08 16:49:39 -08008916 public static class Wakelock extends BatteryStats.Uid.Wakelock {
8917 /**
8918 * BatteryStatsImpl that we are associated with.
8919 */
8920 protected BatteryStatsImpl mBsi;
8921
8922 /**
8923 * BatteryStatsImpl that we are associated with.
8924 */
8925 protected Uid mUid;
8926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 /**
8928 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07008929 * Tracks both the total time and the time while the app was in the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008930 */
Bookatz5b5ec322017-05-26 09:40:38 -07008931 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932
8933 /**
8934 * How long (in ms) this uid has been keeping the device fully awake.
8935 */
Evan Millarc64edde2009-04-18 12:26:32 -07008936 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937
8938 /**
8939 * How long (in ms) this uid has had a window keeping the device awake.
8940 */
Evan Millarc64edde2009-04-18 12:26:32 -07008941 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942
8943 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008944 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07008945 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008946 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07008947
Joe Onoratoabded112016-02-08 16:49:39 -08008948 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
8949 mBsi = bsi;
8950 mUid = uid;
8951 }
8952
Adam Lesinski9425fe22015-06-19 12:02:13 -07008953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 * Reads a possibly null Timer from a Parcel. The timer is associated with the
8955 * proper timer pool from the given BatteryStatsImpl object.
8956 *
8957 * @param in the Parcel to be read from.
8958 * return a new Timer, or null.
8959 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07008960 private StopwatchTimer readStopwatchTimerFromParcel(int type,
8961 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 if (in.readInt() == 0) {
8963 return null;
8964 }
8965
Joe Onoratoabded112016-02-08 16:49:39 -08008966 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 }
8968
Joe Onorato92fd23f2016-07-25 11:18:42 -07008969 /**
8970 * Reads a possibly null Timer from a Parcel. The timer is associated with the
8971 * proper timer pool from the given BatteryStatsImpl object.
8972 *
8973 * @param in the Parcel to be read from.
8974 * return a new Timer, or null.
8975 */
Bookatz5b5ec322017-05-26 09:40:38 -07008976 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
8977 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07008978 if (in.readInt() == 0) {
8979 return null;
8980 }
8981
Bookatz5b5ec322017-05-26 09:40:38 -07008982 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07008983 }
8984
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008985 boolean reset() {
8986 boolean wlactive = false;
8987 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008988 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008989 }
8990 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008991 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008992 }
8993 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008994 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008995 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008996 if (mTimerDraw != null) {
8997 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008998 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008999 if (!wlactive) {
9000 if (mTimerFull != null) {
9001 mTimerFull.detach();
9002 mTimerFull = null;
9003 }
9004 if (mTimerPartial != null) {
9005 mTimerPartial.detach();
9006 mTimerPartial = null;
9007 }
9008 if (mTimerWindow != null) {
9009 mTimerWindow.detach();
9010 mTimerWindow = null;
9011 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07009012 if (mTimerDraw != null) {
9013 mTimerDraw.detach();
9014 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07009015 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009016 }
9017 return !wlactive;
9018 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009019
Bookatz5b5ec322017-05-26 09:40:38 -07009020 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
9021 TimeBase screenOffBgTimeBase, Parcel in) {
9022 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
9023 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07009024 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
9025 mBsi.mFullTimers, timeBase, in);
9026 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
9027 mBsi.mWindowTimers, timeBase, in);
9028 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
9029 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 }
9031
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009032 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
9033 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
9034 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
9035 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07009036 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 }
9038
9039 @Override
9040 public Timer getWakeTime(int type) {
9041 switch (type) {
9042 case WAKE_TYPE_FULL: return mTimerFull;
9043 case WAKE_TYPE_PARTIAL: return mTimerPartial;
9044 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07009045 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 default: throw new IllegalArgumentException("type = " + type);
9047 }
9048 }
9049 }
9050
Joe Onoratoabded112016-02-08 16:49:39 -08009051 public static class Sensor extends BatteryStats.Uid.Sensor {
9052 /**
9053 * BatteryStatsImpl that we are associated with.
9054 */
9055 protected BatteryStatsImpl mBsi;
9056
9057 /**
Bookatz867c0d72017-03-07 18:23:42 -08009058 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08009059 */
9060 protected Uid mUid;
9061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08009063 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08009064
Joe Onoratoabded112016-02-08 16:49:39 -08009065 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
9066 mBsi = bsi;
9067 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009068 mHandle = handle;
9069 }
9070
Bookatz867c0d72017-03-07 18:23:42 -08009071 private DualTimer readTimersFromParcel(
9072 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 if (in.readInt() == 0) {
9074 return null;
9075 }
9076
Joe Onoratoabded112016-02-08 16:49:39 -08009077 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07009079 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08009080 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 }
Bookatz867c0d72017-03-07 18:23:42 -08009082 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08009083 }
9084
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009085 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009086 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009087 mTimer = null;
9088 return true;
9089 }
9090 return false;
9091 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009092
Bookatz867c0d72017-03-07 18:23:42 -08009093 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
9094 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 }
9096
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009097 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07009098 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 }
9100
9101 @Override
9102 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07009103 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009104 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009105
9106 @Override
Bookatz867c0d72017-03-07 18:23:42 -08009107 public Timer getSensorBackgroundTime() {
9108 if (mTimer == null) {
9109 return null;
9110 }
9111 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08009112 }
9113
9114 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 public int getHandle() {
9116 return mHandle;
9117 }
9118 }
9119
9120 /**
9121 * The statistics associated with a particular process.
9122 */
Joe Onoratoabded112016-02-08 16:49:39 -08009123 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
9124 /**
9125 * BatteryStatsImpl that we are associated with.
9126 */
9127 protected BatteryStatsImpl mBsi;
9128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009129 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009130 * The name of this process.
9131 */
9132 final String mName;
9133
9134 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08009135 * Remains true until removed from the stats.
9136 */
9137 boolean mActive = true;
9138
9139 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07009140 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 */
9142 long mUserTime;
9143
9144 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07009145 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 */
9147 long mSystemTime;
9148
9149 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07009150 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009151 */
9152 long mForegroundTime;
9153
9154 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009155 * Number of times the process has been started.
9156 */
9157 int mStarts;
9158
9159 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009160 * Number of times the process has crashed.
9161 */
9162 int mNumCrashes;
9163
9164 /**
9165 * Number of times the process has had an ANR.
9166 */
9167 int mNumAnrs;
9168
9169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 * The amount of user time loaded from a previous save.
9171 */
9172 long mLoadedUserTime;
9173
9174 /**
9175 * The amount of system time loaded from a previous save.
9176 */
9177 long mLoadedSystemTime;
9178
9179 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009180 * The amount of foreground time loaded from a previous save.
9181 */
9182 long mLoadedForegroundTime;
9183
9184 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009185 * The number of times the process has started from a previous save.
9186 */
9187 int mLoadedStarts;
9188
9189 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009190 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009192 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193
9194 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009195 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009197 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009198
9199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 * The amount of user time when last unplugged.
9201 */
9202 long mUnpluggedUserTime;
9203
9204 /**
9205 * The amount of system time when last unplugged.
9206 */
9207 long mUnpluggedSystemTime;
9208
9209 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009210 * The amount of foreground time since unplugged.
9211 */
9212 long mUnpluggedForegroundTime;
9213
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009214 /**
9215 * The number of times the process has started before unplugged.
9216 */
9217 int mUnpluggedStarts;
9218
Dianne Hackborn61659e52014-07-09 16:13:01 -07009219 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009220 * Number of times the process has crashed before unplugged.
9221 */
9222 int mUnpluggedNumCrashes;
9223
9224 /**
9225 * Number of times the process has had an ANR before unplugged.
9226 */
9227 int mUnpluggedNumAnrs;
9228
Dianne Hackborn287952c2010-09-22 22:34:31 -07009229 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009230
Joe Onoratoabded112016-02-08 16:49:39 -08009231 public Proc(BatteryStatsImpl bsi, String name) {
9232 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009233 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08009234 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009236
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009237 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009238 mUnpluggedUserTime = mUserTime;
9239 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009240 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009241 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009242 mUnpluggedNumCrashes = mNumCrashes;
9243 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 }
9245
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009246 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009248
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009249 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009250 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08009251 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009252 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009253
Dianne Hackborn287952c2010-09-22 22:34:31 -07009254 public int countExcessivePowers() {
9255 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009256 }
9257
Dianne Hackborn287952c2010-09-22 22:34:31 -07009258 public ExcessivePower getExcessivePower(int i) {
9259 if (mExcessivePower != null) {
9260 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009261 }
9262 return null;
9263 }
9264
Dianne Hackborn287952c2010-09-22 22:34:31 -07009265 public void addExcessiveCpu(long overTime, long usedTime) {
9266 if (mExcessivePower == null) {
9267 mExcessivePower = new ArrayList<ExcessivePower>();
9268 }
9269 ExcessivePower ew = new ExcessivePower();
9270 ew.type = ExcessivePower.TYPE_CPU;
9271 ew.overTime = overTime;
9272 ew.usedTime = usedTime;
9273 mExcessivePower.add(ew);
9274 }
9275
9276 void writeExcessivePowerToParcelLocked(Parcel out) {
9277 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009278 out.writeInt(0);
9279 return;
9280 }
9281
Dianne Hackborn287952c2010-09-22 22:34:31 -07009282 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009283 out.writeInt(N);
9284 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009285 ExcessivePower ew = mExcessivePower.get(i);
9286 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009287 out.writeLong(ew.overTime);
9288 out.writeLong(ew.usedTime);
9289 }
9290 }
9291
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009292 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009293 final int N = in.readInt();
9294 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009295 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009296 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009297 }
9298
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009299 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009300 throw new ParcelFormatException(
9301 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009302 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009303
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009304 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009305 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009306 ExcessivePower ew = new ExcessivePower();
9307 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009308 ew.overTime = in.readLong();
9309 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07009310 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009311 }
9312 }
9313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 void writeToParcelLocked(Parcel out) {
9315 out.writeLong(mUserTime);
9316 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009317 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009319 out.writeInt(mNumCrashes);
9320 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 out.writeLong(mLoadedUserTime);
9322 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009323 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009324 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009325 out.writeInt(mLoadedNumCrashes);
9326 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 out.writeLong(mUnpluggedUserTime);
9328 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009329 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009330 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009331 out.writeInt(mUnpluggedNumCrashes);
9332 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009333 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 }
9335
9336 void readFromParcelLocked(Parcel in) {
9337 mUserTime = in.readLong();
9338 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009339 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009341 mNumCrashes = in.readInt();
9342 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 mLoadedUserTime = in.readLong();
9344 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009345 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009347 mLoadedNumCrashes = in.readInt();
9348 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 mUnpluggedUserTime = in.readLong();
9350 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009351 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009353 mUnpluggedNumCrashes = in.readInt();
9354 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07009355 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 }
9357
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009358 public void addCpuTimeLocked(int utime, int stime) {
Sudheer Shankac57729a2018-02-09 15:44:42 -08009359 addCpuTimeLocked(utime, stime, mBsi.mOnBatteryTimeBase.isRunning());
9360 }
9361
9362 public void addCpuTimeLocked(int utime, int stime, boolean isRunning) {
9363 if (isRunning) {
9364 mUserTime += utime;
9365 mSystemTime += stime;
9366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 }
9368
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009369 public void addForegroundTimeLocked(long ttime) {
9370 mForegroundTime += ttime;
9371 }
9372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 public void incStartsLocked() {
9374 mStarts++;
9375 }
9376
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009377 public void incNumCrashesLocked() {
9378 mNumCrashes++;
9379 }
9380
9381 public void incNumAnrsLocked() {
9382 mNumAnrs++;
9383 }
9384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009386 public boolean isActive() {
9387 return mActive;
9388 }
9389
9390 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009392 long val = mUserTime;
9393 if (which == STATS_CURRENT) {
9394 val -= mLoadedUserTime;
9395 } else if (which == STATS_SINCE_UNPLUGGED) {
9396 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 }
9398 return val;
9399 }
9400
9401 @Override
9402 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009403 long val = mSystemTime;
9404 if (which == STATS_CURRENT) {
9405 val -= mLoadedSystemTime;
9406 } else if (which == STATS_SINCE_UNPLUGGED) {
9407 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408 }
9409 return val;
9410 }
9411
9412 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009413 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009414 long val = mForegroundTime;
9415 if (which == STATS_CURRENT) {
9416 val -= mLoadedForegroundTime;
9417 } else if (which == STATS_SINCE_UNPLUGGED) {
9418 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07009419 }
9420 return val;
9421 }
9422
9423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009425 int val = mStarts;
9426 if (which == STATS_CURRENT) {
9427 val -= mLoadedStarts;
9428 } else if (which == STATS_SINCE_UNPLUGGED) {
9429 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 }
9431 return val;
9432 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009433
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009434 @Override
9435 public int getNumCrashes(int which) {
9436 int val = mNumCrashes;
9437 if (which == STATS_CURRENT) {
9438 val -= mLoadedNumCrashes;
9439 } else if (which == STATS_SINCE_UNPLUGGED) {
9440 val -= mUnpluggedNumCrashes;
9441 }
9442 return val;
9443 }
9444
9445 @Override
9446 public int getNumAnrs(int which) {
9447 int val = mNumAnrs;
9448 if (which == STATS_CURRENT) {
9449 val -= mLoadedNumAnrs;
9450 } else if (which == STATS_SINCE_UNPLUGGED) {
9451 val -= mUnpluggedNumAnrs;
9452 }
9453 return val;
9454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 }
9456
9457 /**
9458 * The statistics associated with a particular package.
9459 */
Joe Onoratoabded112016-02-08 16:49:39 -08009460 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
9461 /**
9462 * BatteryStatsImpl that we are associated with.
9463 */
9464 protected BatteryStatsImpl mBsi;
9465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009467 * Number of times wakeup alarms have occurred for this app.
Bookatz98d4d5c2017-08-01 19:07:54 -07009468 * On screen-off timebase starting in report v25.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009470 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471
9472 /**
9473 * The statics we have collected for this package's services.
9474 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009475 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476
Joe Onoratoabded112016-02-08 16:49:39 -08009477 public Pkg(BatteryStatsImpl bsi) {
9478 mBsi = bsi;
9479 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009480 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009481
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009482 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 }
9484
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009485 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009487
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009488 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08009489 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009490 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009493 int numWA = in.readInt();
9494 mWakeupAlarms.clear();
9495 for (int i=0; i<numWA; i++) {
9496 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -07009497 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryScreenOffTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499
9500 int numServs = in.readInt();
9501 mServiceStats.clear();
9502 for (int m = 0; m < numServs; m++) {
9503 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08009504 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009505 mServiceStats.put(serviceName, serv);
9506
9507 serv.readFromParcelLocked(in);
9508 }
9509 }
9510
9511 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009512 int numWA = mWakeupAlarms.size();
9513 out.writeInt(numWA);
9514 for (int i=0; i<numWA; i++) {
9515 out.writeString(mWakeupAlarms.keyAt(i));
9516 mWakeupAlarms.valueAt(i).writeToParcel(out);
9517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009518
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009519 final int NS = mServiceStats.size();
9520 out.writeInt(NS);
9521 for (int i=0; i<NS; i++) {
9522 out.writeString(mServiceStats.keyAt(i));
9523 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 serv.writeToParcelLocked(out);
9525 }
9526 }
9527
9528 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009529 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
9530 return mWakeupAlarms;
9531 }
9532
9533 public void noteWakeupAlarmLocked(String tag) {
9534 Counter c = mWakeupAlarms.get(tag);
9535 if (c == null) {
Bookatz98d4d5c2017-08-01 19:07:54 -07009536 c = new Counter(mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009537 mWakeupAlarms.put(tag, c);
9538 }
9539 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 }
9541
9542 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009543 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
9544 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 }
9546
9547 /**
9548 * The statistics associated with a particular service.
9549 */
Joe Onoratoabded112016-02-08 16:49:39 -08009550 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
9551 /**
9552 * BatteryStatsImpl that we are associated with.
9553 */
9554 protected BatteryStatsImpl mBsi;
9555
9556 /**
9557 * The android package in which this service resides.
9558 */
9559 protected Pkg mPkg;
9560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009561 /**
9562 * Total time (ms in battery uptime) the service has been left started.
9563 */
Joe Onoratoabded112016-02-08 16:49:39 -08009564 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565
9566 /**
9567 * If service has been started and not yet stopped, this is
9568 * when it was started.
9569 */
Joe Onoratoabded112016-02-08 16:49:39 -08009570 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009571
9572 /**
9573 * True if we are currently running.
9574 */
Joe Onoratoabded112016-02-08 16:49:39 -08009575 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576
9577 /**
9578 * Total number of times startService() has been called.
9579 */
Joe Onoratoabded112016-02-08 16:49:39 -08009580 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581
9582 /**
9583 * Total time (ms in battery uptime) the service has been left launched.
9584 */
Joe Onoratoabded112016-02-08 16:49:39 -08009585 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586
9587 /**
9588 * If service has been launched and not yet exited, this is
9589 * when it was launched (ms in battery uptime).
9590 */
Joe Onoratoabded112016-02-08 16:49:39 -08009591 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592
9593 /**
9594 * True if we are currently launched.
9595 */
Joe Onoratoabded112016-02-08 16:49:39 -08009596 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597
9598 /**
9599 * Total number times the service has been launched.
9600 */
Joe Onoratoabded112016-02-08 16:49:39 -08009601 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602
9603 /**
9604 * The amount of time spent started loaded from a previous save
9605 * (ms in battery uptime).
9606 */
Joe Onoratoabded112016-02-08 16:49:39 -08009607 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608
9609 /**
9610 * The number of starts loaded from a previous save.
9611 */
Joe Onoratoabded112016-02-08 16:49:39 -08009612 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613
9614 /**
9615 * The number of launches loaded from a previous save.
9616 */
Joe Onoratoabded112016-02-08 16:49:39 -08009617 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009618
9619 /**
9620 * The amount of time spent started as of the last run (ms
9621 * in battery uptime).
9622 */
Joe Onoratoabded112016-02-08 16:49:39 -08009623 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009624
9625 /**
9626 * The number of starts as of the last run.
9627 */
Joe Onoratoabded112016-02-08 16:49:39 -08009628 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629
9630 /**
9631 * The number of launches as of the last run.
9632 */
Joe Onoratoabded112016-02-08 16:49:39 -08009633 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009634
9635 /**
9636 * The amount of time spent started when last unplugged (ms
9637 * in battery uptime).
9638 */
Joe Onoratoabded112016-02-08 16:49:39 -08009639 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640
9641 /**
9642 * The number of starts when last unplugged.
9643 */
Joe Onoratoabded112016-02-08 16:49:39 -08009644 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645
9646 /**
9647 * The number of launches when last unplugged.
9648 */
Joe Onoratoabded112016-02-08 16:49:39 -08009649 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650
Joe Onoratoabded112016-02-08 16:49:39 -08009651 /**
9652 * Construct a Serv. Also adds it to the on-battery time base as a listener.
9653 */
9654 public Serv(BatteryStatsImpl bsi) {
9655 mBsi = bsi;
9656 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009657 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009658
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009659 public void onTimeStarted(long elapsedRealtime, long baseUptime,
9660 long baseRealtime) {
9661 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 mUnpluggedStarts = mStarts;
9663 mUnpluggedLaunches = mLaunches;
9664 }
9665
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009666 public void onTimeStopped(long elapsedRealtime, long baseUptime,
9667 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009668 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009669
Joe Onoratoabded112016-02-08 16:49:39 -08009670 /**
9671 * Remove this Serv as a listener from the time base.
9672 */
9673 public void detach() {
9674 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009675 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009676
Joe Onoratoabded112016-02-08 16:49:39 -08009677 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 mStartTime = in.readLong();
9679 mRunningSince = in.readLong();
9680 mRunning = in.readInt() != 0;
9681 mStarts = in.readInt();
9682 mLaunchedTime = in.readLong();
9683 mLaunchedSince = in.readLong();
9684 mLaunched = in.readInt() != 0;
9685 mLaunches = in.readInt();
9686 mLoadedStartTime = in.readLong();
9687 mLoadedStarts = in.readInt();
9688 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009689 mLastStartTime = 0;
9690 mLastStarts = 0;
9691 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 mUnpluggedStartTime = in.readLong();
9693 mUnpluggedStarts = in.readInt();
9694 mUnpluggedLaunches = in.readInt();
9695 }
9696
Joe Onoratoabded112016-02-08 16:49:39 -08009697 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 out.writeLong(mStartTime);
9699 out.writeLong(mRunningSince);
9700 out.writeInt(mRunning ? 1 : 0);
9701 out.writeInt(mStarts);
9702 out.writeLong(mLaunchedTime);
9703 out.writeLong(mLaunchedSince);
9704 out.writeInt(mLaunched ? 1 : 0);
9705 out.writeInt(mLaunches);
9706 out.writeLong(mLoadedStartTime);
9707 out.writeInt(mLoadedStarts);
9708 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 out.writeLong(mUnpluggedStartTime);
9710 out.writeInt(mUnpluggedStarts);
9711 out.writeInt(mUnpluggedLaunches);
9712 }
9713
Joe Onoratoabded112016-02-08 16:49:39 -08009714 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 if (!mLaunched) return mLaunchedTime;
9716 return mLaunchedTime + batteryUptime - mLaunchedSince;
9717 }
9718
Joe Onoratoabded112016-02-08 16:49:39 -08009719 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009720 if (!mRunning) return mStartTime;
9721 return mStartTime + batteryUptime - mRunningSince;
9722 }
9723
9724 public void startLaunchedLocked() {
9725 if (!mLaunched) {
9726 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08009727 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009728 mLaunched = true;
9729 }
9730 }
9731
9732 public void stopLaunchedLocked() {
9733 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08009734 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009735 if (time > 0) {
9736 mLaunchedTime += time;
9737 } else {
9738 mLaunches--;
9739 }
9740 mLaunched = false;
9741 }
9742 }
9743
9744 public void startRunningLocked() {
9745 if (!mRunning) {
9746 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08009747 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 mRunning = true;
9749 }
9750 }
9751
9752 public void stopRunningLocked() {
9753 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08009754 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 if (time > 0) {
9756 mStartTime += time;
9757 } else {
9758 mStarts--;
9759 }
9760 mRunning = false;
9761 }
9762 }
9763
9764 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08009765 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 }
9767
9768 @Override
9769 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009770 int val = mLaunches;
9771 if (which == STATS_CURRENT) {
9772 val -= mLoadedLaunches;
9773 } else if (which == STATS_SINCE_UNPLUGGED) {
9774 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009776 return val;
9777 }
9778
9779 @Override
9780 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009781 long val = getStartTimeToNowLocked(now);
9782 if (which == STATS_CURRENT) {
9783 val -= mLoadedStartTime;
9784 } else if (which == STATS_SINCE_UNPLUGGED) {
9785 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 return val;
9788 }
9789
9790 @Override
9791 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07009792 int val = mStarts;
9793 if (which == STATS_CURRENT) {
9794 val -= mLoadedStarts;
9795 } else if (which == STATS_SINCE_UNPLUGGED) {
9796 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 }
9798
9799 return val;
9800 }
9801 }
9802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009804 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 }
9806 }
9807
9808 /**
9809 * Retrieve the statistics object for a particular process, creating
9810 * if needed.
9811 */
9812 public Proc getProcessStatsLocked(String name) {
9813 Proc ps = mProcessStats.get(name);
9814 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009815 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 mProcessStats.put(name, ps);
9817 }
9818
9819 return ps;
9820 }
9821
Andreas Gampe3f24e692018-02-05 13:24:28 -08009822 @GuardedBy("mBsi")
Dianne Hackborna8d10942015-11-19 17:55:19 -08009823 public void updateUidProcessStateLocked(int procState) {
9824 int uidRunningState;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009825 // Make special note of Foreground Services
9826 final boolean userAwareService =
9827 (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
Sudheer Shankab2f83c12017-11-13 19:25:01 -08009828 uidRunningState = BatteryStats.mapToInternalProcessState(procState);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009829
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009830 if (mProcessState == uidRunningState && userAwareService == mInForegroundService) {
9831 return;
9832 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08009833
Bookatz867c0d72017-03-07 18:23:42 -08009834 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009835 if (mProcessState != uidRunningState) {
9836 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08009837
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009838 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
9839 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Sudheer Shankab2f83c12017-11-13 19:25:01 -08009840
Sudheer Shanka5c19b892018-01-05 17:25:46 -08009841 if (mBsi.trackPerProcStateCpuTimes()) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08009842 if (mBsi.mPendingUids.size() == 0) {
Sudheer Shankae544d162017-12-28 17:06:20 -08009843 mBsi.mExternalSync.scheduleReadProcStateCpuTimes(
9844 mBsi.mOnBatteryTimeBase.isRunning(),
Sudheer Shankac20379e2018-02-15 00:06:21 -08009845 mBsi.mOnBatteryScreenOffTimeBase.isRunning(),
9846 mBsi.mConstants.PROC_STATE_CPU_TIMES_READ_DELAY_MS);
9847 mBsi.mNumCpuTimeReads++;
9848 } else {
9849 mBsi.mNumBatchedCpuTimeReads++;
Sudheer Shankab2f83c12017-11-13 19:25:01 -08009850 }
9851 if (mBsi.mPendingUids.indexOfKey(mUid) < 0
9852 || ArrayUtils.contains(CRITICAL_PROC_STATES, mProcessState)) {
9853 mBsi.mPendingUids.put(mUid, mProcessState);
9854 }
9855 } else {
9856 mBsi.mPendingUids.clear();
9857 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07009858 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009859 mProcessState = uidRunningState;
9860 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
9861 if (mProcessStateTimer[uidRunningState] == null) {
9862 makeProcessState(uidRunningState, null);
9863 }
9864 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
9865 }
9866
9867 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
9868 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009869 }
Bookatz867c0d72017-03-07 18:23:42 -08009870
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07009871 if (userAwareService != mInForegroundService) {
9872 if (userAwareService) {
9873 noteForegroundServiceResumedLocked(elapsedRealtimeMs);
9874 } else {
9875 noteForegroundServicePausedLocked(elapsedRealtimeMs);
9876 }
9877 mInForegroundService = userAwareService;
9878 }
Bookatz867c0d72017-03-07 18:23:42 -08009879 }
9880
Bookatzc8c44962017-05-11 12:12:54 -07009881 /** Whether to consider Uid to be in the background for background timebase purposes. */
9882 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08009883 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
9884 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07009885 return mProcessState >= PROCESS_STATE_BACKGROUND;
9886 }
9887
9888 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
9889 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
9890 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
9891 }
9892
9893 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
9894 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
9895 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009896 }
9897
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009898 public SparseArray<? extends Pid> getPidStats() {
9899 return mPids;
9900 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009901
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009902 public Pid getPidStatsLocked(int pid) {
9903 Pid p = mPids.get(pid);
9904 if (p == null) {
9905 p = new Pid();
9906 mPids.put(pid, p);
9907 }
9908 return p;
9909 }
9910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 /**
9912 * Retrieve the statistics object for a particular service, creating
9913 * if needed.
9914 */
9915 public Pkg getPackageStatsLocked(String name) {
9916 Pkg ps = mPackageStats.get(name);
9917 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009918 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 mPackageStats.put(name, ps);
9920 }
9921
9922 return ps;
9923 }
9924
9925 /**
9926 * Retrieve the statistics object for a particular service, creating
9927 * if needed.
9928 */
9929 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
9930 Pkg ps = getPackageStatsLocked(pkg);
9931 Pkg.Serv ss = ps.mServiceStats.get(serv);
9932 if (ss == null) {
9933 ss = ps.newServiceStatsLocked();
9934 ps.mServiceStats.put(serv, ss);
9935 }
9936
9937 return ss;
9938 }
9939
Dianne Hackbornd953c532014-08-16 18:17:38 -07009940 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07009941 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009942 timer.readSummaryFromParcelLocked(in);
9943 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009944 }
9945
Dianne Hackbornd953c532014-08-16 18:17:38 -07009946 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07009947 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009948 timer.readSummaryFromParcelLocked(in);
9949 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009950 }
9951
Dianne Hackbornd953c532014-08-16 18:17:38 -07009952 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08009953 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07009954 mWakelockStats.add(wlName, wl);
9955 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07009956 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07009958 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07009959 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07009960 }
9961 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07009962 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07009964 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07009965 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07009966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 }
9968
Bookatz867c0d72017-03-07 18:23:42 -08009969 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 Sensor se = mSensorStats.get(sensor);
9971 if (se == null) {
9972 if (!create) {
9973 return null;
9974 }
Joe Onoratoabded112016-02-08 16:49:39 -08009975 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 mSensorStats.put(sensor, se);
9977 }
Bookatz867c0d72017-03-07 18:23:42 -08009978 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 if (t != null) {
9980 return t;
9981 }
Joe Onoratoabded112016-02-08 16:49:39 -08009982 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009983 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07009984 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08009985 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 }
Bookatz867c0d72017-03-07 18:23:42 -08009987 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
9988 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 se.mTimer = t;
9990 return t;
9991 }
9992
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009993 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07009994 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009995 if (t != null) {
9996 t.startRunningLocked(elapsedRealtimeMs);
Bookatz90867622018-01-31 15:05:57 -08009997 StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, getUid(), null, name,
9998 StatsLog.SYNC_STATE_CHANGED__STATE__ON);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009999 }
10000 }
10001
10002 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -070010003 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010004 if (t != null) {
10005 t.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -070010006 if (!t.isRunningLocked()) { // only tell statsd if truly stopped
Bookatz90867622018-01-31 15:05:57 -080010007 StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, getUid(), null, name,
10008 StatsLog.SYNC_STATE_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -070010009 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010010 }
10011 }
10012
10013 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -070010014 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010015 if (t != null) {
10016 t.startRunningLocked(elapsedRealtimeMs);
Yangster-mac2f5daec2018-01-16 10:23:15 -080010017 StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, getUid(), null,
Bookatz90867622018-01-31 15:05:57 -080010018 name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__STARTED);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010019 }
10020 }
10021
Dianne Hackborn94326cb2017-06-28 16:17:20 -070010022 public void noteStopJobLocked(String name, long elapsedRealtimeMs, int stopReason) {
Bookatzaa4594a2017-03-24 12:39:56 -070010023 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010024 if (t != null) {
10025 t.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -070010026 if (!t.isRunningLocked()) { // only tell statsd if truly stopped
Yangster-mac2f5daec2018-01-16 10:23:15 -080010027 StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, getUid(), null,
Bookatz90867622018-01-31 15:05:57 -080010028 name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__FINISHED);
Bookatzc1a050a2017-10-10 15:49:28 -070010029 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010030 }
Dianne Hackborn94326cb2017-06-28 16:17:20 -070010031 if (mBsi.mOnBatteryTimeBase.isRunning()) {
10032 SparseIntArray types = mJobCompletions.get(name);
10033 if (types == null) {
10034 types = new SparseIntArray();
10035 mJobCompletions.put(name, types);
10036 }
10037 int last = types.get(stopReason, 0);
10038 types.put(stopReason, last + 1);
10039 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010040 }
10041
Bookatz5b5ec322017-05-26 09:40:38 -070010042 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
10043 if (wl == null) {
10044 return null;
10045 }
10046 switch (type) {
10047 case WAKE_TYPE_PARTIAL: {
10048 DualTimer t = wl.mTimerPartial;
10049 if (t == null) {
10050 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
10051 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
10052 mOnBatteryScreenOffBackgroundTimeBase);
10053 wl.mTimerPartial = t;
10054 }
10055 return t;
10056 }
10057 case WAKE_TYPE_FULL: {
10058 StopwatchTimer t = wl.mTimerFull;
10059 if (t == null) {
10060 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
10061 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
10062 wl.mTimerFull = t;
10063 }
10064 return t;
10065 }
10066 case WAKE_TYPE_WINDOW: {
10067 StopwatchTimer t = wl.mTimerWindow;
10068 if (t == null) {
10069 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
10070 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
10071 wl.mTimerWindow = t;
10072 }
10073 return t;
10074 }
10075 case WAKE_TYPE_DRAW: {
10076 StopwatchTimer t = wl.mTimerDraw;
10077 if (t == null) {
10078 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
10079 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
10080 wl.mTimerDraw = t;
10081 }
10082 return t;
10083 }
10084 default:
10085 throw new IllegalArgumentException("type=" + type);
10086 }
10087 }
10088
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010089 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010090 Wakelock wl = mWakelockStats.startObject(name);
10091 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -070010092 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010093 }
Bookatzc8c44962017-05-11 12:12:54 -070010094 if (type == WAKE_TYPE_PARTIAL) {
10095 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
10096 if (pid >= 0) {
10097 Pid p = getPidStatsLocked(pid);
10098 if (p.mWakeNesting++ == 0) {
10099 p.mWakeStartMs = elapsedRealtimeMs;
10100 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -070010101 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070010102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 }
10104
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010105 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010106 Wakelock wl = mWakelockStats.stopObject(name);
10107 if (wl != null) {
Bookatzd6746242017-10-24 18:39:35 -070010108 StopwatchTimer wlt = getWakelockTimerLocked(wl, type);
10109 wlt.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110 }
Bookatzc8c44962017-05-11 12:12:54 -070010111 if (type == WAKE_TYPE_PARTIAL) {
10112 if (mAggregatedPartialWakelockTimer != null) {
10113 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
10114 }
10115 if (pid >= 0) {
10116 Pid p = mPids.get(pid);
10117 if (p != null && p.mWakeNesting > 0) {
10118 if (p.mWakeNesting-- == 1) {
10119 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
10120 p.mWakeStartMs = 0;
10121 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010122 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070010123 }
10124 }
10125 }
10126
Dianne Hackborn287952c2010-09-22 22:34:31 -070010127 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
10128 Proc p = getProcessStatsLocked(proc);
10129 if (p != null) {
10130 p.addExcessiveCpu(overTime, usedTime);
10131 }
10132 }
10133
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010134 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -080010135 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -080010136 t.startRunningLocked(elapsedRealtimeMs);
Narayan Kamath32684dd2018-01-08 17:32:51 +000010137 if (sensor != Sensor.GPS) {
Yangster-mac2f5daec2018-01-16 10:23:15 -080010138 StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, getUid(), null, sensor,
Bookatz90867622018-01-31 15:05:57 -080010139 StatsLog.SENSOR_STATE_CHANGED__STATE__ON);
Bookatzc1a050a2017-10-10 15:49:28 -070010140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 }
10142
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010143 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -080010145 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010147 t.stopRunningLocked(elapsedRealtimeMs);
Narayan Kamath32684dd2018-01-08 17:32:51 +000010148 if (sensor != Sensor.GPS) {
10149 StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, getUid(), null,
Bookatz90867622018-01-31 15:05:57 -080010150 sensor, StatsLog.SENSOR_STATE_CHANGED__STATE__OFF);
Bookatzc1a050a2017-10-10 15:49:28 -070010151 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010153 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010154
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010155 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -080010156 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010158
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010159 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -080010160 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 }
10162
10163 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -080010164 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 }
10166 }
10167
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010168 public long[] getCpuFreqs() {
10169 return mCpuFreqs;
10170 }
10171
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010172 public BatteryStatsImpl(File systemDir, Handler handler, PlatformIdleStateCallback cb,
10173 UserInfoProvider userInfoProvider) {
10174 this(new SystemClocks(), systemDir, handler, cb, userInfoProvider);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -070010175 }
10176
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010177 private BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
10178 PlatformIdleStateCallback cb,
10179 UserInfoProvider userInfoProvider) {
Joe Onoratoabded112016-02-08 16:49:39 -080010180 init(clocks);
10181
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010182 if (systemDir != null) {
10183 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
10184 new File(systemDir, "batterystats.bin.tmp"));
10185 } else {
10186 mFile = null;
10187 }
10188 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010189 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -080010190 mHandler = new MyHandler(handler.getLooper());
Sudheer Shanka5c19b892018-01-05 17:25:46 -080010191 mConstants = new Constants(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -080010193 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -070010194 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010195 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010196 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010197 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010198 }
Joe Onoratoabded112016-02-08 16:49:39 -080010199 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
10200 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10201 mOnBatteryTimeBase);
10202 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
10203 mOnBatteryTimeBase);
10204 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
10205 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
10206 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
10207 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
10208 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
10209 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
10210 mOnBatteryTimeBase);
10211 }
10212 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10213 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010214 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010215 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010216 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010217 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010218 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010219 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
10220 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010221 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010222 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
10223 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10224 NUM_BT_TX_LEVELS);
10225 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10226 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -080010227 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
10228 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10229 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010230 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010231 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
10232 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080010233 mWifiMulticastWakelockTimer = new StopwatchTimer(mClocks, null,
10234 WIFI_AGGREGATE_MULTICAST_ENABLED, null, mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -080010235 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
10236 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010237 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010238 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010239 mOnBatteryTimeBase);
10240 }
Joe Onoratoabded112016-02-08 16:49:39 -080010241 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10242 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
10243 mOnBatteryTimeBase);
10244 }
10245 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10246 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
10247 mOnBatteryTimeBase);
10248 }
Siddharth Rayb50a6842017-12-14 15:15:28 -080010249 mWifiActiveTimer = new StopwatchTimer(mClocks, null, -900, null, mOnBatteryTimeBase);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080010250 for (int i=0; i< GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
10251 mGpsSignalQualityTimer[i] = new StopwatchTimer(mClocks, null, -1000-i, null,
10252 mOnBatteryTimeBase);
10253 }
Joe Onoratoabded112016-02-08 16:49:39 -080010254 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
10255 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
10256 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
10257 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
10258 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010259 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -070010260 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
Mike Ma15313c92017-11-15 17:58:21 -080010261 mDischargeLightDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
10262 mDischargeDeepDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010263 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010264 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010265 long uptime = mClocks.uptimeMillis() * 1000;
10266 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010267 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010268 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -070010269 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010270 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010271 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -070010272 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010273 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010274 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010275 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010276 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -070010277 mPlatformIdleStateCallback = cb;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010278 mUserInfoProvider = userInfoProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 }
10280
10281 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -080010282 this(new SystemClocks(), p);
10283 }
10284
10285 public BatteryStatsImpl(Clocks clocks, Parcel p) {
10286 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010287 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010288 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010289 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070010290 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010291 mExternalSync = null;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080010292 mConstants = new Constants(mHandler);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010293 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -070010295 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 }
10297
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010298 public void setPowerProfileLocked(PowerProfile profile) {
10299 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -070010300
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010301 // We need to initialize the KernelCpuSpeedReaders to read from
10302 // the first cpu of each core. Once we have the PowerProfile, we have access to this
10303 // information.
10304 final int numClusters = mPowerProfile.getNumCpuClusters();
10305 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
10306 int firstCpuOfCluster = 0;
10307 for (int i = 0; i < numClusters; i++) {
10308 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
10309 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
10310 numSpeedSteps);
10311 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
10312 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010313
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010314 if (mEstimatedBatteryCapacity == -1) {
10315 // Initialize the estimated battery capacity to a known preset one.
10316 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -070010317 }
10318 }
10319
Dianne Hackborn0d903a82010-09-07 23:51:03 -070010320 public void setCallback(BatteryCallback cb) {
10321 mCallback = cb;
10322 }
10323
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010324 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -070010325 if (mPhoneSignalScanningTimer != null) {
10326 mPhoneSignalScanningTimer.setTimeout(timeout);
10327 }
10328 }
10329
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070010330 public void setExternalStatsSyncLocked(ExternalStatsSync sync) {
10331 mExternalSync = sync;
10332 }
10333
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010334 public void updateDailyDeadlineLocked() {
10335 // Get the current time.
10336 long currentTime = mDailyStartTime = System.currentTimeMillis();
10337 Calendar calDeadline = Calendar.getInstance();
10338 calDeadline.setTimeInMillis(currentTime);
10339
10340 // Move time up to the next day, ranging from 1am to 3pm.
10341 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
10342 calDeadline.set(Calendar.MILLISECOND, 0);
10343 calDeadline.set(Calendar.SECOND, 0);
10344 calDeadline.set(Calendar.MINUTE, 0);
10345 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
10346 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
10347 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
10348 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
10349 }
10350
10351 public void recordDailyStatsIfNeededLocked(boolean settled) {
10352 long currentTime = System.currentTimeMillis();
10353 if (currentTime >= mNextMaxDailyDeadline) {
10354 recordDailyStatsLocked();
10355 } else if (settled && currentTime >= mNextMinDailyDeadline) {
10356 recordDailyStatsLocked();
10357 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
10358 recordDailyStatsLocked();
10359 }
10360 }
10361
10362 public void recordDailyStatsLocked() {
10363 DailyItem item = new DailyItem();
10364 item.mStartTime = mDailyStartTime;
10365 item.mEndTime = System.currentTimeMillis();
10366 boolean hasData = false;
10367 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
10368 hasData = true;
10369 item.mDischargeSteps = new LevelStepTracker(
10370 mDailyDischargeStepTracker.mNumStepDurations,
10371 mDailyDischargeStepTracker.mStepDurations);
10372 }
10373 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
10374 hasData = true;
10375 item.mChargeSteps = new LevelStepTracker(
10376 mDailyChargeStepTracker.mNumStepDurations,
10377 mDailyChargeStepTracker.mStepDurations);
10378 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010379 if (mDailyPackageChanges != null) {
10380 hasData = true;
10381 item.mPackageChanges = mDailyPackageChanges;
10382 mDailyPackageChanges = null;
10383 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010384 mDailyDischargeStepTracker.init();
10385 mDailyChargeStepTracker.init();
10386 updateDailyDeadlineLocked();
10387
10388 if (hasData) {
Dianne Hackborne17b4452018-01-10 13:15:40 -080010389 final long startTime = SystemClock.uptimeMillis();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010390 mDailyItems.add(item);
10391 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
10392 mDailyItems.remove(0);
10393 }
10394 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
10395 try {
10396 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010010397 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010398 writeDailyItemsLocked(out);
Dianne Hackborne17b4452018-01-10 13:15:40 -080010399 final long initialTime = SystemClock.uptimeMillis() - startTime;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010400 BackgroundThread.getHandler().post(new Runnable() {
10401 @Override
10402 public void run() {
10403 synchronized (mCheckinFile) {
Dianne Hackborne17b4452018-01-10 13:15:40 -080010404 final long startTime2 = SystemClock.uptimeMillis();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010405 FileOutputStream stream = null;
10406 try {
10407 stream = mDailyFile.startWrite();
10408 memStream.writeTo(stream);
10409 stream.flush();
10410 FileUtils.sync(stream);
10411 stream.close();
10412 mDailyFile.finishWrite(stream);
Dianne Hackborne17b4452018-01-10 13:15:40 -080010413 com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
10414 "batterystats-daily",
10415 initialTime + SystemClock.uptimeMillis() - startTime2);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010416 } catch (IOException e) {
10417 Slog.w("BatteryStats",
10418 "Error writing battery daily items", e);
10419 mDailyFile.failWrite(stream);
10420 }
10421 }
10422 }
10423 });
10424 } catch (IOException e) {
10425 }
10426 }
10427 }
10428
10429 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
10430 StringBuilder sb = new StringBuilder(64);
10431 out.startDocument(null, true);
10432 out.startTag(null, "daily-items");
10433 for (int i=0; i<mDailyItems.size(); i++) {
10434 final DailyItem dit = mDailyItems.get(i);
10435 out.startTag(null, "item");
10436 out.attribute(null, "start", Long.toString(dit.mStartTime));
10437 out.attribute(null, "end", Long.toString(dit.mEndTime));
10438 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
10439 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010440 if (dit.mPackageChanges != null) {
10441 for (int j=0; j<dit.mPackageChanges.size(); j++) {
10442 PackageChange pc = dit.mPackageChanges.get(j);
10443 if (pc.mUpdate) {
10444 out.startTag(null, "upd");
10445 out.attribute(null, "pkg", pc.mPackageName);
Dianne Hackborn3accca02013-09-20 09:32:11 -070010446 out.attribute(null, "ver", Long.toString(pc.mVersionCode));
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010447 out.endTag(null, "upd");
10448 } else {
10449 out.startTag(null, "rem");
10450 out.attribute(null, "pkg", pc.mPackageName);
10451 out.endTag(null, "rem");
10452 }
10453 }
10454 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010455 out.endTag(null, "item");
10456 }
10457 out.endTag(null, "daily-items");
10458 out.endDocument();
10459 }
10460
10461 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
10462 StringBuilder tmpBuilder) throws IOException {
10463 if (steps != null) {
10464 out.startTag(null, tag);
10465 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
10466 for (int i=0; i<steps.mNumStepDurations; i++) {
10467 out.startTag(null, "s");
10468 tmpBuilder.setLength(0);
10469 steps.encodeEntryAt(i, tmpBuilder);
10470 out.attribute(null, "v", tmpBuilder.toString());
10471 out.endTag(null, "s");
10472 }
10473 out.endTag(null, tag);
10474 }
10475 }
10476
10477 public void readDailyStatsLocked() {
10478 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
10479 mDailyItems.clear();
10480 FileInputStream stream;
10481 try {
10482 stream = mDailyFile.openRead();
10483 } catch (FileNotFoundException e) {
10484 return;
10485 }
10486 try {
10487 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010010488 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010489 readDailyItemsLocked(parser);
10490 } catch (XmlPullParserException e) {
10491 } finally {
10492 try {
10493 stream.close();
10494 } catch (IOException e) {
10495 }
10496 }
10497 }
10498
10499 private void readDailyItemsLocked(XmlPullParser parser) {
10500 try {
10501 int type;
10502 while ((type = parser.next()) != XmlPullParser.START_TAG
10503 && type != XmlPullParser.END_DOCUMENT) {
10504 ;
10505 }
10506
10507 if (type != XmlPullParser.START_TAG) {
10508 throw new IllegalStateException("no start tag found");
10509 }
10510
10511 int outerDepth = parser.getDepth();
10512 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
10513 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
10514 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
10515 continue;
10516 }
10517
10518 String tagName = parser.getName();
10519 if (tagName.equals("item")) {
10520 readDailyItemTagLocked(parser);
10521 } else {
10522 Slog.w(TAG, "Unknown element under <daily-items>: "
10523 + parser.getName());
10524 XmlUtils.skipCurrentTag(parser);
10525 }
10526 }
10527
10528 } catch (IllegalStateException e) {
10529 Slog.w(TAG, "Failed parsing daily " + e);
10530 } catch (NullPointerException e) {
10531 Slog.w(TAG, "Failed parsing daily " + e);
10532 } catch (NumberFormatException e) {
10533 Slog.w(TAG, "Failed parsing daily " + e);
10534 } catch (XmlPullParserException e) {
10535 Slog.w(TAG, "Failed parsing daily " + e);
10536 } catch (IOException e) {
10537 Slog.w(TAG, "Failed parsing daily " + e);
10538 } catch (IndexOutOfBoundsException e) {
10539 Slog.w(TAG, "Failed parsing daily " + e);
10540 }
10541 }
10542
10543 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
10544 XmlPullParserException, IOException {
10545 DailyItem dit = new DailyItem();
10546 String attr = parser.getAttributeValue(null, "start");
10547 if (attr != null) {
10548 dit.mStartTime = Long.parseLong(attr);
10549 }
10550 attr = parser.getAttributeValue(null, "end");
10551 if (attr != null) {
10552 dit.mEndTime = Long.parseLong(attr);
10553 }
10554 int outerDepth = parser.getDepth();
10555 int type;
10556 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
10557 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
10558 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
10559 continue;
10560 }
10561
10562 String tagName = parser.getName();
10563 if (tagName.equals("dis")) {
10564 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
10565 } else if (tagName.equals("chg")) {
10566 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010567 } else if (tagName.equals("upd")) {
10568 if (dit.mPackageChanges == null) {
10569 dit.mPackageChanges = new ArrayList<>();
10570 }
10571 PackageChange pc = new PackageChange();
10572 pc.mUpdate = true;
10573 pc.mPackageName = parser.getAttributeValue(null, "pkg");
10574 String verStr = parser.getAttributeValue(null, "ver");
Dianne Hackborn3accca02013-09-20 09:32:11 -070010575 pc.mVersionCode = verStr != null ? Long.parseLong(verStr) : 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010576 dit.mPackageChanges.add(pc);
10577 XmlUtils.skipCurrentTag(parser);
10578 } else if (tagName.equals("rem")) {
10579 if (dit.mPackageChanges == null) {
10580 dit.mPackageChanges = new ArrayList<>();
10581 }
10582 PackageChange pc = new PackageChange();
10583 pc.mUpdate = false;
10584 pc.mPackageName = parser.getAttributeValue(null, "pkg");
10585 dit.mPackageChanges.add(pc);
10586 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010587 } else {
10588 Slog.w(TAG, "Unknown element under <item>: "
10589 + parser.getName());
10590 XmlUtils.skipCurrentTag(parser);
10591 }
10592 }
10593 mDailyItems.add(dit);
10594 }
10595
10596 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
10597 String tag)
10598 throws NumberFormatException, XmlPullParserException, IOException {
10599 final String numAttr = parser.getAttributeValue(null, "n");
10600 if (numAttr == null) {
10601 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
10602 XmlUtils.skipCurrentTag(parser);
10603 return;
10604 }
10605 final int num = Integer.parseInt(numAttr);
10606 LevelStepTracker steps = new LevelStepTracker(num);
10607 if (isCharge) {
10608 dit.mChargeSteps = steps;
10609 } else {
10610 dit.mDischargeSteps = steps;
10611 }
10612 int i = 0;
10613 int outerDepth = parser.getDepth();
10614 int type;
10615 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
10616 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
10617 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
10618 continue;
10619 }
10620
10621 String tagName = parser.getName();
10622 if ("s".equals(tagName)) {
10623 if (i < num) {
10624 String valueAttr = parser.getAttributeValue(null, "v");
10625 if (valueAttr != null) {
10626 steps.decodeEntryAt(i, valueAttr);
10627 i++;
10628 }
10629 }
10630 } else {
10631 Slog.w(TAG, "Unknown element under <" + tag + ">: "
10632 + parser.getName());
10633 XmlUtils.skipCurrentTag(parser);
10634 }
10635 }
10636 steps.mNumStepDurations = i;
10637 }
10638
10639 @Override
10640 public DailyItem getDailyItemLocked(int daysAgo) {
10641 int index = mDailyItems.size()-1-daysAgo;
10642 return index >= 0 ? mDailyItems.get(index) : null;
10643 }
10644
10645 @Override
10646 public long getCurrentDailyStartTime() {
10647 return mDailyStartTime;
10648 }
10649
10650 @Override
10651 public long getNextMinDailyDeadline() {
10652 return mNextMinDailyDeadline;
10653 }
10654
10655 @Override
10656 public long getNextMaxDailyDeadline() {
10657 return mNextMaxDailyDeadline;
10658 }
10659
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010660 @Override
10661 public boolean startIteratingOldHistoryLocked() {
10662 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
10663 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010664 if ((mHistoryIterator = mHistory) == null) {
10665 return false;
10666 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010667 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -070010668 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010669 mReadOverflow = false;
10670 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010671 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010672 }
10673
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010674 @Override
10675 public boolean getNextOldHistoryLocked(HistoryItem out) {
10676 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
10677 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010678 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -070010679 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010680 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010681 HistoryItem cur = mHistoryIterator;
10682 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010683 if (!mReadOverflow && !end) {
10684 Slog.w(TAG, "Old history ends before new history!");
10685 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010686 return false;
10687 }
10688 out.setTo(cur);
10689 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010690 if (!mReadOverflow) {
10691 if (end) {
10692 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -070010693 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -070010694 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010695 pw.println("Histories differ!");
10696 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -070010697 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010698 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -070010699 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
10700 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -070010701 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010702 }
10703 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010704 return true;
10705 }
10706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010708 public void finishIteratingOldHistoryLocked() {
10709 mIteratingHistory = false;
10710 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010711 mHistoryIterator = null;
10712 }
10713
10714 public int getHistoryTotalSize() {
10715 return MAX_HISTORY_BUFFER;
10716 }
10717
10718 public int getHistoryUsedSize() {
10719 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010720 }
10721
10722 @Override
10723 public boolean startIteratingHistoryLocked() {
10724 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
10725 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010726 if (mHistoryBuffer.dataSize() <= 0) {
10727 return false;
10728 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010729 mHistoryBuffer.setDataPosition(0);
10730 mReadOverflow = false;
10731 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010732 mReadHistoryStrings = new String[mHistoryTagPool.size()];
10733 mReadHistoryUids = new int[mHistoryTagPool.size()];
10734 mReadHistoryChars = 0;
10735 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10736 final HistoryTag tag = ent.getKey();
10737 final int idx = ent.getValue();
10738 mReadHistoryStrings[idx] = tag.string;
10739 mReadHistoryUids[idx] = tag.uid;
10740 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010741 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010742 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010743 }
10744
10745 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -080010746 public int getHistoryStringPoolSize() {
10747 return mReadHistoryStrings.length;
10748 }
10749
10750 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010751 public int getHistoryStringPoolBytes() {
10752 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
10753 // Each string character is 2 bytes.
10754 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
10755 }
10756
10757 @Override
10758 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010759 return mReadHistoryStrings[index];
10760 }
10761
10762 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010763 public int getHistoryTagPoolUid(int index) {
10764 return mReadHistoryUids[index];
10765 }
10766
10767 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010768 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -070010769 final int pos = mHistoryBuffer.dataPosition();
10770 if (pos == 0) {
10771 out.clear();
10772 }
10773 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010774 if (end) {
10775 return false;
10776 }
10777
Dianne Hackborn99009ea2014-04-18 16:23:42 -070010778 final long lastRealtime = out.time;
10779 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010780 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -070010781 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
10782 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -070010783 out.currentTime = lastWalltime + (out.time - lastRealtime);
10784 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010785 return true;
10786 }
10787
10788 @Override
10789 public void finishIteratingHistoryLocked() {
10790 mIteratingHistory = false;
10791 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -080010792 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010793 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010794
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010795 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -070010796 public long getHistoryBaseTime() {
10797 return mHistoryBaseTime;
10798 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010799
Dianne Hackbornb5e31652010-09-07 12:13:55 -070010800 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 public int getStartCount() {
10802 return mStartCount;
10803 }
10804
10805 public boolean isOnBattery() {
10806 return mOnBattery;
10807 }
10808
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010809 public boolean isCharging() {
10810 return mCharging;
10811 }
10812
Mike Mac2f518a2017-09-19 16:06:03 -070010813 public boolean isScreenOn(int state) {
Chris Phoenix10a4a642017-09-25 13:21:00 -070010814 return state == Display.STATE_ON || state == Display.STATE_VR
10815 || state == Display.STATE_ON_SUSPEND;
Mike Mac2f518a2017-09-19 16:06:03 -070010816 }
10817
10818 public boolean isScreenOff(int state) {
10819 return state == Display.STATE_OFF;
10820 }
10821
10822 public boolean isScreenDoze(int state) {
10823 return state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070010824 }
10825
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010826 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010827 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010828 mOnBatteryTimeBase.init(uptime, realtime);
10829 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010830 mRealtime = 0;
10831 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010832 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -070010833 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010834 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010835
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010836 void initDischarge() {
10837 mLowDischargeAmountSinceCharge = 0;
10838 mHighDischargeAmountSinceCharge = 0;
10839 mDischargeAmountScreenOn = 0;
10840 mDischargeAmountScreenOnSinceCharge = 0;
10841 mDischargeAmountScreenOff = 0;
10842 mDischargeAmountScreenOffSinceCharge = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010843 mDischargeAmountScreenDoze = 0;
10844 mDischargeAmountScreenDozeSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010845 mDischargeStepTracker.init();
10846 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010847 mDischargeScreenOffCounter.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -070010848 mDischargeScreenDozeCounter.reset(false);
Mike Ma15313c92017-11-15 17:58:21 -080010849 mDischargeLightDozeCounter.reset(false);
10850 mDischargeDeepDozeCounter.reset(false);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010851 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010852 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010853
10854 public void resetAllStatsCmdLocked() {
10855 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -080010856 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -070010857 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -080010858 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010859 long realtime = mSecRealtime * 1000;
10860 mDischargeStartLevel = mHistoryCur.batteryLevel;
10861 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -070010862 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010863 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
10864 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010865 mOnBatteryTimeBase.reset(uptime, realtime);
10866 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
10867 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Mike Mac2f518a2017-09-19 16:06:03 -070010868 if (isScreenOn(mScreenState)) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010869 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
Mike Mac2f518a2017-09-19 16:06:03 -070010870 mDischargeScreenDozeUnplugLevel = 0;
10871 mDischargeScreenOffUnplugLevel = 0;
10872 } else if (isScreenDoze(mScreenState)) {
10873 mDischargeScreenOnUnplugLevel = 0;
10874 mDischargeScreenDozeUnplugLevel = mHistoryCur.batteryLevel;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010875 mDischargeScreenOffUnplugLevel = 0;
10876 } else {
10877 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010878 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010879 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
10880 }
10881 mDischargeAmountScreenOn = 0;
10882 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010883 mDischargeAmountScreenDoze = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010884 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010885 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -080010886 }
10887
10888 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -070010889 final long uptimeMillis = mClocks.uptimeMillis();
10890 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010891 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -070010892 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010893 mScreenOnTimer.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -070010894 mScreenDozeTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010895 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010896 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010897 }
Adam Lesinski1a76a622016-06-22 10:28:47 -070010898
10899 if (mPowerProfile != null) {
10900 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
10901 } else {
10902 mEstimatedBatteryCapacity = -1;
10903 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070010904 mMinLearnedBatteryCapacity = -1;
10905 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010906 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010907 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -070010908 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010909 mLongestLightIdleTime = 0;
10910 mLongestFullIdleTime = 0;
10911 mDeviceIdleModeLightTimer.reset(false);
10912 mDeviceIdleModeFullTimer.reset(false);
10913 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010914 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010915 mPhoneOnTimer.reset(false);
10916 mAudioOnTimer.reset(false);
10917 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010918 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010919 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010920 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -080010921 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010922 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010923 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010924 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010925 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010926 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010927 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010928 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010929 mNetworkByteActivityCounters[i].reset(false);
10930 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010931 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010932 mMobileRadioActiveTimer.reset(false);
10933 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010934 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010935 mMobileRadioActiveUnknownTime.reset(false);
10936 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010937 mWifiOnTimer.reset(false);
10938 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010939 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010940 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010941 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010942 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10943 mWifiSupplStateTimer[i].reset(false);
10944 }
10945 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10946 mWifiSignalStrengthsTimer[i].reset(false);
10947 }
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080010948 mWifiMulticastWakelockTimer.reset(false);
Siddharth Rayb50a6842017-12-14 15:15:28 -080010949 mWifiActiveTimer.reset(false);
10950 mWifiActivity.reset(false);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080010951 for (int i=0; i< GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
10952 mGpsSignalQualityTimer[i].reset(false);
10953 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010954 mBluetoothActivity.reset(false);
10955 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010956 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010957
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010958 for (int i=0; i<mUidStats.size(); i++) {
Bookatz993a0be2017-07-21 09:03:23 -070010959 if (mUidStats.valueAt(i).reset(uptimeMillis * 1000, elapsedRealtimeMillis * 1000)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010960 mUidStats.remove(mUidStats.keyAt(i));
10961 i--;
10962 }
10963 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010964
Bookatz50df7112017-08-04 14:53:26 -070010965 if (mRpmStats.size() > 0) {
10966 for (SamplingTimer timer : mRpmStats.values()) {
10967 mOnBatteryTimeBase.remove(timer);
10968 }
10969 mRpmStats.clear();
10970 }
10971 if (mScreenOffRpmStats.size() > 0) {
10972 for (SamplingTimer timer : mScreenOffRpmStats.values()) {
10973 mOnBatteryScreenOffTimeBase.remove(timer);
10974 }
10975 mScreenOffRpmStats.clear();
10976 }
10977
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010978 if (mKernelWakelockStats.size() > 0) {
10979 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010980 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010981 }
10982 mKernelWakelockStats.clear();
10983 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010984
James Carr3a226052016-07-01 14:49:52 -070010985 if (mKernelMemoryStats.size() > 0) {
10986 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
10987 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
10988 }
10989 mKernelMemoryStats.clear();
10990 }
10991
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010992 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010993 for (SamplingTimer timer : mWakeupReasonStats.values()) {
10994 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010995 }
10996 mWakeupReasonStats.clear();
10997 }
10998
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010999 mLastHistoryStepDetails = null;
11000 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
11001 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
11002 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
11003 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
11004 mLastStepStatUserTime = mCurStepStatUserTime = 0;
11005 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
11006 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
11007 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
11008 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
11009 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
11010
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011011 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011012
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011013 clearHistoryLocked();
11014 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011015
Dianne Hackborn40c87252014-03-19 16:55:40 -070011016 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011017 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011018 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
11019 // Not recording process starts/stops.
11020 continue;
11021 }
Dianne Hackborn37de0982014-05-09 09:32:18 -070011022 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011023 if (active == null) {
11024 continue;
11025 }
Dianne Hackborn37de0982014-05-09 09:32:18 -070011026 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
11027 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011028 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -070011029 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
11030 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011031 }
11032 }
11033 }
11034 }
11035
Mike Mac2f518a2017-09-19 16:06:03 -070011036 void updateDischargeScreenLevelsLocked(int oldState, int newState) {
11037 updateOldDischargeScreenLevelLocked(oldState);
11038 updateNewDischargeScreenLevelLocked(newState);
11039 }
11040
11041 private void updateOldDischargeScreenLevelLocked(int state) {
11042 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011043 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
11044 if (diff > 0) {
11045 mDischargeAmountScreenOn += diff;
11046 mDischargeAmountScreenOnSinceCharge += diff;
11047 }
Mike Mac2f518a2017-09-19 16:06:03 -070011048 } else if (isScreenDoze(state)) {
11049 int diff = mDischargeScreenDozeUnplugLevel - mDischargeCurrentLevel;
11050 if (diff > 0) {
11051 mDischargeAmountScreenDoze += diff;
11052 mDischargeAmountScreenDozeSinceCharge += diff;
11053 }
11054 } else if (isScreenOff(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011055 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
11056 if (diff > 0) {
11057 mDischargeAmountScreenOff += diff;
11058 mDischargeAmountScreenOffSinceCharge += diff;
11059 }
11060 }
Mike Mac2f518a2017-09-19 16:06:03 -070011061 }
11062
11063 private void updateNewDischargeScreenLevelLocked(int state) {
11064 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011065 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
11066 mDischargeScreenOffUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070011067 mDischargeScreenDozeUnplugLevel = 0;
11068 } else if (isScreenDoze(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011069 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070011070 mDischargeScreenDozeUnplugLevel = mDischargeCurrentLevel;
11071 mDischargeScreenOffUnplugLevel = 0;
11072 } else if (isScreenOff(state)) {
11073 mDischargeScreenOnUnplugLevel = 0;
11074 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011075 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
11076 }
11077 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011078
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011079 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011080 if (mOnBatteryInternal) {
Mike Mac2f518a2017-09-19 16:06:03 -070011081 updateDischargeScreenLevelsLocked(mScreenState, mScreenState);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011082 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011083 }
11084
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011085 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
11086 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
11087
11088 private final Object mWifiNetworkLock = new Object();
11089
11090 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011091 private String[] mWifiIfaces = EmptyArray.STRING;
11092
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011093 @GuardedBy("mWifiNetworkLock")
11094 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011095
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011096 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011097
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011098 @GuardedBy("mModemNetworkLock")
11099 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011100
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011101 @GuardedBy("mModemNetworkLock")
11102 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
11103
11104 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
11105 try {
11106 if (!ArrayUtils.isEmpty(ifaces)) {
11107 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
11108 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
11109 }
11110 } catch (IOException e) {
11111 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011112 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011113 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011114 }
11115
Chenjie Yu89083392018-01-11 14:53:31 -080011116 /**
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011117 * Distribute WiFi energy info and network traffic to apps.
11118 * @param info The energy information from the WiFi controller.
11119 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011120 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011121 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011122 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011123 }
11124
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011125 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -070011126 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011127 synchronized (mWifiNetworkLock) {
11128 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
11129 if (latestStats != null) {
11130 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
11131 mNetworkStatsPool.acquire());
11132 mNetworkStatsPool.release(mLastWifiNetworkStats);
11133 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -070011134 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011135 }
11136
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011137 synchronized (this) {
11138 if (!mOnBatteryInternal) {
11139 if (delta != null) {
11140 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011141 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011142 return;
11143 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011144
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011145 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
11146 SparseLongArray rxPackets = new SparseLongArray();
11147 SparseLongArray txPackets = new SparseLongArray();
11148 long totalTxPackets = 0;
11149 long totalRxPackets = 0;
11150 if (delta != null) {
11151 NetworkStats.Entry entry = new NetworkStats.Entry();
11152 final int size = delta.size();
11153 for (int i = 0; i < size; i++) {
11154 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011155
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011156 if (DEBUG_ENERGY) {
11157 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
11158 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
11159 + " txPackets=" + entry.txPackets);
11160 }
11161
11162 if (entry.rxBytes == 0 && entry.txBytes == 0) {
11163 // Skip the lookup below since there is no work to do.
11164 continue;
11165 }
11166
11167 final Uid u = getUidStatsLocked(mapUid(entry.uid));
11168 if (entry.rxBytes != 0) {
11169 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -080011170 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011171 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
11172 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
11173 entry.rxPackets);
11174 }
11175 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
11176 entry.rxBytes);
11177 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
11178 entry.rxPackets);
11179
11180 rxPackets.put(u.getUid(), entry.rxPackets);
11181
11182 // Sum the total number of packets so that the Rx Power can
11183 // be evenly distributed amongst the apps.
11184 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -080011185 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011186
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011187 if (entry.txBytes != 0) {
11188 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -080011189 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011190 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
11191 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
11192 entry.txPackets);
11193 }
11194 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
11195 entry.txBytes);
11196 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
11197 entry.txPackets);
11198
11199 txPackets.put(u.getUid(), entry.txPackets);
11200
11201 // Sum the total number of packets so that the Tx Power can
11202 // be evenly distributed amongst the apps.
11203 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -080011204 }
Adam Lesinskiba88e682015-12-08 12:06:55 -080011205 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011206 mNetworkStatsPool.release(delta);
11207 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -070011208 }
11209
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011210 if (info != null) {
11211 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -070011212
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011213 // Measured in mAms
11214 final long txTimeMs = info.getControllerTxTimeMillis();
11215 final long rxTimeMs = info.getControllerRxTimeMillis();
Siddharth Rayb50a6842017-12-14 15:15:28 -080011216 final long scanTimeMs = info.getControllerScanTimeMillis();
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011217 final long idleTimeMs = info.getControllerIdleTimeMillis();
11218 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -070011219
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011220 long leftOverRxTimeMs = rxTimeMs;
11221 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -070011222
Adam Lesinskie08af192015-03-25 16:42:59 -070011223 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011224 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
11225 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
11226 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
11227 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
11228 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Siddharth Rayb50a6842017-12-14 15:15:28 -080011229 Slog.d(TAG, " Scan Time: " + scanTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -070011230 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011231
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011232 long totalWifiLockTimeMs = 0;
11233 long totalScanTimeMs = 0;
11234
11235 // On the first pass, collect some totals so that we can normalize power
11236 // calculations if we need to.
11237 final int uidStatsSize = mUidStats.size();
11238 for (int i = 0; i < uidStatsSize; i++) {
11239 final Uid uid = mUidStats.valueAt(i);
11240
11241 // Sum the total scan power for all apps.
11242 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
11243 elapsedRealtimeMs * 1000) / 1000;
11244
11245 // Sum the total time holding wifi lock for all apps.
11246 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
11247 elapsedRealtimeMs * 1000) / 1000;
11248 }
11249
11250 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
11251 Slog.d(TAG,
11252 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
11253 + rxTimeMs + " ms). Normalizing scan time.");
11254 }
11255 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
11256 Slog.d(TAG,
11257 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
11258 + txTimeMs + " ms). Normalizing scan time.");
11259 }
11260
11261 // Actually assign and distribute power usage to apps.
11262 for (int i = 0; i < uidStatsSize; i++) {
11263 final Uid uid = mUidStats.valueAt(i);
11264
11265 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
11266 elapsedRealtimeMs * 1000) / 1000;
11267 if (scanTimeSinceMarkMs > 0) {
11268 // Set the new mark so that next time we get new data since this point.
11269 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
11270
11271 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
11272 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
11273
11274 // Our total scan time is more than the reported Tx/Rx time.
11275 // This is possible because the cost of a scan is approximate.
11276 // Let's normalize the result so that we evenly blame each app
11277 // scanning.
11278 //
11279 // This means that we may have apps that transmitted/received packets not be
11280 // blamed for this, but this is fine as scans are relatively more expensive.
11281 if (totalScanTimeMs > rxTimeMs) {
11282 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
11283 totalScanTimeMs;
11284 }
11285 if (totalScanTimeMs > txTimeMs) {
11286 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
11287 totalScanTimeMs;
11288 }
11289
11290 if (DEBUG_ENERGY) {
11291 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
11292 + scanRxTimeSinceMarkMs + " ms Tx:"
11293 + scanTxTimeSinceMarkMs + " ms)");
11294 }
11295
11296 ControllerActivityCounterImpl activityCounter =
11297 uid.getOrCreateWifiControllerActivityLocked();
11298 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
11299 activityCounter.getTxTimeCounters()[0].addCountLocked(
11300 scanTxTimeSinceMarkMs);
11301 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
11302 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
11303 }
11304
11305 // Distribute evenly the power consumed while Idle to each app holding a WiFi
11306 // lock.
11307 final long wifiLockTimeSinceMarkMs =
11308 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
11309 elapsedRealtimeMs * 1000) / 1000;
11310 if (wifiLockTimeSinceMarkMs > 0) {
11311 // Set the new mark so that next time we get new data since this point.
11312 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
11313
11314 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
11315 / totalWifiLockTimeMs;
11316 if (DEBUG_ENERGY) {
11317 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
11318 + myIdleTimeMs + " ms");
11319 }
11320 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
11321 .addCountLocked(myIdleTimeMs);
11322 }
11323 }
11324
Adam Lesinskie08af192015-03-25 16:42:59 -070011325 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011326 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
11327 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -070011328 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011329
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011330 // Distribute the remaining Tx power appropriately between all apps that transmitted
11331 // packets.
11332 for (int i = 0; i < txPackets.size(); i++) {
11333 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
11334 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
11335 / totalTxPackets;
11336 if (DEBUG_ENERGY) {
11337 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
11338 }
11339 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
11340 .addCountLocked(myTxTimeMs);
11341 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011342
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011343 // Distribute the remaining Rx power appropriately between all apps that received
11344 // packets.
11345 for (int i = 0; i < rxPackets.size(); i++) {
11346 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
11347 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
11348 / totalRxPackets;
11349 if (DEBUG_ENERGY) {
11350 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
11351 }
11352 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
11353 .addCountLocked(myRxTimeMs);
11354 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011355
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011356 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
11357
11358
11359 // Update WiFi controller stats.
11360 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
11361 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
11362 info.getControllerTxTimeMillis());
Siddharth Rayb50a6842017-12-14 15:15:28 -080011363 mWifiActivity.getScanTimeCounter().addCountLocked(
11364 info.getControllerScanTimeMillis());
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011365 mWifiActivity.getIdleTimeCounter().addCountLocked(
11366 info.getControllerIdleTimeMillis());
11367
11368 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
11369 final double opVolt = mPowerProfile.getAveragePower(
11370 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
11371 if (opVolt != 0) {
11372 // We store the power drain as mAms.
11373 mWifiActivity.getPowerCounter().addCountLocked(
11374 (long) (info.getControllerEnergyUsed() / opVolt));
11375 }
Adam Lesinskie08af192015-03-25 16:42:59 -070011376 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011377 }
11378 }
11379
Chenjie Yu89083392018-01-11 14:53:31 -080011380 private ModemActivityInfo mLastModemActivityInfo =
11381 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
11382
Chenjie Yua54e54d2018-02-14 20:12:52 -080011383 private ModemActivityInfo getDeltaModemActivityInfo(ModemActivityInfo activityInfo) {
11384 if (activityInfo == null) {
11385 return null;
11386 }
11387 int[] txTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
11388 for (int i = 0; i < ModemActivityInfo.TX_POWER_LEVELS; i++) {
11389 txTimeMs[i] = activityInfo.getTxTimeMillis()[i]
11390 - mLastModemActivityInfo.getTxTimeMillis()[i];
11391 }
11392 ModemActivityInfo deltaInfo = new ModemActivityInfo(activityInfo.getTimestamp(),
11393 activityInfo.getSleepTimeMillis() - mLastModemActivityInfo.getSleepTimeMillis(),
11394 activityInfo.getIdleTimeMillis() - mLastModemActivityInfo.getIdleTimeMillis(),
11395 txTimeMs,
11396 activityInfo.getRxTimeMillis() - mLastModemActivityInfo.getRxTimeMillis(),
11397 activityInfo.getEnergyUsed() - mLastModemActivityInfo.getEnergyUsed());
11398 mLastModemActivityInfo = activityInfo;
11399 return deltaInfo;
11400 }
11401
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011402 /**
11403 * Distribute Cell radio energy info and network traffic to apps.
11404 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011405 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011406 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011407 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011408 }
Chenjie Yua54e54d2018-02-14 20:12:52 -080011409 ModemActivityInfo deltaInfo = getDeltaModemActivityInfo(activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011410
Siddharth Rayf5e796a2018-01-22 18:18:17 -080011411 // Add modem tx power to history.
Chenjie Yua54e54d2018-02-14 20:12:52 -080011412 addModemTxPowerToHistory(deltaInfo);
Siddharth Rayf5e796a2018-01-22 18:18:17 -080011413
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011414 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -070011415 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011416 synchronized (mModemNetworkLock) {
11417 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
11418 if (latestStats != null) {
11419 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
11420 mNetworkStatsPool.acquire());
11421 mNetworkStatsPool.release(mLastModemNetworkStats);
11422 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -070011423 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011424 }
11425
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011426 synchronized (this) {
11427 if (!mOnBatteryInternal) {
11428 if (delta != null) {
11429 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011430 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011431 return;
Adam Lesinskie08af192015-03-25 16:42:59 -070011432 }
11433
Chenjie Yua54e54d2018-02-14 20:12:52 -080011434 if (deltaInfo != null) {
Siddharth Ray2038af82018-01-17 17:40:26 -080011435 mHasModemReporting = true;
11436 mModemActivity.getIdleTimeCounter().addCountLocked(
Chenjie Yua54e54d2018-02-14 20:12:52 -080011437 deltaInfo.getIdleTimeMillis());
11438 mModemActivity.getRxTimeCounter().addCountLocked(deltaInfo.getRxTimeMillis());
Siddharth Ray2038af82018-01-17 17:40:26 -080011439 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
11440 mModemActivity.getTxTimeCounters()[lvl]
Chenjie Yua54e54d2018-02-14 20:12:52 -080011441 .addCountLocked(deltaInfo.getTxTimeMillis()[lvl]);
Siddharth Ray2038af82018-01-17 17:40:26 -080011442 }
11443
11444 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
11445 final double opVolt = mPowerProfile.getAveragePower(
11446 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
11447 if (opVolt != 0) {
11448 double energyUsed =
Chenjie Yua54e54d2018-02-14 20:12:52 -080011449 deltaInfo.getSleepTimeMillis() *
Siddharth Ray2038af82018-01-17 17:40:26 -080011450 mPowerProfile.getAveragePower(PowerProfile.POWER_MODEM_CONTROLLER_SLEEP)
Chenjie Yua54e54d2018-02-14 20:12:52 -080011451 + deltaInfo.getIdleTimeMillis() *
Siddharth Ray2038af82018-01-17 17:40:26 -080011452 mPowerProfile.getAveragePower(PowerProfile.POWER_MODEM_CONTROLLER_IDLE)
Chenjie Yua54e54d2018-02-14 20:12:52 -080011453 + deltaInfo.getRxTimeMillis() *
Siddharth Ray2038af82018-01-17 17:40:26 -080011454 mPowerProfile.getAveragePower(PowerProfile.POWER_MODEM_CONTROLLER_RX);
Chenjie Yua54e54d2018-02-14 20:12:52 -080011455 int[] txTimeMs = deltaInfo.getTxTimeMillis();
Chenjie Yu89083392018-01-11 14:53:31 -080011456 for (int i = 0; i < Math.min(txTimeMs.length,
Chenjie Yua54e54d2018-02-14 20:12:52 -080011457 SignalStrength.NUM_SIGNAL_STRENGTH_BINS); i++) {
Chenjie Yu89083392018-01-11 14:53:31 -080011458 energyUsed += txTimeMs[i] * mPowerProfile.getAveragePower(
Chenjie Yua54e54d2018-02-14 20:12:52 -080011459 PowerProfile.POWER_MODEM_CONTROLLER_TX, i);
Siddharth Ray2038af82018-01-17 17:40:26 -080011460 }
11461
11462 // We store the power drain as mAms.
11463 mModemActivity.getPowerCounter().addCountLocked((long) energyUsed);
11464 }
11465 }
11466
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070011467 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011468 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
11469 elapsedRealtimeMs * 1000);
11470 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
11471
11472 long totalRxPackets = 0;
11473 long totalTxPackets = 0;
11474 if (delta != null) {
11475 NetworkStats.Entry entry = new NetworkStats.Entry();
11476 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011477 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011478 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011479 if (entry.rxPackets == 0 && entry.txPackets == 0) {
11480 continue;
11481 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011482
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011483 if (DEBUG_ENERGY) {
11484 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
11485 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
11486 + " txPackets=" + entry.txPackets);
11487 }
11488
11489 totalRxPackets += entry.rxPackets;
11490 totalTxPackets += entry.txPackets;
11491
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011492 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011493 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
11494 entry.rxPackets);
11495 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
11496 entry.txPackets);
11497 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
11498 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
11499 entry.rxBytes, entry.rxPackets);
11500 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
11501 entry.txBytes, entry.txPackets);
11502 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011503
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011504 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
11505 entry.rxBytes);
11506 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
11507 entry.txBytes);
11508 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
11509 entry.rxPackets);
11510 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
11511 entry.txPackets);
11512 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011513
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011514 // Now distribute proportional blame to the apps that did networking.
11515 long totalPackets = totalRxPackets + totalTxPackets;
11516 if (totalPackets > 0) {
11517 for (int i = 0; i < size; i++) {
11518 entry = delta.getValues(i, entry);
11519 if (entry.rxPackets == 0 && entry.txPackets == 0) {
11520 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011521 }
11522
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011523 final Uid u = getUidStatsLocked(mapUid(entry.uid));
11524
11525 // Distribute total radio active time in to this app.
11526 final long appPackets = entry.rxPackets + entry.txPackets;
11527 final long appRadioTime = (radioTime * appPackets) / totalPackets;
11528 u.noteMobileRadioActiveTimeLocked(appRadioTime);
11529
11530 // Remove this app from the totals, so that we don't lose any time
11531 // due to rounding.
11532 radioTime -= appRadioTime;
11533 totalPackets -= appPackets;
11534
Chenjie Yua54e54d2018-02-14 20:12:52 -080011535 if (deltaInfo != null) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011536 ControllerActivityCounterImpl activityCounter =
11537 u.getOrCreateModemControllerActivityLocked();
11538 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Chenjie Yua54e54d2018-02-14 20:12:52 -080011539 final long rxMs = (entry.rxPackets * deltaInfo.getRxTimeMillis())
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011540 / totalRxPackets;
11541 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
11542 }
11543
11544 if (totalTxPackets > 0 && entry.txPackets > 0) {
11545 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
11546 long txMs =
Chenjie Yua54e54d2018-02-14 20:12:52 -080011547 entry.txPackets * deltaInfo.getTxTimeMillis()[lvl];
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011548 txMs /= totalTxPackets;
11549 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
11550 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011551 }
11552 }
11553 }
11554 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070011555
11556 if (radioTime > 0) {
11557 // Whoops, there is some radio time we can't blame on an app!
11558 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
11559 mMobileRadioActiveUnknownCount.addCountLocked(1);
11560 }
11561
11562 mNetworkStatsPool.release(delta);
11563 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011564 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011565 }
11566 }
11567
Chenjie Yu89083392018-01-11 14:53:31 -080011568 // Cache last value for comparison.
11569 private BluetoothActivityEnergyInfo mLastBluetoothActivityEnergyInfo =
11570 new BluetoothActivityEnergyInfo(0, 0, 0, 0, 0, 0);
11571
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011572 /**
Siddharth Rayf5e796a2018-01-22 18:18:17 -080011573 * Add modem tx power to history
11574 * Device is said to be in high cellular transmit power when it has spent most of the transmit
11575 * time at the highest power level.
11576 * @param activityInfo
11577 */
11578 private void addModemTxPowerToHistory(final ModemActivityInfo activityInfo) {
11579 if (activityInfo == null) {
11580 return;
11581 }
11582 int[] txTimeMs = activityInfo.getTxTimeMillis();
11583 if (txTimeMs == null || txTimeMs.length != ModemActivityInfo.TX_POWER_LEVELS) {
11584 return;
11585 }
11586 final long elapsedRealtime = mClocks.elapsedRealtime();
11587 final long uptime = mClocks.uptimeMillis();
11588 int levelMaxTimeSpent = 0;
11589 for (int i = 1; i < txTimeMs.length; i++) {
11590 if (txTimeMs[i] > txTimeMs[levelMaxTimeSpent]) {
11591 levelMaxTimeSpent = i;
11592 }
11593 }
11594 if (levelMaxTimeSpent == ModemActivityInfo.TX_POWER_LEVELS - 1) {
11595 if (!mIsCellularTxPowerHigh) {
11596 mHistoryCur.states2 |= HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG;
11597 addHistoryRecordLocked(elapsedRealtime, uptime);
11598 mIsCellularTxPowerHigh = true;
11599 }
11600 return;
11601 }
11602 if (mIsCellularTxPowerHigh) {
11603 mHistoryCur.states2 &= ~HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG;
11604 addHistoryRecordLocked(elapsedRealtime, uptime);
11605 mIsCellularTxPowerHigh = false;
11606 }
11607 return;
11608 }
11609
11610 /**
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011611 * Distribute Bluetooth energy info and network traffic to apps.
11612 * @param info The energy information from the bluetooth controller.
11613 */
11614 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011615 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -080011616 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011617 }
11618
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011619 if (info == null || !mOnBatteryInternal) {
11620 return;
11621 }
Adam Lesinskie283d332015-04-16 12:29:25 -070011622
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011623 mHasBluetoothReporting = true;
11624
Bookatz867c0d72017-03-07 18:23:42 -080011625 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Chenjie Yu89083392018-01-11 14:53:31 -080011626 final long rxTimeMs = info.getControllerRxTimeMillis() -
11627 mLastBluetoothActivityEnergyInfo.getControllerRxTimeMillis();
11628 final long txTimeMs = info.getControllerTxTimeMillis() -
11629 mLastBluetoothActivityEnergyInfo.getControllerTxTimeMillis();
11630 final long idleTimeMs = info.getControllerIdleTimeMillis() -
11631 mLastBluetoothActivityEnergyInfo.getControllerIdleTimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011632 if (DEBUG_ENERGY) {
11633 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
11634 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
11635 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
Chenjie Yu89083392018-01-11 14:53:31 -080011636 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011637 }
11638
11639 long totalScanTimeMs = 0;
11640
11641 final int uidCount = mUidStats.size();
11642 for (int i = 0; i < uidCount; i++) {
11643 final Uid u = mUidStats.valueAt(i);
11644 if (u.mBluetoothScanTimer == null) {
11645 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -070011646 }
Adam Lesinski50e47602015-12-04 17:04:54 -080011647
Bookatzaa4594a2017-03-24 12:39:56 -070011648 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011649 elapsedRealtimeMs * 1000) / 1000;
11650 }
11651
11652 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
11653 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
11654
11655 if (DEBUG_ENERGY) {
11656 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
11657 + " TX=" + normalizeScanTxTime);
11658 }
11659
11660 long leftOverRxTimeMs = rxTimeMs;
11661 long leftOverTxTimeMs = txTimeMs;
11662
11663 for (int i = 0; i < uidCount; i++) {
11664 final Uid u = mUidStats.valueAt(i);
11665 if (u.mBluetoothScanTimer == null) {
11666 continue;
11667 }
11668
Bookatzaa4594a2017-03-24 12:39:56 -070011669 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011670 elapsedRealtimeMs * 1000) / 1000;
11671 if (scanTimeSinceMarkMs > 0) {
11672 // Set the new mark so that next time we get new data since this point.
11673 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
11674
11675 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
11676 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
11677
11678 if (normalizeScanRxTime) {
11679 // Scan time is longer than the total rx time in the controller,
11680 // so distribute the scan time proportionately. This means regular traffic
11681 // will not blamed, but scans are more expensive anyways.
11682 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
11683 }
11684
11685 if (normalizeScanTxTime) {
11686 // Scan time is longer than the total tx time in the controller,
11687 // so distribute the scan time proportionately. This means regular traffic
11688 // will not blamed, but scans are more expensive anyways.
11689 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
11690 }
11691
11692 final ControllerActivityCounterImpl counter =
11693 u.getOrCreateBluetoothControllerActivityLocked();
11694 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
11695 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
11696
11697 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
11698 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
11699 }
11700 }
11701
11702 if (DEBUG_ENERGY) {
11703 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
11704 + " TX=" + leftOverTxTimeMs);
11705 }
11706
11707 //
11708 // Now distribute blame to apps that did bluetooth traffic.
11709 //
11710
11711 long totalTxBytes = 0;
11712 long totalRxBytes = 0;
11713
11714 final UidTraffic[] uidTraffic = info.getUidTraffic();
Chenjie Yu89083392018-01-11 14:53:31 -080011715 final UidTraffic[] lastUidTraffic = mLastBluetoothActivityEnergyInfo.getUidTraffic();
11716 final ArrayList<UidTraffic> deltaTraffic = new ArrayList<>();
11717 int m = 0, n = 0;
11718 for (; m < uidTraffic.length && n < lastUidTraffic.length; m++) {
11719 final UidTraffic traffic = uidTraffic[m];
11720 final UidTraffic lastTraffic = lastUidTraffic[n];
11721 if (traffic.getUid() == lastTraffic.getUid()) {
11722 deltaTraffic.add(new UidTraffic(traffic.getUid(),
11723 traffic.getRxBytes() - lastTraffic.getRxBytes(),
11724 traffic.getTxBytes() - lastTraffic.getTxBytes()));
11725 n++;
11726 }
11727 }
11728 for (; m < uidTraffic.length; m ++) {
11729 deltaTraffic.add(uidTraffic[m]);
11730 }
11731
11732 for (int i = 0, j = 0; i < deltaTraffic.size(); i++) {
11733 final UidTraffic traffic = deltaTraffic.get(i);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011734
11735 // Add to the global counters.
11736 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
11737 traffic.getRxBytes());
11738 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
11739 traffic.getTxBytes());
11740
11741 // Add to the UID counters.
11742 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
11743 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
11744 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
11745
11746 // Calculate the total traffic.
11747 totalTxBytes += traffic.getTxBytes();
11748 totalRxBytes += traffic.getRxBytes();
11749 }
11750
11751 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
11752 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Chenjie Yu89083392018-01-11 14:53:31 -080011753 for (int i = 0; i < deltaTraffic.size(); i++) {
11754 final UidTraffic traffic = deltaTraffic.get(i);
Adam Lesinski50e47602015-12-04 17:04:54 -080011755
Adam Lesinski50e47602015-12-04 17:04:54 -080011756 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011757 final ControllerActivityCounterImpl counter =
11758 u.getOrCreateBluetoothControllerActivityLocked();
11759
11760 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
11761 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
11762
11763 if (DEBUG_ENERGY) {
11764 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
11765 + " rx_time=" + timeRxMs);
11766 }
11767 counter.getRxTimeCounter().addCountLocked(timeRxMs);
11768 leftOverRxTimeMs -= timeRxMs;
11769 }
11770
11771 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
11772 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
11773
11774 if (DEBUG_ENERGY) {
11775 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
11776 + " tx_time=" + timeTxMs);
11777 }
11778
11779 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
11780 leftOverTxTimeMs -= timeTxMs;
11781 }
Adam Lesinski50e47602015-12-04 17:04:54 -080011782 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011783 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011784
Chenjie Yu89083392018-01-11 14:53:31 -080011785 mBluetoothActivity.getRxTimeCounter().addCountLocked(rxTimeMs);
11786 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(txTimeMs);
11787 mBluetoothActivity.getIdleTimeCounter().addCountLocked(idleTimeMs);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011788
11789 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
11790 final double opVolt = mPowerProfile.getAveragePower(
11791 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
11792 if (opVolt != 0) {
11793 // We store the power drain as mAms.
11794 mBluetoothActivity.getPowerCounter().addCountLocked(
Chenjie Yu89083392018-01-11 14:53:31 -080011795 (long) ((info.getControllerEnergyUsed() -
11796 mLastBluetoothActivityEnergyInfo.getControllerEnergyUsed() )/ opVolt));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011797 }
Chenjie Yu89083392018-01-11 14:53:31 -080011798 mLastBluetoothActivityEnergyInfo = info;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011799 }
11800
11801 /**
Bookatz0b8a0502017-09-13 11:51:52 -070011802 * Read and record Resource Power Manager (RPM) state and voter times.
11803 * If RPM stats were fetched more recently than RPM_STATS_UPDATE_FREQ_MS ago, uses the old data
11804 * instead of fetching it anew.
Bookatz50df7112017-08-04 14:53:26 -070011805 */
11806 public void updateRpmStatsLocked() {
11807 if (mPlatformIdleStateCallback == null) return;
Bookatz0b8a0502017-09-13 11:51:52 -070011808 long now = SystemClock.elapsedRealtime();
11809 if (now - mLastRpmStatsUpdateTimeMs >= RPM_STATS_UPDATE_FREQ_MS) {
11810 mPlatformIdleStateCallback.fillLowPowerStats(mTmpRpmStats);
11811 mLastRpmStatsUpdateTimeMs = now;
11812 }
Bookatz50df7112017-08-04 14:53:26 -070011813
11814 for (Map.Entry<String, RpmStats.PowerStatePlatformSleepState> pstate
11815 : mTmpRpmStats.mPlatformLowPowerStats.entrySet()) {
11816
11817 // Update values for this platform state.
11818 final String pName = pstate.getKey();
11819 final long pTimeUs = pstate.getValue().mTimeMs * 1000;
11820 final int pCount = pstate.getValue().mCount;
11821 getRpmTimerLocked(pName).update(pTimeUs, pCount);
Bookatz82b341172017-09-07 19:06:08 -070011822 if (SCREEN_OFF_RPM_STATS_ENABLED) {
11823 getScreenOffRpmTimerLocked(pName).update(pTimeUs, pCount);
11824 }
Bookatz50df7112017-08-04 14:53:26 -070011825
11826 // Update values for each voter of this platform state.
11827 for (Map.Entry<String, RpmStats.PowerStateElement> voter
11828 : pstate.getValue().mVoters.entrySet()) {
11829 final String vName = pName + "." + voter.getKey();
11830 final long vTimeUs = voter.getValue().mTimeMs * 1000;
11831 final int vCount = voter.getValue().mCount;
11832 getRpmTimerLocked(vName).update(vTimeUs, vCount);
Bookatz82b341172017-09-07 19:06:08 -070011833 if (SCREEN_OFF_RPM_STATS_ENABLED) {
11834 getScreenOffRpmTimerLocked(vName).update(vTimeUs, vCount);
11835 }
Bookatz50df7112017-08-04 14:53:26 -070011836 }
11837 }
11838
11839 for (Map.Entry<String, RpmStats.PowerStateSubsystem> subsys
11840 : mTmpRpmStats.mSubsystemLowPowerStats.entrySet()) {
11841
11842 final String subsysName = subsys.getKey();
11843 for (Map.Entry<String, RpmStats.PowerStateElement> sstate
11844 : subsys.getValue().mStates.entrySet()) {
11845 final String name = subsysName + "." + sstate.getKey();
11846 final long timeUs = sstate.getValue().mTimeMs * 1000;
11847 final int count = sstate.getValue().mCount;
11848 getRpmTimerLocked(name).update(timeUs, count);
Bookatz82b341172017-09-07 19:06:08 -070011849 if (SCREEN_OFF_RPM_STATS_ENABLED) {
11850 getScreenOffRpmTimerLocked(name).update(timeUs, count);
11851 }
Bookatz50df7112017-08-04 14:53:26 -070011852 }
11853 }
11854 }
11855
11856 /**
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011857 * Read and distribute kernel wake lock use across apps.
11858 */
11859 public void updateKernelWakelocksLocked() {
11860 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
11861 mTmpWakelockStats);
11862 if (wakelockStats == null) {
11863 // Not crashing might make board bringup easier.
11864 Slog.w(TAG, "Couldn't get kernel wake lock stats");
11865 return;
11866 }
11867
11868 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
11869 String name = ent.getKey();
11870 KernelWakelockStats.Entry kws = ent.getValue();
11871
11872 SamplingTimer kwlt = mKernelWakelockStats.get(name);
11873 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070011874 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011875 mKernelWakelockStats.put(name, kwlt);
11876 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070011877
Adam Lesinski757c6ea2016-04-21 09:55:41 -070011878 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011879 kwlt.setUpdateVersion(kws.mVersion);
11880 }
11881
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070011882 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070011883 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
11884 // this time.
11885 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11886 SamplingTimer st = ent.getValue();
11887 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
11888 st.endSample();
11889 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011890 }
11891 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070011892
Adam Lesinskid84ad302016-05-17 18:31:02 -070011893 // Record whether we've seen a non-zero time (for debugging b/22716723).
11894 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070011895 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
11896 }
11897
11898 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
11899 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
11900 wakelockStats.kernelWakelockVersion);
11901 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011902 }
11903
Adam Lesinski72478f02015-06-17 15:39:43 -070011904 // We use an anonymous class to access these variables,
11905 // so they can't live on the stack or they'd have to be
11906 // final MutableLong objects (more allocations).
11907 // Used in updateCpuTimeLocked().
11908 long mTempTotalCpuUserTimeUs;
11909 long mTempTotalCpuSystemTimeUs;
Sudheer Shankab8ad5942017-08-08 12:16:09 -070011910 long[][] mWakeLockAllocationsUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070011911
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011912 /**
James Carr3a226052016-07-01 14:49:52 -070011913 * Reads the newest memory stats from the kernel.
11914 */
11915 public void updateKernelMemoryBandwidthLocked() {
11916 mKernelMemoryBandwidthStats.updateStats();
11917 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
11918 final int bandwidthEntryCount = bandwidthEntries.size();
11919 int index;
11920 for (int i = 0; i < bandwidthEntryCount; i++) {
11921 SamplingTimer timer;
11922 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
11923 timer = mKernelMemoryStats.valueAt(index);
11924 } else {
11925 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
11926 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
11927 }
11928 timer.update(bandwidthEntries.valueAt(i), 1);
11929 if (DEBUG_MEMORY) {
11930 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
11931 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
11932 mKernelMemoryStats.get(
11933 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
11934 mKernelMemoryStats.size()));
11935 }
11936 }
11937 }
11938
Sudheer Shankac57729a2018-02-09 15:44:42 -080011939 public boolean isOnBatteryLocked() {
11940 return mOnBatteryTimeBase.isRunning();
11941 }
11942
11943 public boolean isOnBatteryScreenOffLocked() {
11944 return mOnBatteryScreenOffTimeBase.isRunning();
11945 }
11946
James Carr3a226052016-07-01 14:49:52 -070011947 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070011948 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
11949 * and we are on battery with screen off, we give more of the cpu time to those apps holding
11950 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Sudheer Shankac57729a2018-02-09 15:44:42 -080011951 * It's possible this will be invoked after the internal battery/screen states are updated, so
11952 * passing the appropriate battery/screen states to try attribute the cpu times to correct
11953 * buckets.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011954 */
Andreas Gampe3f24e692018-02-05 13:24:28 -080011955 @GuardedBy("this")
Sudheer Shankac57729a2018-02-09 15:44:42 -080011956 public void updateCpuTimeLocked(boolean onBattery, boolean onBatteryScreenOff) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070011957 if (mPowerProfile == null) {
11958 return;
11959 }
11960
Adam Lesinski72478f02015-06-17 15:39:43 -070011961 if (DEBUG_ENERGY_CPU) {
11962 Slog.d(TAG, "!Cpu updating!");
11963 }
11964
Sudheer Shankab8ad5942017-08-08 12:16:09 -070011965 if (mCpuFreqs == null) {
11966 mCpuFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile);
11967 }
11968
Sudheer Shanka38383232017-07-25 09:55:03 -070011969 // Calculate the wakelocks we have to distribute amongst. The system is excluded as it is
11970 // usually holding the wakelock on behalf of an app.
11971 // And Only distribute cpu power to wakelocks if the screen is off and we're on battery.
11972 ArrayList<StopwatchTimer> partialTimersToConsider = null;
Sudheer Shankac57729a2018-02-09 15:44:42 -080011973 if (onBatteryScreenOff) {
Sudheer Shanka38383232017-07-25 09:55:03 -070011974 partialTimersToConsider = new ArrayList<>();
11975 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070011976 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070011977 // Since the collection and blaming of wakelocks can be scheduled to run after
11978 // some delay, the mPartialTimers list may have new entries. We can't blame
11979 // the newly added timer for past cpu time, so we only consider timers that
11980 // were present for one round of collection. Once a timer has gone through
11981 // a round of collection, its mInList field is set to true.
Adam Lesinski72478f02015-06-17 15:39:43 -070011982 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
Sudheer Shanka38383232017-07-25 09:55:03 -070011983 partialTimersToConsider.add(timer);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011984 }
Adam Lesinski72478f02015-06-17 15:39:43 -070011985 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011986 }
Sudheer Shanka38383232017-07-25 09:55:03 -070011987 markPartialTimersAsEligible();
Adam Lesinski72478f02015-06-17 15:39:43 -070011988
Sudheer Shanka38383232017-07-25 09:55:03 -070011989 // When the battery is not on, we don't attribute the cpu times to any timers but we still
11990 // need to take the snapshots.
Sudheer Shankac57729a2018-02-09 15:44:42 -080011991 if (!onBattery) {
Sudheer Shanka38383232017-07-25 09:55:03 -070011992 mKernelUidCpuTimeReader.readDelta(null);
11993 mKernelUidCpuFreqTimeReader.readDelta(null);
Mike Mafae87da2018-01-19 20:07:20 -080011994 if (mConstants.TRACK_CPU_ACTIVE_CLUSTER_TIME) {
11995 mKernelUidCpuActiveTimeReader.readDelta(null);
11996 mKernelUidCpuClusterTimeReader.readDelta(null);
11997 }
Sudheer Shanka38383232017-07-25 09:55:03 -070011998 for (int cluster = mKernelCpuSpeedReaders.length - 1; cluster >= 0; --cluster) {
11999 mKernelCpuSpeedReaders[cluster].readDelta();
12000 }
12001 return;
12002 }
Adam Lesinski72478f02015-06-17 15:39:43 -070012003
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070012004 mUserInfoProvider.refreshUserIds();
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012005 final SparseLongArray updatedUids = mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()
12006 ? null : new SparseLongArray();
Sudheer Shankac57729a2018-02-09 15:44:42 -080012007 readKernelUidCpuTimesLocked(partialTimersToConsider, updatedUids, onBattery);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012008 // updatedUids=null means /proc/uid_time_in_state provides snapshots of per-cluster cpu
12009 // freqs, so no need to approximate these values.
12010 if (updatedUids != null) {
Sudheer Shankac57729a2018-02-09 15:44:42 -080012011 updateClusterSpeedTimes(updatedUids, onBattery);
Sudheer Shanka671985f2017-05-19 11:33:42 -070012012 }
Sudheer Shankac57729a2018-02-09 15:44:42 -080012013 readKernelUidCpuFreqTimesLocked(partialTimersToConsider, onBattery, onBatteryScreenOff);
Mike Mafae87da2018-01-19 20:07:20 -080012014 if (mConstants.TRACK_CPU_ACTIVE_CLUSTER_TIME) {
Sudheer Shankac57729a2018-02-09 15:44:42 -080012015 readKernelUidCpuActiveTimesLocked(onBattery);
12016 readKernelUidCpuClusterTimesLocked(onBattery);
Mike Mafae87da2018-01-19 20:07:20 -080012017 }
Sudheer Shanka38383232017-07-25 09:55:03 -070012018 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012019
Sudheer Shanka38383232017-07-25 09:55:03 -070012020 /**
12021 * Mark the current partial timers as gone through a collection so that they will be
12022 * considered in the next cpu times distribution to wakelock holders.
12023 */
12024 @VisibleForTesting
12025 public void markPartialTimersAsEligible() {
12026 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
12027 // No difference, so each timer is now considered for the next collection.
12028 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
12029 mPartialTimers.get(i).mInList = true;
12030 }
12031 } else {
12032 // The lists are different, meaning we added (or removed a timer) since the last
12033 // collection.
12034 for (int i = mLastPartialTimers.size() - 1; i >= 0; --i) {
12035 mLastPartialTimers.get(i).mInList = false;
12036 }
12037 mLastPartialTimers.clear();
Adam Lesinski72478f02015-06-17 15:39:43 -070012038
Sudheer Shanka38383232017-07-25 09:55:03 -070012039 // Mark the current timers as gone through a collection.
12040 final int numPartialTimers = mPartialTimers.size();
12041 for (int i = 0; i < numPartialTimers; ++i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070012042 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070012043 timer.mInList = true;
12044 mLastPartialTimers.add(timer);
Adam Lesinski72478f02015-06-17 15:39:43 -070012045 }
12046 }
Sudheer Shanka38383232017-07-25 09:55:03 -070012047 }
Adam Lesinski72478f02015-06-17 15:39:43 -070012048
Sudheer Shanka38383232017-07-25 09:55:03 -070012049 /**
12050 * Take snapshot of cpu times (aggregated over all uids) at different frequencies and
12051 * calculate cpu times spent by each uid at different frequencies.
12052 *
12053 * @param updatedUids The uids for which times spent at different frequencies are calculated.
12054 */
12055 @VisibleForTesting
Sudheer Shankac57729a2018-02-09 15:44:42 -080012056 public void updateClusterSpeedTimes(@NonNull SparseLongArray updatedUids, boolean onBattery) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012057 long totalCpuClustersTimeMs = 0;
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012058 // Read the time spent for each cluster at various cpu frequencies.
Sudheer Shankaaf857412017-07-21 00:14:24 -070012059 final long[][] clusterSpeedTimesMs = new long[mKernelCpuSpeedReaders.length][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012060 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012061 clusterSpeedTimesMs[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
12062 if (clusterSpeedTimesMs[cluster] != null) {
12063 for (int speed = clusterSpeedTimesMs[cluster].length - 1; speed >= 0; --speed) {
12064 totalCpuClustersTimeMs += clusterSpeedTimesMs[cluster][speed];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012065 }
12066 }
12067 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070012068 if (totalCpuClustersTimeMs != 0) {
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012069 // We have cpu times per freq aggregated over all uids but we need the times per uid.
12070 // So, we distribute total time spent by an uid to different cpu freqs based on the
12071 // amount of time cpu was running at that freq.
12072 final int updatedUidsCount = updatedUids.size();
12073 for (int i = 0; i < updatedUidsCount; ++i) {
12074 final Uid u = getUidStatsLocked(updatedUids.keyAt(i));
Sudheer Shankaaf857412017-07-21 00:14:24 -070012075 final long appCpuTimeUs = updatedUids.valueAt(i);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012076 // Add the cpu speeds to this UID.
12077 final int numClusters = mPowerProfile.getNumCpuClusters();
Sudheer Shanka38383232017-07-25 09:55:03 -070012078 if (u.mCpuClusterSpeedTimesUs == null ||
12079 u.mCpuClusterSpeedTimesUs.length != numClusters) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012080 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012081 }
12082
Sudheer Shankaaf857412017-07-21 00:14:24 -070012083 for (int cluster = 0; cluster < clusterSpeedTimesMs.length; cluster++) {
12084 final int speedsInCluster = clusterSpeedTimesMs[cluster].length;
12085 if (u.mCpuClusterSpeedTimesUs[cluster] == null || speedsInCluster !=
12086 u.mCpuClusterSpeedTimesUs[cluster].length) {
12087 u.mCpuClusterSpeedTimesUs[cluster]
12088 = new LongSamplingCounter[speedsInCluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012089 }
12090
Sudheer Shankaaf857412017-07-21 00:14:24 -070012091 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeedTimesUs[cluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012092 for (int speed = 0; speed < speedsInCluster; speed++) {
12093 if (cpuSpeeds[speed] == null) {
12094 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
12095 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070012096 cpuSpeeds[speed].addCountLocked(appCpuTimeUs
12097 * clusterSpeedTimesMs[cluster][speed]
Sudheer Shankac57729a2018-02-09 15:44:42 -080012098 / totalCpuClustersTimeMs, onBattery);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012099 }
12100 }
12101 }
12102 }
Sudheer Shanka38383232017-07-25 09:55:03 -070012103 }
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070012104
Sudheer Shanka38383232017-07-25 09:55:03 -070012105 /**
12106 * Take a snapshot of the cpu times spent by each uid and update the corresponding counters.
12107 * If {@param partialTimers} is not null and empty, then we assign a portion of cpu times to
12108 * wakelock holders.
12109 *
12110 * @param partialTimers The wakelock holders among which the cpu times will be distributed.
12111 * @param updatedUids If not null, then the uids found in the snapshot will be added to this.
12112 */
12113 @VisibleForTesting
12114 public void readKernelUidCpuTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
Sudheer Shankac57729a2018-02-09 15:44:42 -080012115 @Nullable SparseLongArray updatedUids, boolean onBattery) {
Sudheer Shanka38383232017-07-25 09:55:03 -070012116 mTempTotalCpuUserTimeUs = mTempTotalCpuSystemTimeUs = 0;
12117 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
12118 final long startTimeMs = mClocks.uptimeMillis();
Adam Lesinski72478f02015-06-17 15:39:43 -070012119
Sudheer Shanka38383232017-07-25 09:55:03 -070012120 mKernelUidCpuTimeReader.readDelta((uid, userTimeUs, systemTimeUs) -> {
12121 uid = mapUid(uid);
12122 if (Process.isIsolated(uid)) {
12123 // This could happen if the isolated uid mapping was removed before that process
12124 // was actually killed.
12125 mKernelUidCpuTimeReader.removeUid(uid);
12126 Slog.d(TAG, "Got readings for an isolated uid with no mapping: " + uid);
12127 return;
12128 }
12129 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
12130 Slog.d(TAG, "Got readings for an invalid user's uid " + uid);
12131 mKernelUidCpuTimeReader.removeUid(uid);
12132 return;
12133 }
12134 final Uid u = getUidStatsLocked(uid);
12135
12136 // Accumulate the total system and user time.
12137 mTempTotalCpuUserTimeUs += userTimeUs;
12138 mTempTotalCpuSystemTimeUs += systemTimeUs;
12139
12140 StringBuilder sb = null;
12141 if (DEBUG_ENERGY_CPU) {
12142 sb = new StringBuilder();
12143 sb.append(" got time for uid=").append(u.mUid).append(": u=");
12144 TimeUtils.formatDuration(userTimeUs / 1000, sb);
12145 sb.append(" s=");
12146 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
12147 sb.append("\n");
12148 }
12149
12150 if (numWakelocks > 0) {
12151 // We have wakelocks being held, so only give a portion of the
12152 // time to the process. The rest will be distributed among wakelock
12153 // holders.
12154 userTimeUs = (userTimeUs * WAKE_LOCK_WEIGHT) / 100;
12155 systemTimeUs = (systemTimeUs * WAKE_LOCK_WEIGHT) / 100;
12156 }
12157
12158 if (sb != null) {
12159 sb.append(" adding to uid=").append(u.mUid).append(": u=");
12160 TimeUtils.formatDuration(userTimeUs / 1000, sb);
12161 sb.append(" s=");
12162 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
12163 Slog.d(TAG, sb.toString());
12164 }
12165
Sudheer Shankac57729a2018-02-09 15:44:42 -080012166 u.mUserCpuTime.addCountLocked(userTimeUs, onBattery);
12167 u.mSystemCpuTime.addCountLocked(systemTimeUs, onBattery);
Sudheer Shanka38383232017-07-25 09:55:03 -070012168 if (updatedUids != null) {
12169 updatedUids.put(u.getUid(), userTimeUs + systemTimeUs);
12170 }
12171 });
12172
12173 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
12174 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
12175 Slog.d(TAG, "Reading cpu stats took " + elapsedTimeMs + "ms");
12176 }
12177
12178 if (numWakelocks > 0) {
12179 // Distribute a portion of the total cpu time to wakelock holders.
12180 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
12181 mTempTotalCpuSystemTimeUs =
12182 (mTempTotalCpuSystemTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
12183
12184 for (int i = 0; i < numWakelocks; ++i) {
12185 final StopwatchTimer timer = partialTimers.get(i);
12186 final int userTimeUs = (int) (mTempTotalCpuUserTimeUs / (numWakelocks - i));
12187 final int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / (numWakelocks - i));
12188
12189 if (DEBUG_ENERGY_CPU) {
12190 final StringBuilder sb = new StringBuilder();
12191 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
12192 .append(": u=");
12193 TimeUtils.formatDuration(userTimeUs / 1000, sb);
12194 sb.append(" s=");
12195 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
12196 Slog.d(TAG, sb.toString());
12197 }
12198
Sudheer Shankac57729a2018-02-09 15:44:42 -080012199 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs, onBattery);
12200 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs, onBattery);
Sudheer Shanka38383232017-07-25 09:55:03 -070012201 if (updatedUids != null) {
12202 final int uid = timer.mUid.getUid();
12203 updatedUids.put(uid, updatedUids.get(uid, 0) + userTimeUs + systemTimeUs);
12204 }
12205
12206 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Sudheer Shankac57729a2018-02-09 15:44:42 -080012207 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000, onBattery);
Sudheer Shanka38383232017-07-25 09:55:03 -070012208
12209 mTempTotalCpuUserTimeUs -= userTimeUs;
12210 mTempTotalCpuSystemTimeUs -= systemTimeUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070012211 }
12212 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012213 }
12214
Sudheer Shanka38383232017-07-25 09:55:03 -070012215 /**
12216 * Take a snapshot of the cpu times spent by each uid in each freq and update the
12217 * corresponding counters.
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012218 *
12219 * @param partialTimers The wakelock holders among which the cpu freq times will be distributed.
Sudheer Shanka38383232017-07-25 09:55:03 -070012220 */
12221 @VisibleForTesting
Sudheer Shankac57729a2018-02-09 15:44:42 -080012222 public void readKernelUidCpuFreqTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
12223 boolean onBattery, boolean onBatteryScreenOff) {
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012224 final boolean perClusterTimesAvailable =
12225 mKernelUidCpuFreqTimeReader.perClusterTimesAvailable();
12226 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
12227 final int numClusters = mPowerProfile.getNumCpuClusters();
12228 mWakeLockAllocationsUs = null;
Sudheer Shankaac5b88f2017-12-11 15:36:35 -080012229 final long startTimeMs = mClocks.uptimeMillis();
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012230 mKernelUidCpuFreqTimeReader.readDelta((uid, cpuFreqTimeMs) -> {
12231 uid = mapUid(uid);
12232 if (Process.isIsolated(uid)) {
12233 mKernelUidCpuFreqTimeReader.removeUid(uid);
12234 Slog.d(TAG, "Got freq readings for an isolated uid with no mapping: " + uid);
12235 return;
Sudheer Shanka38383232017-07-25 09:55:03 -070012236 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012237 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
12238 Slog.d(TAG, "Got freq readings for an invalid user's uid " + uid);
12239 mKernelUidCpuFreqTimeReader.removeUid(uid);
12240 return;
12241 }
12242 final Uid u = getUidStatsLocked(uid);
12243 if (u.mCpuFreqTimeMs == null || u.mCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
12244 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
12245 }
Sudheer Shankac57729a2018-02-09 15:44:42 -080012246 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs, onBattery);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012247 if (u.mScreenOffCpuFreqTimeMs == null ||
12248 u.mScreenOffCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
12249 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
12250 mOnBatteryScreenOffTimeBase);
12251 }
Sudheer Shankac57729a2018-02-09 15:44:42 -080012252 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs, onBatteryScreenOff);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012253
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012254 if (perClusterTimesAvailable) {
12255 if (u.mCpuClusterSpeedTimesUs == null ||
12256 u.mCpuClusterSpeedTimesUs.length != numClusters) {
12257 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070012258 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012259 if (numWakelocks > 0 && mWakeLockAllocationsUs == null) {
12260 mWakeLockAllocationsUs = new long[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070012261 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012262
12263 int freqIndex = 0;
12264 for (int cluster = 0; cluster < numClusters; ++cluster) {
12265 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
12266 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
12267 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
12268 u.mCpuClusterSpeedTimesUs[cluster]
12269 = new LongSamplingCounter[speedsInCluster];
12270 }
12271 if (numWakelocks > 0 && mWakeLockAllocationsUs[cluster] == null) {
12272 mWakeLockAllocationsUs[cluster] = new long[speedsInCluster];
12273 }
12274 final LongSamplingCounter[] cpuTimesUs = u.mCpuClusterSpeedTimesUs[cluster];
12275 for (int speed = 0; speed < speedsInCluster; ++speed) {
12276 if (cpuTimesUs[speed] == null) {
12277 cpuTimesUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
12278 }
12279 final long appAllocationUs;
12280 if (mWakeLockAllocationsUs != null) {
12281 appAllocationUs =
12282 (cpuFreqTimeMs[freqIndex] * 1000 * WAKE_LOCK_WEIGHT) / 100;
12283 mWakeLockAllocationsUs[cluster][speed] +=
12284 (cpuFreqTimeMs[freqIndex] * 1000 - appAllocationUs);
12285 } else {
12286 appAllocationUs = cpuFreqTimeMs[freqIndex] * 1000;
12287 }
Sudheer Shankac57729a2018-02-09 15:44:42 -080012288 cpuTimesUs[speed].addCountLocked(appAllocationUs, onBattery);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012289 freqIndex++;
12290 }
Sudheer Shanka38383232017-07-25 09:55:03 -070012291 }
Sudheer Shanka38383232017-07-25 09:55:03 -070012292 }
12293 });
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012294
Sudheer Shankaac5b88f2017-12-11 15:36:35 -080012295 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
12296 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
12297 Slog.d(TAG, "Reading cpu freq times took " + elapsedTimeMs + "ms");
12298 }
12299
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012300 if (mWakeLockAllocationsUs != null) {
12301 for (int i = 0; i < numWakelocks; ++i) {
12302 final Uid u = partialTimers.get(i).mUid;
12303 if (u.mCpuClusterSpeedTimesUs == null ||
12304 u.mCpuClusterSpeedTimesUs.length != numClusters) {
12305 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
12306 }
12307
12308 for (int cluster = 0; cluster < numClusters; ++cluster) {
12309 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
12310 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
12311 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
12312 u.mCpuClusterSpeedTimesUs[cluster]
12313 = new LongSamplingCounter[speedsInCluster];
12314 }
12315 final LongSamplingCounter[] cpuTimeUs = u.mCpuClusterSpeedTimesUs[cluster];
12316 for (int speed = 0; speed < speedsInCluster; ++speed) {
12317 if (cpuTimeUs[speed] == null) {
12318 cpuTimeUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
12319 }
12320 final long allocationUs =
12321 mWakeLockAllocationsUs[cluster][speed] / (numWakelocks - i);
Sudheer Shankac57729a2018-02-09 15:44:42 -080012322 cpuTimeUs[speed].addCountLocked(allocationUs, onBattery);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070012323 mWakeLockAllocationsUs[cluster][speed] -= allocationUs;
12324 }
12325 }
12326 }
12327 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012328 }
12329
Mike Ma3d422c32017-10-25 11:08:57 -070012330 /**
12331 * Take a snapshot of the cpu active times spent by each uid and update the corresponding
12332 * counters.
12333 */
12334 @VisibleForTesting
Sudheer Shankac57729a2018-02-09 15:44:42 -080012335 public void readKernelUidCpuActiveTimesLocked(boolean onBattery) {
Mike Ma3d422c32017-10-25 11:08:57 -070012336 final long startTimeMs = mClocks.uptimeMillis();
12337 mKernelUidCpuActiveTimeReader.readDelta((uid, cpuActiveTimesUs) -> {
12338 uid = mapUid(uid);
12339 if (Process.isIsolated(uid)) {
12340 mKernelUidCpuActiveTimeReader.removeUid(uid);
12341 Slog.w(TAG, "Got active times for an isolated uid with no mapping: " + uid);
12342 return;
12343 }
12344 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
12345 Slog.w(TAG, "Got active times for an invalid user's uid " + uid);
12346 mKernelUidCpuActiveTimeReader.removeUid(uid);
12347 return;
12348 }
12349 final Uid u = getUidStatsLocked(uid);
Sudheer Shankac57729a2018-02-09 15:44:42 -080012350 u.mCpuActiveTimeMs.addCountLocked(cpuActiveTimesUs, onBattery);
Mike Ma3d422c32017-10-25 11:08:57 -070012351 });
12352
12353 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
12354 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
12355 Slog.d(TAG, "Reading cpu active times took " + elapsedTimeMs + "ms");
12356 }
12357 }
12358
12359 /**
12360 * Take a snapshot of the cpu cluster times spent by each uid and update the corresponding
12361 * counters.
12362 */
12363 @VisibleForTesting
Sudheer Shankac57729a2018-02-09 15:44:42 -080012364 public void readKernelUidCpuClusterTimesLocked(boolean onBattery) {
Mike Ma3d422c32017-10-25 11:08:57 -070012365 final long startTimeMs = mClocks.uptimeMillis();
12366 mKernelUidCpuClusterTimeReader.readDelta((uid, cpuClusterTimesUs) -> {
12367 uid = mapUid(uid);
12368 if (Process.isIsolated(uid)) {
12369 mKernelUidCpuClusterTimeReader.removeUid(uid);
12370 Slog.w(TAG, "Got cluster times for an isolated uid with no mapping: " + uid);
12371 return;
12372 }
12373 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
12374 Slog.w(TAG, "Got cluster times for an invalid user's uid " + uid);
12375 mKernelUidCpuClusterTimeReader.removeUid(uid);
12376 return;
12377 }
12378 final Uid u = getUidStatsLocked(uid);
Sudheer Shankac57729a2018-02-09 15:44:42 -080012379 u.mCpuClusterTimesMs.addCountLocked(cpuClusterTimesUs, onBattery);
Mike Ma3d422c32017-10-25 11:08:57 -070012380 });
12381
12382 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
12383 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
12384 Slog.d(TAG, "Reading cpu cluster times took " + elapsedTimeMs + "ms");
12385 }
12386 }
12387
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012388 boolean setChargingLocked(boolean charging) {
12389 if (mCharging != charging) {
12390 mCharging = charging;
12391 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012392 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012393 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012394 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012395 }
12396 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
12397 return true;
12398 }
12399 return false;
12400 }
12401
Andreas Gampe3f24e692018-02-05 13:24:28 -080012402 @GuardedBy("this")
Mike Mac2f518a2017-09-19 16:06:03 -070012403 protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime,
12404 final boolean onBattery, final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012405 boolean doWrite = false;
12406 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
12407 m.arg1 = onBattery ? 1 : 0;
12408 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012409
Dianne Hackborn40c87252014-03-19 16:55:40 -070012410 final long uptime = mSecUptime * 1000;
12411 final long realtime = mSecRealtime * 1000;
Mike Mac2f518a2017-09-19 16:06:03 -070012412 final int screenState = mScreenState;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012413 if (onBattery) {
12414 // We will reset our status if we are unplugging after the
12415 // battery was last full, or the level is at 100, or
12416 // we have gone through a significant charge (from a very low
12417 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080012418 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070012419 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012420 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070012421 || (mDischargeCurrentLevel < 20 && level >= 80)
12422 || (getHighDischargeAmountSinceCharge() >= 200
12423 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070012424 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070012425 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070012426 + " lowAmount=" + getLowDischargeAmountSinceCharge()
12427 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012428 // Before we write, collect a snapshot of the final aggregated
12429 // stats to be reported in the next checkin. Only do this if we have
12430 // a sufficient amount of data to make it interesting.
12431 if (getLowDischargeAmountSinceCharge() >= 20) {
Dianne Hackborne17b4452018-01-10 13:15:40 -080012432 final long startTime = SystemClock.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012433 final Parcel parcel = Parcel.obtain();
12434 writeSummaryToParcel(parcel, true);
Dianne Hackborne17b4452018-01-10 13:15:40 -080012435 final long initialTime = SystemClock.uptimeMillis() - startTime;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012436 BackgroundThread.getHandler().post(new Runnable() {
12437 @Override public void run() {
12438 synchronized (mCheckinFile) {
Dianne Hackborne17b4452018-01-10 13:15:40 -080012439 final long startTime2 = SystemClock.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012440 FileOutputStream stream = null;
12441 try {
12442 stream = mCheckinFile.startWrite();
12443 stream.write(parcel.marshall());
12444 stream.flush();
12445 FileUtils.sync(stream);
12446 stream.close();
12447 mCheckinFile.finishWrite(stream);
Dianne Hackborne17b4452018-01-10 13:15:40 -080012448 com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
12449 "batterystats-checkin",
12450 initialTime + SystemClock.uptimeMillis() - startTime2);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012451 } catch (IOException e) {
12452 Slog.w("BatteryStats",
12453 "Error writing checkin battery statistics", e);
12454 mCheckinFile.failWrite(stream);
12455 } finally {
12456 parcel.recycle();
12457 }
12458 }
12459 }
12460 });
12461 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012462 doWrite = true;
12463 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080012464 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012465 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080012466 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012467 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012468 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080012469 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012470 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012471 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012472 if (mCharging) {
12473 setChargingLocked(false);
12474 }
12475 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080012476 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012477 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070012478 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012479 mDischargeStepTracker.clearTime();
12480 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012481 mInitStepMode = mCurStepMode;
12482 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012483 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012484 mHistoryCur.batteryLevel = (byte)level;
12485 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
12486 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
12487 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012488 if (reset) {
12489 mRecordingHistory = true;
12490 startRecordingHistory(mSecRealtime, mSecUptime, reset);
12491 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070012492 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012493 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070012494 if (isScreenOn(screenState)) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012495 mDischargeScreenOnUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070012496 mDischargeScreenDozeUnplugLevel = 0;
12497 mDischargeScreenOffUnplugLevel = 0;
12498 } else if (isScreenDoze(screenState)) {
12499 mDischargeScreenOnUnplugLevel = 0;
12500 mDischargeScreenDozeUnplugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012501 mDischargeScreenOffUnplugLevel = 0;
12502 } else {
12503 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070012504 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012505 mDischargeScreenOffUnplugLevel = level;
12506 }
12507 mDischargeAmountScreenOn = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070012508 mDischargeAmountScreenDoze = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012509 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070012510 updateTimeBasesLocked(true, screenState, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012511 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012512 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080012513 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012514 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012515 mHistoryCur.batteryLevel = (byte)level;
12516 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
12517 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
12518 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070012519 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012520 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012521 if (level < mDischargeUnplugLevel) {
12522 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
12523 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
12524 }
Mike Mac2f518a2017-09-19 16:06:03 -070012525 updateDischargeScreenLevelsLocked(screenState, screenState);
12526 updateTimeBasesLocked(false, screenState, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012527 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070012528 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070012529 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012530 mInitStepMode = mCurStepMode;
12531 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080012532 }
12533 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
12534 if (mFile != null) {
12535 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 }
12537 }
12538 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012539
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012540 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
12541 boolean reset) {
12542 mRecordingHistory = true;
12543 mHistoryCur.currentTime = System.currentTimeMillis();
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +000012544 addHistoryBufferLocked(elapsedRealtimeMs,
Dianne Hackborn37de0982014-05-09 09:32:18 -070012545 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012546 mHistoryCur);
12547 mHistoryCur.currentTime = 0;
12548 if (reset) {
12549 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
12550 }
12551 }
12552
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012553 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
12554 final long uptimeMs) {
12555 if (mRecordingHistory) {
12556 mHistoryCur.currentTime = currentTime;
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +000012557 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_CURRENT_TIME, mHistoryCur);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012558 mHistoryCur.currentTime = 0;
12559 }
12560 }
12561
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080012562 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
12563 if (mRecordingHistory) {
12564 mHistoryCur.currentTime = System.currentTimeMillis();
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +000012565 addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_SHUTDOWN, mHistoryCur);
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080012566 mHistoryCur.currentTime = 0;
12567 }
12568 }
12569
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012570 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012571 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012572 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070012573 }
12574 }
12575
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012576 // This should probably be exposed in the API, though it's not critical
Bookatz1a1b0462018-01-12 11:47:03 -080012577 public static final int BATTERY_PLUGGED_NONE = OsProtoEnums.BATTERY_PLUGGED_NONE; // = 0
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012578
Andreas Gampe3f24e692018-02-05 13:24:28 -080012579 @GuardedBy("this")
Bookatz8c6571b2017-10-24 15:04:41 -070012580 public void setBatteryStateLocked(final int status, final int health, final int plugType,
12581 final int level, /* not final */ int temp, final int volt, final int chargeUAh,
12582 final int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070012583 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
12584 temp = Math.max(0, temp);
12585
Bookatz8c6571b2017-10-24 15:04:41 -070012586 reportChangesToStatsLog(mHaveBatteryLevel ? mHistoryCur : null,
12587 status, plugType, level, temp);
12588
Sudheer Shankac57729a2018-02-09 15:44:42 -080012589 final boolean onBattery = isOnBattery(plugType, status);
Joe Onoratoabded112016-02-08 16:49:39 -080012590 final long uptime = mClocks.uptimeMillis();
12591 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012592 if (!mHaveBatteryLevel) {
12593 mHaveBatteryLevel = true;
12594 // We start out assuming that the device is plugged in (not
12595 // on battery). If our first report is now that we are indeed
12596 // plugged in, then twiddle our state to correctly reflect that
12597 // since we won't be going through the full setOnBattery().
12598 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070012599 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012600 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012601 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012602 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012603 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012604 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012605 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012606 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012607 mHistoryCur.batteryStatus = (byte)status;
12608 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012609 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012610 mMaxChargeStepLevel = mMinDischargeStepLevel =
12611 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012612 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012613 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
12614 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
12615 }
12616 int oldStatus = mHistoryCur.batteryStatus;
12617 if (onBattery) {
12618 mDischargeCurrentLevel = level;
12619 if (!mRecordingHistory) {
12620 mRecordingHistory = true;
12621 startRecordingHistory(elapsedRealtime, uptime, true);
12622 }
Todd Poynor1acf06a2017-12-07 19:19:35 -080012623 } else if (level < 96 &&
12624 status != BatteryManager.BATTERY_STATUS_UNKNOWN) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012625 if (!mRecordingHistory) {
12626 mRecordingHistory = true;
12627 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012628 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070012629 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012630 mCurrentBatteryLevel = level;
12631 if (mDischargePlugLevel < 0) {
12632 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070012633 }
Adam Lesinski926969b2016-04-28 17:31:12 -070012634
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012635 if (onBattery != mOnBattery) {
12636 mHistoryCur.batteryLevel = (byte)level;
12637 mHistoryCur.batteryStatus = (byte)status;
12638 mHistoryCur.batteryHealth = (byte)health;
12639 mHistoryCur.batteryPlugType = (byte)plugType;
12640 mHistoryCur.batteryTemperature = (short)temp;
12641 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012642 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
12643 // Only record discharges
12644 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
12645 mDischargeCounter.addCountLocked(chargeDiff);
12646 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070012647 if (isScreenDoze(mScreenState)) {
12648 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
12649 }
Mike Ma15313c92017-11-15 17:58:21 -080012650 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
12651 mDischargeLightDozeCounter.addCountLocked(chargeDiff);
12652 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
12653 mDischargeDeepDozeCounter.addCountLocked(chargeDiff);
12654 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012655 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070012656 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012657 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012658 } else {
12659 boolean changed = false;
12660 if (mHistoryCur.batteryLevel != level) {
12661 mHistoryCur.batteryLevel = (byte)level;
12662 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070012663
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012664 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
12665 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012666 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070012667 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012668 if (mHistoryCur.batteryStatus != status) {
12669 mHistoryCur.batteryStatus = (byte)status;
12670 changed = true;
12671 }
12672 if (mHistoryCur.batteryHealth != health) {
12673 mHistoryCur.batteryHealth = (byte)health;
12674 changed = true;
12675 }
12676 if (mHistoryCur.batteryPlugType != plugType) {
12677 mHistoryCur.batteryPlugType = (byte)plugType;
12678 changed = true;
12679 }
12680 if (temp >= (mHistoryCur.batteryTemperature+10)
12681 || temp <= (mHistoryCur.batteryTemperature-10)) {
12682 mHistoryCur.batteryTemperature = (short)temp;
12683 changed = true;
12684 }
12685 if (volt > (mHistoryCur.batteryVoltage+20)
12686 || volt < (mHistoryCur.batteryVoltage-20)) {
12687 mHistoryCur.batteryVoltage = (char)volt;
12688 changed = true;
12689 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070012690 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
12691 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012692 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
12693 // Only record discharges
12694 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
12695 mDischargeCounter.addCountLocked(chargeDiff);
12696 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070012697 if (isScreenDoze(mScreenState)) {
12698 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
12699 }
Mike Ma15313c92017-11-15 17:58:21 -080012700 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
12701 mDischargeLightDozeCounter.addCountLocked(chargeDiff);
12702 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
12703 mDischargeDeepDozeCounter.addCountLocked(chargeDiff);
12704 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012705 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070012706 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070012707 changed = true;
12708 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012709 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
12710 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
12711 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
12712 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012713 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012714 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
12715 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
12716 modeBits, elapsedRealtime);
12717 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
12718 modeBits, elapsedRealtime);
12719 mLastDischargeStepLevel = level;
12720 mMinDischargeStepLevel = level;
12721 mInitStepMode = mCurStepMode;
12722 mModStepMode = 0;
12723 }
12724 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012725 if (level >= 90) {
12726 // If the battery level is at least 90%, always consider the device to be
12727 // charging even if it happens to go down a level.
12728 changed |= setChargingLocked(true);
12729 mLastChargeStepLevel = level;
12730 } if (!mCharging) {
12731 if (mLastChargeStepLevel < level) {
12732 // We have not reporting that we are charging, but the level has now
12733 // gone up, so consider the state to be charging.
12734 changed |= setChargingLocked(true);
12735 mLastChargeStepLevel = level;
12736 }
12737 } else {
12738 if (mLastChargeStepLevel > level) {
12739 // We had reported that the device was charging, but here we are with
12740 // power connected and the level going down. Looks like the current
12741 // power supplied isn't enough, so consider the device to now be
12742 // discharging.
12743 changed |= setChargingLocked(false);
12744 mLastChargeStepLevel = level;
12745 }
12746 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012747 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
12748 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
12749 modeBits, elapsedRealtime);
12750 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
12751 modeBits, elapsedRealtime);
12752 mLastChargeStepLevel = level;
12753 mMaxChargeStepLevel = level;
12754 mInitStepMode = mCurStepMode;
12755 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070012756 }
12757 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070012758 if (changed) {
12759 addHistoryRecordLocked(elapsedRealtime, uptime);
12760 }
Evan Millarc64edde2009-04-18 12:26:32 -070012761 }
Todd Poynor1acf06a2017-12-07 19:19:35 -080012762 if (!onBattery &&
12763 (status == BatteryManager.BATTERY_STATUS_FULL ||
12764 status == BatteryManager.BATTERY_STATUS_UNKNOWN)) {
12765 // We don't record history while we are plugged in and fully charged
12766 // (or when battery is not present). The next time we are
12767 // unplugged, history will be cleared.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070012768 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080012769 }
Adam Lesinski041d9172016-12-12 12:03:56 -080012770
Jocelyn Dangc627d102017-04-14 13:15:14 -070012771 if (mMinLearnedBatteryCapacity == -1) {
12772 mMinLearnedBatteryCapacity = chargeFullUAh;
12773 } else {
12774 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080012775 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070012776 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070012777 }
12778
Sudheer Shankac57729a2018-02-09 15:44:42 -080012779 public static boolean isOnBattery(int plugType, int status) {
12780 return plugType == BATTERY_PLUGGED_NONE && status != BatteryManager.BATTERY_STATUS_UNKNOWN;
12781 }
12782
Bookatz8c6571b2017-10-24 15:04:41 -070012783 // Inform StatsLog of setBatteryState changes.
12784 // If this is the first reporting, pass in recentPast == null.
12785 private void reportChangesToStatsLog(HistoryItem recentPast,
12786 final int status, final int plugType, final int level, final int temp) {
12787
12788 if (recentPast == null || recentPast.batteryStatus != status) {
12789 StatsLog.write(StatsLog.CHARGING_STATE_CHANGED, status);
12790 }
12791 if (recentPast == null || recentPast.batteryPlugType != plugType) {
12792 StatsLog.write(StatsLog.PLUGGED_STATE_CHANGED, plugType);
12793 }
12794 if (recentPast == null || recentPast.batteryLevel != level) {
12795 StatsLog.write(StatsLog.BATTERY_LEVEL_CHANGED, level);
12796 }
12797 // Let's just always print the temperature, regardless of whether it changed.
12798 StatsLog.write(StatsLog.DEVICE_TEMPERATURE_REPORTED, temp);
12799 }
12800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012801 public long getAwakeTimeBattery() {
12802 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
12803 }
12804
12805 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080012806 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012807 }
12808
12809 @Override
12810 public long computeUptime(long curTime, int which) {
12811 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012812 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012813 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070012814 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815 }
12816 return 0;
12817 }
12818
12819 @Override
12820 public long computeRealtime(long curTime, int which) {
12821 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012822 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070012824 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012825 }
12826 return 0;
12827 }
12828
12829 @Override
12830 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012831 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012832 }
12833
12834 @Override
12835 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012836 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837 }
12838
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012839 @Override
12840 public long computeBatteryScreenOffUptime(long curTime, int which) {
12841 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
12842 }
12843
12844 @Override
12845 public long computeBatteryScreenOffRealtime(long curTime, int which) {
12846 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012847 }
12848
Dianne Hackborn260c5022014-04-29 11:23:16 -070012849 private long computeTimePerLevel(long[] steps, int numSteps) {
12850 // For now we'll do a simple average across all steps.
12851 if (numSteps <= 0) {
12852 return -1;
12853 }
12854 long total = 0;
12855 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012856 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012857 }
12858 return total / numSteps;
12859 /*
12860 long[] buckets = new long[numSteps];
12861 int numBuckets = 0;
12862 int numToAverage = 4;
12863 int i = 0;
12864 while (i < numSteps) {
12865 long totalTime = 0;
12866 int num = 0;
12867 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012868 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012869 num++;
12870 }
12871 buckets[numBuckets] = totalTime / num;
12872 numBuckets++;
12873 numToAverage *= 2;
12874 i += num;
12875 }
12876 if (numBuckets < 1) {
12877 return -1;
12878 }
12879 long averageTime = buckets[numBuckets-1];
12880 for (i=numBuckets-2; i>=0; i--) {
12881 averageTime = (averageTime + buckets[i]) / 2;
12882 }
12883 return averageTime;
12884 */
12885 }
12886
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012887 @Override
12888 public long computeBatteryTimeRemaining(long curTime) {
12889 if (!mOnBattery) {
12890 return -1;
12891 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070012892 /* Simple implementation just looks at the average discharge per level across the
12893 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012894 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
12895 if (discharge < 2) {
12896 return -1;
12897 }
12898 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
12899 if (duration < 1000*1000) {
12900 return -1;
12901 }
12902 long usPerLevel = duration/discharge;
12903 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070012904 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012905 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070012906 return -1;
12907 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012908 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070012909 if (msPerLevel <= 0) {
12910 return -1;
12911 }
12912 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012913 }
12914
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012915 @Override
12916 public LevelStepTracker getDischargeLevelStepTracker() {
12917 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070012918 }
12919
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012920 @Override
12921 public LevelStepTracker getDailyDischargeLevelStepTracker() {
12922 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070012923 }
12924
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012925 @Override
12926 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070012927 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012928 // Not yet working.
12929 return -1;
12930 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070012931 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012932 int curLevel = mCurrentBatteryLevel;
12933 int plugLevel = mDischargePlugLevel;
12934 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
12935 return -1;
12936 }
12937 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
12938 if (duration < 1000*1000) {
12939 return -1;
12940 }
12941 long usPerLevel = duration/(curLevel-plugLevel);
12942 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070012943 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012944 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070012945 return -1;
12946 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012947 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070012948 if (msPerLevel <= 0) {
12949 return -1;
12950 }
12951 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012952 }
12953
Siddharth Raya1fd0572017-11-13 14:20:47 -080012954 /*@hide */
12955 public CellularBatteryStats getCellularBatteryStats() {
12956 CellularBatteryStats s = new CellularBatteryStats();
12957 final int which = STATS_SINCE_CHARGED;
12958 final long rawRealTime = SystemClock.elapsedRealtime() * 1000;
12959 final ControllerActivityCounter counter = getModemControllerActivity();
12960 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
12961 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
12962 final long energyConsumedMaMs = counter.getPowerCounter().getCountLocked(which);
12963 long[] timeInRatMs = new long[BatteryStats.NUM_DATA_CONNECTION_TYPES];
12964 for (int i = 0; i < timeInRatMs.length; i++) {
12965 timeInRatMs[i] = getPhoneDataConnectionTime(i, rawRealTime, which) / 1000;
12966 }
12967 long[] timeInRxSignalStrengthLevelMs = new long[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
12968 for (int i = 0; i < timeInRxSignalStrengthLevelMs.length; i++) {
12969 timeInRxSignalStrengthLevelMs[i]
12970 = getPhoneSignalStrengthTime(i, rawRealTime, which) / 1000;
12971 }
12972 long[] txTimeMs = new long[Math.min(ModemActivityInfo.TX_POWER_LEVELS,
12973 counter.getTxTimeCounters().length)];
12974 long totalTxTimeMs = 0;
12975 for (int i = 0; i < txTimeMs.length; i++) {
12976 txTimeMs[i] = counter.getTxTimeCounters()[i].getCountLocked(which);
12977 totalTxTimeMs += txTimeMs[i];
12978 }
12979 final long totalControllerActivityTimeMs
12980 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
12981 final long sleepTimeMs
12982 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
12983 s.setLoggingDurationMs(computeBatteryRealtime(rawRealTime, which) / 1000);
12984 s.setKernelActiveTimeMs(getMobileRadioActiveTime(rawRealTime, which) / 1000);
12985 s.setNumPacketsTx(getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
12986 s.setNumBytesTx(getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
12987 s.setNumPacketsRx(getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
12988 s.setNumBytesRx(getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
12989 s.setSleepTimeMs(sleepTimeMs);
12990 s.setIdleTimeMs(idleTimeMs);
12991 s.setRxTimeMs(rxTimeMs);
12992 s.setEnergyConsumedMaMs(energyConsumedMaMs);
12993 s.setTimeInRatMs(timeInRatMs);
12994 s.setTimeInRxSignalStrengthLevelMs(timeInRxSignalStrengthLevelMs);
12995 s.setTxTimeMs(txTimeMs);
12996 return s;
12997 }
12998
Siddharth Rayb50a6842017-12-14 15:15:28 -080012999 /*@hide */
13000 public WifiBatteryStats getWifiBatteryStats() {
13001 WifiBatteryStats s = new WifiBatteryStats();
13002 final int which = STATS_SINCE_CHARGED;
13003 final long rawRealTime = SystemClock.elapsedRealtime() * 1000;
13004 final ControllerActivityCounter counter = getWifiControllerActivity();
13005 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
13006 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
13007 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
13008 final long txTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
13009 final long totalControllerActivityTimeMs
13010 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
13011 final long sleepTimeMs
13012 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + txTimeMs);
13013 final long energyConsumedMaMs = counter.getPowerCounter().getCountLocked(which);
13014 long numAppScanRequest = 0;
13015 for (int i = 0; i < mUidStats.size(); i++) {
13016 numAppScanRequest += mUidStats.valueAt(i).mWifiScanTimer.getCountLocked(which);
13017 }
13018 long[] timeInStateMs = new long[NUM_WIFI_STATES];
13019 for (int i=0; i<NUM_WIFI_STATES; i++) {
13020 timeInStateMs[i] = getWifiStateTime(i, rawRealTime, which) / 1000;
13021 }
13022 long[] timeInSupplStateMs = new long[NUM_WIFI_SUPPL_STATES];
13023 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13024 timeInSupplStateMs[i] = getWifiSupplStateTime(i, rawRealTime, which) / 1000;
13025 }
13026 long[] timeSignalStrengthTimeMs = new long[NUM_WIFI_SIGNAL_STRENGTH_BINS];
13027 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13028 timeSignalStrengthTimeMs[i] = getWifiSignalStrengthTime(i, rawRealTime, which) / 1000;
13029 }
13030 s.setLoggingDurationMs(computeBatteryRealtime(rawRealTime, which) / 1000);
13031 s.setKernelActiveTimeMs(getWifiActiveTime(rawRealTime, which) / 1000);
13032 s.setNumPacketsTx(getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
13033 s.setNumBytesTx(getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
13034 s.setNumPacketsRx(getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
13035 s.setNumBytesRx(getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
13036 s.setSleepTimeMs(sleepTimeMs);
13037 s.setIdleTimeMs(idleTimeMs);
13038 s.setRxTimeMs(rxTimeMs);
13039 s.setTxTimeMs(txTimeMs);
13040 s.setScanTimeMs(scanTimeMs);
13041 s.setEnergyConsumedMaMs(energyConsumedMaMs);
13042 s.setNumAppScanRequest(numAppScanRequest);
13043 s.setTimeInStateMs(timeInStateMs);
13044 s.setTimeInSupplicantStateMs(timeInSupplStateMs);
13045 s.setTimeInRxSignalStrengthLevelMs(timeSignalStrengthTimeMs);
13046 return s;
13047 }
13048
Siddharth Ray78ccaf52017-12-23 16:16:21 -080013049 /*@hide */
13050 public GpsBatteryStats getGpsBatteryStats() {
13051 GpsBatteryStats s = new GpsBatteryStats();
13052 final int which = STATS_SINCE_CHARGED;
13053 final long rawRealTime = SystemClock.elapsedRealtime() * 1000;
13054 s.setLoggingDurationMs(computeBatteryRealtime(rawRealTime, which) / 1000);
13055 s.setEnergyConsumedMaMs(getGpsBatteryDrainMaMs());
13056 long[] time = new long[GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS];
13057 for (int i=0; i<time.length; i++) {
13058 time[i] = getGpsSignalQualityTime(i, rawRealTime, which) / 1000;
13059 }
13060 s.setTimeInGpsSignalQualityLevel(time);
13061 return s;
13062 }
13063
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013064 @Override
13065 public LevelStepTracker getChargeLevelStepTracker() {
13066 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070013067 }
13068
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013069 @Override
13070 public LevelStepTracker getDailyChargeLevelStepTracker() {
13071 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070013072 }
13073
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013074 @Override
13075 public ArrayList<PackageChange> getDailyPackageChanges() {
13076 return mDailyPackageChanges;
13077 }
13078
Joe Onoratoe1acd632016-02-23 13:25:10 -080013079 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080013080 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013081 }
13082
13083 @Override
13084 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013085 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013086 }
13087
13088 @Override
13089 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013090 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013091 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070013092
The Android Open Source Project10592532009-03-18 17:39:46 -070013093 @Override
Evan Millar633a1742009-04-02 16:36:33 -070013094 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013095 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070013096 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070013097 }
13098 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013099
Evan Millar633a1742009-04-02 16:36:33 -070013100 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013101 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070013102 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013103
The Android Open Source Project10592532009-03-18 17:39:46 -070013104 @Override
Evan Millar633a1742009-04-02 16:36:33 -070013105 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013106 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070013107 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070013108 }
13109 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013110
Evan Millar633a1742009-04-02 16:36:33 -070013111 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080013112 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070013113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114
Amith Yamasanie43530a2009-08-21 13:11:37 -070013115 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013116 public int getLowDischargeAmountSinceCharge() {
13117 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080013118 int val = mLowDischargeAmountSinceCharge;
13119 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
13120 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
13121 }
13122 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013123 }
13124 }
13125
13126 @Override
13127 public int getHighDischargeAmountSinceCharge() {
13128 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080013129 int val = mHighDischargeAmountSinceCharge;
13130 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
13131 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
13132 }
13133 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013134 }
13135 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070013136
13137 @Override
13138 public int getDischargeAmount(int which) {
13139 int dischargeAmount = which == STATS_SINCE_CHARGED
13140 ? getHighDischargeAmountSinceCharge()
13141 : (getDischargeStartLevel() - getDischargeCurrentLevel());
13142 if (dischargeAmount < 0) {
13143 dischargeAmount = 0;
13144 }
13145 return dischargeAmount;
13146 }
13147
Mike Mac2f518a2017-09-19 16:06:03 -070013148 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013149 public int getDischargeAmountScreenOn() {
13150 synchronized(this) {
13151 int val = mDischargeAmountScreenOn;
Mike Mac2f518a2017-09-19 16:06:03 -070013152 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013153 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
13154 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
13155 }
13156 return val;
13157 }
13158 }
13159
Mike Mac2f518a2017-09-19 16:06:03 -070013160 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013161 public int getDischargeAmountScreenOnSinceCharge() {
13162 synchronized(this) {
13163 int val = mDischargeAmountScreenOnSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -070013164 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013165 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
13166 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
13167 }
13168 return val;
13169 }
13170 }
13171
Mike Mac2f518a2017-09-19 16:06:03 -070013172 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013173 public int getDischargeAmountScreenOff() {
13174 synchronized(this) {
13175 int val = mDischargeAmountScreenOff;
Mike Mac2f518a2017-09-19 16:06:03 -070013176 if (mOnBattery && isScreenOff(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013177 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
13178 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
13179 }
Mike Mac2f518a2017-09-19 16:06:03 -070013180 // For backward compatibility, doze discharge is counted into screen off.
13181 return val + getDischargeAmountScreenDoze();
13182 }
13183 }
13184
13185 @Override
13186 public int getDischargeAmountScreenOffSinceCharge() {
13187 synchronized(this) {
13188 int val = mDischargeAmountScreenOffSinceCharge;
13189 if (mOnBattery && isScreenOff(mScreenState)
13190 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
13191 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
13192 }
13193 // For backward compatibility, doze discharge is counted into screen off.
13194 return val + getDischargeAmountScreenDozeSinceCharge();
13195 }
13196 }
13197
13198 @Override
13199 public int getDischargeAmountScreenDoze() {
13200 synchronized(this) {
13201 int val = mDischargeAmountScreenDoze;
13202 if (mOnBattery && isScreenDoze(mScreenState)
13203 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
13204 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
13205 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013206 return val;
13207 }
13208 }
13209
Mike Mac2f518a2017-09-19 16:06:03 -070013210 @Override
13211 public int getDischargeAmountScreenDozeSinceCharge() {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013212 synchronized(this) {
Mike Mac2f518a2017-09-19 16:06:03 -070013213 int val = mDischargeAmountScreenDozeSinceCharge;
13214 if (mOnBattery && isScreenDoze(mScreenState)
13215 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
13216 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013217 }
13218 return val;
13219 }
13220 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013222 /**
13223 * Retrieve the statistics object for a particular uid, creating if needed.
13224 */
13225 public Uid getUidStatsLocked(int uid) {
13226 Uid u = mUidStats.get(uid);
13227 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080013228 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013229 mUidStats.put(uid, u);
13230 }
13231 return u;
13232 }
13233
Sudheer Shankab2f83c12017-11-13 19:25:01 -080013234 /**
13235 * Retrieve the statistics object for a particular uid. Returns null if the object is not
13236 * available.
13237 */
13238 public Uid getAvailableUidStatsLocked(int uid) {
13239 Uid u = mUidStats.get(uid);
13240 return u;
13241 }
13242
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070013243 public void onCleanupUserLocked(int userId) {
13244 final int firstUidForUser = UserHandle.getUid(userId, 0);
13245 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
13246 mKernelUidCpuFreqTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
13247 mKernelUidCpuTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
Sudheer Shankab2f83c12017-11-13 19:25:01 -080013248 if (mKernelSingleUidTimeReader != null) {
13249 mKernelSingleUidTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
13250 }
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070013251 }
13252
13253 public void onUserRemovedLocked(int userId) {
13254 final int firstUidForUser = UserHandle.getUid(userId, 0);
13255 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
13256 mUidStats.put(firstUidForUser, null);
13257 mUidStats.put(lastUidForUser, null);
13258 final int firstIndex = mUidStats.indexOfKey(firstUidForUser);
13259 final int lastIndex = mUidStats.indexOfKey(lastUidForUser);
13260 mUidStats.removeAtRange(firstIndex, lastIndex - firstIndex + 1);
13261 }
13262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013263 /**
13264 * Remove the statistics object for a particular uid.
13265 */
13266 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070013267 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070013268 mKernelUidCpuFreqTimeReader.removeUid(uid);
Sudheer Shankab2f83c12017-11-13 19:25:01 -080013269 if (mKernelSingleUidTimeReader != null) {
13270 mKernelSingleUidTimeReader.removeUid(uid);
13271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272 mUidStats.remove(uid);
13273 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070013274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013275 /**
13276 * Retrieve the statistics object for a particular process, creating
13277 * if needed.
13278 */
13279 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070013280 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281 Uid u = getUidStatsLocked(uid);
13282 return u.getProcessStatsLocked(name);
13283 }
13284
13285 /**
13286 * Retrieve the statistics object for a particular process, creating
13287 * if needed.
13288 */
13289 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070013290 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 Uid u = getUidStatsLocked(uid);
13292 return u.getPackageStatsLocked(pkg);
13293 }
13294
13295 /**
13296 * Retrieve the statistics object for a particular service, creating
13297 * if needed.
13298 */
13299 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070013300 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013301 Uid u = getUidStatsLocked(uid);
13302 return u.getServiceStatsLocked(pkg, name);
13303 }
13304
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013305 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080013306 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013307 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013308 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013309 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013310
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013311 public boolean trackPerProcStateCpuTimes() {
13312 return mConstants.TRACK_CPU_TIMES_BY_PROC_STATE && mPerProcStateCpuTimesAvailable;
13313 }
13314
13315 public void systemServicesReady(Context context) {
13316 mConstants.startObserving(context.getContentResolver());
13317 }
13318
13319 @VisibleForTesting
13320 public final class Constants extends ContentObserver {
13321 public static final String KEY_TRACK_CPU_TIMES_BY_PROC_STATE
13322 = "track_cpu_times_by_proc_state";
Mike Mafae87da2018-01-19 20:07:20 -080013323 public static final String KEY_TRACK_CPU_ACTIVE_CLUSTER_TIME
13324 = "track_cpu_active_cluster_time";
13325 public static final String KEY_READ_BINARY_CPU_TIME
13326 = "read_binary_cpu_time";
Sudheer Shankac20379e2018-02-15 00:06:21 -080013327 public static final String KEY_PROC_STATE_CPU_TIMES_READ_DELAY_MS
13328 = "proc_state_cpu_times_read_delay_ms";
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013329
13330 private static final boolean DEFAULT_TRACK_CPU_TIMES_BY_PROC_STATE = true;
Mike Mafae87da2018-01-19 20:07:20 -080013331 private static final boolean DEFAULT_TRACK_CPU_ACTIVE_CLUSTER_TIME = true;
13332 private static final boolean DEFAULT_READ_BINARY_CPU_TIME = false;
Sudheer Shankac20379e2018-02-15 00:06:21 -080013333 private static final long DEFAULT_PROC_STATE_CPU_TIMES_READ_DELAY_MS = 5_000;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013334
13335 public boolean TRACK_CPU_TIMES_BY_PROC_STATE = DEFAULT_TRACK_CPU_TIMES_BY_PROC_STATE;
Mike Mafae87da2018-01-19 20:07:20 -080013336 public boolean TRACK_CPU_ACTIVE_CLUSTER_TIME = DEFAULT_TRACK_CPU_ACTIVE_CLUSTER_TIME;
13337 // Not used right now.
13338 public boolean READ_BINARY_CPU_TIME = DEFAULT_READ_BINARY_CPU_TIME;
Sudheer Shankac20379e2018-02-15 00:06:21 -080013339 public long PROC_STATE_CPU_TIMES_READ_DELAY_MS = DEFAULT_PROC_STATE_CPU_TIMES_READ_DELAY_MS;
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013340
13341 private ContentResolver mResolver;
13342 private final KeyValueListParser mParser = new KeyValueListParser(',');
13343
13344 public Constants(Handler handler) {
13345 super(handler);
13346 }
13347
13348 public void startObserving(ContentResolver resolver) {
13349 mResolver = resolver;
13350 mResolver.registerContentObserver(
13351 Settings.Global.getUriFor(Settings.Global.BATTERY_STATS_CONSTANTS),
13352 false /* notifyForDescendants */, this);
13353 updateConstants();
13354 }
13355
13356 @Override
13357 public void onChange(boolean selfChange, Uri uri) {
13358 updateConstants();
13359 }
13360
13361 private void updateConstants() {
13362 synchronized (BatteryStatsImpl.this) {
13363 try {
13364 mParser.setString(Settings.Global.getString(mResolver,
13365 Settings.Global.BATTERY_STATS_CONSTANTS));
13366 } catch (IllegalArgumentException e) {
13367 // Failed to parse the settings string, log this and move on
13368 // with defaults.
13369 Slog.e(TAG, "Bad batterystats settings", e);
13370 }
13371
13372 updateTrackCpuTimesByProcStateLocked(TRACK_CPU_TIMES_BY_PROC_STATE,
13373 mParser.getBoolean(KEY_TRACK_CPU_TIMES_BY_PROC_STATE,
13374 DEFAULT_TRACK_CPU_TIMES_BY_PROC_STATE));
Mike Mafae87da2018-01-19 20:07:20 -080013375 TRACK_CPU_ACTIVE_CLUSTER_TIME = mParser.getBoolean(
13376 KEY_TRACK_CPU_ACTIVE_CLUSTER_TIME, DEFAULT_TRACK_CPU_ACTIVE_CLUSTER_TIME);
13377 READ_BINARY_CPU_TIME = mParser.getBoolean(
13378 KEY_READ_BINARY_CPU_TIME, DEFAULT_READ_BINARY_CPU_TIME);
Sudheer Shankac20379e2018-02-15 00:06:21 -080013379 updateProcStateCpuTimesReadDelayMs(PROC_STATE_CPU_TIMES_READ_DELAY_MS,
13380 mParser.getLong(KEY_PROC_STATE_CPU_TIMES_READ_DELAY_MS,
13381 DEFAULT_PROC_STATE_CPU_TIMES_READ_DELAY_MS));
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013382 }
13383 }
13384
13385 private void updateTrackCpuTimesByProcStateLocked(boolean wasEnabled, boolean isEnabled) {
13386 TRACK_CPU_TIMES_BY_PROC_STATE = isEnabled;
13387 if (isEnabled && !wasEnabled) {
13388 mKernelSingleUidTimeReader.markDataAsStale(true);
13389 mExternalSync.scheduleCpuSyncDueToSettingChange();
Sudheer Shankac20379e2018-02-15 00:06:21 -080013390
13391 mNumCpuTimeReads = 0;
13392 mNumBatchedCpuTimeReads = 0;
13393 mCpuTimeReadsTrackingStartTime = mClocks.uptimeMillis();
13394 }
13395 }
13396
13397 private void updateProcStateCpuTimesReadDelayMs(long oldDelayMillis, long newDelayMillis) {
13398 PROC_STATE_CPU_TIMES_READ_DELAY_MS = newDelayMillis;
13399 if (oldDelayMillis != newDelayMillis) {
13400 mNumCpuTimeReads = 0;
13401 mNumBatchedCpuTimeReads = 0;
13402 mCpuTimeReadsTrackingStartTime = mClocks.uptimeMillis();
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013403 }
13404 }
13405
13406 public void dumpLocked(PrintWriter pw) {
13407 pw.print(KEY_TRACK_CPU_TIMES_BY_PROC_STATE); pw.print("=");
13408 pw.println(TRACK_CPU_TIMES_BY_PROC_STATE);
Mike Mafae87da2018-01-19 20:07:20 -080013409 pw.print(KEY_TRACK_CPU_ACTIVE_CLUSTER_TIME); pw.print("=");
13410 pw.println(TRACK_CPU_ACTIVE_CLUSTER_TIME);
13411 pw.print(KEY_READ_BINARY_CPU_TIME); pw.print("=");
13412 pw.println(READ_BINARY_CPU_TIME);
Sudheer Shankac20379e2018-02-15 00:06:21 -080013413 pw.print(KEY_PROC_STATE_CPU_TIMES_READ_DELAY_MS); pw.print("=");
13414 pw.println(PROC_STATE_CPU_TIMES_READ_DELAY_MS);
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013415 }
13416 }
13417
Andreas Gampe3f24e692018-02-05 13:24:28 -080013418 @GuardedBy("this")
Sudheer Shanka5c19b892018-01-05 17:25:46 -080013419 public void dumpConstantsLocked(PrintWriter pw) {
13420 mConstants.dumpLocked(pw);
13421 }
13422
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013423 Parcel mPendingWrite = null;
13424 final ReentrantLock mWriteLock = new ReentrantLock();
13425
13426 public void writeAsyncLocked() {
13427 writeLocked(false);
13428 }
13429
13430 public void writeSyncLocked() {
13431 writeLocked(true);
13432 }
13433
13434 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013435 if (mFile == null) {
13436 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 return;
13438 }
13439
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013440 if (mShuttingDown) {
13441 return;
13442 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013443
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013444 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013445 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080013446 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013447
13448 if (mPendingWrite != null) {
13449 mPendingWrite.recycle();
13450 }
13451 mPendingWrite = out;
13452
13453 if (sync) {
13454 commitPendingDataToDisk();
13455 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013456 BackgroundThread.getHandler().post(new Runnable() {
13457 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013458 commitPendingDataToDisk();
13459 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013460 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013461 }
13462 }
13463
13464 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070013465 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013466 synchronized (this) {
13467 next = mPendingWrite;
13468 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070013469 if (next == null) {
13470 return;
13471 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013472 }
13473
Amith Yamasanid2450862017-02-07 15:58:24 -080013474 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 try {
Dianne Hackborne17b4452018-01-10 13:15:40 -080013476 final long startTime = SystemClock.uptimeMillis();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013477 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013478 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013479 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070013480 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013482 mFile.commit();
Dianne Hackborne17b4452018-01-10 13:15:40 -080013483 com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
13484 "batterystats", SystemClock.uptimeMillis() - startTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013486 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070013487 mFile.rollback();
13488 } finally {
13489 next.recycle();
13490 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070013491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013492 }
13493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013495 if (mDailyFile != null) {
13496 readDailyStatsLocked();
13497 }
13498
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013499 if (mFile == null) {
13500 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013501 return;
13502 }
13503
13504 mUidStats.clear();
13505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013507 File file = mFile.chooseForRead();
13508 if (!file.exists()) {
13509 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013511 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013513 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013514 Parcel in = Parcel.obtain();
13515 in.unmarshall(raw, 0, raw.length);
13516 in.setDataPosition(0);
13517 stream.close();
13518
13519 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080013520 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013521 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013522 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013524
Dianne Hackborncd0e3352014-08-07 17:08:09 -070013525 mEndPlatformVersion = Build.ID;
13526
Dianne Hackborne5167ca2014-03-08 14:39:10 -080013527 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013528 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080013529 final long elapsedRealtime = mClocks.elapsedRealtime();
13530 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080013531 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013532 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080013533 }
Narayan Kamath5a5a7fe2018-02-05 17:30:06 +000013534 addHistoryBufferLocked(elapsedRealtime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013535 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070013536 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013537
13538 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013539 }
13540
13541 public int describeContents() {
13542 return 0;
13543 }
13544
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013545 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070013546 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013547
13548 mHistoryBuffer.setDataSize(0);
13549 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013550 mHistoryTagPool.clear();
13551 mNextHistoryTagIdx = 0;
13552 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080013553
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013554 int numTags = in.readInt();
13555 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080013556 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013557 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013558 if (str == null) {
13559 throw new ParcelFormatException("null history tag string");
13560 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013561 int uid = in.readInt();
13562 HistoryTag tag = new HistoryTag();
13563 tag.string = str;
13564 tag.uid = uid;
13565 tag.poolIdx = idx;
13566 mHistoryTagPool.put(tag, idx);
13567 if (idx >= mNextHistoryTagIdx) {
13568 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080013569 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013570 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080013571 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013572
13573 int bufSize = in.readInt();
13574 int curPos = in.dataPosition();
13575 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013576 throw new ParcelFormatException("File corrupt: history data buffer too large " +
13577 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013578 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013579 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
13580 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013581 } else {
13582 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
13583 + " bytes at " + curPos);
13584 mHistoryBuffer.appendFrom(in, curPos, bufSize);
13585 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070013586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013587
Dianne Hackbornae384452011-06-28 12:33:48 -070013588 if (andOldHistory) {
13589 readOldHistory(in);
13590 }
13591
13592 if (DEBUG_HISTORY) {
13593 StringBuilder sb = new StringBuilder(128);
13594 sb.append("****************** OLD mHistoryBaseTime: ");
13595 TimeUtils.formatDuration(mHistoryBaseTime, sb);
13596 Slog.i(TAG, sb.toString());
13597 }
13598 mHistoryBaseTime = historyBaseTime;
13599 if (DEBUG_HISTORY) {
13600 StringBuilder sb = new StringBuilder(128);
13601 sb.append("****************** NEW mHistoryBaseTime: ");
13602 TimeUtils.formatDuration(mHistoryBaseTime, sb);
13603 Slog.i(TAG, sb.toString());
13604 }
13605
13606 // We are just arbitrarily going to insert 1 minute from the sample of
13607 // the last run until samples in this run.
13608 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080013609 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070013610 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070013611 if (DEBUG_HISTORY) {
13612 StringBuilder sb = new StringBuilder(128);
13613 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
13614 TimeUtils.formatDuration(mHistoryBaseTime, sb);
13615 Slog.i(TAG, sb.toString());
13616 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070013617 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070013618 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013619
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013620 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070013621 if (!USE_OLD_HISTORY) {
13622 return;
13623 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013624 mHistory = mHistoryEnd = mHistoryCache = null;
13625 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070013626 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013627 HistoryItem rec = new HistoryItem(time, in);
13628 addHistoryRecordLocked(rec);
13629 }
13630 }
13631
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013632 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070013633 if (DEBUG_HISTORY) {
13634 StringBuilder sb = new StringBuilder(128);
13635 sb.append("****************** WRITING mHistoryBaseTime: ");
13636 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070013637 sb.append(" mLastHistoryElapsedRealtime: ");
13638 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070013639 Slog.i(TAG, sb.toString());
13640 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070013641 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013642 if (!inclData) {
13643 out.writeInt(0);
13644 out.writeInt(0);
13645 return;
13646 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013647 out.writeInt(mHistoryTagPool.size());
13648 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
13649 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080013650 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080013651 out.writeString(tag.string);
13652 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080013653 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013654 out.writeInt(mHistoryBuffer.dataSize());
13655 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
13656 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
13657 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070013658
13659 if (andOldHistory) {
13660 writeOldHistory(out);
13661 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013662 }
13663
13664 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070013665 if (!USE_OLD_HISTORY) {
13666 return;
13667 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013668 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070013669 while (rec != null) {
13670 if (rec.time >= 0) rec.writeToParcel(out, 0);
13671 rec = rec.next;
13672 }
13673 out.writeLong(-1);
13674 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013675
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013676 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013677 final int version = in.readInt();
13678 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013679 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013680 + ", expected " + VERSION + "; erasing old stats");
13681 return;
13682 }
13683
Dianne Hackbornae384452011-06-28 12:33:48 -070013684 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013686 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013687 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080013689 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070013690 mStartPlatformVersion = in.readString();
13691 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013692 mOnBatteryTimeBase.readSummaryFromParcel(in);
13693 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013694 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013695 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070013696 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013697 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070013698 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070013699 mMinLearnedBatteryCapacity = in.readInt();
13700 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013701 mLowDischargeAmountSinceCharge = in.readInt();
13702 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013703 mDischargeAmountScreenOnSinceCharge = in.readInt();
13704 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070013705 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013706 mDischargeStepTracker.readFromParcel(in);
13707 mChargeStepTracker.readFromParcel(in);
13708 mDailyDischargeStepTracker.readFromParcel(in);
13709 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070013710 mDischargeCounter.readSummaryFromParcelLocked(in);
13711 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070013712 mDischargeScreenDozeCounter.readSummaryFromParcelLocked(in);
Mike Ma15313c92017-11-15 17:58:21 -080013713 mDischargeLightDozeCounter.readSummaryFromParcelLocked(in);
13714 mDischargeDeepDozeCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013715 int NPKG = in.readInt();
13716 if (NPKG > 0) {
13717 mDailyPackageChanges = new ArrayList<>(NPKG);
13718 while (NPKG > 0) {
13719 NPKG--;
13720 PackageChange pc = new PackageChange();
13721 pc.mPackageName = in.readString();
13722 pc.mUpdate = in.readInt() != 0;
Dianne Hackborn3accca02013-09-20 09:32:11 -070013723 pc.mVersionCode = in.readLong();
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013724 mDailyPackageChanges.add(pc);
13725 }
13726 } else {
13727 mDailyPackageChanges = null;
13728 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013729 mDailyStartTime = in.readLong();
13730 mNextMinDailyDeadline = in.readLong();
13731 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013733 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013734
Jeff Browne95c3cd2014-05-02 16:59:26 -070013735 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 mScreenOnTimer.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070013737 mScreenDozeTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070013738 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
13739 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
13740 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070013741 mInteractive = false;
13742 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070013744 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070013745 mLongestLightIdleTime = in.readLong();
13746 mLongestFullIdleTime = in.readLong();
13747 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
13748 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
13749 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013750 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080013752 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070013753 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
13754 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070013755 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070013756 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
13757 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
13758 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013759 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080013760 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
13761 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013762 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013763 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080013764 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080013765 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013766 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080013767 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
13768 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080013769 mWifiMulticastWakelockTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070013770 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070013771 mWifiOn = false;
13772 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013773 mGlobalWifiRunning = false;
13774 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080013775 for (int i=0; i<NUM_WIFI_STATES; i++) {
13776 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
13777 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013778 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13779 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
13780 }
13781 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13782 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
13783 }
Siddharth Rayb50a6842017-12-14 15:15:28 -080013784 mWifiActiveTimer.readSummaryFromParcelLocked(in);
13785 mWifiActivity.readSummaryFromParcel(in);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080013786 for (int i=0; i<GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
13787 mGpsSignalQualityTimer[i].readSummaryFromParcelLocked(in);
13788 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080013789 mBluetoothActivity.readSummaryFromParcel(in);
13790 mModemActivity.readSummaryFromParcel(in);
13791 mHasWifiReporting = in.readInt() != 0;
13792 mHasBluetoothReporting = in.readInt() != 0;
13793 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070013794
Dianne Hackborn1e01d162014-12-04 17:46:42 -080013795 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013796 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070013797 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013798 mCameraOnNesting = 0;
13799 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013800 mBluetoothScanNesting = 0;
13801 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
Siddharth Rayf5e796a2018-01-22 18:18:17 -080013802 mIsCellularTxPowerHigh = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013803
Bookatz50df7112017-08-04 14:53:26 -070013804 int NRPMS = in.readInt();
13805 if (NRPMS > 10000) {
13806 throw new ParcelFormatException("File corrupt: too many rpm stats " + NRPMS);
13807 }
13808 for (int irpm = 0; irpm < NRPMS; irpm++) {
13809 if (in.readInt() != 0) {
13810 String rpmName = in.readString();
13811 getRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
13812 }
13813 }
13814 int NSORPMS = in.readInt();
13815 if (NSORPMS > 10000) {
13816 throw new ParcelFormatException("File corrupt: too many screen-off rpm stats " + NSORPMS);
13817 }
13818 for (int irpm = 0; irpm < NSORPMS; irpm++) {
13819 if (in.readInt() != 0) {
13820 String rpmName = in.readString();
13821 getScreenOffRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
13822 }
13823 }
13824
Evan Millarc64edde2009-04-18 12:26:32 -070013825 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013826 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013827 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013828 }
Evan Millarc64edde2009-04-18 12:26:32 -070013829 for (int ikw = 0; ikw < NKW; ikw++) {
13830 if (in.readInt() != 0) {
13831 String kwltName = in.readString();
13832 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
13833 }
13834 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070013835
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013836 int NWR = in.readInt();
13837 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013838 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013839 }
13840 for (int iwr = 0; iwr < NWR; iwr++) {
13841 if (in.readInt() != 0) {
13842 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013843 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013844 }
13845 }
13846
James Carr3a226052016-07-01 14:49:52 -070013847 int NMS = in.readInt();
13848 for (int ims = 0; ims < NMS; ims++) {
13849 if (in.readInt() != 0) {
13850 long kmstName = in.readLong();
13851 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
13852 }
13853 }
13854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013855 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013856 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070013857 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070013858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859 for (int iu = 0; iu < NU; iu++) {
13860 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080013861 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013862 mUidStats.put(uid, u);
13863
Bookatz867c0d72017-03-07 18:23:42 -080013864 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070013865 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080013866
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013867 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013868 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013869 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013870 }
The Android Open Source Project10592532009-03-18 17:39:46 -070013871 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013872 if (in.readInt() != 0) {
13873 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
13874 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070013875 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013876 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070013877 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013878 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070013879 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
13880 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
13881 if (in.readInt() != 0) {
13882 u.makeWifiBatchedScanBin(i, null);
13883 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
13884 }
13885 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070013886 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013887 if (in.readInt() != 0) {
13888 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
13889 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013890 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080013891 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013892 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013893 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080013894 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
13895 }
13896 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013897 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
13898 }
13899 if (in.readInt() != 0) {
13900 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
13901 }
13902 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070013903 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
13904 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013905 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070013906 u.createForegroundServiceTimerLocked().readSummaryFromParcelLocked(in);
13907 }
13908 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070013909 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
13910 }
13911 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013912 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
13913 }
Bookatz956f36bf2017-04-28 09:48:17 -070013914 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070013915 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
13916 }
13917 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070013918 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
13919 }
Bookatzb1f04f32017-05-19 13:57:32 -070013920 if (in.readInt() != 0) {
13921 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
13922 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080013923 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070013924 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
13925 if (in.readInt() != 0) {
13926 u.makeProcessState(i, null);
13927 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
13928 }
13929 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070013930 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080013931 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013932 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070013933
Dianne Hackborn617f8772009-03-31 15:04:46 -070013934 if (in.readInt() != 0) {
13935 if (u.mUserActivityCounters == null) {
13936 u.initUserActivityLocked();
13937 }
13938 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
13939 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
13940 }
13941 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013942
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013943 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080013944 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013945 u.initNetworkActivityLocked();
13946 }
13947 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080013948 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
13949 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013950 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080013951 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
13952 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070013953 }
13954
Adam Lesinski06af1fa2015-05-05 17:35:35 -070013955 u.mUserCpuTime.readSummaryFromParcelLocked(in);
13956 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
13957
Adam Lesinski6832f392015-09-05 18:05:40 -070013958 if (in.readInt() != 0) {
13959 final int numClusters = in.readInt();
13960 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
13961 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070013962 }
Adam Lesinski6832f392015-09-05 18:05:40 -070013963
Sudheer Shankaaf857412017-07-21 00:14:24 -070013964 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070013965 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070013966 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070013967 final int NSB = in.readInt();
13968 if (mPowerProfile != null &&
13969 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
13970 throw new ParcelFormatException("File corrupt: too many speed bins " +
13971 NSB);
13972 }
13973
Sudheer Shankaaf857412017-07-21 00:14:24 -070013974 u.mCpuClusterSpeedTimesUs[cluster] = new LongSamplingCounter[NSB];
Adam Lesinski6832f392015-09-05 18:05:40 -070013975 for (int speed = 0; speed < NSB; speed++) {
13976 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070013977 u.mCpuClusterSpeedTimesUs[cluster][speed] = new LongSamplingCounter(
Adam Lesinski6832f392015-09-05 18:05:40 -070013978 mOnBatteryTimeBase);
Sudheer Shankaaf857412017-07-21 00:14:24 -070013979 u.mCpuClusterSpeedTimesUs[cluster][speed].readSummaryFromParcelLocked(in);
Adam Lesinski6832f392015-09-05 18:05:40 -070013980 }
13981 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070013982 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070013983 u.mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070013984 }
13985 }
13986 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070013987 u.mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070013988 }
13989
Sudheer Shanka59f5c002017-05-15 10:57:15 -070013990 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
13991 in, mOnBatteryTimeBase);
13992 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
13993 in, mOnBatteryScreenOffTimeBase);
Mike Ma3d422c32017-10-25 11:08:57 -070013994
13995 u.mCpuActiveTimeMs.readSummaryFromParcelLocked(in);
13996 u.mCpuClusterTimesMs.readSummaryFromParcelLocked(in);
13997
Sudheer Shankab2f83c12017-11-13 19:25:01 -080013998 int length = in.readInt();
13999 if (length == Uid.NUM_PROCESS_STATE) {
14000 u.mProcStateTimeMs = new LongSamplingCounterArray[length];
14001 for (int procState = 0; procState < length; ++procState) {
14002 u.mProcStateTimeMs[procState]
14003 = LongSamplingCounterArray.readSummaryFromParcelLocked(
14004 in, mOnBatteryTimeBase);
14005 }
14006 } else {
14007 u.mProcStateTimeMs = null;
14008 }
14009 length = in.readInt();
14010 if (length == Uid.NUM_PROCESS_STATE) {
14011 u.mProcStateScreenOffTimeMs = new LongSamplingCounterArray[length];
14012 for (int procState = 0; procState < length; ++procState) {
14013 u.mProcStateScreenOffTimeMs[procState]
14014 = LongSamplingCounterArray.readSummaryFromParcelLocked(
14015 in, mOnBatteryScreenOffTimeBase);
14016 }
14017 } else {
14018 u.mProcStateScreenOffTimeMs = null;
14019 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070014020
14021 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070014022 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
14023 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
14024 } else {
14025 u.mMobileRadioApWakeupCount = null;
14026 }
14027
14028 if (in.readInt() != 0) {
14029 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
14030 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
14031 } else {
14032 u.mWifiRadioApWakeupCount = null;
14033 }
14034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070014036 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014037 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070014038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 for (int iw = 0; iw < NW; iw++) {
14040 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070014041 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 }
14043
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014044 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070014045 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014046 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014047 }
14048 for (int is = 0; is < NS; is++) {
14049 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070014050 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014051 }
14052
14053 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070014054 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014055 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014056 }
14057 for (int ij = 0; ij < NJ; ij++) {
14058 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070014059 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014060 }
14061
Dianne Hackborn94326cb2017-06-28 16:17:20 -070014062 u.readJobCompletionsFromParcelLocked(in);
14063
Amith Yamasani977e11f2018-02-16 11:29:54 -080014064 u.mJobsDeferredEventCount.readSummaryFromParcelLocked(in);
14065 u.mJobsDeferredCount.readSummaryFromParcelLocked(in);
14066 u.mJobsFreshnessTimeMs.readSummaryFromParcelLocked(in);
14067 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
14068 if (in.readInt() != 0) {
14069 u.mJobsFreshnessBuckets[i] = new Counter(u.mBsi.mOnBatteryTimeBase);
14070 u.mJobsFreshnessBuckets[i].readSummaryFromParcelLocked(in);
14071 }
14072 }
14073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080014075 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014076 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070014077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014078 for (int is = 0; is < NP; is++) {
14079 int seNumber = in.readInt();
14080 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080014081 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 }
14083 }
14084
14085 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080014086 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014087 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070014088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014089 for (int ip = 0; ip < NP; ip++) {
14090 String procName = in.readString();
14091 Uid.Proc p = u.getProcessStatsLocked(procName);
14092 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070014094 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080014096 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
14097 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014098 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 }
14100
14101 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070014102 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014103 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070014104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 for (int ip = 0; ip < NP; ip++) {
14106 String pkgName = in.readString();
14107 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070014108 final int NWA = in.readInt();
14109 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014110 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070014111 }
14112 p.mWakeupAlarms.clear();
14113 for (int iwa=0; iwa<NWA; iwa++) {
14114 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -070014115 Counter c = new Counter(mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070014116 c.readSummaryFromParcelLocked(in);
14117 p.mWakeupAlarms.put(tag, c);
14118 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014119 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080014120 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070014121 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080014122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014123 for (int is = 0; is < NS; is++) {
14124 String servName = in.readString();
14125 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
14126 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014127 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 }
14130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 }
14132 }
14133
14134 /**
14135 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
14136 * disk. This format does not allow a lossless round-trip.
14137 *
14138 * @param out the Parcel to be written to.
14139 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070014140 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080014141 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070014142
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070014143 // Pull the clock time. This may update the time and make a new history entry
14144 // if we had originally pulled a time before the RTC was set.
14145 long startClockTime = getStartClockTime();
14146
Joe Onoratoabded112016-02-08 16:49:39 -080014147 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
14148 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149
14150 out.writeInt(VERSION);
14151
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070014152 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014155 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014156 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070014157 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070014158 out.writeString(mStartPlatformVersion);
14159 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014160 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
14161 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014162 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014163 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070014164 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014165 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070014166 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070014167 out.writeInt(mMinLearnedBatteryCapacity);
14168 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080014169 out.writeInt(getLowDischargeAmountSinceCharge());
14170 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080014171 out.writeInt(getDischargeAmountScreenOnSinceCharge());
14172 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Mike Mac2f518a2017-09-19 16:06:03 -070014173 out.writeInt(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080014174 mDischargeStepTracker.writeToParcel(out);
14175 mChargeStepTracker.writeToParcel(out);
14176 mDailyDischargeStepTracker.writeToParcel(out);
14177 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070014178 mDischargeCounter.writeSummaryFromParcelLocked(out);
14179 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Mike Mac2f518a2017-09-19 16:06:03 -070014180 mDischargeScreenDozeCounter.writeSummaryFromParcelLocked(out);
Mike Ma15313c92017-11-15 17:58:21 -080014181 mDischargeLightDozeCounter.writeSummaryFromParcelLocked(out);
14182 mDischargeDeepDozeCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070014183 if (mDailyPackageChanges != null) {
14184 final int NPKG = mDailyPackageChanges.size();
14185 out.writeInt(NPKG);
14186 for (int i=0; i<NPKG; i++) {
14187 PackageChange pc = mDailyPackageChanges.get(i);
14188 out.writeString(pc.mPackageName);
14189 out.writeInt(pc.mUpdate ? 1 : 0);
Dianne Hackborn3accca02013-09-20 09:32:11 -070014190 out.writeLong(pc.mVersionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070014191 }
14192 } else {
14193 out.writeInt(0);
14194 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080014195 out.writeLong(mDailyStartTime);
14196 out.writeLong(mNextMinDailyDeadline);
14197 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014198
14199 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Mike Mac2f518a2017-09-19 16:06:03 -070014200 mScreenDozeTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070014201 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014202 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070014203 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070014204 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070014205 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070014206 out.writeLong(mLongestLightIdleTime);
14207 out.writeLong(mLongestFullIdleTime);
14208 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14209 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14210 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070014211 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014212 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080014213 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014214 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070014215 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014216 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070014217 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014218 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070014219 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070014220 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080014221 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
14222 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070014223 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014224 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14225 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014226 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080014227 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
14228 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080014229 mWifiMulticastWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014230 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14231 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080014232 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014233 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080014234 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070014235 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
14236 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14237 }
14238 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
14239 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14240 }
Siddharth Rayb50a6842017-12-14 15:15:28 -080014241 mWifiActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14242 mWifiActivity.writeSummaryToParcel(out);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080014243 for (int i=0; i< GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
14244 mGpsSignalQualityTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14245 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080014246 mBluetoothActivity.writeSummaryToParcel(out);
14247 mModemActivity.writeSummaryToParcel(out);
14248 out.writeInt(mHasWifiReporting ? 1 : 0);
14249 out.writeInt(mHasBluetoothReporting ? 1 : 0);
14250 out.writeInt(mHasModemReporting ? 1 : 0);
14251
Dianne Hackborn1e01d162014-12-04 17:46:42 -080014252 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070014253 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014254 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080014255 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256
Bookatz50df7112017-08-04 14:53:26 -070014257 out.writeInt(mRpmStats.size());
14258 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
14259 Timer rpmt = ent.getValue();
14260 if (rpmt != null) {
14261 out.writeInt(1);
14262 out.writeString(ent.getKey());
14263 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14264 } else {
14265 out.writeInt(0);
14266 }
14267 }
14268 out.writeInt(mScreenOffRpmStats.size());
14269 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
14270 Timer rpmt = ent.getValue();
14271 if (rpmt != null) {
14272 out.writeInt(1);
14273 out.writeString(ent.getKey());
14274 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14275 } else {
14276 out.writeInt(0);
14277 }
14278 }
14279
Evan Millarc64edde2009-04-18 12:26:32 -070014280 out.writeInt(mKernelWakelockStats.size());
14281 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
14282 Timer kwlt = ent.getValue();
14283 if (kwlt != null) {
14284 out.writeInt(1);
14285 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014286 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14287 } else {
14288 out.writeInt(0);
14289 }
14290 }
14291
14292 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070014293 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
14294 SamplingTimer timer = ent.getValue();
14295 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014296 out.writeInt(1);
14297 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070014298 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070014299 } else {
14300 out.writeInt(0);
14301 }
14302 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014303
James Carr3a226052016-07-01 14:49:52 -070014304 out.writeInt(mKernelMemoryStats.size());
14305 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
14306 Timer kmt = mKernelMemoryStats.valueAt(i);
14307 if (kmt != null) {
14308 out.writeInt(1);
14309 out.writeLong(mKernelMemoryStats.keyAt(i));
14310 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14311 } else {
14312 out.writeInt(0);
14313 }
14314 }
14315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014316 final int NU = mUidStats.size();
14317 out.writeInt(NU);
14318 for (int iu = 0; iu < NU; iu++) {
14319 out.writeInt(mUidStats.keyAt(iu));
14320 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014321
Bookatz867c0d72017-03-07 18:23:42 -080014322 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070014323 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080014324
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070014325 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014326 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014327 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014328 } else {
14329 out.writeInt(0);
14330 }
14331 if (u.mFullWifiLockTimer != null) {
14332 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014333 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014334 } else {
14335 out.writeInt(0);
14336 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070014337 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014338 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014339 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014340 } else {
14341 out.writeInt(0);
14342 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070014343 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
14344 if (u.mWifiBatchedScanTimer[i] != null) {
14345 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014346 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070014347 } else {
14348 out.writeInt(0);
14349 }
14350 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014351 if (u.mWifiMulticastTimer != null) {
14352 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014353 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014354 } else {
14355 out.writeInt(0);
14356 }
14357 if (u.mAudioTurnedOnTimer != null) {
14358 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014359 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014360 } else {
14361 out.writeInt(0);
14362 }
14363 if (u.mVideoTurnedOnTimer != null) {
14364 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014365 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014366 } else {
14367 out.writeInt(0);
14368 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014369 if (u.mFlashlightTurnedOnTimer != null) {
14370 out.writeInt(1);
14371 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14372 } else {
14373 out.writeInt(0);
14374 }
14375 if (u.mCameraTurnedOnTimer != null) {
14376 out.writeInt(1);
14377 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14378 } else {
14379 out.writeInt(0);
14380 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070014381 if (u.mForegroundActivityTimer != null) {
14382 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014383 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070014384 } else {
14385 out.writeInt(0);
14386 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070014387 if (u.mForegroundServiceTimer != null) {
14388 out.writeInt(1);
14389 u.mForegroundServiceTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14390 } else {
14391 out.writeInt(0);
14392 }
Bookatzc8c44962017-05-11 12:12:54 -070014393 if (u.mAggregatedPartialWakelockTimer != null) {
14394 out.writeInt(1);
14395 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14396 } else {
14397 out.writeInt(0);
14398 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080014399 if (u.mBluetoothScanTimer != null) {
14400 out.writeInt(1);
14401 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14402 } else {
14403 out.writeInt(0);
14404 }
Bookatzb1f04f32017-05-19 13:57:32 -070014405 if (u.mBluetoothUnoptimizedScanTimer != null) {
14406 out.writeInt(1);
14407 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14408 } else {
14409 out.writeInt(0);
14410 }
Bookatz956f36bf2017-04-28 09:48:17 -070014411 if (u.mBluetoothScanResultCounter != null) {
14412 out.writeInt(1);
14413 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
14414 } else {
14415 out.writeInt(0);
14416 }
Bookatzb1f04f32017-05-19 13:57:32 -070014417 if (u.mBluetoothScanResultBgCounter != null) {
14418 out.writeInt(1);
14419 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
14420 } else {
14421 out.writeInt(0);
14422 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070014423 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
14424 if (u.mProcessStateTimer[i] != null) {
14425 out.writeInt(1);
14426 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14427 } else {
14428 out.writeInt(0);
14429 }
14430 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080014431 if (u.mVibratorOnTimer != null) {
14432 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014433 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080014434 } else {
14435 out.writeInt(0);
14436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014437
Dianne Hackborn617f8772009-03-31 15:04:46 -070014438 if (u.mUserActivityCounters == null) {
14439 out.writeInt(0);
14440 } else {
14441 out.writeInt(1);
14442 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
14443 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
14444 }
14445 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014446
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080014447 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070014448 out.writeInt(0);
14449 } else {
14450 out.writeInt(1);
14451 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080014452 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
14453 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070014454 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080014455 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
14456 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070014457 }
14458
Adam Lesinski06af1fa2015-05-05 17:35:35 -070014459 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
14460 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
14461
Sudheer Shankaaf857412017-07-21 00:14:24 -070014462 if (u.mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -070014463 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -070014464 out.writeInt(u.mCpuClusterSpeedTimesUs.length);
14465 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -070014466 if (cpuSpeeds != null) {
14467 out.writeInt(1);
14468 out.writeInt(cpuSpeeds.length);
14469 for (LongSamplingCounter c : cpuSpeeds) {
14470 if (c != null) {
14471 out.writeInt(1);
14472 c.writeSummaryFromParcelLocked(out);
14473 } else {
14474 out.writeInt(0);
14475 }
14476 }
14477 } else {
14478 out.writeInt(0);
14479 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070014480 }
Adam Lesinski6832f392015-09-05 18:05:40 -070014481 } else {
14482 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070014483 }
14484
Sudheer Shanka59f5c002017-05-15 10:57:15 -070014485 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
14486 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070014487
Mike Ma3d422c32017-10-25 11:08:57 -070014488 u.mCpuActiveTimeMs.writeSummaryFromParcelLocked(out);
14489 u.mCpuClusterTimesMs.writeSummaryToParcelLocked(out);
14490
Sudheer Shankab2f83c12017-11-13 19:25:01 -080014491 if (u.mProcStateTimeMs != null) {
14492 out.writeInt(u.mProcStateTimeMs.length);
14493 for (LongSamplingCounterArray counters : u.mProcStateTimeMs) {
14494 LongSamplingCounterArray.writeSummaryToParcelLocked(out, counters);
14495 }
14496 } else {
14497 out.writeInt(0);
14498 }
14499 if (u.mProcStateScreenOffTimeMs != null) {
14500 out.writeInt(u.mProcStateScreenOffTimeMs.length);
14501 for (LongSamplingCounterArray counters : u.mProcStateScreenOffTimeMs) {
14502 LongSamplingCounterArray.writeSummaryToParcelLocked(out, counters);
14503 }
14504 } else {
14505 out.writeInt(0);
14506 }
14507
Adam Lesinski5f056f62016-07-14 16:56:08 -070014508 if (u.mMobileRadioApWakeupCount != null) {
14509 out.writeInt(1);
14510 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
14511 } else {
14512 out.writeInt(0);
14513 }
14514
14515 if (u.mWifiRadioApWakeupCount != null) {
14516 out.writeInt(1);
14517 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
14518 } else {
14519 out.writeInt(0);
14520 }
14521
Dianne Hackbornd953c532014-08-16 18:17:38 -070014522 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
14523 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070014525 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070014526 out.writeString(wakeStats.keyAt(iw));
14527 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070014528 if (wl.mTimerFull != null) {
14529 out.writeInt(1);
14530 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14531 } else {
14532 out.writeInt(0);
14533 }
14534 if (wl.mTimerPartial != null) {
14535 out.writeInt(1);
14536 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14537 } else {
14538 out.writeInt(0);
14539 }
14540 if (wl.mTimerWindow != null) {
14541 out.writeInt(1);
14542 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14543 } else {
14544 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014545 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070014546 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070014547 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070014548 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070014549 } else {
14550 out.writeInt(0);
14551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 }
14553
Bookatz2bffb5b2017-04-13 11:59:33 -070014554 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070014555 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014556 out.writeInt(NS);
14557 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070014558 out.writeString(syncStats.keyAt(is));
14559 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014560 }
14561
Bookatzaa4594a2017-03-24 12:39:56 -070014562 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070014563 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014564 out.writeInt(NJ);
14565 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070014566 out.writeString(jobStats.keyAt(ij));
14567 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014568 }
14569
Dianne Hackborn94326cb2017-06-28 16:17:20 -070014570 u.writeJobCompletionsToParcelLocked(out);
14571
Amith Yamasani977e11f2018-02-16 11:29:54 -080014572 u.mJobsDeferredEventCount.writeSummaryFromParcelLocked(out);
14573 u.mJobsDeferredCount.writeSummaryFromParcelLocked(out);
14574 u.mJobsFreshnessTimeMs.writeSummaryFromParcelLocked(out);
14575 for (int i = 0; i < JOB_FRESHNESS_BUCKETS.length; i++) {
14576 if (u.mJobsFreshnessBuckets[i] != null) {
14577 u.mJobsFreshnessBuckets[i].writeSummaryFromParcelLocked(out);
14578 } else {
14579 out.writeInt(0);
14580 }
14581 }
14582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014583 int NSE = u.mSensorStats.size();
14584 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070014585 for (int ise=0; ise<NSE; ise++) {
14586 out.writeInt(u.mSensorStats.keyAt(ise));
14587 Uid.Sensor se = u.mSensorStats.valueAt(ise);
14588 if (se.mTimer != null) {
14589 out.writeInt(1);
14590 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
14591 } else {
14592 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014593 }
14594 }
14595
14596 int NP = u.mProcessStats.size();
14597 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070014598 for (int ip=0; ip<NP; ip++) {
14599 out.writeString(u.mProcessStats.keyAt(ip));
14600 Uid.Proc ps = u.mProcessStats.valueAt(ip);
14601 out.writeLong(ps.mUserTime);
14602 out.writeLong(ps.mSystemTime);
14603 out.writeLong(ps.mForegroundTime);
14604 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080014605 out.writeInt(ps.mNumCrashes);
14606 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070014607 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014608 }
14609
14610 NP = u.mPackageStats.size();
14611 out.writeInt(NP);
14612 if (NP > 0) {
14613 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
14614 : u.mPackageStats.entrySet()) {
14615 out.writeString(ent.getKey());
14616 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070014617 final int NWA = ps.mWakeupAlarms.size();
14618 out.writeInt(NWA);
14619 for (int iwa=0; iwa<NWA; iwa++) {
14620 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
14621 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
14622 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014623 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070014625 for (int is=0; is<NS; is++) {
14626 out.writeString(ps.mServiceStats.keyAt(is));
14627 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
14628 long time = ss.getStartTimeToNowLocked(
14629 mOnBatteryTimeBase.getUptime(NOW_SYS));
14630 out.writeLong(time);
14631 out.writeInt(ss.mStarts);
14632 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014633 }
14634 }
14635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014636 }
14637 }
14638
14639 public void readFromParcel(Parcel in) {
14640 readFromParcelLocked(in);
14641 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014643 void readFromParcelLocked(Parcel in) {
14644 int magic = in.readInt();
14645 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070014646 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 }
14648
Dianne Hackbornae384452011-06-28 12:33:48 -070014649 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080014652 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070014653 mStartPlatformVersion = in.readString();
14654 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014655 mUptime = in.readLong();
14656 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657 mRealtime = in.readLong();
14658 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070014660 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070014661 mMinLearnedBatteryCapacity = in.readInt();
14662 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014664 mOnBatteryTimeBase.readFromParcel(in);
14665 mOnBatteryScreenOffTimeBase.readFromParcel(in);
14666
Jeff Browne95c3cd2014-05-02 16:59:26 -070014667 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080014668 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070014669 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014670 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014671 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
14672 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014673 }
Dianne Hackborn29325132014-05-21 15:01:03 -070014674 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080014675 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014676 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080014677 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
14678 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070014679 mLongestLightIdleTime = in.readLong();
14680 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080014681 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
14682 mOnBatteryTimeBase, in);
14683 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
14684 mOnBatteryTimeBase, in);
14685 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
14686 mOnBatteryTimeBase, in);
14687 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
14688 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014689 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014690 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014691 null, mOnBatteryTimeBase, in);
14692 }
Joe Onoratoabded112016-02-08 16:49:39 -080014693 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
14694 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014695 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014696 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014697 null, mOnBatteryTimeBase, in);
14698 }
14699 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
14700 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
14701 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
14702 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014703 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080014704 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
14705 mOnBatteryTimeBase, in);
Kweku Adams87b19ec2017-10-09 12:40:03 -070014706 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
Joe Onoratoabded112016-02-08 16:49:39 -080014707 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014708 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014709 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
14710 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080014711 mWifiMulticastWakelockTimer = new StopwatchTimer(mClocks, null, -4, null,
14712 mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070014713 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014714 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080014715 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014716 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080014717 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
14718 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014719 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014720 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014721 null, mOnBatteryTimeBase, in);
14722 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070014723 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014724 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070014725 null, mOnBatteryTimeBase, in);
14726 }
14727 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080014728 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070014729 null, mOnBatteryTimeBase, in);
14730 }
Siddharth Rayb50a6842017-12-14 15:15:28 -080014731 mWifiActiveTimer = new StopwatchTimer(mClocks, null, -900, null,
14732 mOnBatteryTimeBase, in);
14733 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
14734 NUM_WIFI_TX_LEVELS, in);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080014735 for (int i=0; i<GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
14736 mGpsSignalQualityTimer[i] = new StopwatchTimer(mClocks, null, -1000-i,
14737 null, mOnBatteryTimeBase, in);
14738 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080014739 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
14740 NUM_BT_TX_LEVELS, in);
14741 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
14742 ModemActivityInfo.TX_POWER_LEVELS, in);
14743 mHasWifiReporting = in.readInt() != 0;
14744 mHasBluetoothReporting = in.readInt() != 0;
14745 mHasModemReporting = in.readInt() != 0;
14746
Dianne Hackborn1e01d162014-12-04 17:46:42 -080014747 mNumConnectivityChange = in.readInt();
14748 mLoadedNumConnectivityChange = in.readInt();
14749 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070014750 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080014751 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070014752 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080014753 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014754 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080014755 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014756 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080014757 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080014758 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080014759 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Siddharth Rayf5e796a2018-01-22 18:18:17 -080014760 mIsCellularTxPowerHigh = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014761 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014762 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070014763 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014764 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070014765 mLowDischargeAmountSinceCharge = in.readInt();
14766 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080014767 mDischargeAmountScreenOn = in.readInt();
14768 mDischargeAmountScreenOnSinceCharge = in.readInt();
14769 mDischargeAmountScreenOff = in.readInt();
14770 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070014771 mDischargeAmountScreenDoze = in.readInt();
14772 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080014773 mDischargeStepTracker.readFromParcel(in);
14774 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070014775 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070014776 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase, in);
14777 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
Mike Ma15313c92017-11-15 17:58:21 -080014778 mDischargeLightDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
14779 mDischargeDeepDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014780 mLastWriteTime = in.readLong();
14781
Bookatz50df7112017-08-04 14:53:26 -070014782 mRpmStats.clear();
14783 int NRPMS = in.readInt();
14784 for (int irpm = 0; irpm < NRPMS; irpm++) {
14785 if (in.readInt() != 0) {
14786 String rpmName = in.readString();
14787 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
14788 mRpmStats.put(rpmName, rpmt);
14789 }
14790 }
14791 mScreenOffRpmStats.clear();
14792 int NSORPMS = in.readInt();
14793 for (int irpm = 0; irpm < NSORPMS; irpm++) {
14794 if (in.readInt() != 0) {
14795 String rpmName = in.readString();
14796 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
14797 mScreenOffRpmStats.put(rpmName, rpmt);
14798 }
14799 }
14800
Evan Millarc64edde2009-04-18 12:26:32 -070014801 mKernelWakelockStats.clear();
14802 int NKW = in.readInt();
14803 for (int ikw = 0; ikw < NKW; ikw++) {
14804 if (in.readInt() != 0) {
14805 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080014806 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070014807 mKernelWakelockStats.put(wakelockName, kwlt);
14808 }
14809 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014810
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014811 mWakeupReasonStats.clear();
14812 int NWR = in.readInt();
14813 for (int iwr = 0; iwr < NWR; iwr++) {
14814 if (in.readInt() != 0) {
14815 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080014816 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070014817 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014818 }
14819 }
14820
James Carr3a226052016-07-01 14:49:52 -070014821 mKernelMemoryStats.clear();
14822 int nmt = in.readInt();
14823 for (int imt = 0; imt < nmt; imt++) {
14824 if (in.readInt() != 0) {
14825 Long bucket = in.readLong();
14826 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
14827 mKernelMemoryStats.put(bucket, kmt);
14828 }
14829 }
14830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 mPartialTimers.clear();
14832 mFullTimers.clear();
14833 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070014834 mWifiRunningTimers.clear();
14835 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070014836 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070014837 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070014838 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070014839 mAudioTurnedOnTimers.clear();
14840 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014841 mFlashlightTurnedOnTimers.clear();
14842 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843
14844 int numUids = in.readInt();
14845 mUidStats.clear();
14846 for (int i = 0; i < numUids; i++) {
14847 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080014848 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014849 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014850 mUidStats.append(uid, u);
14851 }
14852 }
14853
14854 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070014855 writeToParcelLocked(out, true, flags);
14856 }
14857
14858 public void writeToParcelWithoutUids(Parcel out, int flags) {
14859 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014861
14862 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070014863 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070014864 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080014865 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070014866
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070014867 // Pull the clock time. This may update the time and make a new history entry
14868 // if we had originally pulled a time before the RTC was set.
14869 long startClockTime = getStartClockTime();
14870
Joe Onoratoabded112016-02-08 16:49:39 -080014871 final long uSecUptime = mClocks.uptimeMillis() * 1000;
14872 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014873 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
14874 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014876 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014877
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070014878 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070014879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070014881 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070014882 out.writeString(mStartPlatformVersion);
14883 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014884 out.writeLong(mUptime);
14885 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014886 out.writeLong(mRealtime);
14887 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070014889 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070014890 out.writeInt(mMinLearnedBatteryCapacity);
14891 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014892 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
14893 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
14894
14895 mScreenOnTimer.writeToParcel(out, uSecRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -070014896 mScreenDozeTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014897 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
14898 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
14899 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070014900 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070014901 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070014902 out.writeLong(mLongestLightIdleTime);
14903 out.writeLong(mLongestFullIdleTime);
14904 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
14905 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
14906 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070014907 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014908 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
14909 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
14910 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
14911 }
14912 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
14913 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
14914 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
14915 }
14916 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
14917 mNetworkByteActivityCounters[i].writeToParcel(out);
14918 mNetworkPacketActivityCounters[i].writeToParcel(out);
14919 }
14920 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
14921 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070014922 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014923 mMobileRadioActiveUnknownTime.writeToParcel(out);
14924 mMobileRadioActiveUnknownCount.writeToParcel(out);
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080014925 mWifiMulticastWakelockTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080014926 mWifiOnTimer.writeToParcel(out, uSecRealtime);
14927 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
14928 for (int i=0; i<NUM_WIFI_STATES; i++) {
14929 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
14930 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070014931 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
14932 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
14933 }
14934 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
14935 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
14936 }
Siddharth Rayb50a6842017-12-14 15:15:28 -080014937 mWifiActiveTimer.writeToParcel(out, uSecRealtime);
14938 mWifiActivity.writeToParcel(out, 0);
Siddharth Ray78ccaf52017-12-23 16:16:21 -080014939 for (int i=0; i< GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
14940 mGpsSignalQualityTimer[i].writeToParcel(out, uSecRealtime);
14941 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080014942 mBluetoothActivity.writeToParcel(out, 0);
14943 mModemActivity.writeToParcel(out, 0);
14944 out.writeInt(mHasWifiReporting ? 1 : 0);
14945 out.writeInt(mHasBluetoothReporting ? 1 : 0);
14946 out.writeInt(mHasModemReporting ? 1 : 0);
14947
Dianne Hackborn1e01d162014-12-04 17:46:42 -080014948 out.writeInt(mNumConnectivityChange);
14949 out.writeInt(mLoadedNumConnectivityChange);
14950 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070014951 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070014952 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080014953 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070014954 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014955 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070014956 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070014957 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070014958 out.writeInt(mLowDischargeAmountSinceCharge);
14959 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080014960 out.writeInt(mDischargeAmountScreenOn);
14961 out.writeInt(mDischargeAmountScreenOnSinceCharge);
14962 out.writeInt(mDischargeAmountScreenOff);
14963 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Mike Mac2f518a2017-09-19 16:06:03 -070014964 out.writeInt(mDischargeAmountScreenDoze);
14965 out.writeInt(mDischargeAmountScreenDozeSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080014966 mDischargeStepTracker.writeToParcel(out);
14967 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070014968 mDischargeCounter.writeToParcel(out);
14969 mDischargeScreenOffCounter.writeToParcel(out);
Mike Mac2f518a2017-09-19 16:06:03 -070014970 mDischargeScreenDozeCounter.writeToParcel(out);
Mike Ma15313c92017-11-15 17:58:21 -080014971 mDischargeLightDozeCounter.writeToParcel(out);
14972 mDischargeDeepDozeCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014973 out.writeLong(mLastWriteTime);
14974
Bookatz50df7112017-08-04 14:53:26 -070014975 out.writeInt(mRpmStats.size());
14976 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
14977 SamplingTimer rpmt = ent.getValue();
14978 if (rpmt != null) {
14979 out.writeInt(1);
14980 out.writeString(ent.getKey());
14981 rpmt.writeToParcel(out, uSecRealtime);
14982 } else {
14983 out.writeInt(0);
14984 }
14985 }
14986 out.writeInt(mScreenOffRpmStats.size());
14987 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
14988 SamplingTimer rpmt = ent.getValue();
14989 if (rpmt != null) {
14990 out.writeInt(1);
14991 out.writeString(ent.getKey());
14992 rpmt.writeToParcel(out, uSecRealtime);
14993 } else {
14994 out.writeInt(0);
14995 }
14996 }
14997
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070014998 if (inclUids) {
14999 out.writeInt(mKernelWakelockStats.size());
15000 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
15001 SamplingTimer kwlt = ent.getValue();
15002 if (kwlt != null) {
15003 out.writeInt(1);
15004 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070015005 kwlt.writeToParcel(out, uSecRealtime);
15006 } else {
15007 out.writeInt(0);
15008 }
15009 }
15010 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070015011 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
15012 SamplingTimer timer = ent.getValue();
15013 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070015014 out.writeInt(1);
15015 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070015016 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070015017 } else {
15018 out.writeInt(0);
15019 }
Evan Millarc64edde2009-04-18 12:26:32 -070015020 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070015021 } else {
Bookatzba78b2b2017-10-10 11:06:33 -070015022 out.writeInt(0);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070015023 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070015024 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070015025
James Carr3a226052016-07-01 14:49:52 -070015026 out.writeInt(mKernelMemoryStats.size());
15027 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
15028 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
15029 if (kmt != null) {
15030 out.writeInt(1);
15031 out.writeLong(mKernelMemoryStats.keyAt(i));
15032 kmt.writeToParcel(out, uSecRealtime);
15033 } else {
15034 out.writeInt(0);
15035 }
15036 }
15037
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070015038 if (inclUids) {
15039 int size = mUidStats.size();
15040 out.writeInt(size);
15041 for (int i = 0; i < size; i++) {
15042 out.writeInt(mUidStats.keyAt(i));
15043 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015044
Bookatz867c0d72017-03-07 18:23:42 -080015045 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070015046 }
15047 } else {
15048 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015049 }
15050 }
15051
15052 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
15053 new Parcelable.Creator<BatteryStatsImpl>() {
15054 public BatteryStatsImpl createFromParcel(Parcel in) {
15055 return new BatteryStatsImpl(in);
15056 }
15057
15058 public BatteryStatsImpl[] newArray(int size) {
15059 return new BatteryStatsImpl[size];
15060 }
15061 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070015062
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070015063 public void prepareForDumpLocked() {
15064 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080015065 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070015066
15067 // Pull the clock time. This may update the time and make a new history entry
15068 // if we had originally pulled a time before the RTC was set.
15069 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070015070 }
15071
Dianne Hackbornc51cf032014-03-02 19:08:15 -080015072 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080015074 pw.println("mOnBatteryTimeBase:");
15075 mOnBatteryTimeBase.dump(pw, " ");
15076 pw.println("mOnBatteryScreenOffTimeBase:");
15077 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015078 Printer pr = new PrintWriterPrinter(pw);
Mike Mac2f518a2017-09-19 16:06:03 -070015079 pr.println("*** Screen on timer:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015080 mScreenOnTimer.logState(pr, " ");
Mike Mac2f518a2017-09-19 16:06:03 -070015081 pr.println("*** Screen doze timer:");
15082 mScreenDozeTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070015083 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015084 pr.println("*** Screen brightness #" + i + ":");
15085 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070015086 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070015087 pr.println("*** Interactive timer:");
15088 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070015089 pr.println("*** Power save mode timer:");
15090 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070015091 pr.println("*** Device idle mode light timer:");
15092 mDeviceIdleModeLightTimer.logState(pr, " ");
15093 pr.println("*** Device idle mode full timer:");
15094 mDeviceIdleModeFullTimer.logState(pr, " ");
15095 pr.println("*** Device light idling timer:");
15096 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070015097 pr.println("*** Device idling timer:");
15098 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015099 pr.println("*** Phone timer:");
15100 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080015101 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070015102 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015103 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070015104 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070015105 pr.println("*** Signal scanning :");
15106 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070015107 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015108 pr.println("*** Data connection type #" + i + ":");
15109 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070015110 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070015111 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080015112 pr.println("*** Mobile network active timer:");
15113 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070015114 pr.println("*** Mobile network active adjusted timer:");
15115 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -080015116 pr.println("*** Wifi Multicast WakeLock Timer:");
15117 mWifiMulticastWakelockTimer.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070015118 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070015119 pr.println("*** Wifi timer:");
15120 mWifiOnTimer.logState(pr, " ");
15121 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070015122 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080015123 for (int i=0; i<NUM_WIFI_STATES; i++) {
15124 pr.println("*** Wifi state #" + i + ":");
15125 mWifiStateTimer[i].logState(pr, " ");
15126 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070015127 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
15128 pr.println("*** Wifi suppl state #" + i + ":");
15129 mWifiSupplStateTimer[i].logState(pr, " ");
15130 }
15131 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
15132 pr.println("*** Wifi signal strength #" + i + ":");
15133 mWifiSignalStrengthsTimer[i].logState(pr, " ");
15134 }
Siddharth Ray78ccaf52017-12-23 16:16:21 -080015135 for (int i=0; i<GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
15136 pr.println("*** GPS signal quality #" + i + ":");
15137 mGpsSignalQualityTimer[i].logState(pr, " ");
15138 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070015139 pr.println("*** Flashlight timer:");
15140 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070015141 pr.println("*** Camera timer:");
15142 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080015144 super.dumpLocked(context, pw, flags, reqUid, histStart);
Sudheer Shankac20379e2018-02-15 00:06:21 -080015145 pw.print("Total cpu time reads: ");
15146 pw.println(mNumCpuTimeReads);
15147 pw.print("Batched cpu time reads: ");
15148 pw.println(mNumBatchedCpuTimeReads);
15149 pw.print("Batching Duration (min): ");
15150 pw.println((mClocks.uptimeMillis() - mCpuTimeReadsTrackingStartTime) / (60 * 1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015152}