blob: 6ede72d49806b7d1f522ec76d47ea39e5465c463 [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;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080024import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070025import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070026import android.net.ConnectivityManager;
27import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070028import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070029import android.net.wifi.WifiManager;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070030import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070032import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070033import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070034import android.os.Handler;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080035import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6f357d32014-01-15 20:40:55 -080036import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070037import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Parcel;
39import android.os.ParcelFormatException;
40import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070041import android.os.Process;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.SystemClock;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070045import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070046import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070047import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080048import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070049import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070050import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070051import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070052import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070053import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070055import android.util.LogWriter;
James Carr3a226052016-07-01 14:49:52 -070056import android.util.LongSparseArray;
57import android.util.LongSparseLongArray;
Dianne Hackbornd953c532014-08-16 18:17:38 -070058import android.util.MutableInt;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070059import android.util.Pools;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070060import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070062import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080064import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070065import android.util.SparseLongArray;
Bookatzc1a050a2017-10-10 15:49:28 -070066import android.util.StatsLog;
Dianne Hackbornae384452011-06-28 12:33:48 -070067import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080068import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070069import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Adam Lesinski14ae39a2017-05-26 11:50:40 -070071import com.android.internal.annotations.GuardedBy;
Adam Lesinski98f0d462016-04-19 16:46:20 -070072import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070073import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080074import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070075import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080076import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070077import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080078import com.android.internal.util.XmlUtils;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070079
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070080import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080081import org.xmlpull.v1.XmlPullParser;
82import org.xmlpull.v1.XmlPullParserException;
83import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070084
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080085import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.io.File;
87import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080088import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.io.FileOutputStream;
90import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070091import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010092import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.util.ArrayList;
Sudheer Shanka9b735c52017-05-09 18:26:18 -070094import java.util.Arrays;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080095import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070097import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.util.Map;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070099import java.util.concurrent.Future;
Christopher Tate4cee7252010-03-19 14:50:40 -0700100import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700101import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103/**
104 * All information we are collecting about things that can happen that impact
105 * battery life. All times are represented in microseconds except where indicated
106 * otherwise.
107 */
Joe Onoratoabded112016-02-08 16:49:39 -0800108public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800110 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700111 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800112 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700113 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700114 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700115 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700116
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700117 // TODO: remove "tcp" from network methods, since we measure total stats.
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700120 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 // Current on-disk Parcel version
Mike Ma15313c92017-11-15 17:58:21 -0800123 private static final int VERSION = 169 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700124
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700125 // Maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100126 private static final int MAX_HISTORY_ITEMS;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700127
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700128 // No, really, THIS is the maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100129 private static final int MAX_MAX_HISTORY_ITEMS;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700130
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800131 // The maximum number of names wakelocks we will keep track of
132 // per uid; once the limit is reached, we batch the remaining wakelocks
133 // in to one common name.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100134 private static final int MAX_WAKELOCKS_PER_UID;
135
136 static final int MAX_HISTORY_BUFFER; // 256KB
137 static final int MAX_MAX_HISTORY_BUFFER; // 320KB
138
139 static {
140 if (ActivityManager.isLowRamDeviceStatic()) {
141 MAX_HISTORY_ITEMS = 800;
142 MAX_MAX_HISTORY_ITEMS = 1200;
143 MAX_WAKELOCKS_PER_UID = 40;
144 MAX_HISTORY_BUFFER = 96*1024; // 96KB
145 MAX_MAX_HISTORY_BUFFER = 128*1024; // 128KB
146 } else {
147 MAX_HISTORY_ITEMS = 2000;
148 MAX_MAX_HISTORY_ITEMS = 3000;
149 MAX_WAKELOCKS_PER_UID = 100;
150 MAX_HISTORY_BUFFER = 256*1024; // 256KB
151 MAX_MAX_HISTORY_BUFFER = 320*1024; // 256KB
152 }
153 }
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700154
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800155 // Number of transmit power states the Wifi controller can be in.
156 private static final int NUM_WIFI_TX_LEVELS = 1;
157
158 // Number of transmit power states the Bluetooth controller can be in.
159 private static final int NUM_BT_TX_LEVELS = 1;
160
Sudheer Shanka38383232017-07-25 09:55:03 -0700161 /**
162 * Holding a wakelock costs more than just using the cpu.
163 * Currently, we assign only half the cpu time to an app that is running but
164 * not holding a wakelock. The apps holding wakelocks get the rest of the blame.
165 * If no app is holding a wakelock, then the distribution is normal.
166 */
167 @VisibleForTesting
168 public static final int WAKE_LOCK_WEIGHT = 50;
169
Joe Onoratoabded112016-02-08 16:49:39 -0800170 protected Clocks mClocks;
171
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700172 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700173 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800174 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700176 static final int MSG_UPDATE_WAKELOCKS = 1;
177 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700178 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700179 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700180
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700181 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
182 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
183
Sudheer Shanka38383232017-07-25 09:55:03 -0700184 @VisibleForTesting
185 protected KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
186 @VisibleForTesting
187 protected KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
188 @VisibleForTesting
189 protected KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700190 new KernelUidCpuFreqTimeReader();
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700191
James Carr3a226052016-07-01 14:49:52 -0700192 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
193 = new KernelMemoryBandwidthStats();
194 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
195 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
196 return mKernelMemoryStats;
197 }
198
Bookatz0b8a0502017-09-13 11:51:52 -0700199 /** Container for Resource Power Manager stats. Updated by updateRpmStatsLocked. */
Bookatz50df7112017-08-04 14:53:26 -0700200 private final RpmStats mTmpRpmStats = new RpmStats();
Bookatz0b8a0502017-09-13 11:51:52 -0700201 /** The soonest the RPM stats can be updated after it was last updated. */
202 private static final long RPM_STATS_UPDATE_FREQ_MS = 1000;
203 /** Last time that RPM stats were updated by updateRpmStatsLocked. */
204 private long mLastRpmStatsUpdateTimeMs = -RPM_STATS_UPDATE_FREQ_MS;
Bookatz50df7112017-08-04 14:53:26 -0700205
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700206 public interface BatteryCallback {
207 public void batteryNeedsCpuUpdate();
208 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700209 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700210 }
211
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700212 public interface PlatformIdleStateCallback {
Bookatz50df7112017-08-04 14:53:26 -0700213 public void fillLowPowerStats(RpmStats rpmStats);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700214 public String getPlatformLowPowerStats();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000215 public String getSubsystemLowPowerStats();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700216 }
217
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700218 public static abstract class UserInfoProvider {
219 private int[] userIds;
220 protected abstract @Nullable int[] getUserIds();
Sudheer Shanka38383232017-07-25 09:55:03 -0700221 @VisibleForTesting
222 public final void refreshUserIds() {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700223 userIds = getUserIds();
224 }
Sudheer Shanka38383232017-07-25 09:55:03 -0700225 @VisibleForTesting
226 public boolean exists(int userId) {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700227 return userIds != null ? ArrayUtils.contains(userIds, userId) : true;
228 }
229 }
230
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700231 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
232
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700233 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800234 public MyHandler(Looper looper) {
235 super(looper, null, true);
236 }
237
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700238 @Override
239 public void handleMessage(Message msg) {
240 BatteryCallback cb = mCallback;
241 switch (msg.what) {
242 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700243 synchronized (BatteryStatsImpl.this) {
Sudheer Shankab8ad5942017-08-08 12:16:09 -0700244 updateCpuTimeLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -0700245 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700246 if (cb != null) {
247 cb.batteryNeedsCpuUpdate();
248 }
249 break;
250 case MSG_REPORT_POWER_CHANGE:
251 if (cb != null) {
252 cb.batteryPowerChanged(msg.arg1 != 0);
253 }
254 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700255 case MSG_REPORT_CHARGING:
256 if (cb != null) {
257 final String action;
258 synchronized (BatteryStatsImpl.this) {
259 action = mCharging ? BatteryManager.ACTION_CHARGING
260 : BatteryManager.ACTION_DISCHARGING;
261 }
262 Intent intent = new Intent(action);
263 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
264 cb.batterySendBroadcast(intent);
265 }
266 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700267 }
268 }
269 }
270
Joe Onoratoabded112016-02-08 16:49:39 -0800271 public interface Clocks {
272 public long elapsedRealtime();
273 public long uptimeMillis();
274 }
275
276 public static class SystemClocks implements Clocks {
277 public long elapsedRealtime() {
278 return SystemClock.elapsedRealtime();
279 }
280
281 public long uptimeMillis() {
282 return SystemClock.uptimeMillis();
283 }
284 }
285
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700286 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700287 int UPDATE_CPU = 0x01;
288 int UPDATE_WIFI = 0x02;
289 int UPDATE_RADIO = 0x04;
290 int UPDATE_BT = 0x08;
Bookatz50df7112017-08-04 14:53:26 -0700291 int UPDATE_RPM = 0x10; // 16
292 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT | UPDATE_RPM;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800293
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700294 Future<?> scheduleSync(String reason, int flags);
295 Future<?> scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700296 }
297
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700298 public final MyHandler mHandler;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700299 private ExternalStatsSync mExternalSync = null;
Sudheer Shanka38383232017-07-25 09:55:03 -0700300 @VisibleForTesting
301 protected UserInfoProvider mUserInfoProvider = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700302
303 private BatteryCallback mCallback;
304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800306 * Mapping isolated uids to the actual owning app uid.
307 */
308 final SparseIntArray mIsolatedUids = new SparseIntArray();
309
310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 * The statistics we have collected organized by uids.
312 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800313 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314
315 // A set of pools of currently active timers. When a timer is queried, we will divide the
316 // elapsed time by the number of active timers to arrive at that timer's share of the time.
317 // In order to do this, we must refresh each timer whenever the number of active timers
318 // changes.
Sudheer Shanka38383232017-07-25 09:55:03 -0700319 @VisibleForTesting
320 protected ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700321 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
322 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700323 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700324 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
325 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
326 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
327 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
328 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
329 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
330 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
331 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700332 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
333 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800334 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700336 // Last partial timers we use for distributing CPU usage.
Sudheer Shanka38383232017-07-25 09:55:03 -0700337 @VisibleForTesting
338 protected ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 // These are the objects that will want to do something when the device
341 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800342 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800343
344 // These are the objects that will want to do something when the device
Mike Mac2f518a2017-09-19 16:06:03 -0700345 // is unplugged from power *and* the screen is off or doze.
346 protected final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800347
348 // Set to true when we want to distribute CPU across wakelocks for the next
349 // CPU update, even if we aren't currently running wake locks.
350 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700351
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700352 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700353
Dianne Hackborn37de0982014-05-09 09:32:18 -0700354 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800355
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700356 long mHistoryBaseTime;
357 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700358 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700359 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700360
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700361 final Parcel mHistoryBuffer = Parcel.obtain();
362 final HistoryItem mHistoryLastWritten = new HistoryItem();
363 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700364 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700365 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700366 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800367 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800368 int[] mReadHistoryUids;
369 int mReadHistoryChars;
370 int mNextHistoryTagIdx = 0;
371 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700372 int mHistoryBufferLastPos = -1;
373 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700374 int mActiveHistoryStates = 0xffffffff;
375 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700376 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700377 long mTrackRunningHistoryElapsedRealtime = 0;
378 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700379
380 final HistoryItem mHistoryCur = new HistoryItem();
381
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700382 HistoryItem mHistory;
383 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700384 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700385 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700386
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800387 // Used by computeHistoryStepDetails
388 HistoryStepDetails mLastHistoryStepDetails = null;
389 byte mLastHistoryStepLevel = 0;
390 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
391 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
392 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700393
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800394 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700395 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800396 */
397 long mLastStepCpuUserTime;
398 long mCurStepCpuUserTime;
399 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700400 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800401 */
402 long mLastStepCpuSystemTime;
403 long mCurStepCpuSystemTime;
404 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700405 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800406 */
407 long mLastStepStatUserTime;
408 long mLastStepStatSystemTime;
409 long mLastStepStatIOWaitTime;
410 long mLastStepStatIrqTime;
411 long mLastStepStatSoftIrqTime;
412 long mLastStepStatIdleTime;
413 long mCurStepStatUserTime;
414 long mCurStepStatSystemTime;
415 long mCurStepStatIOWaitTime;
416 long mCurStepStatIrqTime;
417 long mCurStepStatSoftIrqTime;
418 long mCurStepStatIdleTime;
419
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700420 private HistoryItem mHistoryIterator;
421 private boolean mReadOverflow;
422 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 int mStartCount;
425
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800426 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700427 String mStartPlatformVersion;
428 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 long mUptime;
431 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 long mRealtime;
433 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700434
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800435 int mWakeLockNesting;
436 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700437 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700438 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800439
Mike Mac2f518a2017-09-19 16:06:03 -0700440 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
441 protected int mScreenState = Display.STATE_UNKNOWN;
442 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
443 protected StopwatchTimer mScreenOnTimer;
444 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
445 protected StopwatchTimer mScreenDozeTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700446
Dianne Hackborn617f8772009-03-31 15:04:46 -0700447 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700448 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700449
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800450 boolean mPretendScreenOff;
451
Jeff Browne95c3cd2014-05-02 16:59:26 -0700452 boolean mInteractive;
453 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700454
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700455 boolean mPowerSaveModeEnabled;
456 StopwatchTimer mPowerSaveModeEnabledTimer;
457
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700458 boolean mDeviceIdling;
459 StopwatchTimer mDeviceIdlingTimer;
460
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700461 boolean mDeviceLightIdling;
462 StopwatchTimer mDeviceLightIdlingTimer;
463
464 int mDeviceIdleMode;
465 long mLastIdleTimeStart;
466 long mLongestLightIdleTime;
467 long mLongestFullIdleTime;
468 StopwatchTimer mDeviceIdleModeLightTimer;
469 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700472 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700473
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700474 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700475 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700476
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700477 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700478 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700479
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700480 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700481 StopwatchTimer mFlashlightOnTimer;
482
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700483 int mCameraOnNesting;
484 StopwatchTimer mCameraOnTimer;
485
Dianne Hackborn627bba72009-03-24 22:32:56 -0700486 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800487 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700488 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800489 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700490
491 StopwatchTimer mPhoneSignalScanningTimer;
492
Dianne Hackborn627bba72009-03-24 22:32:56 -0700493 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700494 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700495 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700496
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800497 final LongSamplingCounter[] mNetworkByteActivityCounters =
498 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
499 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700500 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
501
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800502 /**
503 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
504 */
505 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700506
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800507 /**
508 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
509 */
510 ControllerActivityCounterImpl mBluetoothActivity;
511
512 /**
513 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
514 */
515 ControllerActivityCounterImpl mModemActivity;
516
517 /**
518 * Whether the device supports WiFi controller energy reporting. This is set to true on
519 * the first WiFi energy report. See {@link #mWifiActivity}.
520 */
521 boolean mHasWifiReporting = false;
522
523 /**
524 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
525 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
526 */
527 boolean mHasBluetoothReporting = false;
528
529 /**
530 * Whether the device supports Modem controller energy reporting. This is set to true on
531 * the first Modem energy report. See {@link #mModemActivity}.
532 */
533 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700534
The Android Open Source Project10592532009-03-18 17:39:46 -0700535 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700536 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700537
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700538 boolean mGlobalWifiRunning;
539 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700540
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800541 int mWifiState = -1;
542 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
543
Dianne Hackborn3251b902014-06-20 14:40:53 -0700544 int mWifiSupplState = -1;
545 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
546
547 int mWifiSignalStrengthBin = -1;
548 final StopwatchTimer[] mWifiSignalStrengthsTimer =
549 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
550
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800551 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800552 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
553 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800554
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700555 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700556 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800557 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800558 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700559 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800560 LongSamplingCounter mMobileRadioActiveUnknownTime;
561 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800562
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700563 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 /**
566 * These provide time bases that discount the time the device is plugged
567 * in to power.
568 */
569 boolean mOnBattery;
Sudheer Shanka38383232017-07-25 09:55:03 -0700570 @VisibleForTesting
571 protected boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700572
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700573 /**
574 * External reporting of whether the device is actually charging.
575 */
576 boolean mCharging = true;
577 int mLastChargingStateLevel;
578
The Android Open Source Project10592532009-03-18 17:39:46 -0700579 /*
580 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
581 */
Evan Millar633a1742009-04-02 16:36:33 -0700582 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700583 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700584 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700585 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700586 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700587 int mLowDischargeAmountSinceCharge;
588 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800589 int mDischargeScreenOnUnplugLevel;
590 int mDischargeScreenOffUnplugLevel;
Mike Mac2f518a2017-09-19 16:06:03 -0700591 int mDischargeScreenDozeUnplugLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800592 int mDischargeAmountScreenOn;
593 int mDischargeAmountScreenOnSinceCharge;
594 int mDischargeAmountScreenOff;
595 int mDischargeAmountScreenOffSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -0700596 int mDischargeAmountScreenDoze;
597 int mDischargeAmountScreenDozeSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700598
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700599 private LongSamplingCounter mDischargeScreenOffCounter;
Mike Mac2f518a2017-09-19 16:06:03 -0700600 private LongSamplingCounter mDischargeScreenDozeCounter;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700601 private LongSamplingCounter mDischargeCounter;
Mike Ma15313c92017-11-15 17:58:21 -0800602 private LongSamplingCounter mDischargeLightDozeCounter;
603 private LongSamplingCounter mDischargeDeepDozeCounter;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700604
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700605 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700606
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700607 int mInitStepMode = 0;
608 int mCurStepMode = 0;
609 int mModStepMode = 0;
610
Dianne Hackborn260c5022014-04-29 11:23:16 -0700611 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700612 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800613 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
614 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700615 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700616
617 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700618 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800619 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
620 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
621
622 static final int MAX_DAILY_ITEMS = 10;
623
624 long mDailyStartTime = 0;
625 long mNextMinDailyDeadline = 0;
626 long mNextMaxDailyDeadline = 0;
627
628 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700631
Amith Yamasanif37447b2009-10-08 18:28:01 -0700632 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800633 private int mPhoneServiceStateRaw = -1;
634 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700635
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800636 private int mNumConnectivityChange;
637 private int mLoadedNumConnectivityChange;
638 private int mUnpluggedNumConnectivityChange;
639
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700640 private int mEstimatedBatteryCapacity = -1;
641
Jocelyn Dangc627d102017-04-14 13:15:14 -0700642 private int mMinLearnedBatteryCapacity = -1;
643 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800644
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700645 private long[] mCpuFreqs;
646
Sudheer Shanka38383232017-07-25 09:55:03 -0700647 @VisibleForTesting
648 protected PowerProfile mPowerProfile;
Adam Lesinskie08af192015-03-25 16:42:59 -0700649
Evan Millarc64edde2009-04-18 12:26:32 -0700650 /*
Bookatz50df7112017-08-04 14:53:26 -0700651 * Holds a SamplingTimer associated with each Resource Power Manager state and voter,
652 * recording their times when on-battery (regardless of screen state).
653 */
654 private final HashMap<String, SamplingTimer> mRpmStats = new HashMap<>();
655 /** Times for each Resource Power Manager state and voter when screen-off and on-battery. */
656 private final HashMap<String, SamplingTimer> mScreenOffRpmStats = new HashMap<>();
657
658 @Override
659 public Map<String, ? extends Timer> getRpmStats() {
660 return mRpmStats;
661 }
662
Bookatz82b341172017-09-07 19:06:08 -0700663 // TODO: Note: screenOffRpmStats has been disabled via SCREEN_OFF_RPM_STATS_ENABLED.
Bookatz50df7112017-08-04 14:53:26 -0700664 @Override
665 public Map<String, ? extends Timer> getScreenOffRpmStats() {
666 return mScreenOffRpmStats;
667 }
668
669 /*
Evan Millarc64edde2009-04-18 12:26:32 -0700670 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
671 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700672 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700673
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700674 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700675 return mKernelWakelockStats;
676 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700677
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700678 String mLastWakeupReason = null;
679 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700680 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700681
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700682 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700683 return mWakeupReasonStats;
684 }
685
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700686 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700687 public long getUahDischarge(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700688 return mDischargeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700689 }
690
691 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700692 public long getUahDischargeScreenOff(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700693 return mDischargeScreenOffCounter.getCountLocked(which);
694 }
695
696 @Override
Kweku Adams87b19ec2017-10-09 12:40:03 -0700697 public long getUahDischargeScreenDoze(int which) {
Mike Mac2f518a2017-09-19 16:06:03 -0700698 return mDischargeScreenDozeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700699 }
700
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700701 @Override
Mike Ma15313c92017-11-15 17:58:21 -0800702 public long getUahDischargeLightDoze(int which) {
703 return mDischargeLightDozeCounter.getCountLocked(which);
704 }
705
706 @Override
707 public long getUahDischargeDeepDoze(int which) {
708 return mDischargeDeepDozeCounter.getCountLocked(which);
709 }
710
711 @Override
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700712 public int getEstimatedBatteryCapacity() {
713 return mEstimatedBatteryCapacity;
714 }
715
Jocelyn Dangc627d102017-04-14 13:15:14 -0700716 @Override
717 public int getMinLearnedBatteryCapacity() {
718 return mMinLearnedBatteryCapacity;
719 }
720
721 @Override
722 public int getMaxLearnedBatteryCapacity() {
723 return mMaxLearnedBatteryCapacity;
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800727 this(new SystemClocks());
728 }
729
730 public BatteryStatsImpl(Clocks clocks) {
731 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700732 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700733 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800734 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700735 mHandler = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700736 mPlatformIdleStateCallback = null;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700737 mUserInfoProvider = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700738 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
Joe Onoratoabded112016-02-08 16:49:39 -0800741 private void init(Clocks clocks) {
742 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800743 }
744
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700745 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800746 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
747 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
748 }
749
Joe Onoratoabded112016-02-08 16:49:39 -0800750 // methods are protected not private to be VisibleForTesting
751 public static class TimeBase {
752 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800753
Joe Onoratoabded112016-02-08 16:49:39 -0800754 protected long mUptime;
755 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800756
Joe Onoratoabded112016-02-08 16:49:39 -0800757 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800758
Joe Onoratoabded112016-02-08 16:49:39 -0800759 protected long mPastUptime;
760 protected long mUptimeStart;
761 protected long mPastRealtime;
762 protected long mRealtimeStart;
763 protected long mUnpluggedUptime;
764 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800765
766 public void dump(PrintWriter pw, String prefix) {
767 StringBuilder sb = new StringBuilder(128);
768 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
769 sb.setLength(0);
770 sb.append(prefix);
771 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700772 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800773 pw.println(sb.toString());
774 sb.setLength(0);
775 sb.append(prefix);
776 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700777 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800778 pw.println(sb.toString());
779 sb.setLength(0);
780 sb.append(prefix);
781 sb.append("mPastUptime=");
782 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
783 formatTimeMs(sb, mUptimeStart / 1000);
784 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
785 pw.println(sb.toString());
786 sb.setLength(0);
787 sb.append(prefix);
788 sb.append("mPastRealtime=");
789 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
790 formatTimeMs(sb, mRealtimeStart / 1000);
791 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
792 pw.println(sb.toString());
793 }
794
795 public void add(TimeBaseObs observer) {
796 mObservers.add(observer);
797 }
798
799 public void remove(TimeBaseObs observer) {
800 if (!mObservers.remove(observer)) {
801 Slog.wtf(TAG, "Removed unknown observer: " + observer);
802 }
803 }
804
Joe Onoratoabded112016-02-08 16:49:39 -0800805 public boolean hasObserver(TimeBaseObs observer) {
806 return mObservers.contains(observer);
807 }
808
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800809 public void init(long uptime, long realtime) {
810 mRealtime = 0;
811 mUptime = 0;
812 mPastUptime = 0;
813 mPastRealtime = 0;
814 mUptimeStart = uptime;
815 mRealtimeStart = realtime;
816 mUnpluggedUptime = getUptime(mUptimeStart);
817 mUnpluggedRealtime = getRealtime(mRealtimeStart);
818 }
819
820 public void reset(long uptime, long realtime) {
821 if (!mRunning) {
822 mPastUptime = 0;
823 mPastRealtime = 0;
824 } else {
825 mUptimeStart = uptime;
826 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800827 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
828 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800829 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800830 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800831 mUnpluggedRealtime = getRealtime(realtime);
832 }
833 }
834
835 public long computeUptime(long curTime, int which) {
836 switch (which) {
837 case STATS_SINCE_CHARGED:
838 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800839 case STATS_CURRENT:
840 return getUptime(curTime);
841 case STATS_SINCE_UNPLUGGED:
842 return getUptime(curTime) - mUnpluggedUptime;
843 }
844 return 0;
845 }
846
847 public long computeRealtime(long curTime, int which) {
848 switch (which) {
849 case STATS_SINCE_CHARGED:
850 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800851 case STATS_CURRENT:
852 return getRealtime(curTime);
853 case STATS_SINCE_UNPLUGGED:
854 return getRealtime(curTime) - mUnpluggedRealtime;
855 }
856 return 0;
857 }
858
859 public long getUptime(long curTime) {
860 long time = mPastUptime;
861 if (mRunning) {
862 time += curTime - mUptimeStart;
863 }
864 return time;
865 }
866
867 public long getRealtime(long curTime) {
868 long time = mPastRealtime;
869 if (mRunning) {
870 time += curTime - mRealtimeStart;
871 }
872 return time;
873 }
874
875 public long getUptimeStart() {
876 return mUptimeStart;
877 }
878
879 public long getRealtimeStart() {
880 return mRealtimeStart;
881 }
882
883 public boolean isRunning() {
884 return mRunning;
885 }
886
887 public boolean setRunning(boolean running, long uptime, long realtime) {
888 if (mRunning != running) {
889 mRunning = running;
890 if (running) {
891 mUptimeStart = uptime;
892 mRealtimeStart = realtime;
893 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
894 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
895
896 for (int i = mObservers.size() - 1; i >= 0; i--) {
897 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
898 }
899 } else {
900 mPastUptime += uptime - mUptimeStart;
901 mPastRealtime += realtime - mRealtimeStart;
902
903 long batteryUptime = getUptime(uptime);
904 long batteryRealtime = getRealtime(realtime);
905
906 for (int i = mObservers.size() - 1; i >= 0; i--) {
907 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
908 }
909 }
910 return true;
911 }
912 return false;
913 }
914
915 public void readSummaryFromParcel(Parcel in) {
916 mUptime = in.readLong();
917 mRealtime = in.readLong();
918 }
919
920 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
921 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
922 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
923 }
924
925 public void readFromParcel(Parcel in) {
926 mRunning = false;
927 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800928 mPastUptime = in.readLong();
929 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700930 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800931 mPastRealtime = in.readLong();
932 mRealtimeStart = in.readLong();
933 mUnpluggedUptime = in.readLong();
934 mUnpluggedRealtime = in.readLong();
935 }
936
937 public void writeToParcel(Parcel out, long uptime, long realtime) {
938 final long runningUptime = getUptime(uptime);
939 final long runningRealtime = getRealtime(realtime);
940 out.writeLong(mUptime);
941 out.writeLong(runningUptime);
942 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700943 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 out.writeLong(runningRealtime);
945 out.writeLong(mRealtimeStart);
946 out.writeLong(mUnpluggedUptime);
947 out.writeLong(mUnpluggedRealtime);
948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700952 * State for keeping track of counting information.
953 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800954 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700955 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800956 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700957 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700958 int mUnpluggedCount;
959 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700960
Bookatz8c6f3c52017-05-24 12:00:17 -0700961 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800962 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700963 mPluggedCount = in.readInt();
964 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700965 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700966 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800967 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700968 }
969
Bookatz8c6f3c52017-05-24 12:00:17 -0700970 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800971 mTimeBase = timeBase;
972 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700973 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700974
Dianne Hackborn617f8772009-03-31 15:04:46 -0700975 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700976 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700977 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700978 out.writeInt(mUnpluggedCount);
979 }
980
Bookatz8c6f3c52017-05-24 12:00:17 -0700981 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800982 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700983 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700984 }
985
Bookatz8c6f3c52017-05-24 12:00:17 -0700986 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800987 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700988 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700989 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700990
Dianne Hackborn617f8772009-03-31 15:04:46 -0700991 /**
992 * Writes a possibly null Counter to a Parcel.
993 *
994 * @param out the Parcel to be written to.
995 * @param counter a Counter, or null.
996 */
997 public static void writeCounterToParcel(Parcel out, Counter counter) {
998 if (counter == null) {
999 out.writeInt(0); // indicates null
1000 return;
1001 }
1002 out.writeInt(1); // indicates non-null
1003
1004 counter.writeToParcel(out);
1005 }
1006
1007 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001008 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001009 int val = mCount.get();
1010 if (which == STATS_SINCE_UNPLUGGED) {
1011 val -= mUnpluggedCount;
1012 } else if (which != STATS_SINCE_CHARGED) {
1013 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001014 }
1015
1016 return val;
1017 }
1018
1019 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001020 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -07001021 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -07001022 + " mUnpluggedCount=" + mUnpluggedCount
1023 + " mPluggedCount=" + mPluggedCount);
1024 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001025
Bookatz8c6f3c52017-05-24 12:00:17 -07001026 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1027 public void stepAtomic() {
1028 if (mTimeBase.isRunning()) {
1029 mCount.incrementAndGet();
1030 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001031 }
1032
Bookatz4ebc0642017-05-11 12:21:19 -07001033 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001034 if (mTimeBase.isRunning()) {
1035 mCount.addAndGet(delta);
1036 }
Bookatz4ebc0642017-05-11 12:21:19 -07001037 }
1038
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001039 /**
1040 * Clear state of this counter.
1041 */
1042 void reset(boolean detachIfReset) {
1043 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -07001044 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001045 if (detachIfReset) {
1046 detach();
1047 }
1048 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001049
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001050 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001051 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001052 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001053
Bookatz8c6f3c52017-05-24 12:00:17 -07001054 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1055 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001056 int count = mCount.get();
1057 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001058 }
1059
Bookatz8c6f3c52017-05-24 12:00:17 -07001060 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1061 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001062 mLoadedCount = in.readInt();
1063 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -07001064 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001065 }
1066 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07001067
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001068 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001069 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
1070 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001071 public long[] mCounts;
1072 public long[] mLoadedCounts;
1073 public long[] mUnpluggedCounts;
1074 public long[] mPluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001075
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001076 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001077 mTimeBase = timeBase;
1078 mPluggedCounts = in.createLongArray();
1079 mCounts = copyArray(mPluggedCounts, mCounts);
1080 mLoadedCounts = in.createLongArray();
1081 mUnpluggedCounts = in.createLongArray();
1082 timeBase.add(this);
1083 }
1084
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001085 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001086 mTimeBase = timeBase;
1087 timeBase.add(this);
1088 }
1089
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001090 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001091 out.writeLongArray(mCounts);
1092 out.writeLongArray(mLoadedCounts);
1093 out.writeLongArray(mUnpluggedCounts);
1094 }
1095
1096 @Override
1097 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
1098 mUnpluggedCounts = copyArray(mPluggedCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001099 }
1100
1101 @Override
1102 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1103 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1104 }
1105
1106 @Override
1107 public long[] getCountsLocked(int which) {
1108 long[] val = copyArray(mTimeBase.isRunning() ? mCounts : mPluggedCounts, null);
1109 if (which == STATS_SINCE_UNPLUGGED) {
1110 subtract(val, mUnpluggedCounts);
1111 } else if (which != STATS_SINCE_CHARGED) {
1112 subtract(val, mLoadedCounts);
1113 }
1114 return val;
1115 }
1116
1117 @Override
1118 public void logState(Printer pw, String prefix) {
1119 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1120 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
1121 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts)
1122 + " mPluggedCounts=" + Arrays.toString(mPluggedCounts));
1123 }
1124
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001125 public void addCountLocked(long[] counts) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001126 if (counts == null) {
1127 return;
1128 }
Bookatz8c6f3c52017-05-24 12:00:17 -07001129 if (mTimeBase.isRunning()) {
1130 if (mCounts == null) {
1131 mCounts = new long[counts.length];
1132 }
1133 for (int i = 0; i < counts.length; ++i) {
1134 mCounts[i] += counts[i];
1135 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001136 }
1137 }
1138
Sudheer Shankab8ad5942017-08-08 12:16:09 -07001139 public int getSize() {
1140 return mCounts == null ? 0 : mCounts.length;
1141 }
1142
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001143 /**
1144 * Clear state of this counter.
1145 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001146 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001147 fillArray(mCounts, 0);
1148 fillArray(mLoadedCounts, 0);
1149 fillArray(mPluggedCounts, 0);
1150 fillArray(mUnpluggedCounts, 0);
1151 if (detachIfReset) {
1152 detach();
1153 }
1154 }
1155
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001156 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001157 mTimeBase.remove(this);
1158 }
1159
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001160 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001161 out.writeLongArray(mCounts);
1162 }
1163
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001164 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001165 mCounts = in.createLongArray();
1166 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1167 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
1168 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1169 }
1170
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001171 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1172 if (counterArray != null) {
1173 out.writeInt(1);
1174 counterArray.writeToParcel(out);
1175 } else {
1176 out.writeInt(0);
1177 }
1178 }
1179
1180 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1181 if (in.readInt() != 0) {
1182 return new LongSamplingCounterArray(timeBase, in);
1183 } else {
1184 return null;
1185 }
1186 }
1187
1188 public static void writeSummaryToParcelLocked(Parcel out,
1189 LongSamplingCounterArray counterArray) {
1190 if (counterArray != null) {
1191 out.writeInt(1);
1192 counterArray.writeSummaryToParcelLocked(out);
1193 } else {
1194 out.writeInt(0);
1195 }
1196 }
1197
1198 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1199 TimeBase timeBase) {
1200 if (in.readInt() != 0) {
1201 final LongSamplingCounterArray counterArray
1202 = new LongSamplingCounterArray(timeBase);
1203 counterArray.readSummaryFromParcelLocked(in);
1204 return counterArray;
1205 } else {
1206 return null;
1207 }
1208 }
1209
Bookatz8c6f3c52017-05-24 12:00:17 -07001210 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001211 if (a != null) {
1212 Arrays.fill(a, val);
1213 }
1214 }
1215
Bookatz8c6f3c52017-05-24 12:00:17 -07001216 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001217 if (toSubtract == null) {
1218 return;
1219 }
1220 for (int i = 0; i < val.length; i++) {
1221 val[i] -= toSubtract[i];
1222 }
1223 }
1224
Bookatz8c6f3c52017-05-24 12:00:17 -07001225 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001226 if (src == null) {
1227 return null;
1228 } else {
1229 if (dest == null) {
1230 dest = new long[src.length];
1231 }
1232 System.arraycopy(src, 0, dest, 0, src.length);
1233 return dest;
1234 }
1235 }
1236 }
1237
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001238 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001239 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001240 long mCount;
1241 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001242 long mUnpluggedCount;
1243 long mPluggedCount;
1244
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001245 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1246 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001247 mPluggedCount = in.readLong();
1248 mCount = mPluggedCount;
1249 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001250 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001251 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001252 }
1253
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001254 LongSamplingCounter(TimeBase timeBase) {
1255 mTimeBase = timeBase;
1256 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001257 }
1258
1259 public void writeToParcel(Parcel out) {
1260 out.writeLong(mCount);
1261 out.writeLong(mLoadedCount);
1262 out.writeLong(mUnpluggedCount);
1263 }
1264
1265 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001266 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001267 mUnpluggedCount = mPluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001268 }
1269
1270 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001271 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001272 mPluggedCount = mCount;
1273 }
1274
1275 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001276 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001277 if (which == STATS_SINCE_UNPLUGGED) {
1278 val -= mUnpluggedCount;
1279 } else if (which != STATS_SINCE_CHARGED) {
1280 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001281 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001282 return val;
1283 }
1284
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001285 @Override
1286 public void logState(Printer pw, String prefix) {
1287 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001288 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001289 + " mUnpluggedCount=" + mUnpluggedCount
1290 + " mPluggedCount=" + mPluggedCount);
1291 }
1292
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001293 void addCountLocked(long count) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001294 if (mTimeBase.isRunning()) {
1295 mCount += count;
1296 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001297 }
1298
1299 /**
1300 * Clear state of this counter.
1301 */
1302 void reset(boolean detachIfReset) {
1303 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001304 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001305 if (detachIfReset) {
1306 detach();
1307 }
1308 }
1309
1310 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001311 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001312 }
1313
1314 void writeSummaryFromParcelLocked(Parcel out) {
1315 out.writeLong(mCount);
1316 }
1317
1318 void readSummaryFromParcelLocked(Parcel in) {
1319 mLoadedCount = in.readLong();
1320 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001321 mUnpluggedCount = mPluggedCount = mLoadedCount;
1322 }
1323 }
1324
Dianne Hackborn617f8772009-03-31 15:04:46 -07001325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 * State for keeping track of timing information.
1327 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001328 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001329 protected final Clocks mClocks;
1330 protected final int mType;
1331 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001332
Joe Onoratoabded112016-02-08 16:49:39 -08001333 protected int mCount;
1334 protected int mLoadedCount;
1335 protected int mLastCount;
1336 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // Times are in microseconds for better accuracy when dividing by the
1339 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 /**
1342 * The total time we have accumulated since the start of the original
1343 * boot, to the last time something interesting happened in the
1344 * current run.
1345 */
Joe Onoratoabded112016-02-08 16:49:39 -08001346 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 /**
1349 * The total time we loaded for the previous runs. Subtract this from
1350 * mTotalTime to find the time for the current run of the system.
1351 */
Joe Onoratoabded112016-02-08 16:49:39 -08001352 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 /**
1355 * The run time of the last run of the system, as loaded from the
1356 * saved data.
1357 */
Joe Onoratoabded112016-02-08 16:49:39 -08001358 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * The value of mTotalTime when unplug() was last called. Subtract
1362 * this from mTotalTime to find the time since the last unplug from
1363 * power.
1364 */
Joe Onoratoabded112016-02-08 16:49:39 -08001365 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001366
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001367 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001368 * The total time this timer has been running until the latest mark has been set.
1369 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1370 */
Joe Onoratoabded112016-02-08 16:49:39 -08001371 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001372
1373 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001374 * Constructs from a parcel.
1375 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001376 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001377 * @param in
1378 */
Joe Onoratoabded112016-02-08 16:49:39 -08001379 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1380 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001382 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mCount = in.readInt();
1385 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001386 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 mUnpluggedCount = in.readInt();
1388 mTotalTime = in.readLong();
1389 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001390 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001392 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001393 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001394 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
Joe Onoratoabded112016-02-08 16:49:39 -08001397 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1398 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001400 mTimeBase = timeBase;
1401 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
Evan Millarc64edde2009-04-18 12:26:32 -07001403
1404 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001405
Evan Millarc64edde2009-04-18 12:26:32 -07001406 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001407
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001408 /**
1409 * Clear state of this timer. Returns true if the timer is inactive
1410 * so can be completely dropped.
1411 */
Joe Onoratoabded112016-02-08 16:49:39 -08001412 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001413 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001414 mCount = mLoadedCount = mLastCount = 0;
1415 if (detachIfReset) {
1416 detach();
1417 }
1418 return true;
1419 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001420
Joe Onoratoabded112016-02-08 16:49:39 -08001421 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001422 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001423 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001424
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001425 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001426 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1427 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001428 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001431 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001434 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436
Adam Lesinskie08af192015-03-25 16:42:59 -07001437 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001438 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001440 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 + " old mUnpluggedTime=" + mUnpluggedTime
1442 + " old mUnpluggedCount=" + mUnpluggedCount);
1443 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001444 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001445 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 if (DEBUG && mType < 0) {
1447 Log.v(TAG, "unplug #" + mType
1448 + ": new mUnpluggedTime=" + mUnpluggedTime
1449 + " new mUnpluggedCount=" + mUnpluggedCount);
1450 }
1451 }
1452
Adam Lesinskie08af192015-03-25 16:42:59 -07001453 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001454 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001455 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001456 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001457 + " old mTotalTime=" + mTotalTime);
1458 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001459 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001460 mCount = computeCurrentCountLocked();
1461 if (DEBUG && mType < 0) {
1462 Log.v(TAG, "plug #" + mType
1463 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 /**
1468 * Writes a possibly null Timer to a Parcel.
1469 *
1470 * @param out the Parcel to be written to.
1471 * @param timer a Timer, or null.
1472 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001473 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 if (timer == null) {
1475 out.writeInt(0); // indicates null
1476 return;
1477 }
1478 out.writeInt(1); // indicates non-null
1479
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001480 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482
1483 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001484 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001485 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1486 if (which == STATS_SINCE_UNPLUGGED) {
1487 val -= mUnpluggedTime;
1488 } else if (which != STATS_SINCE_CHARGED) {
1489 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491
1492 return val;
1493 }
1494
1495 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001496 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001497 int val = computeCurrentCountLocked();
1498 if (which == STATS_SINCE_UNPLUGGED) {
1499 val -= mUnpluggedCount;
1500 } else if (which != STATS_SINCE_CHARGED) {
1501 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
1503
1504 return val;
1505 }
1506
Adam Lesinskie08af192015-03-25 16:42:59 -07001507 @Override
1508 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1509 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1510 return val - mTimeBeforeMark;
1511 }
1512
1513 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001514 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001515 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1517 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001518 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001520 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001522 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001523
1524
Joe Onoratoabded112016-02-08 16:49:39 -08001525 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001526 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1527 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001528 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001529 }
1530
Joe Onoratoabded112016-02-08 16:49:39 -08001531 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001532 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001533 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001534 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001535 mUnpluggedTime = mTotalTime;
1536 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001537 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001538 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001539
1540 // When reading the summary, we set the mark to be the latest information.
1541 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001542 }
1543 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001544
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001545 /**
1546 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1547 * method. The state of the timer according to its {@link TimeBase} will determine how much
1548 * of the value is recorded.
1549 *
1550 * If the value being recorded resets, {@link #endSample()} can be called in order to
1551 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1552 * between calls, the {@link #endSample()} is automatically called and the new value is
1553 * expected to increase monotonically from that point on.
1554 */
Joe Onoratoabded112016-02-08 16:49:39 -08001555 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001556
Evan Millarc64edde2009-04-18 12:26:32 -07001557 /**
1558 * The most recent reported count from /proc/wakelocks.
1559 */
1560 int mCurrentReportedCount;
1561
1562 /**
1563 * The reported count from /proc/wakelocks when unplug() was last
1564 * called.
1565 */
1566 int mUnpluggedReportedCount;
1567
1568 /**
1569 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001570 */
Evan Millarc64edde2009-04-18 12:26:32 -07001571 long mCurrentReportedTotalTime;
1572
1573
1574 /**
1575 * The reported total_time from /proc/wakelocks when unplug() was last
1576 * called.
1577 */
1578 long mUnpluggedReportedTotalTime;
1579
1580 /**
1581 * Whether we are currently in a discharge cycle.
1582 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001583 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001584
1585 /**
1586 * Whether we are currently recording reported values.
1587 */
1588 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001589
Evan Millarc64edde2009-04-18 12:26:32 -07001590 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001591 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001592 */
1593 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001594
Adam Lesinski98f0d462016-04-19 16:46:20 -07001595 @VisibleForTesting
1596 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001597 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001598 mCurrentReportedCount = in.readInt();
1599 mUnpluggedReportedCount = in.readInt();
1600 mCurrentReportedTotalTime = in.readLong();
1601 mUnpluggedReportedTotalTime = in.readLong();
1602 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001603 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001604 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001605
Adam Lesinski98f0d462016-04-19 16:46:20 -07001606 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001607 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001608 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001609 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001610 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001611 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001612
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001613 /**
1614 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1615 * be less than the values used for a previous invocation.
1616 */
1617 public void endSample() {
1618 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1619 mCount = computeCurrentCountLocked();
1620 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1621 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001622 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001623
Evan Millarc64edde2009-04-18 12:26:32 -07001624 public void setUpdateVersion(int version) {
1625 mUpdateVersion = version;
1626 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001627
Evan Millarc64edde2009-04-18 12:26:32 -07001628 public int getUpdateVersion() {
1629 return mUpdateVersion;
1630 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001631
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001632 /**
1633 * Updates the current recorded values. These are meant to be monotonically increasing
1634 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1635 *
1636 * If the values being recorded have been reset, the monotonically increasing requirement
1637 * will be broken. In this case, {@link #endSample()} is automatically called and
1638 * the total value of totalTime and count are recorded, starting a new monotonically
1639 * increasing sample.
1640 *
1641 * @param totalTime total time of sample in microseconds.
1642 * @param count total number of times the event being sampled occurred.
1643 */
1644 public void update(long totalTime, int count) {
1645 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001646 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001647 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001648 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001649 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001650
1651 mTrackingReportedValues = true;
1652
1653 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1654 endSample();
1655 }
1656
1657 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001658 mCurrentReportedCount = count;
1659 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001660
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001661 /**
1662 * Adds deltaTime and deltaCount to the current sample.
1663 *
1664 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1665 * @param deltaCount additional number of times the event being sampled occurred.
1666 */
1667 public void add(long deltaTime, int deltaCount) {
1668 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001669 }
1670
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001671 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001672 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1673 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001674 if (mTrackingReportedValues) {
1675 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1676 mUnpluggedReportedCount = mCurrentReportedCount;
1677 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001679 }
1680
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001681 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001682 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1683 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1684 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001686
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001687 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001688 public void logState(Printer pw, String prefix) {
1689 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001690 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001691 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1692 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1693 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1694 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001695
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001696 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001697 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001698 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001699 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1700 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001701
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001702 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001703 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001704 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001705 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1706 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001707
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001708 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001709 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1710 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001711 out.writeInt(mCurrentReportedCount);
1712 out.writeInt(mUnpluggedReportedCount);
1713 out.writeLong(mCurrentReportedTotalTime);
1714 out.writeLong(mUnpluggedReportedTotalTime);
1715 out.writeInt(mTrackingReportedValues ? 1 : 0);
1716 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001717
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001718 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001719 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001720 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001721 mTrackingReportedValues = false;
1722 mUnpluggedReportedTotalTime = 0;
1723 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001724 return true;
1725 }
Evan Millarc64edde2009-04-18 12:26:32 -07001726 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001727
Evan Millarc64edde2009-04-18 12:26:32 -07001728 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001729 * A timer that increments in batches. It does not run for durations, but just jumps
1730 * for a pre-determined amount.
1731 */
Joe Onoratoabded112016-02-08 16:49:39 -08001732 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001733 final Uid mUid;
1734
1735 /**
1736 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1737 */
1738 long mLastAddedTime;
1739
1740 /**
1741 * The last duration that we added to the timer. This is in microseconds.
1742 */
1743 long mLastAddedDuration;
1744
1745 /**
1746 * Whether we are currently in a discharge cycle.
1747 */
1748 boolean mInDischarge;
1749
Joe Onoratoabded112016-02-08 16:49:39 -08001750 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1751 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001752 mUid = uid;
1753 mLastAddedTime = in.readLong();
1754 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001755 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001756 }
1757
Joe Onoratoabded112016-02-08 16:49:39 -08001758 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1759 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001760 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001761 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001762 }
1763
1764 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001765 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1766 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001767 out.writeLong(mLastAddedTime);
1768 out.writeLong(mLastAddedDuration);
1769 }
1770
1771 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001772 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001773 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001774 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001775 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001776 }
1777
1778 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001779 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001780 recomputeLastDuration(elapsedRealtime, false);
1781 mInDischarge = true;
1782 // If we are still within the last added duration, then re-added whatever remains.
1783 if (mLastAddedTime == elapsedRealtime) {
1784 mTotalTime += mLastAddedDuration;
1785 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001786 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001787 }
1788
1789 @Override
1790 public void logState(Printer pw, String prefix) {
1791 super.logState(pw, prefix);
1792 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1793 + " mLastAddedDuration=" + mLastAddedDuration);
1794 }
1795
1796 private long computeOverage(long curTime) {
1797 if (mLastAddedTime > 0) {
1798 return mLastTime + mLastAddedDuration - curTime;
1799 }
1800 return 0;
1801 }
1802
1803 private void recomputeLastDuration(long curTime, boolean abort) {
1804 final long overage = computeOverage(curTime);
1805 if (overage > 0) {
1806 // Aborting before the duration ran out -- roll back the remaining
1807 // duration. Only do this if currently discharging; otherwise we didn't
1808 // actually add the time.
1809 if (mInDischarge) {
1810 mTotalTime -= overage;
1811 }
1812 if (abort) {
1813 mLastAddedTime = 0;
1814 } else {
1815 mLastAddedTime = curTime;
1816 mLastAddedDuration -= overage;
1817 }
1818 }
1819 }
1820
1821 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001822 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001823 recomputeLastDuration(now, true);
1824 mLastAddedTime = now;
1825 mLastAddedDuration = durationMillis * 1000;
1826 if (mInDischarge) {
1827 mTotalTime += mLastAddedDuration;
1828 mCount++;
1829 }
1830 }
1831
1832 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001833 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001834 recomputeLastDuration(now, true);
1835 }
1836
1837 @Override
1838 protected int computeCurrentCountLocked() {
1839 return mCount;
1840 }
1841
1842 @Override
1843 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001844 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001845 if (overage > 0) {
1846 return mTotalTime = overage;
1847 }
1848 return mTotalTime;
1849 }
1850
1851 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001852 public boolean reset(boolean detachIfReset) {
1853 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001854 recomputeLastDuration(now, true);
1855 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001856 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001857 return !stillActive;
1858 }
1859 }
1860
Joe Onorato92fd23f2016-07-25 11:18:42 -07001861
1862 /**
1863 * A StopwatchTimer that also tracks the total and max individual
1864 * time spent active according to the given timebase. Whereas
1865 * StopwatchTimer apportions the time amongst all in the pool,
1866 * the total and max durations are not apportioned.
1867 */
1868 public static class DurationTimer extends StopwatchTimer {
1869 /**
1870 * The time (in ms) that the timer was last acquired or the time base
1871 * last (re-)started. Increasing the nesting depth does not reset this time.
1872 *
1873 * -1 if the timer is currently not running or the time base is not running.
1874 *
1875 * If written to a parcel, the start time is reset, as is mNesting in the base class
1876 * StopwatchTimer.
1877 */
1878 long mStartTimeMs = -1;
1879
1880 /**
Bookatz867c0d72017-03-07 18:23:42 -08001881 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001882 */
1883 long mMaxDurationMs;
1884
1885 /**
Bookatz867c0d72017-03-07 18:23:42 -08001886 * The time (in ms) that that the timer has been active since most recent
1887 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001888 */
1889 long mCurrentDurationMs;
1890
Bookatz867c0d72017-03-07 18:23:42 -08001891 /**
1892 * The total time (in ms) that that the timer has been active since most recent reset()
1893 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1894 * (but not including the present currentDuration) since reset. Not pooled.
1895 */
1896 long mTotalDurationMs;
1897
Joe Onorato92fd23f2016-07-25 11:18:42 -07001898 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1899 TimeBase timeBase, Parcel in) {
1900 super(clocks, uid, type, timerPool, timeBase, in);
1901 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001902 mTotalDurationMs = in.readLong();
jackqdyulei610a0a02017-08-09 14:02:01 -07001903 mCurrentDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001904 }
1905
1906 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1907 TimeBase timeBase) {
1908 super(clocks, uid, type, timerPool, timeBase);
1909 }
1910
1911 @Override
1912 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1913 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001914 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
jackqdyulei610a0a02017-08-09 14:02:01 -07001915 out.writeLong(mTotalDurationMs);
1916 out.writeLong(getCurrentDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001917 }
1918
1919 /**
1920 * Write the summary to the parcel.
1921 *
1922 * Since the time base is probably meaningless after we come back, reading
1923 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001924 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001925 */
1926 @Override
1927 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1928 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001929 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001930 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001931 }
1932
1933 /**
1934 * Read the summary parcel.
1935 *
1936 * Has the side effect of stopping the timer.
1937 */
1938 @Override
1939 public void readSummaryFromParcelLocked(Parcel in) {
1940 super.readSummaryFromParcelLocked(in);
1941 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001942 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001943 mStartTimeMs = -1;
1944 mCurrentDurationMs = 0;
1945 }
1946
1947 /**
1948 * The TimeBase time started (again).
1949 *
1950 * If the timer is also running, store the start time.
1951 */
1952 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1953 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1954 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001955 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001956 }
1957 }
1958
1959 /**
1960 * The TimeBase stopped running.
1961 *
1962 * If the timer is running, add the duration into mCurrentDurationMs.
1963 */
1964 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001965 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1966 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001967 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001968 // baseRealtimeUs has already been converted to the timebase's realtime.
1969 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001970 }
1971 mStartTimeMs = -1;
1972 }
1973
1974 @Override
1975 public void logState(Printer pw, String prefix) {
1976 super.logState(pw, prefix);
1977 }
1978
1979 @Override
1980 public void startRunningLocked(long elapsedRealtimeMs) {
1981 super.startRunningLocked(elapsedRealtimeMs);
1982 if (mNesting == 1 && mTimeBase.isRunning()) {
1983 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001984 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001985 }
1986 }
1987
1988 /**
1989 * Decrements the mNesting ref-count on this timer.
1990 *
1991 * If it actually stopped (mNesting went to 0), then possibly update
1992 * mMaxDuration if the current duration was the longest ever.
1993 */
1994 @Override
1995 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001996 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001997 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001998 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001999 if (durationMs > mMaxDurationMs) {
2000 mMaxDurationMs = durationMs;
2001 }
2002 mStartTimeMs = -1;
2003 mCurrentDurationMs = 0;
2004 }
Kweku Adams47db5a82016-12-09 19:04:50 -08002005 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
2006 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
2007 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002008 }
2009
2010 @Override
2011 public boolean reset(boolean detachIfReset) {
2012 boolean result = super.reset(detachIfReset);
2013 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08002014 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002015 mCurrentDurationMs = 0;
2016 if (mNesting > 0) {
2017 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
2018 } else {
2019 mStartTimeMs = -1;
2020 }
2021 return result;
2022 }
2023
2024 /**
2025 * Returns the max duration that this timer has ever seen.
2026 *
2027 * Note that this time is NOT split between the timers in the timer group that
2028 * this timer is attached to. It is the TOTAL time.
2029 */
2030 @Override
2031 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
2032 if (mNesting > 0) {
2033 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
2034 if (durationMs > mMaxDurationMs) {
2035 return durationMs;
2036 }
2037 }
2038 return mMaxDurationMs;
2039 }
2040
2041 /**
2042 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08002043 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002044 *
2045 * Note that this time is NOT split between the timers in the timer group that
2046 * this timer is attached to. It is the TOTAL time.
jackqdyulei610a0a02017-08-09 14:02:01 -07002047 *
2048 * Note that if running timer is parceled and unparceled, this method will return
2049 * current duration value at the time of parceling even though timer may not be
2050 * currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002051 */
2052 @Override
2053 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
2054 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08002055 if (mNesting > 0 && mTimeBase.isRunning()) {
2056 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
2057 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002058 }
2059 return durationMs;
2060 }
Bookatz867c0d72017-03-07 18:23:42 -08002061
2062 /**
2063 * Returns the total cumulative duration that this timer has been on since reset().
2064 * If mTimerPool == null, this should be the same
2065 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
2066 *
2067 * Note that this time is NOT split between the timers in the timer group that
2068 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
2069 * the result will not be equivalent to getTotalTimeLocked.
2070 */
2071 @Override
2072 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
2073 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
2074 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002075 }
2076
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002077 /**
Evan Millarc64edde2009-04-18 12:26:32 -07002078 * State for keeping track of timing information.
2079 */
Joe Onoratoabded112016-02-08 16:49:39 -08002080 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002081 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07002082 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002083
Evan Millarc64edde2009-04-18 12:26:32 -07002084 int mNesting;
2085
Evan Millarc64edde2009-04-18 12:26:32 -07002086 /**
2087 * The last time at which we updated the timer. If mNesting is > 0,
2088 * subtract this from the current battery time to find the amount of
2089 * time we have been running since we last computed an update.
2090 */
2091 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002092
Evan Millarc64edde2009-04-18 12:26:32 -07002093 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002094 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07002095 * was actually held for an interesting duration. If time base was not running when timer
2096 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07002097 */
Bookatzceebafe2017-04-06 11:59:13 -07002098 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07002099
Amith Yamasanif37447b2009-10-08 18:28:01 -07002100 long mTimeout;
2101
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002102 /**
2103 * For partial wake locks, keep track of whether we are in the list
2104 * to consume CPU cycles.
2105 */
Sudheer Shanka38383232017-07-25 09:55:03 -07002106 @VisibleForTesting
2107 public boolean mInList;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002108
Joe Onoratoabded112016-02-08 16:49:39 -08002109 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002110 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08002111 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002112 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002113 mTimerPool = timerPool;
2114 mUpdateTime = in.readLong();
2115 }
2116
Joe Onoratoabded112016-02-08 16:49:39 -08002117 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002118 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002119 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002120 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002121 mTimerPool = timerPool;
2122 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002123
Joe Onoratoabded112016-02-08 16:49:39 -08002124 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002125 mTimeout = timeout;
2126 }
2127
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002128 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2129 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002130 out.writeLong(mUpdateTime);
2131 }
2132
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002133 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002134 if (mNesting > 0) {
2135 if (DEBUG && mType < 0) {
2136 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2137 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002138 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2139 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002140 if (DEBUG && mType < 0) {
2141 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2142 }
2143 }
2144 }
2145
2146 public void logState(Printer pw, String prefix) {
2147 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002148 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 + " mAcquireTime=" + mAcquireTime);
2150 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002151
Joe Onoratoabded112016-02-08 16:49:39 -08002152 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002154 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002155 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 if (mTimerPool != null) {
2157 // Accumulate time to all currently active timers before adding
2158 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002159 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 // Add this timer to the active pool
2161 mTimerPool.add(this);
2162 }
Bookatzceebafe2017-04-06 11:59:13 -07002163 if (mTimeBase.isRunning()) {
2164 // Increment the count
2165 mCount++;
2166 mAcquireTime = mTotalTime;
2167 } else {
2168 mAcquireTime = -1;
2169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 if (DEBUG && mType < 0) {
2171 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2172 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2173 + " mAcquireTime=" + mAcquireTime);
2174 }
2175 }
2176 }
2177
Joe Onoratoabded112016-02-08 16:49:39 -08002178 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002179 return mNesting > 0;
2180 }
2181
Joe Onoratoabded112016-02-08 16:49:39 -08002182 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 // Ignore attempt to stop a timer that isn't running
2184 if (mNesting == 0) {
2185 return;
2186 }
2187 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002188 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 if (mTimerPool != null) {
2190 // Accumulate time to all active counters, scaled by the total
2191 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002192 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 // Remove this timer from the active pool
2194 mTimerPool.remove(this);
2195 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 mNesting = 1;
2197 mTotalTime = computeRunTimeLocked(batteryRealtime);
2198 mNesting = 0;
2199 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 if (DEBUG && mType < 0) {
2202 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2203 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2204 + " mAcquireTime=" + mAcquireTime);
2205 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002206
Bookatzceebafe2017-04-06 11:59:13 -07002207 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 // If there was no change in the time, then discard this
2209 // count. A somewhat cheezy strategy, but hey.
2210 mCount--;
2211 }
2212 }
2213 }
2214
Joe Onoratoabded112016-02-08 16:49:39 -08002215 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002216 if (mNesting > 0) {
2217 mNesting = 1;
2218 stopRunningLocked(elapsedRealtimeMs);
2219 }
2220 }
2221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 // Update the total time for all other running Timers with the same type as this Timer
2223 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002224 private static long refreshTimersLocked(long batteryRealtime,
2225 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002226 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 final int N = pool.size();
2228 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002229 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 long heldTime = batteryRealtime - t.mUpdateTime;
2231 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002232 final long myTime = heldTime / N;
2233 if (t == self) {
2234 selfTime = myTime;
2235 }
2236 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
2238 t.mUpdateTime = batteryRealtime;
2239 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002240 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242
Evan Millarc64edde2009-04-18 12:26:32 -07002243 @Override
2244 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002245 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2246 curBatteryRealtime = mUpdateTime + mTimeout;
2247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 return mTotalTime + (mNesting > 0
2249 ? (curBatteryRealtime - mUpdateTime)
2250 / (mTimerPool != null ? mTimerPool.size() : 1)
2251 : 0);
2252 }
2253
Evan Millarc64edde2009-04-18 12:26:32 -07002254 @Override
2255 protected int computeCurrentCountLocked() {
2256 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258
Adam Lesinskie08af192015-03-25 16:42:59 -07002259 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002260 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002261 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002262 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002263 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002264 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002265 }
Bookatzceebafe2017-04-06 11:59:13 -07002266 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002267 return canDetach;
2268 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002269
Adam Lesinskie08af192015-03-25 16:42:59 -07002270 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002271 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002272 super.detach();
2273 if (mTimerPool != null) {
2274 mTimerPool.remove(this);
2275 }
2276 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002277
Adam Lesinskie08af192015-03-25 16:42:59 -07002278 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002279 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002280 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 mNesting = 0;
2282 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002283
2284 /**
2285 * Set the mark so that we can query later for the total time the timer has
2286 * accumulated since this point. The timer can be running or not.
2287 *
2288 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2289 */
2290 public void setMark(long elapsedRealtimeMs) {
2291 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2292 if (mNesting > 0) {
2293 // We are running.
2294 if (mTimerPool != null) {
2295 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2296 } else {
2297 mTotalTime += batteryRealtime - mUpdateTime;
2298 mUpdateTime = batteryRealtime;
2299 }
2300 }
2301 mTimeBeforeMark = mTotalTime;
2302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002304
Bookatz867c0d72017-03-07 18:23:42 -08002305 /**
2306 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2307 * TimeBase is effectively a subset of the other.
2308 */
Bookatzaa4594a2017-03-24 12:39:56 -07002309 public static class DualTimer extends DurationTimer {
2310 // This class both is a DurationTimer and also holds a second DurationTimer.
2311 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2312 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2313 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002314 // mSubTimer typically tracks only part of the total time, such as background time, as
2315 // determined by a subTimeBase. It is NOT pooled.
2316 private final DurationTimer mSubTimer;
2317
2318 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002319 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2320 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002321 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002322 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002323 */
2324 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2325 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002326 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002327 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2328 }
2329
2330 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002331 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2332 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002333 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002334 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002335 */
2336 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2337 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002338 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002339 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2340 }
2341
Bookatz867c0d72017-03-07 18:23:42 -08002342 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002343 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002344 public DurationTimer getSubTimer() {
2345 return mSubTimer;
2346 }
2347
Bookatzaa4594a2017-03-24 12:39:56 -07002348 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002349 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002350 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002351 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2352 }
2353
Bookatzaa4594a2017-03-24 12:39:56 -07002354 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002355 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002356 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002357 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2358 }
2359
Bookatzaa4594a2017-03-24 12:39:56 -07002360 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002361 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002362 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002363 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2364 }
2365
Bookatzaa4594a2017-03-24 12:39:56 -07002366 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002367 public boolean reset(boolean detachIfReset) {
2368 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002369 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2370 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002371 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002372 return !active;
2373 }
2374
Bookatzaa4594a2017-03-24 12:39:56 -07002375 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002376 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002377 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002378 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002379 }
2380
Bookatzaa4594a2017-03-24 12:39:56 -07002381 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002382 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002383 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002384 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2385 }
2386
Bookatzaa4594a2017-03-24 12:39:56 -07002387 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002388 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002389 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002390 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2391 }
2392
Bookatzaa4594a2017-03-24 12:39:56 -07002393 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002394 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002395 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002396 mSubTimer.readSummaryFromParcelLocked(in);
2397 }
2398 }
2399
2400
Dianne Hackbornd953c532014-08-16 18:17:38 -07002401 public abstract class OverflowArrayMap<T> {
2402 private static final String OVERFLOW_NAME = "*overflow*";
2403
Dianne Hackborn657153b2016-07-29 14:54:14 -07002404 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002405 final ArrayMap<String, T> mMap = new ArrayMap<>();
2406 T mCurOverflow;
2407 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002408 long mLastOverflowTime;
2409 long mLastOverflowFinishTime;
2410 long mLastClearTime;
2411 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002412
Dianne Hackborn657153b2016-07-29 14:54:14 -07002413 public OverflowArrayMap(int uid) {
2414 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002415 }
2416
2417 public ArrayMap<String, T> getMap() {
2418 return mMap;
2419 }
2420
2421 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002422 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002423 mMap.clear();
2424 mCurOverflow = null;
2425 mActiveOverflow = null;
2426 }
2427
2428 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002429 if (name == null) {
2430 name = "";
2431 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002432 mMap.put(name, obj);
2433 if (OVERFLOW_NAME.equals(name)) {
2434 mCurOverflow = obj;
2435 }
2436 }
2437
2438 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002439 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002440 if (mActiveOverflow != null) {
2441 if (mActiveOverflow.size() == 0) {
2442 mActiveOverflow = null;
2443 }
2444 }
2445 if (mActiveOverflow == null) {
2446 // There is no currently active overflow, so we should no longer have
2447 // an overflow entry.
2448 if (mMap.containsKey(OVERFLOW_NAME)) {
2449 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2450 + mMap.get(OVERFLOW_NAME));
2451 mMap.remove(OVERFLOW_NAME);
2452 }
2453 mCurOverflow = null;
2454 } else {
2455 // There is currently active overflow, so we should still have an overflow entry.
2456 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2457 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2458 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2459 }
2460 }
2461 }
2462
2463 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002464 if (name == null) {
2465 name = "";
2466 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002467 T obj = mMap.get(name);
2468 if (obj != null) {
2469 return obj;
2470 }
2471
2472 // No object exists for the given name, but do we currently have it
2473 // running as part of the overflow?
2474 if (mActiveOverflow != null) {
2475 MutableInt over = mActiveOverflow.get(name);
2476 if (over != null) {
2477 // We are already actively counting this name in the overflow object.
2478 obj = mCurOverflow;
2479 if (obj == null) {
2480 // Shouldn't be here, but we'll try to recover.
2481 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2482 obj = mCurOverflow = instantiateObject();
2483 mMap.put(OVERFLOW_NAME, obj);
2484 }
2485 over.value++;
2486 return obj;
2487 }
2488 }
2489
2490 // No object exists for given name nor in the overflow; we need to make
2491 // a new one.
2492 final int N = mMap.size();
2493 if (N >= MAX_WAKELOCKS_PER_UID) {
2494 // Went over the limit on number of objects to track; this one goes
2495 // in to the overflow.
2496 obj = mCurOverflow;
2497 if (obj == null) {
2498 // Need to start overflow now...
2499 obj = mCurOverflow = instantiateObject();
2500 mMap.put(OVERFLOW_NAME, obj);
2501 }
2502 if (mActiveOverflow == null) {
2503 mActiveOverflow = new ArrayMap<>();
2504 }
2505 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002506 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002507 return obj;
2508 }
2509
2510 // Normal case where we just need to make a new object.
2511 obj = instantiateObject();
2512 mMap.put(name, obj);
2513 return obj;
2514 }
2515
2516 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002517 if (name == null) {
2518 name = "";
2519 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002520 T obj = mMap.get(name);
2521 if (obj != null) {
2522 return obj;
2523 }
2524
2525 // No object exists for the given name, but do we currently have it
2526 // running as part of the overflow?
2527 if (mActiveOverflow != null) {
2528 MutableInt over = mActiveOverflow.get(name);
2529 if (over != null) {
2530 // We are already actively counting this name in the overflow object.
2531 obj = mCurOverflow;
2532 if (obj != null) {
2533 over.value--;
2534 if (over.value <= 0) {
2535 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002536 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002537 }
2538 return obj;
2539 }
2540 }
2541 }
2542
2543 // Huh, they are stopping an active operation but we can't find one!
2544 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002545 StringBuilder sb = new StringBuilder();
2546 sb.append("Unable to find object for ");
2547 sb.append(name);
2548 sb.append(" in uid ");
2549 sb.append(mUid);
2550 sb.append(" mapsize=");
2551 sb.append(mMap.size());
2552 sb.append(" activeoverflow=");
2553 sb.append(mActiveOverflow);
2554 sb.append(" curoverflow=");
2555 sb.append(mCurOverflow);
2556 long now = SystemClock.elapsedRealtime();
2557 if (mLastOverflowTime != 0) {
2558 sb.append(" lastOverflowTime=");
2559 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2560 }
2561 if (mLastOverflowFinishTime != 0) {
2562 sb.append(" lastOverflowFinishTime=");
2563 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2564 }
2565 if (mLastClearTime != 0) {
2566 sb.append(" lastClearTime=");
2567 TimeUtils.formatDuration(mLastClearTime-now, sb);
2568 }
2569 if (mLastCleanupTime != 0) {
2570 sb.append(" lastCleanupTime=");
2571 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2572 }
2573 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002574 return null;
2575 }
2576
2577 public abstract T instantiateObject();
2578 }
2579
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002580 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2581 implements Parcelable {
2582 private final LongSamplingCounter mIdleTimeMillis;
2583 private final LongSamplingCounter mRxTimeMillis;
2584 private final LongSamplingCounter[] mTxTimeMillis;
2585 private final LongSamplingCounter mPowerDrainMaMs;
2586
2587 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2588 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2589 mRxTimeMillis = new LongSamplingCounter(timeBase);
2590 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2591 for (int i = 0; i < numTxStates; i++) {
2592 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2593 }
2594 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2595 }
2596
2597 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2598 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2599 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2600 final int recordedTxStates = in.readInt();
2601 if (recordedTxStates != numTxStates) {
2602 throw new ParcelFormatException("inconsistent tx state lengths");
2603 }
2604
2605 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2606 for (int i = 0; i < numTxStates; i++) {
2607 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2608 }
2609 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2610 }
2611
2612 public void readSummaryFromParcel(Parcel in) {
2613 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2614 mRxTimeMillis.readSummaryFromParcelLocked(in);
2615 final int recordedTxStates = in.readInt();
2616 if (recordedTxStates != mTxTimeMillis.length) {
2617 throw new ParcelFormatException("inconsistent tx state lengths");
2618 }
2619 for (LongSamplingCounter counter : mTxTimeMillis) {
2620 counter.readSummaryFromParcelLocked(in);
2621 }
2622 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2623 }
2624
2625 @Override
2626 public int describeContents() {
2627 return 0;
2628 }
2629
2630 public void writeSummaryToParcel(Parcel dest) {
2631 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2632 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2633 dest.writeInt(mTxTimeMillis.length);
2634 for (LongSamplingCounter counter : mTxTimeMillis) {
2635 counter.writeSummaryFromParcelLocked(dest);
2636 }
2637 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2638 }
2639
2640 @Override
2641 public void writeToParcel(Parcel dest, int flags) {
2642 mIdleTimeMillis.writeToParcel(dest);
2643 mRxTimeMillis.writeToParcel(dest);
2644 dest.writeInt(mTxTimeMillis.length);
2645 for (LongSamplingCounter counter : mTxTimeMillis) {
2646 counter.writeToParcel(dest);
2647 }
2648 mPowerDrainMaMs.writeToParcel(dest);
2649 }
2650
2651 public void reset(boolean detachIfReset) {
2652 mIdleTimeMillis.reset(detachIfReset);
2653 mRxTimeMillis.reset(detachIfReset);
2654 for (LongSamplingCounter counter : mTxTimeMillis) {
2655 counter.reset(detachIfReset);
2656 }
2657 mPowerDrainMaMs.reset(detachIfReset);
2658 }
2659
2660 public void detach() {
2661 mIdleTimeMillis.detach();
2662 mRxTimeMillis.detach();
2663 for (LongSamplingCounter counter : mTxTimeMillis) {
2664 counter.detach();
2665 }
2666 mPowerDrainMaMs.detach();
2667 }
2668
2669 /**
2670 * @return a LongSamplingCounter, measuring time spent in the idle state in
2671 * milliseconds.
2672 */
2673 @Override
2674 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002675 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002676 }
2677
2678 /**
2679 * @return a LongSamplingCounter, measuring time spent in the receive state in
2680 * milliseconds.
2681 */
2682 @Override
2683 public LongSamplingCounter getRxTimeCounter() {
2684 return mRxTimeMillis;
2685 }
2686
2687 /**
2688 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2689 * milliseconds.
2690 */
2691 @Override
2692 public LongSamplingCounter[] getTxTimeCounters() {
2693 return mTxTimeMillis;
2694 }
2695
2696 /**
2697 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2698 */
2699 @Override
2700 public LongSamplingCounter getPowerCounter() {
2701 return mPowerDrainMaMs;
2702 }
2703 }
2704
Bookatz50df7112017-08-04 14:53:26 -07002705 /** Get Resource Power Manager stats. Create a new one if it doesn't already exist. */
2706 public SamplingTimer getRpmTimerLocked(String name) {
2707 SamplingTimer rpmt = mRpmStats.get(name);
2708 if (rpmt == null) {
2709 rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2710 mRpmStats.put(name, rpmt);
2711 }
2712 return rpmt;
2713 }
2714
2715 /** Get Screen-off Resource Power Manager stats. Create new one if it doesn't already exist. */
2716 public SamplingTimer getScreenOffRpmTimerLocked(String name) {
2717 SamplingTimer rpmt = mScreenOffRpmStats.get(name);
2718 if (rpmt == null) {
2719 rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
2720 mScreenOffRpmStats.put(name, rpmt);
2721 }
2722 return rpmt;
2723 }
2724
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002725 /*
2726 * Get the wakeup reason counter, and create a new one if one
2727 * doesn't already exist.
2728 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002729 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2730 SamplingTimer timer = mWakeupReasonStats.get(name);
2731 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002732 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002733 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002734 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002735 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002736 }
2737
Evan Millarc64edde2009-04-18 12:26:32 -07002738 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002739 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002740 * doesn't already exist.
2741 */
2742 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2743 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2744 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002745 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002746 mKernelWakelockStats.put(name, kwlt);
2747 }
2748 return kwlt;
2749 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002750
James Carr3a226052016-07-01 14:49:52 -07002751 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2752 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2753 if (kmt == null) {
2754 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2755 mKernelMemoryStats.put(bucket, kmt);
2756 }
2757 return kmt;
2758 }
2759
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002760 private int writeHistoryTag(HistoryTag tag) {
2761 Integer idxObj = mHistoryTagPool.get(tag);
2762 int idx;
2763 if (idxObj != null) {
2764 idx = idxObj;
2765 } else {
2766 idx = mNextHistoryTagIdx;
2767 HistoryTag key = new HistoryTag();
2768 key.setTo(tag);
2769 tag.poolIdx = idx;
2770 mHistoryTagPool.put(key, idx);
2771 mNextHistoryTagIdx++;
2772 mNumHistoryTagChars += key.string.length() + 1;
2773 }
2774 return idx;
2775 }
2776
2777 private void readHistoryTag(int index, HistoryTag tag) {
2778 tag.string = mReadHistoryStrings[index];
2779 tag.uid = mReadHistoryUids[index];
2780 tag.poolIdx = index;
2781 }
2782
Adam Lesinski926969b2016-04-28 17:31:12 -07002783 /*
2784 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2785
2786 There is always the first token, which may contain the delta time, or an indicator of
2787 the length of the time (int or long) following this token.
2788
2789 First token: always present,
2790 31 23 15 7 0
2791 â–ˆ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â–ˆ
2792
2793 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2794 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2795 delta time.
2796 A: battery level changed and an int follows with battery data.
2797 B: state changed and an int follows with state change data.
2798 C: state2 has changed and an int follows with state2 change data.
2799 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2800 E: event data has changed and an event struct follows.
2801 F: battery charge in coulombs has changed and an int with the charge follows.
2802 G: state flag denoting that the mobile radio was active.
2803 H: state flag denoting that the wifi radio was active.
2804 I: state flag denoting that a wifi scan occurred.
2805 J: state flag denoting that a wifi full lock was held.
2806 K: state flag denoting that the gps was on.
2807 L: state flag denoting that a wakelock was held.
2808 M: state flag denoting that the cpu was running.
2809
2810 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2811 with the time delta.
2812
2813 Battery level int: if A in the first token is set,
2814 31 23 15 7 0
2815 â–ˆ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â–ˆ
2816
2817 D: indicates that extra history details follow.
2818 V: the battery voltage.
2819 T: the battery temperature.
2820 L: the battery level (out of 100).
2821
2822 State change int: if B in the first token is set,
2823 31 23 15 7 0
2824 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2825
2826 A: wifi multicast was on.
2827 B: battery was plugged in.
2828 C: screen was on.
2829 D: phone was scanning for signal.
2830 E: audio was on.
2831 F: a sensor was active.
2832
2833 State2 change int: if C in the first token is set,
2834 31 23 15 7 0
2835 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2836
2837 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2838 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2839 C: a bluetooth scan was active.
2840 D: the camera was active.
2841 E: bluetooth was on.
2842 F: a phone call was active.
2843 G: the device was charging.
2844 H: 2 bits indicating the device-idle (doze) state: off, light, full
2845 I: the flashlight was on.
2846 J: wifi was on.
2847 K: wifi was running.
2848 L: video was playing.
2849 M: power save mode was on.
2850
2851 Wakelock/wakereason struct: if D in the first token is set,
2852 TODO(adamlesinski): describe wakelock/wakereason struct.
2853
2854 Event struct: if E in the first token is set,
2855 TODO(adamlesinski): describe the event struct.
2856
2857 History step details struct: if D in the battery level int is set,
2858 TODO(adamlesinski): describe the history step details struct.
2859
2860 Battery charge int: if F in the first token is set, an int representing the battery charge
2861 in coulombs follows.
2862 */
2863
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002864 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002865 static final int DELTA_TIME_MASK = 0x7ffff;
2866 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2867 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2868 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002869 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002870 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002871 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002872 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002873 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002874 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002875 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002876 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002877 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002878 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002879 // Flag in delta int: contains the battery charge count in uAh.
2880 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002881 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002882 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002883
2884 // These are the pieces of battery state that are packed in to the upper bits of
2885 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002886 // in STATE_BATTERY_MASK.
2887 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002888 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2889 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2890 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2891 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2892 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2893 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002894
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002895 // We use the low bit of the battery state int to indicate that we have full details
2896 // from a battery level change.
2897 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2898
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002899 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002900 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002901 dest.writeInt(DELTA_TIME_ABS);
2902 cur.writeToParcel(dest, 0);
2903 return;
2904 }
2905
2906 final long deltaTime = cur.time - last.time;
2907 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2908 final int lastStateInt = buildStateInt(last);
2909
2910 int deltaTimeToken;
2911 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2912 deltaTimeToken = DELTA_TIME_LONG;
2913 } else if (deltaTime >= DELTA_TIME_ABS) {
2914 deltaTimeToken = DELTA_TIME_INT;
2915 } else {
2916 deltaTimeToken = (int)deltaTime;
2917 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002918 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002919 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2920 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2921 final boolean computeStepDetails = includeStepDetails != 0
2922 || mLastHistoryStepDetails == null;
2923 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002924 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2925 if (batteryLevelIntChanged) {
2926 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2927 }
2928 final int stateInt = buildStateInt(cur);
2929 final boolean stateIntChanged = stateInt != lastStateInt;
2930 if (stateIntChanged) {
2931 firstToken |= DELTA_STATE_FLAG;
2932 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002933 final boolean state2IntChanged = cur.states2 != last.states2;
2934 if (state2IntChanged) {
2935 firstToken |= DELTA_STATE2_FLAG;
2936 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002937 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002938 firstToken |= DELTA_WAKELOCK_FLAG;
2939 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002940 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2941 firstToken |= DELTA_EVENT_FLAG;
2942 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002943
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002944 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2945 if (batteryChargeChanged) {
2946 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002947 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002948 dest.writeInt(firstToken);
2949 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2950 + " deltaTime=" + deltaTime);
2951
2952 if (deltaTimeToken >= DELTA_TIME_INT) {
2953 if (deltaTimeToken == DELTA_TIME_INT) {
2954 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2955 dest.writeInt((int)deltaTime);
2956 } else {
2957 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2958 dest.writeLong(deltaTime);
2959 }
2960 }
2961 if (batteryLevelIntChanged) {
2962 dest.writeInt(batteryLevelInt);
2963 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2964 + Integer.toHexString(batteryLevelInt)
2965 + " batteryLevel=" + cur.batteryLevel
2966 + " batteryTemp=" + cur.batteryTemperature
2967 + " batteryVolt=" + (int)cur.batteryVoltage);
2968 }
2969 if (stateIntChanged) {
2970 dest.writeInt(stateInt);
2971 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2972 + Integer.toHexString(stateInt)
2973 + " batteryStatus=" + cur.batteryStatus
2974 + " batteryHealth=" + cur.batteryHealth
2975 + " batteryPlugType=" + cur.batteryPlugType
2976 + " states=0x" + Integer.toHexString(cur.states));
2977 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002978 if (state2IntChanged) {
2979 dest.writeInt(cur.states2);
2980 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2981 + Integer.toHexString(cur.states2));
2982 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002983 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2984 int wakeLockIndex;
2985 int wakeReasonIndex;
2986 if (cur.wakelockTag != null) {
2987 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2988 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2989 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2990 } else {
2991 wakeLockIndex = 0xffff;
2992 }
2993 if (cur.wakeReasonTag != null) {
2994 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2995 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2996 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2997 } else {
2998 wakeReasonIndex = 0xffff;
2999 }
3000 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003001 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003002 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003003 int index = writeHistoryTag(cur.eventTag);
3004 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003005 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003006 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
3007 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3008 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003009 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003010 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07003011 if (mPlatformIdleStateCallback != null) {
3012 mCurHistoryStepDetails.statPlatformIdleState =
3013 mPlatformIdleStateCallback.getPlatformLowPowerStats();
3014 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
3015 mCurHistoryStepDetails.statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00003016
3017 mCurHistoryStepDetails.statSubsystemPowerState =
3018 mPlatformIdleStateCallback.getSubsystemLowPowerStats();
3019 if (DEBUG) Slog.i(TAG, "WRITE SubsystemPowerState:" +
3020 mCurHistoryStepDetails.statSubsystemPowerState);
3021
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07003022 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003023 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
3024 if (includeStepDetails != 0) {
3025 mCurHistoryStepDetails.writeToParcel(dest);
3026 }
3027 cur.stepDetails = mCurHistoryStepDetails;
3028 mLastHistoryStepDetails = mCurHistoryStepDetails;
3029 } else {
3030 cur.stepDetails = null;
3031 }
3032 if (mLastHistoryStepLevel < cur.batteryLevel) {
3033 mLastHistoryStepDetails = null;
3034 }
3035 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07003036
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003037 if (batteryChargeChanged) {
3038 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
3039 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07003040 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003041 }
3042
3043 private int buildBatteryLevelInt(HistoryItem h) {
3044 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08003045 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
3046 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
3047 }
3048
3049 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
3050 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
3051 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
3052 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003053 }
3054
3055 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003056 int plugType = 0;
3057 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
3058 plugType = 1;
3059 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
3060 plugType = 2;
3061 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
3062 plugType = 3;
3063 }
3064 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
3065 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
3066 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07003067 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003068 }
3069
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003070 private void computeHistoryStepDetails(final HistoryStepDetails out,
3071 final HistoryStepDetails last) {
3072 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
3073
3074 // Perform a CPU update right after we do this collection, so we have started
3075 // collecting good data for the next step.
3076 requestImmediateCpuUpdate();
3077
3078 if (last == null) {
3079 // We are not generating a delta, so all we need to do is reset the stats
3080 // we will later be doing a delta from.
3081 final int NU = mUidStats.size();
3082 for (int i=0; i<NU; i++) {
3083 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3084 uid.mLastStepUserTime = uid.mCurStepUserTime;
3085 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3086 }
3087 mLastStepCpuUserTime = mCurStepCpuUserTime;
3088 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3089 mLastStepStatUserTime = mCurStepStatUserTime;
3090 mLastStepStatSystemTime = mCurStepStatSystemTime;
3091 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3092 mLastStepStatIrqTime = mCurStepStatIrqTime;
3093 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3094 mLastStepStatIdleTime = mCurStepStatIdleTime;
3095 tmp.clear();
3096 return;
3097 }
3098 if (DEBUG) {
3099 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
3100 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
3101 + " irq=" + mLastStepStatIrqTime + " sirq="
3102 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
3103 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
3104 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
3105 + " irq=" + mCurStepStatIrqTime + " sirq="
3106 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
3107 }
3108 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
3109 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
3110 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
3111 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
3112 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
3113 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
3114 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
3115 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
3116 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
3117 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
3118 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
3119 final int NU = mUidStats.size();
3120 for (int i=0; i<NU; i++) {
3121 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3122 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
3123 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
3124 final int totalTime = totalUTime + totalSTime;
3125 uid.mLastStepUserTime = uid.mCurStepUserTime;
3126 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3127 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
3128 continue;
3129 }
3130 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
3131 out.appCpuUid3 = uid.mUid;
3132 out.appCpuUTime3 = totalUTime;
3133 out.appCpuSTime3 = totalSTime;
3134 } else {
3135 out.appCpuUid3 = out.appCpuUid2;
3136 out.appCpuUTime3 = out.appCpuUTime2;
3137 out.appCpuSTime3 = out.appCpuSTime2;
3138 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
3139 out.appCpuUid2 = uid.mUid;
3140 out.appCpuUTime2 = totalUTime;
3141 out.appCpuSTime2 = totalSTime;
3142 } else {
3143 out.appCpuUid2 = out.appCpuUid1;
3144 out.appCpuUTime2 = out.appCpuUTime1;
3145 out.appCpuSTime2 = out.appCpuSTime1;
3146 out.appCpuUid1 = uid.mUid;
3147 out.appCpuUTime1 = totalUTime;
3148 out.appCpuSTime1 = totalSTime;
3149 }
3150 }
3151 }
3152 mLastStepCpuUserTime = mCurStepCpuUserTime;
3153 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3154 mLastStepStatUserTime = mCurStepStatUserTime;
3155 mLastStepStatSystemTime = mCurStepStatSystemTime;
3156 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3157 mLastStepStatIrqTime = mCurStepStatIrqTime;
3158 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3159 mLastStepStatIdleTime = mCurStepStatIdleTime;
3160 }
3161
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003162 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3163 int firstToken = src.readInt();
3164 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003165 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003166 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003167 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3168 + " deltaTimeToken=" + deltaTimeToken);
3169
3170 if (deltaTimeToken < DELTA_TIME_ABS) {
3171 cur.time += deltaTimeToken;
3172 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3173 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003174 cur.numReadInts += 2;
3175 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003176 cur.readFromParcel(src);
3177 return;
3178 } else if (deltaTimeToken == DELTA_TIME_INT) {
3179 int delta = src.readInt();
3180 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003181 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003182 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3183 } else {
3184 long delta = src.readLong();
3185 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3186 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003187 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003188 }
3189
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003190 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003191 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003192 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003193 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003194 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003195 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
3196 + Integer.toHexString(batteryLevelInt)
3197 + " batteryLevel=" + cur.batteryLevel
3198 + " batteryTemp=" + cur.batteryTemperature
3199 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003200 } else {
3201 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003202 }
3203
3204 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3205 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003206 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003207 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3208 & STATE_BATTERY_STATUS_MASK);
3209 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3210 & STATE_BATTERY_HEALTH_MASK);
3211 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3212 & STATE_BATTERY_PLUG_MASK);
3213 switch (cur.batteryPlugType) {
3214 case 1:
3215 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3216 break;
3217 case 2:
3218 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3219 break;
3220 case 3:
3221 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3222 break;
3223 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003224 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003225 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3226 + Integer.toHexString(stateInt)
3227 + " batteryStatus=" + cur.batteryStatus
3228 + " batteryHealth=" + cur.batteryHealth
3229 + " batteryPlugType=" + cur.batteryPlugType
3230 + " states=0x" + Integer.toHexString(cur.states));
3231 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003232 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003233 }
3234
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003235 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3236 cur.states2 = src.readInt();
3237 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3238 + Integer.toHexString(cur.states2));
3239 }
3240
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003241 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003242 int indexes = src.readInt();
3243 int wakeLockIndex = indexes&0xffff;
3244 int wakeReasonIndex = (indexes>>16)&0xffff;
3245 if (wakeLockIndex != 0xffff) {
3246 cur.wakelockTag = cur.localWakelockTag;
3247 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3248 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3249 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3250 } else {
3251 cur.wakelockTag = null;
3252 }
3253 if (wakeReasonIndex != 0xffff) {
3254 cur.wakeReasonTag = cur.localWakeReasonTag;
3255 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3256 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3257 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3258 } else {
3259 cur.wakeReasonTag = null;
3260 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003261 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003262 } else {
3263 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003264 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003265 }
3266
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003267 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003268 cur.eventTag = cur.localEventTag;
3269 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003270 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003271 final int index = ((codeAndIndex>>16)&0xffff);
3272 readHistoryTag(index, cur.eventTag);
3273 cur.numReadInts += 1;
3274 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3275 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3276 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003277 } else {
3278 cur.eventCode = HistoryItem.EVENT_NONE;
3279 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003280
3281 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3282 cur.stepDetails = mReadHistoryStepDetails;
3283 cur.stepDetails.readFromParcel(src);
3284 } else {
3285 cur.stepDetails = null;
3286 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003287
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003288 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3289 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003290 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003291 }
3292
Dianne Hackbornfc064132014-06-02 12:42:12 -07003293 @Override
3294 public void commitCurrentHistoryBatchLocked() {
3295 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3296 }
3297
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003298 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003299 if (!mHaveBatteryLevel || !mRecordingHistory) {
3300 return;
3301 }
3302
Dianne Hackborn40c87252014-03-19 16:55:40 -07003303 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003304 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3305 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003306 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003307 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003308 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3309 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003310 + Integer.toHexString(lastDiffStates) + " diff2="
3311 + Integer.toHexString(diffStates2) + " lastDiff2="
3312 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003313 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003314 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003315 && (diffStates2&lastDiffStates2) == 0
3316 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3317 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003318 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003319 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003320 || cur.eventCode == HistoryItem.EVENT_NONE)
3321 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3322 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3323 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3324 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3325 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3326 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003327 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003328 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003329 // as long as no bit has changed both between now and the last entry, as
3330 // well as the last entry and the one before it (so we capture any toggles).
3331 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003332 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3333 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3334 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003335 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003336 // If the last written history had a wakelock tag, we need to retain it.
3337 // Note that the condition above made sure that we aren't in a case where
3338 // both it and the current history item have a wakelock tag.
3339 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003340 cur.wakelockTag = cur.localWakelockTag;
3341 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003342 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003343 // If the last written history had a wake reason tag, we need to retain it.
3344 // Note that the condition above made sure that we aren't in a case where
3345 // both it and the current history item have a wakelock tag.
3346 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003347 cur.wakeReasonTag = cur.localWakeReasonTag;
3348 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003349 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003350 // If the last written history had an event, we need to retain it.
3351 // Note that the condition above made sure that we aren't in a case where
3352 // both it and the current history item have an event.
3353 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003354 cur.eventCode = mHistoryLastWritten.eventCode;
3355 cur.eventTag = cur.localEventTag;
3356 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003357 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003358 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003359 }
3360
Adam Lesinski45489782016-12-15 23:45:17 -08003361 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003362 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003363 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3364 // Clients can't deal with history buffers this large. This only
3365 // really happens when the device is on charger and interacted with
3366 // for long periods of time, like in retail mode. Since the device is
3367 // most likely charged, when unplugged, stats would have reset anyways.
3368 // Reset the stats and mark that we overflowed.
3369 // b/32540341
3370 resetAllStatsLocked();
3371
3372 // Mark that we want to set *OVERFLOW* event and the RESET:START
3373 // events.
3374 recordResetDueToOverflow = true;
3375
3376 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003377 if (!mHistoryOverflow) {
3378 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003379 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3380 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003381 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003382 }
3383
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003384 // After overflow, we allow various bit-wise states to settle to 0.
3385 boolean writeAnyway = false;
3386 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3387 & mActiveHistoryStates;
3388 if (mHistoryLastWritten.states != curStates) {
3389 // mActiveHistoryStates keeps track of which bits in .states are now being
3390 // forced to 0.
3391 int old = mActiveHistoryStates;
3392 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3393 writeAnyway |= old != mActiveHistoryStates;
3394 }
3395 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3396 & mActiveHistoryStates2;
3397 if (mHistoryLastWritten.states2 != curStates2) {
3398 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3399 // forced to 0.
3400 int old = mActiveHistoryStates2;
3401 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3402 writeAnyway |= old != mActiveHistoryStates2;
3403 }
3404
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003405 // Once we've reached the maximum number of items, we only
3406 // record changes to the battery level and the most interesting states.
3407 // Once we've reached the maximum maximum number of items, we only
3408 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003409 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003410 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003411 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003412 & HistoryItem.MOST_INTERESTING_STATES) == 0
3413 || ((mHistoryLastWritten.states2^cur.states2)
3414 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003415 return;
3416 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003417
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003418 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003419 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003420 }
3421
Adam Lesinski45489782016-12-15 23:45:17 -08003422 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003423 // The history is currently empty; we need it to start with a time stamp.
3424 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003425 if (recordResetDueToOverflow) {
3426 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3427 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003428 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3429 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003430 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003431 }
3432
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003433 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3434 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003435 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003436 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003437 }
3438 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3439 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003440 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003441 mHistoryLastWritten.states &= mActiveHistoryStates;
3442 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003443 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003444 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003445 cur.wakelockTag = null;
3446 cur.wakeReasonTag = null;
3447 cur.eventCode = HistoryItem.EVENT_NONE;
3448 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003449 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3450 + " now " + mHistoryBuffer.dataPosition()
3451 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003452 }
3453
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003454 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003455 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003456
Dianne Hackborn40c87252014-03-19 16:55:40 -07003457 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003458 if (mTrackRunningHistoryElapsedRealtime != 0) {
3459 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3460 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3461 if (diffUptime < (diffElapsed-20)) {
3462 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3463 mHistoryAddTmp.setTo(mHistoryLastWritten);
3464 mHistoryAddTmp.wakelockTag = null;
3465 mHistoryAddTmp.wakeReasonTag = null;
3466 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3467 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3468 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3469 }
3470 }
3471 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3472 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3473 mTrackRunningHistoryUptime = uptimeMs;
3474 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3475 }
3476
3477 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3478 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003479
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003480 if (!USE_OLD_HISTORY) {
3481 return;
3482 }
3483
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003484 if (!mHaveBatteryLevel || !mRecordingHistory) {
3485 return;
3486 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003487
3488 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003489 // and no states have since the last recorded entry changed and
3490 // are now resetting back to their original value, then just collapse
3491 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003492 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003493 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003494 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3495 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003496 // If the current is the same as the one before, then we no
3497 // longer need the entry.
3498 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003499 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003500 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003501 mHistoryLastEnd.next = null;
3502 mHistoryEnd.next = mHistoryCache;
3503 mHistoryCache = mHistoryEnd;
3504 mHistoryEnd = mHistoryLastEnd;
3505 mHistoryLastEnd = null;
3506 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003507 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3508 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003509 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003510 }
3511 return;
3512 }
3513
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003514 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003515 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003516
3517 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3518 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003519 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003520 }
3521
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003522 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3523 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003524 // record changes to the battery level and the most interesting states.
3525 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003526 // record changes to the battery level.
3527 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003528 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003529 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003530 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003531 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003532 return;
3533 }
3534 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003535
Dianne Hackborn40c87252014-03-19 16:55:40 -07003536 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003537 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003538
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003539 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003540 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003541 mHistoryCur.eventCode = code;
3542 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3543 mHistoryCur.eventTag.string = name;
3544 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003545 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003546 }
3547
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003548 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003549 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003550 if (rec != null) {
3551 mHistoryCache = rec.next;
3552 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003553 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003554 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003555 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003556
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003557 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003558 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003559
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003560 void addHistoryRecordLocked(HistoryItem rec) {
3561 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003562 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003563 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003564 if (mHistoryEnd != null) {
3565 mHistoryEnd.next = rec;
3566 mHistoryEnd = rec;
3567 } else {
3568 mHistory = mHistoryEnd = rec;
3569 }
3570 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003571
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003572 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003573 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003574 if (USE_OLD_HISTORY) {
3575 if (mHistory != null) {
3576 mHistoryEnd.next = mHistoryCache;
3577 mHistoryCache = mHistory;
3578 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3579 }
3580 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003581 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003582
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003583 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003584 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003585 mTrackRunningHistoryElapsedRealtime = 0;
3586 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003587
3588 mHistoryBuffer.setDataSize(0);
3589 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003590 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003591 mHistoryLastLastWritten.clear();
3592 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003593 mHistoryTagPool.clear();
3594 mNextHistoryTagIdx = 0;
3595 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003596 mHistoryBufferLastPos = -1;
3597 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003598 mActiveHistoryStates = 0xffffffff;
3599 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003600 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003601
Mike Mac2f518a2017-09-19 16:06:03 -07003602 public void updateTimeBasesLocked(boolean unplugged, int screenState, long uptime,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003603 long realtime) {
Mike Maa7724752017-10-10 15:29:25 -07003604 final boolean screenOff = !isScreenOn(screenState);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003605 final boolean updateOnBatteryTimeBase = unplugged != mOnBatteryTimeBase.isRunning();
3606 final boolean updateOnBatteryScreenOffTimeBase =
3607 (unplugged && screenOff) != mOnBatteryScreenOffTimeBase.isRunning();
Bookatz867c0d72017-03-07 18:23:42 -08003608
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003609 if (updateOnBatteryScreenOffTimeBase || updateOnBatteryTimeBase) {
3610 if (updateOnBatteryScreenOffTimeBase) {
3611 updateKernelWakelocksLocked();
3612 updateBatteryPropertiesLocked();
Bookatz867c0d72017-03-07 18:23:42 -08003613 }
Bookatz82b341172017-09-07 19:06:08 -07003614 // This if{} is only necessary due to SCREEN_OFF_RPM_STATS_ENABLED, which exists because
3615 // updateRpmStatsLocked is too slow to run each screen change. When the speed is
3616 // improved, remove the surrounding if{}.
3617 if (SCREEN_OFF_RPM_STATS_ENABLED || updateOnBatteryTimeBase) {
3618 updateRpmStatsLocked(); // if either OnBattery or OnBatteryScreenOff timebase changes.
3619 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003620 if (DEBUG_ENERGY_CPU) {
Mike Mac2f518a2017-09-19 16:06:03 -07003621 Slog.d(TAG, "Updating cpu time because screen is now "
3622 + Display.stateToString(screenState)
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003623 + " and battery is " + (unplugged ? "on" : "off"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003624 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -07003625 updateCpuTimeLocked();
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003626
3627 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Mike Mac2f518a2017-09-19 16:06:03 -07003628 if (updateOnBatteryTimeBase) {
3629 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3630 mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003631 }
Mike Mac2f518a2017-09-19 16:06:03 -07003632 }
3633 if (updateOnBatteryScreenOffTimeBase) {
3634 mOnBatteryScreenOffTimeBase.setRunning(unplugged && screenOff, uptime, realtime);
3635 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3636 mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003637 }
Bookatzc8c44962017-05-11 12:12:54 -07003638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003641
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003642 private void updateBatteryPropertiesLocked() {
3643 try {
3644 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3645 ServiceManager.getService("batteryproperties"));
3646 registrar.scheduleUpdate();
3647 } catch (RemoteException e) {
3648 // Ignore.
3649 }
3650 }
3651
Dianne Hackborn099bc622014-01-22 13:39:16 -08003652 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3653 mIsolatedUids.put(isolatedUid, appUid);
David Chen21582962017-11-01 17:32:46 -07003654 StatsLog.write(StatsLog.ISOLATED_UID_CHANGED, appUid, isolatedUid, 1);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003655 }
3656
Adam Lesinski61db88f2015-07-01 15:05:07 -07003657 /**
3658 * Schedules a read of the latest cpu times before removing the isolated UID.
3659 * @see #removeIsolatedUidLocked(int)
3660 */
3661 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003662 int curUid = mIsolatedUids.get(isolatedUid, -1);
3663 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003664 if (mExternalSync != null) {
3665 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3666 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003667 }
3668 }
3669
Adam Lesinski61db88f2015-07-01 15:05:07 -07003670 /**
3671 * This should only be called after the cpu times have been read.
3672 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3673 */
3674 public void removeIsolatedUidLocked(int isolatedUid) {
David Chen21582962017-11-01 17:32:46 -07003675 StatsLog.write(
3676 StatsLog.ISOLATED_UID_CHANGED, mIsolatedUids.get(isolatedUid, -1), isolatedUid, 0);
3677 mIsolatedUids.delete(isolatedUid);
3678 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3679 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Adam Lesinski61db88f2015-07-01 15:05:07 -07003680 }
3681
Dianne Hackborn099bc622014-01-22 13:39:16 -08003682 public int mapUid(int uid) {
3683 int isolated = mIsolatedUids.get(uid, -1);
3684 return isolated > 0 ? isolated : uid;
3685 }
3686
3687 public void noteEventLocked(int code, String name, int uid) {
3688 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003689 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3690 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003691 }
Joe Onoratoabded112016-02-08 16:49:39 -08003692 final long elapsedRealtime = mClocks.elapsedRealtime();
3693 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003694 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003695 }
3696
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003697 boolean ensureStartClockTime(final long currentTime) {
3698 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3699 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3700 // If the start clock time has changed by more than a year, then presumably
3701 // the previous time was completely bogus. So we are going to figure out a
3702 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003703 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003704 return true;
3705 }
3706 return false;
3707 }
3708
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003709 public void noteCurrentTimeChangedLocked() {
3710 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003711 final long elapsedRealtime = mClocks.elapsedRealtime();
3712 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003713 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003714 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003715 }
3716
Dianne Hackborn61659e52014-07-09 16:13:01 -07003717 public void noteProcessStartLocked(String name, int uid) {
3718 uid = mapUid(uid);
3719 if (isOnBattery()) {
3720 Uid u = getUidStatsLocked(uid);
3721 u.getProcessStatsLocked(name).incStartsLocked();
3722 }
3723 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3724 return;
3725 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003726 if (!mRecordAllHistory) {
3727 return;
3728 }
Joe Onoratoabded112016-02-08 16:49:39 -08003729 final long elapsedRealtime = mClocks.elapsedRealtime();
3730 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003731 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3732 }
3733
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003734 public void noteProcessCrashLocked(String name, int uid) {
3735 uid = mapUid(uid);
3736 if (isOnBattery()) {
3737 Uid u = getUidStatsLocked(uid);
3738 u.getProcessStatsLocked(name).incNumCrashesLocked();
3739 }
3740 }
3741
3742 public void noteProcessAnrLocked(String name, int uid) {
3743 uid = mapUid(uid);
3744 if (isOnBattery()) {
3745 Uid u = getUidStatsLocked(uid);
3746 u.getProcessStatsLocked(name).incNumAnrsLocked();
3747 }
3748 }
3749
Dianne Hackborna8d10942015-11-19 17:55:19 -08003750 public void noteUidProcessStateLocked(int uid, int state) {
Amith Yamasanifd3caf62017-07-26 11:48:35 -07003751 int parentUid = mapUid(uid);
3752 if (uid != parentUid) {
3753 // Isolated UIDs process state is already rolled up into parent, so no need to track
3754 // Otherwise the parent's process state will get downgraded incorrectly
3755 return;
3756 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08003757 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003758 }
3759
3760 public void noteProcessFinishLocked(String name, int uid) {
3761 uid = mapUid(uid);
3762 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3763 return;
3764 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003765 if (!mRecordAllHistory) {
3766 return;
3767 }
Joe Onoratoabded112016-02-08 16:49:39 -08003768 final long elapsedRealtime = mClocks.elapsedRealtime();
3769 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003770 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003771 }
3772
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003773 public void noteSyncStartLocked(String name, int uid) {
3774 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003775 final long elapsedRealtime = mClocks.elapsedRealtime();
3776 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003777 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3778 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3779 return;
3780 }
3781 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3782 }
3783
3784 public void noteSyncFinishLocked(String name, int uid) {
3785 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003786 final long elapsedRealtime = mClocks.elapsedRealtime();
3787 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003788 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3789 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3790 return;
3791 }
3792 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3793 }
3794
3795 public void noteJobStartLocked(String name, int uid) {
3796 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003797 final long elapsedRealtime = mClocks.elapsedRealtime();
3798 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003799 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3800 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3801 return;
3802 }
3803 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3804 }
3805
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003806 public void noteJobFinishLocked(String name, int uid, int stopReason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003807 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003808 final long elapsedRealtime = mClocks.elapsedRealtime();
3809 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003810 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime, stopReason);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003811 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3812 return;
3813 }
3814 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3815 }
3816
Dianne Hackborn1e383822015-04-10 14:02:33 -07003817 public void noteAlarmStartLocked(String name, int uid) {
3818 if (!mRecordAllHistory) {
3819 return;
3820 }
3821 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003822 final long elapsedRealtime = mClocks.elapsedRealtime();
3823 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003824 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3825 return;
3826 }
3827 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3828 }
3829
3830 public void noteAlarmFinishLocked(String name, int uid) {
3831 if (!mRecordAllHistory) {
3832 return;
3833 }
3834 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003835 final long elapsedRealtime = mClocks.elapsedRealtime();
3836 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003837 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3838 return;
3839 }
3840 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3841 }
3842
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003843 private void requestWakelockCpuUpdate() {
3844 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3845 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3846 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3847 }
3848 }
3849
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003850 private void requestImmediateCpuUpdate() {
3851 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3852 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3853 }
3854
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003855 public void setRecordAllHistoryLocked(boolean enabled) {
3856 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003857 if (!enabled) {
3858 // Clear out any existing state.
3859 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003860 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003861 // Record the currently running processes as stopping, now that we are no
3862 // longer tracking them.
3863 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3864 HistoryItem.EVENT_PROC);
3865 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003866 long mSecRealtime = mClocks.elapsedRealtime();
3867 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003868 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3869 SparseIntArray uids = ent.getValue();
3870 for (int j=0; j<uids.size(); j++) {
3871 addHistoryEventLocked(mSecRealtime, mSecUptime,
3872 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3873 }
3874 }
3875 }
3876 } else {
3877 // Record the currently running processes as starting, now that we are tracking them.
3878 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3879 HistoryItem.EVENT_PROC);
3880 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003881 long mSecRealtime = mClocks.elapsedRealtime();
3882 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003883 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3884 SparseIntArray uids = ent.getValue();
3885 for (int j=0; j<uids.size(); j++) {
3886 addHistoryEventLocked(mSecRealtime, mSecUptime,
3887 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3888 }
3889 }
3890 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003891 }
3892 }
3893
Dianne Hackborn9a755432014-05-15 17:05:22 -07003894 public void setNoAutoReset(boolean enabled) {
3895 mNoAutoReset = enabled;
3896 }
3897
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003898 public void setPretendScreenOff(boolean pretendScreenOff) {
Mike Mac2f518a2017-09-19 16:06:03 -07003899 if (mPretendScreenOff != pretendScreenOff) {
3900 mPretendScreenOff = pretendScreenOff;
3901 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3902 }
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003903 }
3904
Dianne Hackborn9a755432014-05-15 17:05:22 -07003905 private String mInitialAcquireWakeName;
3906 private int mInitialAcquireWakeUid = -1;
3907
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003908 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003909 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003910 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003911 if (type == WAKE_TYPE_PARTIAL) {
3912 // Only care about partial wake locks, since full wake locks
3913 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003914 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003915 if (historyName == null) {
3916 historyName = name;
3917 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003918 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003919 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3920 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003921 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003922 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003923 }
3924 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003925 if (mWakeLockNesting == 0) {
3926 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3927 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3928 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003929 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003930 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003931 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003932 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003933 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003934 } else if (!mWakeLockImportant && !unimportantForLogging
3935 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003936 if (mHistoryLastWritten.wakelockTag != null) {
3937 // We'll try to update the last tag.
3938 mHistoryLastWritten.wakelockTag = null;
3939 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003940 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003941 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003942 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003943 }
3944 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003945 }
3946 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003947 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003948 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003949 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3950 // We only update the cpu time when a wake lock is acquired if the screen is off.
3951 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3952 if (DEBUG_ENERGY_CPU) {
3953 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3954 }
3955 requestWakelockCpuUpdate();
3956 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003957 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003958 }
3959 }
3960
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003961 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3962 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003963 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003964 if (type == WAKE_TYPE_PARTIAL) {
3965 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003966 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003967 if (historyName == null) {
3968 historyName = name;
3969 }
3970 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3971 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003972 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003973 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003974 }
3975 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003976 if (mWakeLockNesting == 0) {
3977 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3978 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3979 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003980 mInitialAcquireWakeName = null;
3981 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003982 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003983 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003984 }
3985 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003986 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3987 if (DEBUG_ENERGY_CPU) {
3988 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3989 }
3990 requestWakelockCpuUpdate();
3991 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003992 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003993 }
3994 }
3995
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003996 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3997 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003998 final long elapsedRealtime = mClocks.elapsedRealtime();
3999 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004000 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004001 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004002 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07004003 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004004 }
4005 }
4006
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004007 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
4008 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004009 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08004010 final long elapsedRealtime = mClocks.elapsedRealtime();
4011 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004012 // For correct semantics, we start the need worksources first, so that we won't
4013 // make inappropriate history items as if all wake locks went away and new ones
4014 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07004015 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004016 for (int i=0; i<NN; i++) {
4017 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07004018 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004019 }
4020 final int NO = ws.size();
4021 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004022 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004023 }
4024 }
4025
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004026 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
4027 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08004028 final long elapsedRealtime = mClocks.elapsedRealtime();
4029 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004030 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004031 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004032 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004033 }
4034 }
4035
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004036 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
4037 uid = mapUid(uid);
4038 final long elapsedRealtime = mClocks.elapsedRealtime();
4039 final long uptime = mClocks.uptimeMillis();
4040 if (historyName == null) {
4041 historyName = name;
4042 }
4043 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
4044 0)) {
4045 return;
4046 }
4047 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
4048 historyName, uid);
Bookatzc1a050a2017-10-10 15:49:28 -07004049 StatsLog.write(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED, uid, name, historyName, 1);
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004050 }
4051
4052 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
4053 uid = mapUid(uid);
4054 final long elapsedRealtime = mClocks.elapsedRealtime();
4055 final long uptime = mClocks.uptimeMillis();
4056 if (historyName == null) {
4057 historyName = name;
4058 }
4059 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
4060 0)) {
4061 return;
4062 }
4063 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
4064 historyName, uid);
Bookatzc1a050a2017-10-10 15:49:28 -07004065 StatsLog.write(StatsLog.LONG_PARTIAL_WAKELOCK_STATE_CHANGED, uid, name, historyName, 0);
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004066 }
4067
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004068 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
4069 if (mLastWakeupReason != null) {
4070 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004071 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07004072 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Bookatz8c6571b2017-10-24 15:04:41 -07004073 StatsLog.write(StatsLog.KERNEL_WAKEUP_REPORTED, mLastWakeupReason, deltaUptime * 1000);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004074 mLastWakeupReason = null;
4075 }
4076 }
4077
4078 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08004079 final long elapsedRealtime = mClocks.elapsedRealtime();
4080 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004081 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004082 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004083 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004084 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
4085 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004086 mHistoryCur.wakeReasonTag.uid = 0;
4087 mLastWakeupReason = reason;
4088 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004089 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004090 }
4091
Adam Lesinski72478f02015-06-17 15:39:43 -07004092 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004093 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07004094 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004095 }
4096
Adam Lesinski72478f02015-06-17 15:39:43 -07004097 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
4098 int statSystemTime, int statIOWaitTime, int statIrqTime,
4099 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004100 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
4101 + " user=" + statUserTime + " sys=" + statSystemTime
4102 + " io=" + statIOWaitTime + " irq=" + statIrqTime
4103 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
4104 mCurStepCpuUserTime += totalUTime;
4105 mCurStepCpuSystemTime += totalSTime;
4106 mCurStepStatUserTime += statUserTime;
4107 mCurStepStatSystemTime += statSystemTime;
4108 mCurStepStatIOWaitTime += statIOWaitTime;
4109 mCurStepStatIrqTime += statIrqTime;
4110 mCurStepStatSoftIrqTime += statSoftIrqTime;
4111 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004112 }
4113
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004114 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004115 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004116 Uid u = mUidStats.get(uid);
4117 if (u != null) {
4118 u.mPids.remove(pid);
4119 }
4120 }
4121
4122 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004123 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004124 Uid u = mUidStats.get(uid);
4125 if (u != null) {
4126 Uid.Pid p = u.mPids.get(pid);
4127 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004128 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004129 }
4130 }
4131 return 0;
4132 }
4133
Dianne Hackborn287952c2010-09-22 22:34:31 -07004134 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004135 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004136 Uid u = mUidStats.get(uid);
4137 if (u != null) {
4138 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
4139 }
4140 }
4141
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004142 int mSensorNesting;
4143
4144 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004145 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004146 final long elapsedRealtime = mClocks.elapsedRealtime();
4147 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004148 if (mSensorNesting == 0) {
4149 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
4150 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
4151 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004152 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004153 }
4154 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004155 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004156 }
4157
4158 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004159 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004160 final long elapsedRealtime = mClocks.elapsedRealtime();
4161 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004162 mSensorNesting--;
4163 if (mSensorNesting == 0) {
4164 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4165 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4166 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004167 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004168 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004169 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004170 }
4171
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004172 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004173
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004174 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004175 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004176 final long elapsedRealtime = mClocks.elapsedRealtime();
4177 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004178 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004179 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004180 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4181 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004182 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004183 }
4184 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004185 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004187
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004188 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004189 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004190 final long elapsedRealtime = mClocks.elapsedRealtime();
4191 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004192 mGpsNesting--;
4193 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004194 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004195 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4196 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004197 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004198 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004199 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004201
Jeff Browne95c3cd2014-05-02 16:59:26 -07004202 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004203 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004204
4205 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4206 // original 4 are mapped to one of the originals.
4207 if (state > MAX_TRACKED_SCREEN_STATE) {
4208 switch (state) {
4209 case Display.STATE_VR:
4210 state = Display.STATE_ON;
4211 break;
4212 default:
4213 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4214 break;
4215 }
4216 }
4217
Jeff Browne95c3cd2014-05-02 16:59:26 -07004218 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004219 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004220 final int oldState = mScreenState;
4221 mScreenState = state;
4222 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4223 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004224
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004225 if (state != Display.STATE_UNKNOWN) {
4226 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004227 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4228 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4229 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004230 } else {
4231 Slog.wtf(TAG, "Unexpected screen state: " + state);
4232 }
4233 }
4234
Mike Mac2f518a2017-09-19 16:06:03 -07004235 final long elapsedRealtime = mClocks.elapsedRealtime();
4236 final long uptime = mClocks.uptimeMillis();
4237
4238 boolean updateHistory = false;
4239 if (isScreenDoze(state)) {
4240 mHistoryCur.states |= HistoryItem.STATE_SCREEN_DOZE_FLAG;
4241 mScreenDozeTimer.startRunningLocked(elapsedRealtime);
4242 updateHistory = true;
4243 } else if (isScreenDoze(oldState)) {
4244 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_DOZE_FLAG;
4245 mScreenDozeTimer.stopRunningLocked(elapsedRealtime);
4246 updateHistory = true;
4247 }
4248 if (isScreenOn(state)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004249 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4250 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4251 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004252 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4253 if (mScreenBrightnessBin >= 0) {
4254 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4255 }
Mike Mac2f518a2017-09-19 16:06:03 -07004256 updateHistory = true;
4257 } else if (isScreenOn(oldState)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004258 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4259 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4260 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004261 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4262 if (mScreenBrightnessBin >= 0) {
4263 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4264 }
Mike Mac2f518a2017-09-19 16:06:03 -07004265 updateHistory = true;
4266 }
4267 if (updateHistory) {
4268 if (DEBUG_HISTORY) Slog.v(TAG, "Screen state to: "
4269 + Display.stateToString(state));
4270 addHistoryRecordLocked(elapsedRealtime, uptime);
4271 }
4272 if (isScreenOn(state)) {
4273 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
4274 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
4275 // Fake a wake lock, so we consider the device waked as long as the screen is on.
4276 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
4277 elapsedRealtime, uptime);
4278 } else if (isScreenOn(oldState)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004279 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004280 elapsedRealtime, uptime);
Mike Mac2f518a2017-09-19 16:06:03 -07004281 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
Joe Onoratoabded112016-02-08 16:49:39 -08004282 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Mike Mac2f518a2017-09-19 16:06:03 -07004283 }
4284 // Update discharge amounts.
4285 if (mOnBatteryInternal) {
4286 updateDischargeScreenLevelsLocked(oldState, state);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004287 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004288 }
4289 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004290
Dianne Hackborn617f8772009-03-31 15:04:46 -07004291 public void noteScreenBrightnessLocked(int brightness) {
4292 // Bin the brightness.
4293 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4294 if (bin < 0) bin = 0;
4295 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4296 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004297 final long elapsedRealtime = mClocks.elapsedRealtime();
4298 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004299 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4300 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004301 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4302 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004303 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004304 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004305 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004306 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004307 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004308 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004309 }
4310 mScreenBrightnessBin = bin;
4311 }
4312 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004313
Dianne Hackborn617f8772009-03-31 15:04:46 -07004314 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004315 if (mOnBatteryInternal) {
4316 uid = mapUid(uid);
4317 getUidStatsLocked(uid).noteUserActivityLocked(event);
4318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004320
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004321 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004322 final long elapsedRealtime = mClocks.elapsedRealtime();
4323 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004324 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4325 reason, reasonUid);
4326 }
4327
Jeff Browne95c3cd2014-05-02 16:59:26 -07004328 public void noteInteractiveLocked(boolean interactive) {
4329 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004330 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004331 mInteractive = interactive;
4332 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4333 if (interactive) {
4334 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4335 } else {
4336 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4337 }
4338 }
4339 }
4340
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004341 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004342 final long elapsedRealtime = mClocks.elapsedRealtime();
4343 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004344 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4345 extra, type);
4346 mNumConnectivityChange++;
4347 }
4348
Adam Lesinski5f056f62016-07-14 16:56:08 -07004349 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4350 final long uptimeMillis, int uid) {
4351 uid = mapUid(uid);
4352 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4353 uid);
4354 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4355 }
4356
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004357 /**
4358 * Updates the radio power state and returns true if an external stats collection should occur.
4359 */
4360 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004361 final long elapsedRealtime = mClocks.elapsedRealtime();
4362 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004363 if (mMobileRadioPowerState != powerState) {
4364 long realElapsedRealtimeMs;
4365 final boolean active =
4366 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4367 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4368 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004369 if (uid > 0) {
4370 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4371 }
4372
Adam Lesinski9acfd812016-04-19 18:29:50 -07004373 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004374 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4375 } else {
4376 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004377 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004378 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4379 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4380 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004381 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004382 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4383 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4384 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004385 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004386 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4387 }
4388 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4389 + Integer.toHexString(mHistoryCur.states));
4390 addHistoryRecordLocked(elapsedRealtime, uptime);
4391 mMobileRadioPowerState = powerState;
4392 if (active) {
4393 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4394 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4395 } else {
4396 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004397 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004398 // Tell the caller to collect radio network/power stats.
4399 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004400 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004401 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004402 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004403 }
4404
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004405 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004406 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004407 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4408 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4409 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004410 final long elapsedRealtime = mClocks.elapsedRealtime();
4411 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004412 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004413 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004414 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4415 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004416 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004417 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004418 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004419 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4420 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004421 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004422 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004423 }
4424 addHistoryRecordLocked(elapsedRealtime, uptime);
Bookatzc1a050a2017-10-10 15:49:28 -07004425 StatsLog.write(StatsLog.BATTERY_SAVER_MODE_STATE_CHANGED, enabled ? 1 : 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004426 }
4427 }
4428
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004429 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004430 final long elapsedRealtime = mClocks.elapsedRealtime();
4431 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004432 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004433 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004434 // We don't go out of general idling mode until explicitly taken out of
4435 // device idle through going active or significant motion.
4436 nowIdling = true;
4437 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4439 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4440 // We don't go out of general light idling mode until explicitly taken out of
4441 // device idle through going active or significant motion.
4442 nowLightIdling = true;
4443 }
4444 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4445 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4446 activeReason, activeUid);
4447 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004448 if (mDeviceIdling != nowIdling) {
4449 mDeviceIdling = nowIdling;
4450 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4451 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4452 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004453 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004454 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4455 } else {
4456 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4457 }
4458 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004459 if (mDeviceLightIdling != nowLightIdling) {
4460 mDeviceLightIdling = nowLightIdling;
4461 if (nowLightIdling) {
4462 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004463 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004464 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004465 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004466 }
4467 if (mDeviceIdleMode != mode) {
4468 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4469 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4470 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4471 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004472 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004473 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4474 mLastIdleTimeStart = elapsedRealtime;
4475 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4476 if (lastDuration > mLongestLightIdleTime) {
4477 mLongestLightIdleTime = lastDuration;
4478 }
4479 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004480 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004481 if (lastDuration > mLongestFullIdleTime) {
4482 mLongestFullIdleTime = lastDuration;
4483 }
4484 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4485 }
4486 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4487 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004488 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004489 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4490 }
4491 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004492 }
4493 }
4494
4495 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004496 final long elapsedRealtime = mClocks.elapsedRealtime();
4497 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004498 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4499 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004500 PackageChange pc = new PackageChange();
4501 pc.mPackageName = pkgName;
4502 pc.mUpdate = true;
4503 pc.mVersionCode = versionCode;
4504 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004505 }
4506
4507 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004508 final long elapsedRealtime = mClocks.elapsedRealtime();
4509 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004510 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4511 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004512 PackageChange pc = new PackageChange();
4513 pc.mPackageName = pkgName;
4514 pc.mUpdate = true;
4515 addPackageChange(pc);
4516 }
4517
4518 private void addPackageChange(PackageChange pc) {
4519 if (mDailyPackageChanges == null) {
4520 mDailyPackageChanges = new ArrayList<>();
4521 }
4522 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004523 }
4524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 public void notePhoneOnLocked() {
4526 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004527 final long elapsedRealtime = mClocks.elapsedRealtime();
4528 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004529 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004530 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4531 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004532 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004534 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 }
4536 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 public void notePhoneOffLocked() {
4539 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004540 final long elapsedRealtime = mClocks.elapsedRealtime();
4541 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004542 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004543 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4544 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004545 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004547 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 }
4549 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004550
Dianne Hackborn3251b902014-06-20 14:40:53 -07004551 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004552 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004553 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004554 if (i == except) {
4555 continue;
4556 }
4557 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004558 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004559 }
4560 }
4561 }
4562
Dianne Hackborne4a59512010-12-07 11:08:07 -08004563 private int fixPhoneServiceState(int state, int signalBin) {
4564 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4565 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4566 // to infer that we are scanning from other data.
4567 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004568 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004569 state = ServiceState.STATE_IN_SERVICE;
4570 }
4571 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004572
Dianne Hackborne4a59512010-12-07 11:08:07 -08004573 return state;
4574 }
4575
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004576 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004577 boolean scanning = false;
4578 boolean newHistory = false;
4579
4580 mPhoneServiceStateRaw = state;
4581 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004582 mPhoneSignalStrengthBinRaw = strengthBin;
4583
Joe Onoratoabded112016-02-08 16:49:39 -08004584 final long elapsedRealtime = mClocks.elapsedRealtime();
4585 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004586
4587 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4588 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4589 // to infer that we are scanning from other data.
4590 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004591 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004592 state = ServiceState.STATE_IN_SERVICE;
4593 }
4594 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004595
4596 // If the phone is powered off, stop all timers.
4597 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004598 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004599
Dianne Hackborne4a59512010-12-07 11:08:07 -08004600 // If we are in service, make sure the correct signal string timer is running.
4601 } else if (state == ServiceState.STATE_IN_SERVICE) {
4602 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004603
4604 // If we're out of service, we are in the lowest signal strength
4605 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004606 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004607 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004608 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004609 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004610 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004611 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004612 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4613 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004614 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004615 }
4616 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004617
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004618 if (!scanning) {
4619 // If we are no longer scanning, then stop the scanning timer.
4620 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4621 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4622 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4623 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004624 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004625 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004626 }
4627 }
4628
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004629 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004630 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4631 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004632 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004633 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004634 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004635 mPhoneServiceState = state;
4636 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004637
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004638 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004639 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004640 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004641 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004642 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004643 if (strengthBin >= 0) {
4644 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004645 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004646 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004647 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4648 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004649 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004650 + Integer.toHexString(mHistoryCur.states));
4651 newHistory = true;
Bookatze5885242017-10-24 20:10:31 -07004652 StatsLog.write(StatsLog.PHONE_SIGNAL_STRENGTH_CHANGED, strengthBin);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004653 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004654 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004655 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004656 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004657 }
4658
4659 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004660 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004661 }
4662 }
4663
4664 /**
4665 * Telephony stack updates the phone state.
4666 * @param state phone state from ServiceState.getState()
4667 */
4668 public void notePhoneStateLocked(int state, int simState) {
4669 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004670 }
4671
Wink Savillee9b06d72009-05-18 21:47:50 -07004672 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004673 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004674 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004675 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004676 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004677
Dianne Hackborn627bba72009-03-24 22:32:56 -07004678 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4679 int bin = DATA_CONNECTION_NONE;
4680 if (hasData) {
4681 switch (dataType) {
4682 case TelephonyManager.NETWORK_TYPE_EDGE:
4683 bin = DATA_CONNECTION_EDGE;
4684 break;
4685 case TelephonyManager.NETWORK_TYPE_GPRS:
4686 bin = DATA_CONNECTION_GPRS;
4687 break;
4688 case TelephonyManager.NETWORK_TYPE_UMTS:
4689 bin = DATA_CONNECTION_UMTS;
4690 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004691 case TelephonyManager.NETWORK_TYPE_CDMA:
4692 bin = DATA_CONNECTION_CDMA;
4693 break;
4694 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4695 bin = DATA_CONNECTION_EVDO_0;
4696 break;
4697 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4698 bin = DATA_CONNECTION_EVDO_A;
4699 break;
4700 case TelephonyManager.NETWORK_TYPE_1xRTT:
4701 bin = DATA_CONNECTION_1xRTT;
4702 break;
4703 case TelephonyManager.NETWORK_TYPE_HSDPA:
4704 bin = DATA_CONNECTION_HSDPA;
4705 break;
4706 case TelephonyManager.NETWORK_TYPE_HSUPA:
4707 bin = DATA_CONNECTION_HSUPA;
4708 break;
4709 case TelephonyManager.NETWORK_TYPE_HSPA:
4710 bin = DATA_CONNECTION_HSPA;
4711 break;
4712 case TelephonyManager.NETWORK_TYPE_IDEN:
4713 bin = DATA_CONNECTION_IDEN;
4714 break;
4715 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4716 bin = DATA_CONNECTION_EVDO_B;
4717 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004718 case TelephonyManager.NETWORK_TYPE_LTE:
4719 bin = DATA_CONNECTION_LTE;
4720 break;
4721 case TelephonyManager.NETWORK_TYPE_EHRPD:
4722 bin = DATA_CONNECTION_EHRPD;
4723 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004724 case TelephonyManager.NETWORK_TYPE_HSPAP:
4725 bin = DATA_CONNECTION_HSPAP;
4726 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004727 default:
4728 bin = DATA_CONNECTION_OTHER;
4729 break;
4730 }
4731 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004732 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004733 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004734 final long elapsedRealtime = mClocks.elapsedRealtime();
4735 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004736 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4737 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004738 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4739 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004740 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004741 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004742 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004743 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004744 }
4745 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004746 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004747 }
4748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004749
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004750 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004751 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004752 final long elapsedRealtime = mClocks.elapsedRealtime();
4753 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004754 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004755 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4756 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004757 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004758 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004759 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004760 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004761 }
4762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004763
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004764 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004765 final long elapsedRealtime = mClocks.elapsedRealtime();
4766 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004767 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004768 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004769 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4770 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004771 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004772 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004773 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004774 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004775 }
4776 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004777
4778 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004779 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004780 final long elapsedRealtime = mClocks.elapsedRealtime();
4781 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004782 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004783 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004784 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4785 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004786 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004787 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004788 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004789 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004790 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004791 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004792
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004793 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004794 if (mAudioOnNesting == 0) {
4795 return;
4796 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004797 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004798 final long elapsedRealtime = mClocks.elapsedRealtime();
4799 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004800 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004801 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004802 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4803 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004804 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004805 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004806 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004808 }
4809
4810 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004811 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004812 final long elapsedRealtime = mClocks.elapsedRealtime();
4813 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004814 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004815 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004816 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4817 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004818 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004819 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004820 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004821 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004822 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004823 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004824
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004825 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004826 if (mVideoOnNesting == 0) {
4827 return;
4828 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004829 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004830 final long elapsedRealtime = mClocks.elapsedRealtime();
4831 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004832 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004833 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004834 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4835 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004836 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004837 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004838 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004839 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004840 }
4841
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004842 public void noteResetAudioLocked() {
4843 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004844 final long elapsedRealtime = mClocks.elapsedRealtime();
4845 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004846 mAudioOnNesting = 0;
4847 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4848 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4849 + Integer.toHexString(mHistoryCur.states));
4850 addHistoryRecordLocked(elapsedRealtime, uptime);
4851 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4852 for (int i=0; i<mUidStats.size(); i++) {
4853 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4854 uid.noteResetAudioLocked(elapsedRealtime);
4855 }
4856 }
4857 }
4858
4859 public void noteResetVideoLocked() {
4860 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004861 final long elapsedRealtime = mClocks.elapsedRealtime();
4862 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004863 mAudioOnNesting = 0;
4864 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4865 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4866 + Integer.toHexString(mHistoryCur.states));
4867 addHistoryRecordLocked(elapsedRealtime, uptime);
4868 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4869 for (int i=0; i<mUidStats.size(); i++) {
4870 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4871 uid.noteResetVideoLocked(elapsedRealtime);
4872 }
4873 }
4874 }
4875
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004876 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004877 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004878 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004879 }
4880
4881 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004882 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004883 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004884 }
4885
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004886 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004887 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004888 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4889 }
4890
4891 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004892 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004893 getUidStatsLocked(uid).noteVibratorOffLocked();
4894 }
4895
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004896 public void noteFlashlightOnLocked(int uid) {
4897 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004898 final long elapsedRealtime = mClocks.elapsedRealtime();
4899 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004900 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004901 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4902 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004903 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004904 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004905 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4906 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004907 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4908 }
4909
4910 public void noteFlashlightOffLocked(int uid) {
4911 if (mFlashlightOnNesting == 0) {
4912 return;
4913 }
4914 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004915 final long elapsedRealtime = mClocks.elapsedRealtime();
4916 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004917 if (--mFlashlightOnNesting == 0) {
4918 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4919 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4920 + Integer.toHexString(mHistoryCur.states2));
4921 addHistoryRecordLocked(elapsedRealtime, uptime);
4922 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4923 }
4924 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4925 }
4926
4927 public void noteCameraOnLocked(int uid) {
4928 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004929 final long elapsedRealtime = mClocks.elapsedRealtime();
4930 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004931 if (mCameraOnNesting++ == 0) {
4932 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4933 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4934 + Integer.toHexString(mHistoryCur.states2));
4935 addHistoryRecordLocked(elapsedRealtime, uptime);
4936 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4937 }
4938 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4939 }
4940
4941 public void noteCameraOffLocked(int uid) {
4942 if (mCameraOnNesting == 0) {
4943 return;
4944 }
4945 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004946 final long elapsedRealtime = mClocks.elapsedRealtime();
4947 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004948 if (--mCameraOnNesting == 0) {
4949 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4950 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4951 + Integer.toHexString(mHistoryCur.states2));
4952 addHistoryRecordLocked(elapsedRealtime, uptime);
4953 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4954 }
4955 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4956 }
4957
4958 public void noteResetCameraLocked() {
4959 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004960 final long elapsedRealtime = mClocks.elapsedRealtime();
4961 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004962 mCameraOnNesting = 0;
4963 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4964 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4965 + Integer.toHexString(mHistoryCur.states2));
4966 addHistoryRecordLocked(elapsedRealtime, uptime);
4967 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4968 for (int i=0; i<mUidStats.size(); i++) {
4969 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4970 uid.noteResetCameraLocked(elapsedRealtime);
4971 }
4972 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004973 }
4974
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004975 public void noteResetFlashlightLocked() {
4976 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004977 final long elapsedRealtime = mClocks.elapsedRealtime();
4978 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004979 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004980 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4981 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004982 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004983 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004984 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4985 for (int i=0; i<mUidStats.size(); i++) {
4986 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4987 uid.noteResetFlashlightLocked(elapsedRealtime);
4988 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004989 }
4990 }
4991
Bookatzb1f04f32017-05-19 13:57:32 -07004992 private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004993 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004994 final long elapsedRealtime = mClocks.elapsedRealtime();
4995 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004996 if (mBluetoothScanNesting == 0) {
4997 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4998 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4999 + Integer.toHexString(mHistoryCur.states2));
5000 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07005001 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005002 }
5003 mBluetoothScanNesting++;
Bookatzb1f04f32017-05-19 13:57:32 -07005004 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005005 }
5006
Bookatzb1f04f32017-05-19 13:57:32 -07005007 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005008 final int N = ws.size();
5009 for (int i = 0; i < N; i++) {
Bookatzb1f04f32017-05-19 13:57:32 -07005010 noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005011 }
5012 }
5013
Bookatz94c5a312017-07-11 16:49:17 -07005014 private void noteBluetoothScanStoppedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005015 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08005016 final long elapsedRealtime = mClocks.elapsedRealtime();
5017 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005018 mBluetoothScanNesting--;
5019 if (mBluetoothScanNesting == 0) {
5020 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5021 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
5022 + Integer.toHexString(mHistoryCur.states2));
5023 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07005024 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005025 }
Bookatz94c5a312017-07-11 16:49:17 -07005026 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005027 }
5028
Bookatz94c5a312017-07-11 16:49:17 -07005029 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005030 final int N = ws.size();
5031 for (int i = 0; i < N; i++) {
Bookatz94c5a312017-07-11 16:49:17 -07005032 noteBluetoothScanStoppedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005033 }
5034 }
5035
5036 public void noteResetBluetoothScanLocked() {
5037 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005038 final long elapsedRealtime = mClocks.elapsedRealtime();
5039 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005040 mBluetoothScanNesting = 0;
5041 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5042 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
5043 + Integer.toHexString(mHistoryCur.states2));
5044 addHistoryRecordLocked(elapsedRealtime, uptime);
5045 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
5046 for (int i=0; i<mUidStats.size(); i++) {
5047 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5048 uid.noteResetBluetoothScanLocked(elapsedRealtime);
5049 }
5050 }
5051 }
5052
Bookatz4ebc0642017-05-11 12:21:19 -07005053 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07005054 final int N = ws.size();
5055 for (int i = 0; i < N; i++) {
5056 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07005057 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07005058 }
5059 }
5060
Adam Lesinski5f056f62016-07-14 16:56:08 -07005061 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
5062 final long uptimeMillis, int uid) {
5063 uid = mapUid(uid);
5064 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
5065 uid);
5066 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
5067 }
5068
5069 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005070 final long elapsedRealtime = mClocks.elapsedRealtime();
5071 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005072 if (mWifiRadioPowerState != powerState) {
5073 final boolean active =
5074 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
5075 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
5076 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07005077 if (uid > 0) {
5078 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
5079 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005080 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
5081 } else {
5082 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
5083 }
5084 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
5085 + Integer.toHexString(mHistoryCur.states));
5086 addHistoryRecordLocked(elapsedRealtime, uptime);
5087 mWifiRadioPowerState = powerState;
5088 }
5089 }
5090
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005091 public void noteWifiRunningLocked(WorkSource ws) {
5092 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005093 final long elapsedRealtime = mClocks.elapsedRealtime();
5094 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005095 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005096 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
5097 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005098 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005099 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005100 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005101 int N = ws.size();
5102 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005103 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005104 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005105 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005106 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005107 } else {
5108 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005109 }
5110 }
5111
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005112 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
5113 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005114 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005115 int N = oldWs.size();
5116 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005117 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005118 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005119 }
5120 N = newWs.size();
5121 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005122 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005123 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005124 }
5125 } else {
5126 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
5127 }
5128 }
5129
5130 public void noteWifiStoppedLocked(WorkSource ws) {
5131 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005132 final long elapsedRealtime = mClocks.elapsedRealtime();
5133 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005134 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005135 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
5136 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005137 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005138 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005139 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005140 int N = ws.size();
5141 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005142 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005143 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005144 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005145 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005146 } else {
5147 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005148 }
5149 }
5150
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005151 public void noteWifiStateLocked(int wifiState, String accessPoint) {
5152 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
5153 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005154 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005155 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005156 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005157 }
5158 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005159 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005160 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005161 }
5162 }
5163
Dianne Hackborn3251b902014-06-20 14:40:53 -07005164 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
5165 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5166 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005167 final long elapsedRealtime = mClocks.elapsedRealtime();
5168 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005169 if (mWifiSupplState >= 0) {
5170 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5171 }
5172 mWifiSupplState = supplState;
5173 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5174 mHistoryCur.states2 =
5175 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5176 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5177 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5178 + Integer.toHexString(mHistoryCur.states2));
5179 addHistoryRecordLocked(elapsedRealtime, uptime);
5180 }
5181 }
5182
5183 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005184 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005185 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5186 if (i == except) {
5187 continue;
5188 }
5189 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5190 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5191 }
5192 }
5193 }
5194
5195 public void noteWifiRssiChangedLocked(int newRssi) {
5196 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5197 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5198 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005199 final long elapsedRealtime = mClocks.elapsedRealtime();
5200 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005201 if (mWifiSignalStrengthBin >= 0) {
5202 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5203 elapsedRealtime);
5204 }
5205 if (strengthBin >= 0) {
5206 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5207 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Bookatze5885242017-10-24 20:10:31 -07005208 StatsLog.write(StatsLog.WIFI_SIGNAL_STRENGTH_CHANGED, strengthBin);
Dianne Hackborn3251b902014-06-20 14:40:53 -07005209 }
5210 mHistoryCur.states2 =
5211 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5212 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5213 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5214 + Integer.toHexString(mHistoryCur.states2));
5215 addHistoryRecordLocked(elapsedRealtime, uptime);
5216 } else {
5217 stopAllWifiSignalStrengthTimersLocked(-1);
5218 }
5219 mWifiSignalStrengthBin = strengthBin;
5220 }
5221 }
5222
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005223 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005224
The Android Open Source Project10592532009-03-18 17:39:46 -07005225 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005226 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005227 final long elapsedRealtime = mClocks.elapsedRealtime();
5228 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005229 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005230 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005231 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5232 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005233 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005234 }
5235 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005236 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005237 }
5238
5239 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005240 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005241 final long elapsedRealtime = mClocks.elapsedRealtime();
5242 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005243 mWifiFullLockNesting--;
5244 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005245 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005246 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5247 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005248 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005249 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005250 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005251 }
5252
Nick Pelly6ccaa542012-06-15 15:22:47 -07005253 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005254
Nick Pelly6ccaa542012-06-15 15:22:47 -07005255 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005256 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005257 final long elapsedRealtime = mClocks.elapsedRealtime();
5258 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005259 if (mWifiScanNesting == 0) {
5260 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
5261 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005262 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005263 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005264 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005265 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005266 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005267 }
5268
Nick Pelly6ccaa542012-06-15 15:22:47 -07005269 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005270 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005271 final long elapsedRealtime = mClocks.elapsedRealtime();
5272 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005273 mWifiScanNesting--;
5274 if (mWifiScanNesting == 0) {
5275 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
5276 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005277 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005278 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005279 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005280 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005281 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005282
Robert Greenwalta029ea12013-09-25 16:38:12 -07005283 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005284 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005285 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005286 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005287 }
5288
5289 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005290 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005291 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005292 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005293 }
5294
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005295 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005296
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005297 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005298 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005299 final long elapsedRealtime = mClocks.elapsedRealtime();
5300 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005301 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005302 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005303 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
5304 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005305 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005306 }
5307 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005308 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005309 }
5310
5311 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005312 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005313 final long elapsedRealtime = mClocks.elapsedRealtime();
5314 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005315 mWifiMulticastNesting--;
5316 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005317 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005318 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
5319 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005320 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005321 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005322 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005323 }
5324
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005325 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
5326 int N = ws.size();
5327 for (int i=0; i<N; i++) {
5328 noteFullWifiLockAcquiredLocked(ws.get(i));
5329 }
5330 }
5331
5332 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
5333 int N = ws.size();
5334 for (int i=0; i<N; i++) {
5335 noteFullWifiLockReleasedLocked(ws.get(i));
5336 }
5337 }
5338
Nick Pelly6ccaa542012-06-15 15:22:47 -07005339 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005340 int N = ws.size();
5341 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005342 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005343 }
5344 }
5345
Nick Pelly6ccaa542012-06-15 15:22:47 -07005346 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005347 int N = ws.size();
5348 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005349 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005350 }
5351 }
5352
Robert Greenwalta029ea12013-09-25 16:38:12 -07005353 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
5354 int N = ws.size();
5355 for (int i=0; i<N; i++) {
5356 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5357 }
5358 }
5359
5360 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5361 int N = ws.size();
5362 for (int i=0; i<N; i++) {
5363 noteWifiBatchedScanStoppedLocked(ws.get(i));
5364 }
5365 }
5366
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005367 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5368 int N = ws.size();
5369 for (int i=0; i<N; i++) {
5370 noteWifiMulticastEnabledLocked(ws.get(i));
5371 }
5372 }
5373
5374 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5375 int N = ws.size();
5376 for (int i=0; i<N; i++) {
5377 noteWifiMulticastDisabledLocked(ws.get(i));
5378 }
5379 }
5380
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005381 private static String[] includeInStringArray(String[] array, String str) {
5382 if (ArrayUtils.indexOf(array, str) >= 0) {
5383 return array;
5384 }
5385 String[] newArray = new String[array.length+1];
5386 System.arraycopy(array, 0, newArray, 0, array.length);
5387 newArray[array.length] = str;
5388 return newArray;
5389 }
5390
5391 private static String[] excludeFromStringArray(String[] array, String str) {
5392 int index = ArrayUtils.indexOf(array, str);
5393 if (index >= 0) {
5394 String[] newArray = new String[array.length-1];
5395 if (index > 0) {
5396 System.arraycopy(array, 0, newArray, 0, index);
5397 }
5398 if (index < array.length-1) {
5399 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5400 }
5401 return newArray;
5402 }
5403 return array;
5404 }
5405
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005406 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005407 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005408
Adam Lesinski14ae39a2017-05-26 11:50:40 -07005409 synchronized (mModemNetworkLock) {
5410 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
5411 mModemIfaces = includeInStringArray(mModemIfaces, iface);
5412 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
5413 } else {
5414 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
5415 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
5416 }
5417 }
5418
5419 synchronized (mWifiNetworkLock) {
5420 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
5421 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5422 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
5423 } else {
5424 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5425 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
5426 }
5427 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005428 }
5429
David Chenc8a43242017-10-17 16:23:28 -07005430 public String[] getWifiIfaces() {
5431 synchronized (mWifiNetworkLock) {
5432 return mWifiIfaces;
5433 }
5434 }
5435
5436 public String[] getMobileIfaces() {
5437 synchronized (mModemNetworkLock) {
5438 return mModemIfaces;
5439 }
5440 }
5441
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005442 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5443 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005445
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005446 @Override public int getScreenOnCount(int which) {
5447 return mScreenOnTimer.getCountLocked(which);
5448 }
5449
Mike Mac2f518a2017-09-19 16:06:03 -07005450 @Override public long getScreenDozeTime(long elapsedRealtimeUs, int which) {
5451 return mScreenDozeTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5452 }
5453
5454 @Override public int getScreenDozeCount(int which) {
5455 return mScreenDozeTimer.getCountLocked(which);
5456 }
5457
Dianne Hackborn617f8772009-03-31 15:04:46 -07005458 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005459 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005460 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005461 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005462 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005463
Kweku Adams87b19ec2017-10-09 12:40:03 -07005464 @Override public Timer getScreenBrightnessTimer(int brightnessBin) {
5465 return mScreenBrightnessTimer[brightnessBin];
5466 }
5467
Jeff Browne95c3cd2014-05-02 16:59:26 -07005468 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5469 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005470 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005471
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005472 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5473 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005474 }
5475
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005476 @Override public int getPowerSaveModeEnabledCount(int which) {
5477 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5478 }
5479
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005480 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5481 int which) {
5482 switch (mode) {
5483 case DEVICE_IDLE_MODE_LIGHT:
5484 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005485 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005486 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5487 }
5488 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005489 }
5490
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005491 @Override public int getDeviceIdleModeCount(int mode, int which) {
5492 switch (mode) {
5493 case DEVICE_IDLE_MODE_LIGHT:
5494 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005495 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005496 return mDeviceIdleModeFullTimer.getCountLocked(which);
5497 }
5498 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005499 }
5500
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005501 @Override public long getLongestDeviceIdleModeTime(int mode) {
5502 switch (mode) {
5503 case DEVICE_IDLE_MODE_LIGHT:
5504 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005505 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005506 return mLongestFullIdleTime;
5507 }
5508 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005509 }
5510
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005511 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5512 switch (mode) {
5513 case DEVICE_IDLE_MODE_LIGHT:
5514 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005515 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005516 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5517 }
5518 return 0;
5519 }
5520
5521 @Override public int getDeviceIdlingCount(int mode, int which) {
5522 switch (mode) {
5523 case DEVICE_IDLE_MODE_LIGHT:
5524 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005525 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005526 return mDeviceIdlingTimer.getCountLocked(which);
5527 }
5528 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005529 }
5530
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005531 @Override public int getNumConnectivityChange(int which) {
5532 int val = mNumConnectivityChange;
5533 if (which == STATS_CURRENT) {
5534 val -= mLoadedNumConnectivityChange;
5535 } else if (which == STATS_SINCE_UNPLUGGED) {
5536 val -= mUnpluggedNumConnectivityChange;
5537 }
5538 return val;
5539 }
5540
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005541 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5542 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005544
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005545 @Override public int getPhoneOnCount(int which) {
5546 return mPhoneOnTimer.getCountLocked(which);
5547 }
5548
Dianne Hackborn627bba72009-03-24 22:32:56 -07005549 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005550 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005551 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005552 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005553 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005554
5555 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005556 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005557 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005558 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005559 }
5560
Kweku Adams87b19ec2017-10-09 12:40:03 -07005561 @Override public Timer getPhoneSignalScanningTimer() {
5562 return mPhoneSignalScanningTimer;
5563 }
5564
Catherine Liufb900812012-07-17 14:12:56 -05005565 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5566 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005567 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005568
Kweku Adams87b19ec2017-10-09 12:40:03 -07005569 @Override public Timer getPhoneSignalStrengthTimer(int strengthBin) {
5570 return mPhoneSignalStrengthsTimer[strengthBin];
5571 }
5572
Dianne Hackborn627bba72009-03-24 22:32:56 -07005573 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005574 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005575 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005576 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005577 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005578
Dianne Hackborn617f8772009-03-31 15:04:46 -07005579 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005580 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005581 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005582
Kweku Adams87b19ec2017-10-09 12:40:03 -07005583 @Override public Timer getPhoneDataConnectionTimer(int dataType) {
5584 return mPhoneDataConnectionsTimer[dataType];
5585 }
5586
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005587 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5588 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005589 }
5590
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005591 @Override public int getMobileRadioActiveCount(int which) {
5592 return mMobileRadioActiveTimer.getCountLocked(which);
5593 }
5594
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005595 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5596 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5597 }
5598
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005599 @Override public long getMobileRadioActiveUnknownTime(int which) {
5600 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5601 }
5602
5603 @Override public int getMobileRadioActiveUnknownCount(int which) {
5604 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5605 }
5606
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005607 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5608 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005609 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005610
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005611 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5612 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005613 }
5614
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005615 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005616 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005617 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005618 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005619 }
5620
5621 @Override public int getWifiStateCount(int wifiState, int which) {
5622 return mWifiStateTimer[wifiState].getCountLocked(which);
5623 }
5624
Kweku Adams87b19ec2017-10-09 12:40:03 -07005625 @Override public Timer getWifiStateTimer(int wifiState) {
5626 return mWifiStateTimer[wifiState];
5627 }
5628
Dianne Hackborn3251b902014-06-20 14:40:53 -07005629 @Override public long getWifiSupplStateTime(int state,
5630 long elapsedRealtimeUs, int which) {
5631 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5632 elapsedRealtimeUs, which);
5633 }
5634
5635 @Override public int getWifiSupplStateCount(int state, int which) {
5636 return mWifiSupplStateTimer[state].getCountLocked(which);
5637 }
5638
Kweku Adams87b19ec2017-10-09 12:40:03 -07005639 @Override public Timer getWifiSupplStateTimer(int state) {
5640 return mWifiSupplStateTimer[state];
5641 }
5642
Dianne Hackborn3251b902014-06-20 14:40:53 -07005643 @Override public long getWifiSignalStrengthTime(int strengthBin,
5644 long elapsedRealtimeUs, int which) {
5645 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5646 elapsedRealtimeUs, which);
5647 }
5648
5649 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5650 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5651 }
5652
Kweku Adams87b19ec2017-10-09 12:40:03 -07005653 @Override public Timer getWifiSignalStrengthTimer(int strengthBin) {
5654 return mWifiSignalStrengthsTimer[strengthBin];
5655 }
5656
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005657 @Override
5658 public ControllerActivityCounter getBluetoothControllerActivity() {
5659 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005660 }
5661
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005662 @Override
5663 public ControllerActivityCounter getWifiControllerActivity() {
5664 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005665 }
5666
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005667 @Override
5668 public ControllerActivityCounter getModemControllerActivity() {
5669 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005670 }
5671
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005672 @Override
5673 public boolean hasBluetoothActivityReporting() {
5674 return mHasBluetoothReporting;
5675 }
5676
5677 @Override
5678 public boolean hasWifiActivityReporting() {
5679 return mHasWifiReporting;
5680 }
5681
5682 @Override
5683 public boolean hasModemActivityReporting() {
5684 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005685 }
5686
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005687 @Override
5688 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005689 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5690 }
5691
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005692 @Override
5693 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005694 return mFlashlightOnTimer.getCountLocked(which);
5695 }
5696
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005697 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005698 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5699 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5700 }
5701
5702 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005703 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5704 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5705 }
5706
5707 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005708 public long getNetworkActivityBytes(int type, int which) {
5709 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5710 return mNetworkByteActivityCounters[type].getCountLocked(which);
5711 } else {
5712 return 0;
5713 }
5714 }
5715
5716 @Override
5717 public long getNetworkActivityPackets(int type, int which) {
5718 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5719 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005720 } else {
5721 return 0;
5722 }
5723 }
5724
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005725 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005726 final long currentTime = System.currentTimeMillis();
5727 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005728 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5729 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005730 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005731 return mStartClockTime;
5732 }
5733
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005734 @Override public String getStartPlatformVersion() {
5735 return mStartPlatformVersion;
5736 }
5737
5738 @Override public String getEndPlatformVersion() {
5739 return mEndPlatformVersion;
5740 }
5741
5742 @Override public int getParcelVersion() {
5743 return VERSION;
5744 }
5745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 @Override public boolean getIsOnBattery() {
5747 return mOnBattery;
5748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5751 return mUidStats;
5752 }
5753
Adam Lesinski5f056f62016-07-14 16:56:08 -07005754 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5755 if (timer != null) {
5756 timer.detach();
5757 }
5758 }
5759
5760 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5761 boolean detachIfReset) {
5762 if (timer != null) {
5763 return timer.reset(detachIfReset);
5764 }
5765 return true;
5766 }
5767
Bookatz867c0d72017-03-07 18:23:42 -08005768 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5769 if (timer != null) {
5770 return timer.reset(detachIfReset);
5771 }
5772 return true;
5773 }
5774
Adam Lesinski5f056f62016-07-14 16:56:08 -07005775 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5776 if (counter != null) {
5777 counter.detach();
5778 }
5779 }
5780
5781 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5782 boolean detachIfReset) {
5783 if (counter != null) {
5784 counter.reset(detachIfReset);
5785 }
5786 }
5787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 /**
5789 * The statistics associated with a particular uid.
5790 */
Joe Onoratoabded112016-02-08 16:49:39 -08005791 public static class Uid extends BatteryStats.Uid {
5792 /**
5793 * BatteryStatsImpl that we are associated with.
5794 */
5795 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005798
Bookatz867c0d72017-03-07 18:23:42 -08005799 /** TimeBase for when uid is in background and device is on battery. */
5800 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5801 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07005802 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5803 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08005804
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005805 boolean mWifiRunning;
5806 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005807
The Android Open Source Project10592532009-03-18 17:39:46 -07005808 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005809 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005810
Nick Pelly6ccaa542012-06-15 15:22:47 -07005811 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005812 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005813
Dianne Hackborn61659e52014-07-09 16:13:01 -07005814 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005815 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5816 StopwatchTimer[] mWifiBatchedScanTimer;
5817
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005818 boolean mWifiMulticastEnabled;
5819 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005820
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005821 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005822 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005823 StopwatchTimer mFlashlightTurnedOnTimer;
5824 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005825 StopwatchTimer mForegroundActivityTimer;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005826 StopwatchTimer mForegroundServiceTimer;
Bookatzc8c44962017-05-11 12:12:54 -07005827 /** Total time spent by the uid holding any partial wakelocks. */
5828 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005829 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07005830 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07005831 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07005832 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005833
Dianne Hackborna8d10942015-11-19 17:55:19 -08005834 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005835 StopwatchTimer[] mProcessStateTimer;
5836
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005837 boolean mInForegroundService = false;
5838
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005839 BatchTimer mVibratorOnTimer;
5840
Dianne Hackborn617f8772009-03-31 15:04:46 -07005841 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005842
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005843 LongSamplingCounter[] mNetworkByteActivityCounters;
5844 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005845 LongSamplingCounter mMobileRadioActiveTime;
5846 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005849 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5850 */
5851 private LongSamplingCounter mMobileRadioApWakeupCount;
5852
5853 /**
5854 * How many times this UID woke up the Application Processor due to a Wifi packet.
5855 */
5856 private LongSamplingCounter mWifiRadioApWakeupCount;
5857
5858 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005859 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005860 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005861 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005862 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005863
5864 /**
5865 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005866 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005867 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005868 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5869
5870 /**
5871 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5872 * Can be null if the UID has had no such activity.
5873 */
5874 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005875
5876 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005877 * The CPU times we had at the last history details update.
5878 */
5879 long mLastStepUserTime;
5880 long mLastStepSystemTime;
5881 long mCurStepUserTime;
5882 long mCurStepSystemTime;
5883
Joe Onoratoabded112016-02-08 16:49:39 -08005884 LongSamplingCounter mUserCpuTime;
5885 LongSamplingCounter mSystemCpuTime;
Sudheer Shankaaf857412017-07-21 00:14:24 -07005886 LongSamplingCounter[][] mCpuClusterSpeedTimesUs;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005887
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005888 LongSamplingCounterArray mCpuFreqTimeMs;
5889 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
5890
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005891 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 * The statistics we have collected for this uid's wake locks.
5893 */
Joe Onoratoabded112016-02-08 16:49:39 -08005894 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895
5896 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005897 * The statistics we have collected for this uid's syncs.
5898 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005899 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005900
5901 /**
5902 * The statistics we have collected for this uid's jobs.
5903 */
Bookatzaa4594a2017-03-24 12:39:56 -07005904 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005905
5906 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005907 * Count of the jobs that have completed and the reasons why they completed.
5908 */
5909 final ArrayMap<String, SparseIntArray> mJobCompletions = new ArrayMap<>();
5910
5911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * The statistics we have collected for this uid's sensor activations.
5913 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005914 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915
5916 /**
5917 * The statistics we have collected for this uid's processes.
5918 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005919 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920
5921 /**
5922 * The statistics we have collected for this uid's processes.
5923 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005924 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005925
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005926 /**
5927 * The transient wake stats we have collected for this uid's pids.
5928 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005929 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005930
Joe Onoratoabded112016-02-08 16:49:39 -08005931 public Uid(BatteryStatsImpl bsi, int uid) {
5932 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005934
Bookatz867c0d72017-03-07 18:23:42 -08005935 mOnBatteryBackgroundTimeBase = new TimeBase();
5936 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5937 mBsi.mClocks.elapsedRealtime() * 1000);
5938
Bookatzc8c44962017-05-11 12:12:54 -07005939 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
5940 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5941 mBsi.mClocks.elapsedRealtime() * 1000);
5942
Joe Onoratoabded112016-02-08 16:49:39 -08005943 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5944 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005945
Dianne Hackborn657153b2016-07-29 14:54:14 -07005946 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005947 @Override public Wakelock instantiateObject() {
5948 return new Wakelock(mBsi, Uid.this);
5949 }
5950 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005951 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5952 @Override public DualTimer instantiateObject() {
5953 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5954 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005955 }
5956 };
Bookatzaa4594a2017-03-24 12:39:56 -07005957 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5958 @Override public DualTimer instantiateObject() {
5959 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5960 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005961 }
5962 };
5963
5964 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5965 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5966 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5967 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005968 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5969 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005970 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005971 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5972 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005973 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 }
5975
5976 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005977 public long[] getCpuFreqTimes(int which) {
5978 if (mCpuFreqTimeMs == null) {
5979 return null;
5980 }
5981 final long[] cpuFreqTimes = mCpuFreqTimeMs.getCountsLocked(which);
5982 if (cpuFreqTimes == null) {
5983 return null;
5984 }
5985 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5986 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5987 if (cpuFreqTimes[i] != 0) {
5988 return cpuFreqTimes;
5989 }
5990 }
5991 return null;
5992 }
5993
5994 @Override
5995 public long[] getScreenOffCpuFreqTimes(int which) {
5996 if (mScreenOffCpuFreqTimeMs == null) {
5997 return null;
5998 }
5999 final long[] cpuFreqTimes = mScreenOffCpuFreqTimeMs.getCountsLocked(which);
6000 if (cpuFreqTimes == null) {
6001 return null;
6002 }
6003 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
6004 for (int i = 0; i < cpuFreqTimes.length; ++i) {
6005 if (cpuFreqTimes[i] != 0) {
6006 return cpuFreqTimes;
6007 }
6008 }
6009 return null;
6010 }
6011
6012 @Override
Bookatzc8c44962017-05-11 12:12:54 -07006013 public Timer getAggregatedPartialWakelockTimer() {
6014 return mAggregatedPartialWakelockTimer;
6015 }
6016
6017 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006018 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006019 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 }
6021
6022 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006023 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006024 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006025 }
6026
6027 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006028 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006029 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006030 }
6031
6032 @Override
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006033 public ArrayMap<String, SparseIntArray> getJobCompletionStats() {
6034 return mJobCompletions;
6035 }
6036
6037 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07006038 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 return mSensorStats;
6040 }
6041
6042 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006043 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044 return mProcessStats;
6045 }
6046
6047 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006048 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 return mPackageStats;
6050 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006051
6052 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 public int getUid() {
6054 return mUid;
6055 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006056
6057 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006058 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006059 if (!mWifiRunning) {
6060 mWifiRunning = true;
6061 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006062 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6063 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006064 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006065 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006066 }
6067 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006068
Dianne Hackborn617f8772009-03-31 15:04:46 -07006069 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006070 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006071 if (mWifiRunning) {
6072 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006073 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006074 }
6075 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006076
Dianne Hackborn617f8772009-03-31 15:04:46 -07006077 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006078 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07006079 if (!mFullWifiLockOut) {
6080 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006081 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006082 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6083 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006084 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006085 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
Bookatze5885242017-10-24 20:10:31 -07006086 // TODO(statsd): Possibly use a worksource instead of a uid.
6087 StatsLog.write(StatsLog.WIFI_LOCK_STATE_CHANGED, getUid(), 1);
The Android Open Source Project10592532009-03-18 17:39:46 -07006088 }
6089 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006090
The Android Open Source Project10592532009-03-18 17:39:46 -07006091 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006092 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07006093 if (mFullWifiLockOut) {
6094 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006095 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatze5885242017-10-24 20:10:31 -07006096 if (!mFullWifiLockTimer.isRunningLocked()) { // only tell statsd if truly stopped
6097 // TODO(statsd): Possibly use a worksource instead of a uid.
6098 StatsLog.write(StatsLog.WIFI_LOCK_STATE_CHANGED, getUid(), 0);
6099 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006100 }
6101 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006102
The Android Open Source Project10592532009-03-18 17:39:46 -07006103 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006104 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006105 if (!mWifiScanStarted) {
6106 mWifiScanStarted = true;
6107 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006108 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6109 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
6110 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006111 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006112 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
Bookatze5885242017-10-24 20:10:31 -07006113 // TODO(statsd): Possibly use a worksource instead of a uid.
6114 StatsLog.write(StatsLog.WIFI_SCAN_STATE_CHANGED, getUid(), 1);
The Android Open Source Project10592532009-03-18 17:39:46 -07006115 }
6116 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006117
The Android Open Source Project10592532009-03-18 17:39:46 -07006118 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006119 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006120 if (mWifiScanStarted) {
6121 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006122 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatze5885242017-10-24 20:10:31 -07006123 if (!mWifiScanTimer.isRunningLocked()) { // only tell statsd if truly stopped
6124 // TODO(statsd): Possibly use a worksource instead of a uid.
6125 StatsLog.write(StatsLog.WIFI_SCAN_STATE_CHANGED, getUid(), 0);
6126 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006127 }
6128 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006129
6130 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006131 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006132 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07006133 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006134 csph = csph >> 3;
6135 bin++;
6136 }
6137
6138 if (mWifiBatchedScanBinStarted == bin) return;
6139
6140 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
6141 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006142 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006143 }
6144 mWifiBatchedScanBinStarted = bin;
6145 if (mWifiBatchedScanTimer[bin] == null) {
6146 makeWifiBatchedScanBin(bin, null);
6147 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006148 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006149 }
6150
6151 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006152 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006153 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
6154 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006155 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006156 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6157 }
6158 }
6159
6160 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006161 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006162 if (!mWifiMulticastEnabled) {
6163 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006164 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006165 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6166 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006167 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006168 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006169 }
6170 }
6171
6172 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006173 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006174 if (mWifiMulticastEnabled) {
6175 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006176 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006177 }
6178 }
6179
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006180 @Override
6181 public ControllerActivityCounter getWifiControllerActivity() {
6182 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07006183 }
6184
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006185 @Override
6186 public ControllerActivityCounter getBluetoothControllerActivity() {
6187 return mBluetoothControllerActivity;
6188 }
6189
6190 @Override
6191 public ControllerActivityCounter getModemControllerActivity() {
6192 return mModemControllerActivity;
6193 }
6194
6195 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
6196 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006197 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006198 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08006199 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006200 return mWifiControllerActivity;
6201 }
6202
6203 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
6204 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006205 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006206 NUM_BT_TX_LEVELS);
6207 }
6208 return mBluetoothControllerActivity;
6209 }
6210
6211 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
6212 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006213 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006214 ModemActivityInfo.TX_POWER_LEVELS);
6215 }
6216 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08006217 }
6218
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006219 public StopwatchTimer createAudioTurnedOnTimerLocked() {
6220 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006221 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6222 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006223 }
6224 return mAudioTurnedOnTimer;
6225 }
6226
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006227 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006228 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006229 // TODO(statsd): Possibly use a worksource instead of a uid.
6230 StatsLog.write(StatsLog.AUDIO_STATE_CHANGED, getUid(), 1);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006231 }
6232
6233 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
6234 if (mAudioTurnedOnTimer != null) {
6235 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006236 if (!mAudioTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
6237 // TODO(statsd): Possibly use a worksource instead of a uid.
6238 StatsLog.write(StatsLog.AUDIO_STATE_CHANGED, getUid(), 0);
6239 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006240 }
6241 }
6242
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006243 public void noteResetAudioLocked(long elapsedRealtimeMs) {
6244 if (mAudioTurnedOnTimer != null) {
6245 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006246 // TODO(statsd): Possibly use a worksource instead of a uid.
6247 StatsLog.write(StatsLog.AUDIO_STATE_CHANGED, getUid(), 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006248 }
6249 }
6250
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006251 public StopwatchTimer createVideoTurnedOnTimerLocked() {
6252 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006253 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6254 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006255 }
6256 return mVideoTurnedOnTimer;
6257 }
6258
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006259 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006260 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006261 // TODO(statsd): Possibly use a worksource instead of a uid.
6262 StatsLog.write(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(), 1);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006263 }
6264
6265 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
6266 if (mVideoTurnedOnTimer != null) {
6267 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006268 if (!mVideoTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
6269 // TODO(statsd): Possibly use a worksource instead of a uid.
6270 StatsLog.write(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(), 0);
6271 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006272 }
6273 }
6274
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006275 public void noteResetVideoLocked(long elapsedRealtimeMs) {
6276 if (mVideoTurnedOnTimer != null) {
6277 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006278 // TODO(statsd): Possibly use a worksource instead of a uid.
6279 StatsLog.write(StatsLog.MEDIA_CODEC_ACTIVITY_CHANGED, getUid(), 0);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006280 }
6281 }
6282
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006283 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
6284 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006285 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6286 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006287 }
6288 return mFlashlightTurnedOnTimer;
6289 }
6290
6291 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
6292 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006293 // TODO(statsd): Possibly use a worksource instead of a uid.
6294 StatsLog.write(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), 1);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006295 }
6296
6297 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
6298 if (mFlashlightTurnedOnTimer != null) {
6299 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006300 if (!mFlashlightTurnedOnTimer.isRunningLocked()) {
6301 // TODO(statsd): Possibly use a worksource instead of a uid.
6302 StatsLog.write(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), 0);
6303 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006304 }
6305 }
6306
6307 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
6308 if (mFlashlightTurnedOnTimer != null) {
6309 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006310 // TODO(statsd): Possibly use a worksource instead of a uid.
6311 StatsLog.write(StatsLog.FLASHLIGHT_STATE_CHANGED, getUid(), 0);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006312 }
6313 }
6314
6315 public StopwatchTimer createCameraTurnedOnTimerLocked() {
6316 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006317 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6318 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006319 }
6320 return mCameraTurnedOnTimer;
6321 }
6322
6323 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
6324 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006325 // TODO(statsd): Possibly use a worksource instead of a uid.
6326 StatsLog.write(StatsLog.CAMERA_STATE_CHANGED, getUid(), 1);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006327 }
6328
6329 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
6330 if (mCameraTurnedOnTimer != null) {
6331 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006332 if (!mCameraTurnedOnTimer.isRunningLocked()) { // only tell statsd if truly stopped
6333 // TODO(statsd): Possibly use a worksource instead of a uid.
6334 StatsLog.write(StatsLog.CAMERA_STATE_CHANGED, getUid(), 0);
6335 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006336 }
6337 }
6338
6339 public void noteResetCameraLocked(long elapsedRealtimeMs) {
6340 if (mCameraTurnedOnTimer != null) {
6341 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006342 // TODO(statsd): Possibly use a worksource instead of a uid.
6343 StatsLog.write(StatsLog.CAMERA_STATE_CHANGED, getUid(), 0);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006344 }
6345 }
6346
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006347 public StopwatchTimer createForegroundActivityTimerLocked() {
6348 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006349 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6350 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006351 }
6352 return mForegroundActivityTimer;
6353 }
6354
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006355 public StopwatchTimer createForegroundServiceTimerLocked() {
6356 if (mForegroundServiceTimer == null) {
6357 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6358 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase);
6359 }
6360 return mForegroundServiceTimer;
6361 }
6362
Bookatzc8c44962017-05-11 12:12:54 -07006363 public DualTimer createAggregatedPartialWakelockTimerLocked() {
6364 if (mAggregatedPartialWakelockTimer == null) {
6365 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
6366 AGGREGATED_WAKE_TYPE_PARTIAL, null,
6367 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
6368 }
6369 return mAggregatedPartialWakelockTimer;
6370 }
6371
Bookatz867c0d72017-03-07 18:23:42 -08006372 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006373 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006374 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6375 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6376 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006377 }
6378 return mBluetoothScanTimer;
6379 }
6380
Bookatzb1f04f32017-05-19 13:57:32 -07006381 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
6382 if (mBluetoothUnoptimizedScanTimer == null) {
6383 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
6384 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
6385 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
6386 }
6387 return mBluetoothUnoptimizedScanTimer;
6388 }
6389
6390 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006391 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006392 // TODO(statsd): Possibly use a worksource instead of a uid.
6393 StatsLog.write(StatsLog.BLE_SCAN_STATE_CHANGED, getUid(), 1);
Bookatzb1f04f32017-05-19 13:57:32 -07006394 if (isUnoptimized) {
6395 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006396 // TODO(statsd): Possibly use a worksource instead of a uid.
6397 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED, getUid(), 1);
Bookatzb1f04f32017-05-19 13:57:32 -07006398 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006399 }
6400
Bookatz94c5a312017-07-11 16:49:17 -07006401 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006402 if (mBluetoothScanTimer != null) {
6403 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006404 if (!mBluetoothScanTimer.isRunningLocked()) { // only tell statsd if truly stopped
6405 // TODO(statsd): Possibly use a worksource instead of a uid.
6406 StatsLog.write(StatsLog.BLE_SCAN_STATE_CHANGED, getUid(), 0);
6407 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006408 }
Bookatz94c5a312017-07-11 16:49:17 -07006409 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07006410 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006411 if (!mBluetoothUnoptimizedScanTimer.isRunningLocked()) {
6412 // TODO(statsd): Possibly use a worksource instead of a uid.
6413 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED, getUid(), 0);
6414 }
Bookatzb1f04f32017-05-19 13:57:32 -07006415 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006416 }
6417
6418 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
6419 if (mBluetoothScanTimer != null) {
6420 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006421 // TODO(statsd): Possibly use a worksource instead of a uid.
6422 StatsLog.write(StatsLog.BLE_SCAN_STATE_CHANGED, getUid(), 0);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006423 }
Bookatzb1f04f32017-05-19 13:57:32 -07006424 if (mBluetoothUnoptimizedScanTimer != null) {
6425 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07006426 // TODO(statsd): Possibly use a worksource instead of a uid.
6427 StatsLog.write(StatsLog.BLE_UNOPTIMIZED_SCAN_STATE_CHANGED, getUid(), 0);
Bookatzb1f04f32017-05-19 13:57:32 -07006428 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006429 }
6430
Bookatz956f36bf2017-04-28 09:48:17 -07006431 public Counter createBluetoothScanResultCounterLocked() {
6432 if (mBluetoothScanResultCounter == null) {
6433 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
6434 }
6435 return mBluetoothScanResultCounter;
6436 }
6437
Bookatzb1f04f32017-05-19 13:57:32 -07006438 public Counter createBluetoothScanResultBgCounterLocked() {
6439 if (mBluetoothScanResultBgCounter == null) {
6440 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
6441 }
6442 return mBluetoothScanResultBgCounter;
6443 }
6444
Bookatz4ebc0642017-05-11 12:21:19 -07006445 public void noteBluetoothScanResultsLocked(int numNewResults) {
6446 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07006447 // Uses background timebase, so the count will only be incremented if uid in background.
6448 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatzc1a050a2017-10-10 15:49:28 -07006449 // TODO(statsd): Possibly use a worksource instead of a uid.
6450 // TODO(statsd): This could be in AppScanStats instead, if desired.
6451 StatsLog.write(StatsLog.BLE_SCAN_RESULT_RECEIVED, getUid(), numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07006452 }
6453
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006454 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006455 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006456 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006457 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006458 }
6459
6460 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006461 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006462 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006463 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006464 }
6465 }
6466
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006467 public void noteForegroundServiceResumedLocked(long elapsedRealtimeMs) {
6468 createForegroundServiceTimerLocked().startRunningLocked(elapsedRealtimeMs);
6469 }
6470
6471 public void noteForegroundServicePausedLocked(long elapsedRealtimeMs) {
6472 if (mForegroundServiceTimer != null) {
6473 mForegroundServiceTimer.stopRunningLocked(elapsedRealtimeMs);
6474 }
6475 }
6476
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006477 public BatchTimer createVibratorOnTimerLocked() {
6478 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006479 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6480 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006481 }
6482 return mVibratorOnTimer;
6483 }
6484
6485 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08006486 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006487 }
6488
6489 public void noteVibratorOffLocked() {
6490 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006491 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006492 }
6493 }
6494
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006495 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006496 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006497 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006498 return 0;
6499 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006500 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006501 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006502
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006503 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006504 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006505 if (mFullWifiLockTimer == null) {
6506 return 0;
6507 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006508 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006509 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006510
6511 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006512 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006513 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006514 return 0;
6515 }
Bookatzaa4594a2017-03-24 12:39:56 -07006516 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006517 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006518
6519 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006520 public int getWifiScanCount(int which) {
6521 if (mWifiScanTimer == null) {
6522 return 0;
6523 }
Bookatzaa4594a2017-03-24 12:39:56 -07006524 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08006525 }
6526
6527 @Override
Kweku Adams103351f2017-10-16 14:39:34 -07006528 public Timer getWifiScanTimer() {
6529 return mWifiScanTimer;
6530 }
6531
6532 @Override
Bookatz867c0d72017-03-07 18:23:42 -08006533 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07006534 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006535 return 0;
6536 }
6537 return mWifiScanTimer.getSubTimer().getCountLocked(which);
6538 }
6539
6540 @Override
6541 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
6542 if (mWifiScanTimer == null) {
6543 return 0;
6544 }
6545 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07006546 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08006547 }
6548
6549 @Override
6550 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07006551 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006552 return 0;
6553 }
6554 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
6555 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07006556 }
6557
6558 @Override
Kweku Adams103351f2017-10-16 14:39:34 -07006559 public Timer getWifiScanBackgroundTimer() {
6560 if (mWifiScanTimer == null) {
6561 return null;
6562 }
6563 return mWifiScanTimer.getSubTimer();
6564 }
6565
6566 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006567 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006568 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6569 if (mWifiBatchedScanTimer[csphBin] == null) {
6570 return 0;
6571 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006572 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006573 }
6574
6575 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006576 public int getWifiBatchedScanCount(int csphBin, int which) {
6577 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6578 if (mWifiBatchedScanTimer[csphBin] == null) {
6579 return 0;
6580 }
6581 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
6582 }
6583
6584 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006585 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006586 if (mWifiMulticastTimer == null) {
6587 return 0;
6588 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006589 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006590 }
6591
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006592 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006593 public Timer getAudioTurnedOnTimer() {
6594 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006595 }
6596
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006597 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006598 public Timer getVideoTurnedOnTimer() {
6599 return mVideoTurnedOnTimer;
6600 }
6601
6602 @Override
6603 public Timer getFlashlightTurnedOnTimer() {
6604 return mFlashlightTurnedOnTimer;
6605 }
6606
6607 @Override
6608 public Timer getCameraTurnedOnTimer() {
6609 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006610 }
6611
Dianne Hackborn617f8772009-03-31 15:04:46 -07006612 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006613 public Timer getForegroundActivityTimer() {
6614 return mForegroundActivityTimer;
6615 }
6616
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006617 @Override
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006618 public Timer getForegroundServiceTimer() {
6619 return mForegroundServiceTimer;
6620 }
6621
6622 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006623 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006624 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006625 }
6626
6627 @Override
6628 public Timer getBluetoothScanBackgroundTimer() {
6629 if (mBluetoothScanTimer == null) {
6630 return null;
6631 }
6632 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006633 }
6634
Bookatz956f36bf2017-04-28 09:48:17 -07006635 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07006636 public Timer getBluetoothUnoptimizedScanTimer() {
6637 return mBluetoothUnoptimizedScanTimer;
6638 }
6639
6640 @Override
6641 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
6642 if (mBluetoothUnoptimizedScanTimer == null) {
6643 return null;
6644 }
6645 return mBluetoothUnoptimizedScanTimer.getSubTimer();
6646 }
6647
6648 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07006649 public Counter getBluetoothScanResultCounter() {
6650 return mBluetoothScanResultCounter;
6651 }
6652
Bookatzb1f04f32017-05-19 13:57:32 -07006653 @Override
6654 public Counter getBluetoothScanResultBgCounter() {
6655 return mBluetoothScanResultBgCounter;
6656 }
6657
Dianne Hackborn61659e52014-07-09 16:13:01 -07006658 void makeProcessState(int i, Parcel in) {
6659 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6660
6661 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006662 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6663 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006664 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006665 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6666 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006667 }
6668 }
6669
6670 @Override
6671 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6672 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6673 if (mProcessStateTimer[state] == null) {
6674 return 0;
6675 }
6676 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6677 }
6678
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006679 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006680 public Timer getProcessStateTimer(int state) {
6681 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6682 return mProcessStateTimer[state];
6683 }
6684
6685 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006686 public Timer getVibratorOnTimer() {
6687 return mVibratorOnTimer;
6688 }
6689
6690 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006691 public void noteUserActivityLocked(int type) {
6692 if (mUserActivityCounters == null) {
6693 initUserActivityLocked();
6694 }
Jeff Browndf693de2012-07-27 12:03:38 -07006695 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6696 mUserActivityCounters[type].stepAtomic();
6697 } else {
6698 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6699 new Throwable());
6700 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006701 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006702
Dianne Hackborn617f8772009-03-31 15:04:46 -07006703 @Override
6704 public boolean hasUserActivity() {
6705 return mUserActivityCounters != null;
6706 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006707
Dianne Hackborn617f8772009-03-31 15:04:46 -07006708 @Override
6709 public int getUserActivityCount(int type, int which) {
6710 if (mUserActivityCounters == null) {
6711 return 0;
6712 }
Evan Millarc64edde2009-04-18 12:26:32 -07006713 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006714 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006715
Robert Greenwalta029ea12013-09-25 16:38:12 -07006716 void makeWifiBatchedScanBin(int i, Parcel in) {
6717 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6718
Joe Onoratoabded112016-02-08 16:49:39 -08006719 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006720 if (collected == null) {
6721 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006722 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006723 }
6724 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006725 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6726 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006727 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006728 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6729 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006730 }
6731 }
6732
6733
Dianne Hackborn617f8772009-03-31 15:04:46 -07006734 void initUserActivityLocked() {
6735 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6736 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006737 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006738 }
6739 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006740
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006741 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6742 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006743 initNetworkActivityLocked();
6744 }
6745 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006746 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6747 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006748 } else {
6749 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6750 new Throwable());
6751 }
6752 }
6753
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006754 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6755 if (mNetworkByteActivityCounters == null) {
6756 initNetworkActivityLocked();
6757 }
6758 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6759 mMobileRadioActiveCount.addCountLocked(1);
6760 }
6761
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006762 @Override
6763 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006764 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006765 }
6766
6767 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006768 public long getNetworkActivityBytes(int type, int which) {
6769 if (mNetworkByteActivityCounters != null && type >= 0
6770 && type < mNetworkByteActivityCounters.length) {
6771 return mNetworkByteActivityCounters[type].getCountLocked(which);
6772 } else {
6773 return 0;
6774 }
6775 }
6776
6777 @Override
6778 public long getNetworkActivityPackets(int type, int which) {
6779 if (mNetworkPacketActivityCounters != null && type >= 0
6780 && type < mNetworkPacketActivityCounters.length) {
6781 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006782 } else {
6783 return 0;
6784 }
6785 }
6786
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006787 @Override
6788 public long getMobileRadioActiveTime(int which) {
6789 return mMobileRadioActiveTime != null
6790 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6791 }
6792
6793 @Override
6794 public int getMobileRadioActiveCount(int which) {
6795 return mMobileRadioActiveCount != null
6796 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6797 }
6798
Adam Lesinskie08af192015-03-25 16:42:59 -07006799 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006800 public long getUserCpuTimeUs(int which) {
6801 return mUserCpuTime.getCountLocked(which);
6802 }
6803
6804 @Override
6805 public long getSystemCpuTimeUs(int which) {
6806 return mSystemCpuTime.getCountLocked(which);
6807 }
6808
6809 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006810 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07006811 if (mCpuClusterSpeedTimesUs != null) {
6812 if (cluster >= 0 && cluster < mCpuClusterSpeedTimesUs.length) {
6813 final LongSamplingCounter[] cpuSpeedTimesUs = mCpuClusterSpeedTimesUs[cluster];
6814 if (cpuSpeedTimesUs != null) {
6815 if (step >= 0 && step < cpuSpeedTimesUs.length) {
6816 final LongSamplingCounter c = cpuSpeedTimesUs[step];
Adam Lesinski6832f392015-09-05 18:05:40 -07006817 if (c != null) {
6818 return c.getCountLocked(which);
6819 }
6820 }
6821 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006822 }
6823 }
6824 return 0;
6825 }
6826
Adam Lesinski5f056f62016-07-14 16:56:08 -07006827 public void noteMobileRadioApWakeupLocked() {
6828 if (mMobileRadioApWakeupCount == null) {
6829 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6830 }
6831 mMobileRadioApWakeupCount.addCountLocked(1);
6832 }
6833
6834 @Override
6835 public long getMobileRadioApWakeupCount(int which) {
6836 if (mMobileRadioApWakeupCount != null) {
6837 return mMobileRadioApWakeupCount.getCountLocked(which);
6838 }
6839 return 0;
6840 }
6841
6842 public void noteWifiRadioApWakeupLocked() {
6843 if (mWifiRadioApWakeupCount == null) {
6844 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6845 }
6846 mWifiRadioApWakeupCount.addCountLocked(1);
6847 }
6848
6849 @Override
6850 public long getWifiRadioApWakeupCount(int which) {
6851 if (mWifiRadioApWakeupCount != null) {
6852 return mWifiRadioApWakeupCount.getCountLocked(which);
6853 }
6854 return 0;
6855 }
6856
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006857 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006858 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6859 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006860 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006861 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6862 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006863 }
Joe Onoratoabded112016-02-08 16:49:39 -08006864 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6865 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006867
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006868 /**
6869 * Clear all stats for this uid. Returns true if the uid is completely
6870 * inactive so can be dropped.
6871 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006872 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Bookatz993a0be2017-07-21 09:03:23 -07006873 public boolean reset(long uptime, long realtime) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006874 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006875
Bookatz993a0be2017-07-21 09:03:23 -07006876 mOnBatteryBackgroundTimeBase.init(uptime, realtime);
6877 mOnBatteryScreenOffBackgroundTimeBase.init(uptime, realtime);
6878
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006879 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006880 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006881 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006882 }
6883 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006884 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006885 active |= mFullWifiLockOut;
6886 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006887 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006888 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006889 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006890 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006891 if (mWifiBatchedScanTimer != null) {
6892 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6893 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006894 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006895 }
6896 }
6897 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6898 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006899 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006900 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006901 active |= mWifiMulticastEnabled;
6902 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006903
6904 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6905 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6906 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6907 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6908 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006909 active |= !resetTimerIfNotNull(mForegroundServiceTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07006910 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07006911 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07006912 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07006913 if (mBluetoothScanResultCounter != null) {
6914 mBluetoothScanResultCounter.reset(false);
6915 }
Bookatzb1f04f32017-05-19 13:57:32 -07006916 if (mBluetoothScanResultBgCounter != null) {
6917 mBluetoothScanResultBgCounter.reset(false);
6918 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006919
Dianne Hackborn61659e52014-07-09 16:13:01 -07006920 if (mProcessStateTimer != null) {
6921 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6922 if (mProcessStateTimer[i] != null) {
6923 active |= !mProcessStateTimer[i].reset(false);
6924 }
6925 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006926 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006927 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006928 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006929 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006930 mVibratorOnTimer.detach();
6931 mVibratorOnTimer = null;
6932 } else {
6933 active = true;
6934 }
6935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006936
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006937 if (mUserActivityCounters != null) {
6938 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6939 mUserActivityCounters[i].reset(false);
6940 }
6941 }
6942
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006943 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006944 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006945 mNetworkByteActivityCounters[i].reset(false);
6946 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006947 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006948 mMobileRadioActiveTime.reset(false);
6949 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006950 }
6951
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006952 if (mWifiControllerActivity != null) {
6953 mWifiControllerActivity.reset(false);
6954 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006955
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006956 if (mBluetoothControllerActivity != null) {
6957 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006958 }
6959
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006960 if (mModemControllerActivity != null) {
6961 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006962 }
6963
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006964 mUserCpuTime.reset(false);
6965 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006966
Sudheer Shankaaf857412017-07-21 00:14:24 -07006967 if (mCpuClusterSpeedTimesUs != null) {
6968 for (LongSamplingCounter[] speeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006969 if (speeds != null) {
6970 for (LongSamplingCounter speed : speeds) {
6971 if (speed != null) {
6972 speed.reset(false);
6973 }
6974 }
6975 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006976 }
6977 }
6978
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006979 if (mCpuFreqTimeMs != null) {
6980 mCpuFreqTimeMs.reset(false);
6981 }
6982 if (mScreenOffCpuFreqTimeMs != null) {
6983 mScreenOffCpuFreqTimeMs.reset(false);
6984 }
6985
Adam Lesinski5f056f62016-07-14 16:56:08 -07006986 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6987 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6988
Dianne Hackbornd953c532014-08-16 18:17:38 -07006989 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6990 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6991 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006992 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006993 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006994 } else {
6995 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006996 }
6997 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006998 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006999 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007000 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007001 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007002 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007003 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007004 timer.detach();
7005 } else {
7006 active = true;
7007 }
7008 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007009 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07007010 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007011 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07007012 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007013 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007014 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007015 timer.detach();
7016 } else {
7017 active = true;
7018 }
7019 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007020 mJobStats.cleanup();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007021 mJobCompletions.clear();
Dianne Hackborn61659e52014-07-09 16:13:01 -07007022 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
7023 Sensor s = mSensorStats.valueAt(ise);
7024 if (s.reset()) {
7025 mSensorStats.removeAt(ise);
7026 } else {
7027 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007028 }
7029 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007030 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
7031 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08007032 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007033 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007034 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007035 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007036 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007037 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007038 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007039 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007040 } else {
7041 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007042 }
7043 }
7044 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007045 if (mPackageStats.size() > 0) {
7046 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
7047 while (it.hasNext()) {
7048 Map.Entry<String, Pkg> pkgEntry = it.next();
7049 Pkg p = pkgEntry.getValue();
7050 p.detach();
7051 if (p.mServiceStats.size() > 0) {
7052 Iterator<Map.Entry<String, Pkg.Serv>> it2
7053 = p.mServiceStats.entrySet().iterator();
7054 while (it2.hasNext()) {
7055 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
7056 servEntry.getValue().detach();
7057 }
7058 }
7059 }
7060 mPackageStats.clear();
7061 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007062
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08007063 mLastStepUserTime = mLastStepSystemTime = 0;
7064 mCurStepUserTime = mCurStepSystemTime = 0;
7065
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007066 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007067 if (mWifiRunningTimer != null) {
7068 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007069 }
7070 if (mFullWifiLockTimer != null) {
7071 mFullWifiLockTimer.detach();
7072 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007073 if (mWifiScanTimer != null) {
7074 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007075 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007076 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7077 if (mWifiBatchedScanTimer[i] != null) {
7078 mWifiBatchedScanTimer[i].detach();
7079 }
7080 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007081 if (mWifiMulticastTimer != null) {
7082 mWifiMulticastTimer.detach();
7083 }
7084 if (mAudioTurnedOnTimer != null) {
7085 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007086 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007087 }
7088 if (mVideoTurnedOnTimer != null) {
7089 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007090 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007091 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007092 if (mFlashlightTurnedOnTimer != null) {
7093 mFlashlightTurnedOnTimer.detach();
7094 mFlashlightTurnedOnTimer = null;
7095 }
7096 if (mCameraTurnedOnTimer != null) {
7097 mCameraTurnedOnTimer.detach();
7098 mCameraTurnedOnTimer = null;
7099 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007100 if (mForegroundActivityTimer != null) {
7101 mForegroundActivityTimer.detach();
7102 mForegroundActivityTimer = null;
7103 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007104 if (mForegroundServiceTimer != null) {
7105 mForegroundServiceTimer.detach();
7106 mForegroundServiceTimer = null;
7107 }
Bookatzc8c44962017-05-11 12:12:54 -07007108 if (mAggregatedPartialWakelockTimer != null) {
7109 mAggregatedPartialWakelockTimer.detach();
7110 mAggregatedPartialWakelockTimer = null;
7111 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007112 if (mBluetoothScanTimer != null) {
7113 mBluetoothScanTimer.detach();
7114 mBluetoothScanTimer = null;
7115 }
Bookatzb1f04f32017-05-19 13:57:32 -07007116 if (mBluetoothUnoptimizedScanTimer != null) {
7117 mBluetoothUnoptimizedScanTimer.detach();
7118 mBluetoothUnoptimizedScanTimer = null;
7119 }
Bookatz956f36bf2017-04-28 09:48:17 -07007120 if (mBluetoothScanResultCounter != null) {
7121 mBluetoothScanResultCounter.detach();
7122 mBluetoothScanResultCounter = null;
7123 }
Bookatzb1f04f32017-05-19 13:57:32 -07007124 if (mBluetoothScanResultBgCounter != null) {
7125 mBluetoothScanResultBgCounter.detach();
7126 mBluetoothScanResultBgCounter = null;
7127 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007128 if (mUserActivityCounters != null) {
7129 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7130 mUserActivityCounters[i].detach();
7131 }
7132 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007133 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007134 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007135 mNetworkByteActivityCounters[i].detach();
7136 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007137 }
7138 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007139
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007140 if (mWifiControllerActivity != null) {
7141 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07007142 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007143
7144 if (mBluetoothControllerActivity != null) {
7145 mBluetoothControllerActivity.detach();
7146 }
7147
7148 if (mModemControllerActivity != null) {
7149 mModemControllerActivity.detach();
7150 }
7151
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007152 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007153
7154 mUserCpuTime.detach();
7155 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07007156
Sudheer Shankaaf857412017-07-21 00:14:24 -07007157 if (mCpuClusterSpeedTimesUs != null) {
7158 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007159 if (cpuSpeeds != null) {
7160 for (LongSamplingCounter c : cpuSpeeds) {
7161 if (c != null) {
7162 c.detach();
7163 }
7164 }
7165 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007166 }
7167 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007168
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007169 if (mCpuFreqTimeMs != null) {
7170 mCpuFreqTimeMs.detach();
7171 }
7172 if (mScreenOffCpuFreqTimeMs != null) {
7173 mScreenOffCpuFreqTimeMs.detach();
7174 }
7175
Adam Lesinski5f056f62016-07-14 16:56:08 -07007176 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
7177 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007178 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007179
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007180 return !active;
7181 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007182
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007183 void writeJobCompletionsToParcelLocked(Parcel out) {
7184 int NJC = mJobCompletions.size();
7185 out.writeInt(NJC);
7186 for (int ijc=0; ijc<NJC; ijc++) {
7187 out.writeString(mJobCompletions.keyAt(ijc));
7188 SparseIntArray types = mJobCompletions.valueAt(ijc);
7189 int NT = types.size();
7190 out.writeInt(NT);
7191 for (int it=0; it<NT; it++) {
7192 out.writeInt(types.keyAt(it));
7193 out.writeInt(types.valueAt(it));
7194 }
7195 }
7196 }
7197
Bookatz867c0d72017-03-07 18:23:42 -08007198 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
7199 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07007200 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08007201
Dianne Hackbornd953c532014-08-16 18:17:38 -07007202 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
7203 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07007204 out.writeInt(NW);
7205 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007206 out.writeString(wakeStats.keyAt(iw));
7207 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007208 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210
Bookatz2bffb5b2017-04-13 11:59:33 -07007211 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007212 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007213 out.writeInt(NS);
7214 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007215 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07007216 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007217 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
7218 }
7219
Bookatzaa4594a2017-03-24 12:39:56 -07007220 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007221 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007222 out.writeInt(NJ);
7223 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007224 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07007225 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007226 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
7227 }
7228
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007229 writeJobCompletionsToParcelLocked(out);
7230
Dianne Hackborn61659e52014-07-09 16:13:01 -07007231 int NSE = mSensorStats.size();
7232 out.writeInt(NSE);
7233 for (int ise=0; ise<NSE; ise++) {
7234 out.writeInt(mSensorStats.keyAt(ise));
7235 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007236 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 }
7238
Dianne Hackborn61659e52014-07-09 16:13:01 -07007239 int NP = mProcessStats.size();
7240 out.writeInt(NP);
7241 for (int ip=0; ip<NP; ip++) {
7242 out.writeString(mProcessStats.keyAt(ip));
7243 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 proc.writeToParcelLocked(out);
7245 }
7246
7247 out.writeInt(mPackageStats.size());
7248 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
7249 out.writeString(pkgEntry.getKey());
7250 Uid.Pkg pkg = pkgEntry.getValue();
7251 pkg.writeToParcelLocked(out);
7252 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007253
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007254 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007255 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007256 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007257 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007258 out.writeInt(0);
7259 }
7260 if (mFullWifiLockTimer != null) {
7261 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007262 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007263 } else {
7264 out.writeInt(0);
7265 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007266 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007267 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007268 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007269 } else {
7270 out.writeInt(0);
7271 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007272 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7273 if (mWifiBatchedScanTimer[i] != null) {
7274 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007275 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007276 } else {
7277 out.writeInt(0);
7278 }
7279 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007280 if (mWifiMulticastTimer != null) {
7281 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007282 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007283 } else {
7284 out.writeInt(0);
7285 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007286
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007287 if (mAudioTurnedOnTimer != null) {
7288 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007289 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007290 } else {
7291 out.writeInt(0);
7292 }
7293 if (mVideoTurnedOnTimer != null) {
7294 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007295 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007296 } else {
7297 out.writeInt(0);
7298 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007299 if (mFlashlightTurnedOnTimer != null) {
7300 out.writeInt(1);
7301 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7302 } else {
7303 out.writeInt(0);
7304 }
7305 if (mCameraTurnedOnTimer != null) {
7306 out.writeInt(1);
7307 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7308 } else {
7309 out.writeInt(0);
7310 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007311 if (mForegroundActivityTimer != null) {
7312 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007313 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007314 } else {
7315 out.writeInt(0);
7316 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007317 if (mForegroundServiceTimer != null) {
7318 out.writeInt(1);
7319 mForegroundServiceTimer.writeToParcel(out, elapsedRealtimeUs);
7320 } else {
7321 out.writeInt(0);
7322 }
Bookatzc8c44962017-05-11 12:12:54 -07007323 if (mAggregatedPartialWakelockTimer != null) {
7324 out.writeInt(1);
7325 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
7326 } else {
7327 out.writeInt(0);
7328 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007329 if (mBluetoothScanTimer != null) {
7330 out.writeInt(1);
7331 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
7332 } else {
7333 out.writeInt(0);
7334 }
Bookatzb1f04f32017-05-19 13:57:32 -07007335 if (mBluetoothUnoptimizedScanTimer != null) {
7336 out.writeInt(1);
7337 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
7338 } else {
7339 out.writeInt(0);
7340 }
Bookatz956f36bf2017-04-28 09:48:17 -07007341 if (mBluetoothScanResultCounter != null) {
7342 out.writeInt(1);
7343 mBluetoothScanResultCounter.writeToParcel(out);
7344 } else {
7345 out.writeInt(0);
7346 }
Bookatzb1f04f32017-05-19 13:57:32 -07007347 if (mBluetoothScanResultBgCounter != null) {
7348 out.writeInt(1);
7349 mBluetoothScanResultBgCounter.writeToParcel(out);
7350 } else {
7351 out.writeInt(0);
7352 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007353 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7354 if (mProcessStateTimer[i] != null) {
7355 out.writeInt(1);
7356 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
7357 } else {
7358 out.writeInt(0);
7359 }
7360 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007361 if (mVibratorOnTimer != null) {
7362 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007363 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007364 } else {
7365 out.writeInt(0);
7366 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007367 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007368 out.writeInt(1);
7369 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7370 mUserActivityCounters[i].writeToParcel(out);
7371 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007372 } else {
7373 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007374 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007375 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007376 out.writeInt(1);
7377 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007378 mNetworkByteActivityCounters[i].writeToParcel(out);
7379 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007380 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007381 mMobileRadioActiveTime.writeToParcel(out);
7382 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007383 } else {
7384 out.writeInt(0);
7385 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007386
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007387 if (mWifiControllerActivity != null) {
7388 out.writeInt(1);
7389 mWifiControllerActivity.writeToParcel(out, 0);
7390 } else {
7391 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007392 }
7393
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007394 if (mBluetoothControllerActivity != null) {
7395 out.writeInt(1);
7396 mBluetoothControllerActivity.writeToParcel(out, 0);
7397 } else {
7398 out.writeInt(0);
7399 }
7400
7401 if (mModemControllerActivity != null) {
7402 out.writeInt(1);
7403 mModemControllerActivity.writeToParcel(out, 0);
7404 } else {
7405 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007406 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007407
7408 mUserCpuTime.writeToParcel(out);
7409 mSystemCpuTime.writeToParcel(out);
7410
Sudheer Shankaaf857412017-07-21 00:14:24 -07007411 if (mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007412 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -07007413 out.writeInt(mCpuClusterSpeedTimesUs.length);
7414 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007415 if (cpuSpeeds != null) {
7416 out.writeInt(1);
7417 out.writeInt(cpuSpeeds.length);
7418 for (LongSamplingCounter c : cpuSpeeds) {
7419 if (c != null) {
7420 out.writeInt(1);
7421 c.writeToParcel(out);
7422 } else {
7423 out.writeInt(0);
7424 }
7425 }
7426 } else {
7427 out.writeInt(0);
7428 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007429 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007430 } else {
7431 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007432 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007433
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007434 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
7435 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007436
Adam Lesinski5f056f62016-07-14 16:56:08 -07007437 if (mMobileRadioApWakeupCount != null) {
7438 out.writeInt(1);
7439 mMobileRadioApWakeupCount.writeToParcel(out);
7440 } else {
7441 out.writeInt(0);
7442 }
7443
7444 if (mWifiRadioApWakeupCount != null) {
7445 out.writeInt(1);
7446 mWifiRadioApWakeupCount.writeToParcel(out);
7447 } else {
7448 out.writeInt(0);
7449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 }
7451
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007452 void readJobCompletionsFromParcelLocked(Parcel in) {
7453 int numJobCompletions = in.readInt();
7454 mJobCompletions.clear();
7455 for (int j = 0; j < numJobCompletions; j++) {
7456 String jobName = in.readString();
7457 int numTypes = in.readInt();
7458 if (numTypes > 0) {
7459 SparseIntArray types = new SparseIntArray();
7460 for (int k = 0; k < numTypes; k++) {
7461 int type = in.readInt();
7462 int count = in.readInt();
7463 types.put(type, count);
7464 }
7465 mJobCompletions.put(jobName, types);
7466 }
7467 }
7468 }
7469
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007470 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08007471 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07007472 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08007473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 int numWakelocks = in.readInt();
7475 mWakelockStats.clear();
7476 for (int j = 0; j < numWakelocks; j++) {
7477 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007478 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07007479 wakelock.readFromParcelLocked(
7480 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007481 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 }
7483
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007484 int numSyncs = in.readInt();
7485 mSyncStats.clear();
7486 for (int j = 0; j < numSyncs; j++) {
7487 String syncName = in.readString();
7488 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007489 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
7490 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007491 }
7492 }
7493
7494 int numJobs = in.readInt();
7495 mJobStats.clear();
7496 for (int j = 0; j < numJobs; j++) {
7497 String jobName = in.readString();
7498 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07007499 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
7500 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007501 }
7502 }
7503
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007504 readJobCompletionsFromParcelLocked(in);
7505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 int numSensors = in.readInt();
7507 mSensorStats.clear();
7508 for (int k = 0; k < numSensors; k++) {
7509 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007510 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08007511 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7512 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 mSensorStats.put(sensorNumber, sensor);
7514 }
7515
7516 int numProcs = in.readInt();
7517 mProcessStats.clear();
7518 for (int k = 0; k < numProcs; k++) {
7519 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007520 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 proc.readFromParcelLocked(in);
7522 mProcessStats.put(processName, proc);
7523 }
7524
7525 int numPkgs = in.readInt();
7526 mPackageStats.clear();
7527 for (int l = 0; l < numPkgs; l++) {
7528 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007529 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530 pkg.readFromParcelLocked(in);
7531 mPackageStats.put(packageName, pkg);
7532 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007533
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007534 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007535 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007536 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7537 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007538 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007539 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007540 }
7541 mFullWifiLockOut = false;
7542 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007543 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7544 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007545 } else {
7546 mFullWifiLockTimer = null;
7547 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007548 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007549 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007550 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7551 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7552 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007553 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007554 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007555 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007556 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7557 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7558 if (in.readInt() != 0) {
7559 makeWifiBatchedScanBin(i, in);
7560 } else {
7561 mWifiBatchedScanTimer[i] = null;
7562 }
7563 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007564 mWifiMulticastEnabled = false;
7565 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007566 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
7567 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007568 } else {
7569 mWifiMulticastTimer = null;
7570 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007571 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007572 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7573 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007574 } else {
7575 mAudioTurnedOnTimer = null;
7576 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007577 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007578 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7579 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007580 } else {
7581 mVideoTurnedOnTimer = null;
7582 }
7583 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007584 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7585 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007586 } else {
7587 mFlashlightTurnedOnTimer = null;
7588 }
7589 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007590 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7591 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007592 } else {
7593 mCameraTurnedOnTimer = null;
7594 }
7595 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007596 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7597 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007598 } else {
7599 mForegroundActivityTimer = null;
7600 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007601 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007602 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7603 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase, in);
7604 } else {
7605 mForegroundServiceTimer = null;
7606 }
7607 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07007608 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7609 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7610 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
7611 in);
7612 } else {
7613 mAggregatedPartialWakelockTimer = null;
7614 }
7615 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007616 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7617 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7618 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007619 } else {
7620 mBluetoothScanTimer = null;
7621 }
Bookatz956f36bf2017-04-28 09:48:17 -07007622 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07007623 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7624 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7625 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
7626 } else {
7627 mBluetoothUnoptimizedScanTimer = null;
7628 }
7629 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07007630 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
7631 } else {
7632 mBluetoothScanResultCounter = null;
7633 }
Bookatzb1f04f32017-05-19 13:57:32 -07007634 if (in.readInt() != 0) {
7635 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
7636 } else {
7637 mBluetoothScanResultBgCounter = null;
7638 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007639 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007640 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7641 if (in.readInt() != 0) {
7642 makeProcessState(i, in);
7643 } else {
7644 mProcessStateTimer[i] = null;
7645 }
7646 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007647 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007648 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7649 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007650 } else {
7651 mVibratorOnTimer = null;
7652 }
7653 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007654 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7655 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007656 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007657 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007658 } else {
7659 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07007660 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007661 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007662 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7663 mNetworkPacketActivityCounters
7664 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007665 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007666 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007667 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007668 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007669 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007670 }
Joe Onoratoabded112016-02-08 16:49:39 -08007671 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7672 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007673 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007674 mNetworkByteActivityCounters = null;
7675 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007676 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007677
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007678 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007679 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007680 NUM_WIFI_TX_LEVELS, in);
7681 } else {
7682 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007683 }
7684
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007685 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007686 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007687 NUM_BT_TX_LEVELS, in);
7688 } else {
7689 mBluetoothControllerActivity = null;
7690 }
7691
7692 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007693 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007694 ModemActivityInfo.TX_POWER_LEVELS, in);
7695 } else {
7696 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007697 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007698
Joe Onoratoabded112016-02-08 16:49:39 -08007699 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7700 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007701
Adam Lesinski6832f392015-09-05 18:05:40 -07007702 if (in.readInt() != 0) {
7703 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007704 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007705 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007706 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007707
Sudheer Shankaaf857412017-07-21 00:14:24 -07007708 mCpuClusterSpeedTimesUs = new LongSamplingCounter[numCpuClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07007709 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
7710 if (in.readInt() != 0) {
7711 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007712 if (mBsi.mPowerProfile != null &&
7713 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007714 throw new ParcelFormatException("Incompatible number of cpu speeds");
7715 }
7716
7717 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
Sudheer Shankaaf857412017-07-21 00:14:24 -07007718 mCpuClusterSpeedTimesUs[cluster] = cpuSpeeds;
Adam Lesinski6832f392015-09-05 18:05:40 -07007719 for (int speed = 0; speed < numSpeeds; speed++) {
7720 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007721 cpuSpeeds[speed] = new LongSamplingCounter(
7722 mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07007723 }
7724 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07007725 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007726 mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07007727 }
7728 }
7729 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007730 mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007731 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007732
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007733 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
7734 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
7735 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007736
7737 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07007738 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7739 } else {
7740 mMobileRadioApWakeupCount = null;
7741 }
7742
7743 if (in.readInt() != 0) {
7744 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7745 } else {
7746 mWifiRadioApWakeupCount = null;
7747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 }
7749
7750 /**
7751 * The statistics associated with a particular wake lock.
7752 */
Joe Onoratoabded112016-02-08 16:49:39 -08007753 public static class Wakelock extends BatteryStats.Uid.Wakelock {
7754 /**
7755 * BatteryStatsImpl that we are associated with.
7756 */
7757 protected BatteryStatsImpl mBsi;
7758
7759 /**
7760 * BatteryStatsImpl that we are associated with.
7761 */
7762 protected Uid mUid;
7763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007764 /**
7765 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07007766 * 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 -08007767 */
Bookatz5b5ec322017-05-26 09:40:38 -07007768 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769
7770 /**
7771 * How long (in ms) this uid has been keeping the device fully awake.
7772 */
Evan Millarc64edde2009-04-18 12:26:32 -07007773 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774
7775 /**
7776 * How long (in ms) this uid has had a window keeping the device awake.
7777 */
Evan Millarc64edde2009-04-18 12:26:32 -07007778 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779
7780 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007781 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07007782 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007783 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007784
Joe Onoratoabded112016-02-08 16:49:39 -08007785 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
7786 mBsi = bsi;
7787 mUid = uid;
7788 }
7789
Adam Lesinski9425fe22015-06-19 12:02:13 -07007790 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7792 * proper timer pool from the given BatteryStatsImpl object.
7793 *
7794 * @param in the Parcel to be read from.
7795 * return a new Timer, or null.
7796 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007797 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7798 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 if (in.readInt() == 0) {
7800 return null;
7801 }
7802
Joe Onoratoabded112016-02-08 16:49:39 -08007803 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 }
7805
Joe Onorato92fd23f2016-07-25 11:18:42 -07007806 /**
7807 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7808 * proper timer pool from the given BatteryStatsImpl object.
7809 *
7810 * @param in the Parcel to be read from.
7811 * return a new Timer, or null.
7812 */
Bookatz5b5ec322017-05-26 09:40:38 -07007813 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
7814 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007815 if (in.readInt() == 0) {
7816 return null;
7817 }
7818
Bookatz5b5ec322017-05-26 09:40:38 -07007819 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007820 }
7821
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007822 boolean reset() {
7823 boolean wlactive = false;
7824 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007825 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007826 }
7827 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007828 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007829 }
7830 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007831 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007832 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007833 if (mTimerDraw != null) {
7834 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007835 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007836 if (!wlactive) {
7837 if (mTimerFull != null) {
7838 mTimerFull.detach();
7839 mTimerFull = null;
7840 }
7841 if (mTimerPartial != null) {
7842 mTimerPartial.detach();
7843 mTimerPartial = null;
7844 }
7845 if (mTimerWindow != null) {
7846 mTimerWindow.detach();
7847 mTimerWindow = null;
7848 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007849 if (mTimerDraw != null) {
7850 mTimerDraw.detach();
7851 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007852 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007853 }
7854 return !wlactive;
7855 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007856
Bookatz5b5ec322017-05-26 09:40:38 -07007857 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
7858 TimeBase screenOffBgTimeBase, Parcel in) {
7859 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
7860 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007861 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7862 mBsi.mFullTimers, timeBase, in);
7863 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7864 mBsi.mWindowTimers, timeBase, in);
7865 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7866 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 }
7868
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007869 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7870 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7871 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7872 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007873 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 }
7875
7876 @Override
7877 public Timer getWakeTime(int type) {
7878 switch (type) {
7879 case WAKE_TYPE_FULL: return mTimerFull;
7880 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7881 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007882 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 default: throw new IllegalArgumentException("type = " + type);
7884 }
7885 }
7886 }
7887
Joe Onoratoabded112016-02-08 16:49:39 -08007888 public static class Sensor extends BatteryStats.Uid.Sensor {
7889 /**
7890 * BatteryStatsImpl that we are associated with.
7891 */
7892 protected BatteryStatsImpl mBsi;
7893
7894 /**
Bookatz867c0d72017-03-07 18:23:42 -08007895 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007896 */
7897 protected Uid mUid;
7898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007900 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007901
Joe Onoratoabded112016-02-08 16:49:39 -08007902 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7903 mBsi = bsi;
7904 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 mHandle = handle;
7906 }
7907
Bookatz867c0d72017-03-07 18:23:42 -08007908 private DualTimer readTimersFromParcel(
7909 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 if (in.readInt() == 0) {
7911 return null;
7912 }
7913
Joe Onoratoabded112016-02-08 16:49:39 -08007914 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007916 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007917 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 }
Bookatz867c0d72017-03-07 18:23:42 -08007919 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007920 }
7921
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007922 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007923 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007924 mTimer = null;
7925 return true;
7926 }
7927 return false;
7928 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007929
Bookatz867c0d72017-03-07 18:23:42 -08007930 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7931 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 }
7933
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007934 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007935 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 }
7937
7938 @Override
7939 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007940 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007942
7943 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007944 public Timer getSensorBackgroundTime() {
7945 if (mTimer == null) {
7946 return null;
7947 }
7948 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007949 }
7950
7951 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 public int getHandle() {
7953 return mHandle;
7954 }
7955 }
7956
7957 /**
7958 * The statistics associated with a particular process.
7959 */
Joe Onoratoabded112016-02-08 16:49:39 -08007960 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7961 /**
7962 * BatteryStatsImpl that we are associated with.
7963 */
7964 protected BatteryStatsImpl mBsi;
7965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007966 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007967 * The name of this process.
7968 */
7969 final String mName;
7970
7971 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007972 * Remains true until removed from the stats.
7973 */
7974 boolean mActive = true;
7975
7976 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007977 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 */
7979 long mUserTime;
7980
7981 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007982 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 */
7984 long mSystemTime;
7985
7986 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007987 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007988 */
7989 long mForegroundTime;
7990
7991 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007992 * Number of times the process has been started.
7993 */
7994 int mStarts;
7995
7996 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007997 * Number of times the process has crashed.
7998 */
7999 int mNumCrashes;
8000
8001 /**
8002 * Number of times the process has had an ANR.
8003 */
8004 int mNumAnrs;
8005
8006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 * The amount of user time loaded from a previous save.
8008 */
8009 long mLoadedUserTime;
8010
8011 /**
8012 * The amount of system time loaded from a previous save.
8013 */
8014 long mLoadedSystemTime;
8015
8016 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008017 * The amount of foreground time loaded from a previous save.
8018 */
8019 long mLoadedForegroundTime;
8020
8021 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008022 * The number of times the process has started from a previous save.
8023 */
8024 int mLoadedStarts;
8025
8026 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008027 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008029 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030
8031 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008032 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008034 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008035
8036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 * The amount of user time when last unplugged.
8038 */
8039 long mUnpluggedUserTime;
8040
8041 /**
8042 * The amount of system time when last unplugged.
8043 */
8044 long mUnpluggedSystemTime;
8045
8046 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008047 * The amount of foreground time since unplugged.
8048 */
8049 long mUnpluggedForegroundTime;
8050
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008051 /**
8052 * The number of times the process has started before unplugged.
8053 */
8054 int mUnpluggedStarts;
8055
Dianne Hackborn61659e52014-07-09 16:13:01 -07008056 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008057 * Number of times the process has crashed before unplugged.
8058 */
8059 int mUnpluggedNumCrashes;
8060
8061 /**
8062 * Number of times the process has had an ANR before unplugged.
8063 */
8064 int mUnpluggedNumAnrs;
8065
Dianne Hackborn287952c2010-09-22 22:34:31 -07008066 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008067
Joe Onoratoabded112016-02-08 16:49:39 -08008068 public Proc(BatteryStatsImpl bsi, String name) {
8069 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008070 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08008071 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008073
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008074 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 mUnpluggedUserTime = mUserTime;
8076 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008077 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008078 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008079 mUnpluggedNumCrashes = mNumCrashes;
8080 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 }
8082
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008083 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008085
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008086 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008087 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008088 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008089 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008090
Dianne Hackborn287952c2010-09-22 22:34:31 -07008091 public int countExcessivePowers() {
8092 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008093 }
8094
Dianne Hackborn287952c2010-09-22 22:34:31 -07008095 public ExcessivePower getExcessivePower(int i) {
8096 if (mExcessivePower != null) {
8097 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008098 }
8099 return null;
8100 }
8101
Dianne Hackborn287952c2010-09-22 22:34:31 -07008102 public void addExcessiveCpu(long overTime, long usedTime) {
8103 if (mExcessivePower == null) {
8104 mExcessivePower = new ArrayList<ExcessivePower>();
8105 }
8106 ExcessivePower ew = new ExcessivePower();
8107 ew.type = ExcessivePower.TYPE_CPU;
8108 ew.overTime = overTime;
8109 ew.usedTime = usedTime;
8110 mExcessivePower.add(ew);
8111 }
8112
8113 void writeExcessivePowerToParcelLocked(Parcel out) {
8114 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008115 out.writeInt(0);
8116 return;
8117 }
8118
Dianne Hackborn287952c2010-09-22 22:34:31 -07008119 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008120 out.writeInt(N);
8121 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07008122 ExcessivePower ew = mExcessivePower.get(i);
8123 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008124 out.writeLong(ew.overTime);
8125 out.writeLong(ew.usedTime);
8126 }
8127 }
8128
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07008129 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008130 final int N = in.readInt();
8131 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07008132 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07008133 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008134 }
8135
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08008136 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07008137 throw new ParcelFormatException(
8138 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08008139 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008140
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07008141 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008142 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07008143 ExcessivePower ew = new ExcessivePower();
8144 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008145 ew.overTime = in.readLong();
8146 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07008147 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008148 }
8149 }
8150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 void writeToParcelLocked(Parcel out) {
8152 out.writeLong(mUserTime);
8153 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008154 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008156 out.writeInt(mNumCrashes);
8157 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 out.writeLong(mLoadedUserTime);
8159 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008160 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008162 out.writeInt(mLoadedNumCrashes);
8163 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 out.writeLong(mUnpluggedUserTime);
8165 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008166 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008167 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008168 out.writeInt(mUnpluggedNumCrashes);
8169 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07008170 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 }
8172
8173 void readFromParcelLocked(Parcel in) {
8174 mUserTime = in.readLong();
8175 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008176 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008177 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008178 mNumCrashes = in.readInt();
8179 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 mLoadedUserTime = in.readLong();
8181 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008182 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008184 mLoadedNumCrashes = in.readInt();
8185 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 mUnpluggedUserTime = in.readLong();
8187 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008188 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008190 mUnpluggedNumCrashes = in.readInt();
8191 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07008192 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 }
8194
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008195 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 mUserTime += utime;
8197 mSystemTime += stime;
8198 }
8199
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008200 public void addForegroundTimeLocked(long ttime) {
8201 mForegroundTime += ttime;
8202 }
8203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 public void incStartsLocked() {
8205 mStarts++;
8206 }
8207
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008208 public void incNumCrashesLocked() {
8209 mNumCrashes++;
8210 }
8211
8212 public void incNumAnrsLocked() {
8213 mNumAnrs++;
8214 }
8215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008217 public boolean isActive() {
8218 return mActive;
8219 }
8220
8221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008223 long val = mUserTime;
8224 if (which == STATS_CURRENT) {
8225 val -= mLoadedUserTime;
8226 } else if (which == STATS_SINCE_UNPLUGGED) {
8227 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 }
8229 return val;
8230 }
8231
8232 @Override
8233 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008234 long val = mSystemTime;
8235 if (which == STATS_CURRENT) {
8236 val -= mLoadedSystemTime;
8237 } else if (which == STATS_SINCE_UNPLUGGED) {
8238 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 }
8240 return val;
8241 }
8242
8243 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008244 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008245 long val = mForegroundTime;
8246 if (which == STATS_CURRENT) {
8247 val -= mLoadedForegroundTime;
8248 } else if (which == STATS_SINCE_UNPLUGGED) {
8249 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008250 }
8251 return val;
8252 }
8253
8254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008256 int val = mStarts;
8257 if (which == STATS_CURRENT) {
8258 val -= mLoadedStarts;
8259 } else if (which == STATS_SINCE_UNPLUGGED) {
8260 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
8262 return val;
8263 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008264
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008265 @Override
8266 public int getNumCrashes(int which) {
8267 int val = mNumCrashes;
8268 if (which == STATS_CURRENT) {
8269 val -= mLoadedNumCrashes;
8270 } else if (which == STATS_SINCE_UNPLUGGED) {
8271 val -= mUnpluggedNumCrashes;
8272 }
8273 return val;
8274 }
8275
8276 @Override
8277 public int getNumAnrs(int which) {
8278 int val = mNumAnrs;
8279 if (which == STATS_CURRENT) {
8280 val -= mLoadedNumAnrs;
8281 } else if (which == STATS_SINCE_UNPLUGGED) {
8282 val -= mUnpluggedNumAnrs;
8283 }
8284 return val;
8285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 }
8287
8288 /**
8289 * The statistics associated with a particular package.
8290 */
Joe Onoratoabded112016-02-08 16:49:39 -08008291 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
8292 /**
8293 * BatteryStatsImpl that we are associated with.
8294 */
8295 protected BatteryStatsImpl mBsi;
8296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008298 * Number of times wakeup alarms have occurred for this app.
Bookatz98d4d5c2017-08-01 19:07:54 -07008299 * On screen-off timebase starting in report v25.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008301 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302
8303 /**
8304 * The statics we have collected for this package's services.
8305 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008306 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307
Joe Onoratoabded112016-02-08 16:49:39 -08008308 public Pkg(BatteryStatsImpl bsi) {
8309 mBsi = bsi;
8310 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008312
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008313 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 }
8315
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008316 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008318
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008319 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08008320 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008321 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008324 int numWA = in.readInt();
8325 mWakeupAlarms.clear();
8326 for (int i=0; i<numWA; i++) {
8327 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -07008328 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryScreenOffTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330
8331 int numServs = in.readInt();
8332 mServiceStats.clear();
8333 for (int m = 0; m < numServs; m++) {
8334 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008335 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 mServiceStats.put(serviceName, serv);
8337
8338 serv.readFromParcelLocked(in);
8339 }
8340 }
8341
8342 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008343 int numWA = mWakeupAlarms.size();
8344 out.writeInt(numWA);
8345 for (int i=0; i<numWA; i++) {
8346 out.writeString(mWakeupAlarms.keyAt(i));
8347 mWakeupAlarms.valueAt(i).writeToParcel(out);
8348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008350 final int NS = mServiceStats.size();
8351 out.writeInt(NS);
8352 for (int i=0; i<NS; i++) {
8353 out.writeString(mServiceStats.keyAt(i));
8354 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 serv.writeToParcelLocked(out);
8356 }
8357 }
8358
8359 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008360 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
8361 return mWakeupAlarms;
8362 }
8363
8364 public void noteWakeupAlarmLocked(String tag) {
8365 Counter c = mWakeupAlarms.get(tag);
8366 if (c == null) {
Bookatz98d4d5c2017-08-01 19:07:54 -07008367 c = new Counter(mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008368 mWakeupAlarms.put(tag, c);
8369 }
8370 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 }
8372
8373 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008374 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
8375 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 }
8377
8378 /**
8379 * The statistics associated with a particular service.
8380 */
Joe Onoratoabded112016-02-08 16:49:39 -08008381 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
8382 /**
8383 * BatteryStatsImpl that we are associated with.
8384 */
8385 protected BatteryStatsImpl mBsi;
8386
8387 /**
8388 * The android package in which this service resides.
8389 */
8390 protected Pkg mPkg;
8391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 /**
8393 * Total time (ms in battery uptime) the service has been left started.
8394 */
Joe Onoratoabded112016-02-08 16:49:39 -08008395 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396
8397 /**
8398 * If service has been started and not yet stopped, this is
8399 * when it was started.
8400 */
Joe Onoratoabded112016-02-08 16:49:39 -08008401 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402
8403 /**
8404 * True if we are currently running.
8405 */
Joe Onoratoabded112016-02-08 16:49:39 -08008406 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407
8408 /**
8409 * Total number of times startService() has been called.
8410 */
Joe Onoratoabded112016-02-08 16:49:39 -08008411 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412
8413 /**
8414 * Total time (ms in battery uptime) the service has been left launched.
8415 */
Joe Onoratoabded112016-02-08 16:49:39 -08008416 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417
8418 /**
8419 * If service has been launched and not yet exited, this is
8420 * when it was launched (ms in battery uptime).
8421 */
Joe Onoratoabded112016-02-08 16:49:39 -08008422 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423
8424 /**
8425 * True if we are currently launched.
8426 */
Joe Onoratoabded112016-02-08 16:49:39 -08008427 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428
8429 /**
8430 * Total number times the service has been launched.
8431 */
Joe Onoratoabded112016-02-08 16:49:39 -08008432 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433
8434 /**
8435 * The amount of time spent started loaded from a previous save
8436 * (ms in battery uptime).
8437 */
Joe Onoratoabded112016-02-08 16:49:39 -08008438 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439
8440 /**
8441 * The number of starts loaded from a previous save.
8442 */
Joe Onoratoabded112016-02-08 16:49:39 -08008443 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444
8445 /**
8446 * The number of launches loaded from a previous save.
8447 */
Joe Onoratoabded112016-02-08 16:49:39 -08008448 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449
8450 /**
8451 * The amount of time spent started as of the last run (ms
8452 * in battery uptime).
8453 */
Joe Onoratoabded112016-02-08 16:49:39 -08008454 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455
8456 /**
8457 * The number of starts as of the last run.
8458 */
Joe Onoratoabded112016-02-08 16:49:39 -08008459 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460
8461 /**
8462 * The number of launches as of the last run.
8463 */
Joe Onoratoabded112016-02-08 16:49:39 -08008464 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465
8466 /**
8467 * The amount of time spent started when last unplugged (ms
8468 * in battery uptime).
8469 */
Joe Onoratoabded112016-02-08 16:49:39 -08008470 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471
8472 /**
8473 * The number of starts when last unplugged.
8474 */
Joe Onoratoabded112016-02-08 16:49:39 -08008475 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476
8477 /**
8478 * The number of launches when last unplugged.
8479 */
Joe Onoratoabded112016-02-08 16:49:39 -08008480 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481
Joe Onoratoabded112016-02-08 16:49:39 -08008482 /**
8483 * Construct a Serv. Also adds it to the on-battery time base as a listener.
8484 */
8485 public Serv(BatteryStatsImpl bsi) {
8486 mBsi = bsi;
8487 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008489
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008490 public void onTimeStarted(long elapsedRealtime, long baseUptime,
8491 long baseRealtime) {
8492 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 mUnpluggedStarts = mStarts;
8494 mUnpluggedLaunches = mLaunches;
8495 }
8496
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008497 public void onTimeStopped(long elapsedRealtime, long baseUptime,
8498 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008500
Joe Onoratoabded112016-02-08 16:49:39 -08008501 /**
8502 * Remove this Serv as a listener from the time base.
8503 */
8504 public void detach() {
8505 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008506 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008507
Joe Onoratoabded112016-02-08 16:49:39 -08008508 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 mStartTime = in.readLong();
8510 mRunningSince = in.readLong();
8511 mRunning = in.readInt() != 0;
8512 mStarts = in.readInt();
8513 mLaunchedTime = in.readLong();
8514 mLaunchedSince = in.readLong();
8515 mLaunched = in.readInt() != 0;
8516 mLaunches = in.readInt();
8517 mLoadedStartTime = in.readLong();
8518 mLoadedStarts = in.readInt();
8519 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008520 mLastStartTime = 0;
8521 mLastStarts = 0;
8522 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 mUnpluggedStartTime = in.readLong();
8524 mUnpluggedStarts = in.readInt();
8525 mUnpluggedLaunches = in.readInt();
8526 }
8527
Joe Onoratoabded112016-02-08 16:49:39 -08008528 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 out.writeLong(mStartTime);
8530 out.writeLong(mRunningSince);
8531 out.writeInt(mRunning ? 1 : 0);
8532 out.writeInt(mStarts);
8533 out.writeLong(mLaunchedTime);
8534 out.writeLong(mLaunchedSince);
8535 out.writeInt(mLaunched ? 1 : 0);
8536 out.writeInt(mLaunches);
8537 out.writeLong(mLoadedStartTime);
8538 out.writeInt(mLoadedStarts);
8539 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 out.writeLong(mUnpluggedStartTime);
8541 out.writeInt(mUnpluggedStarts);
8542 out.writeInt(mUnpluggedLaunches);
8543 }
8544
Joe Onoratoabded112016-02-08 16:49:39 -08008545 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 if (!mLaunched) return mLaunchedTime;
8547 return mLaunchedTime + batteryUptime - mLaunchedSince;
8548 }
8549
Joe Onoratoabded112016-02-08 16:49:39 -08008550 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 if (!mRunning) return mStartTime;
8552 return mStartTime + batteryUptime - mRunningSince;
8553 }
8554
8555 public void startLaunchedLocked() {
8556 if (!mLaunched) {
8557 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08008558 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 mLaunched = true;
8560 }
8561 }
8562
8563 public void stopLaunchedLocked() {
8564 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08008565 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 if (time > 0) {
8567 mLaunchedTime += time;
8568 } else {
8569 mLaunches--;
8570 }
8571 mLaunched = false;
8572 }
8573 }
8574
8575 public void startRunningLocked() {
8576 if (!mRunning) {
8577 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08008578 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 mRunning = true;
8580 }
8581 }
8582
8583 public void stopRunningLocked() {
8584 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08008585 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 if (time > 0) {
8587 mStartTime += time;
8588 } else {
8589 mStarts--;
8590 }
8591 mRunning = false;
8592 }
8593 }
8594
8595 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008596 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 }
8598
8599 @Override
8600 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008601 int val = mLaunches;
8602 if (which == STATS_CURRENT) {
8603 val -= mLoadedLaunches;
8604 } else if (which == STATS_SINCE_UNPLUGGED) {
8605 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 return val;
8608 }
8609
8610 @Override
8611 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008612 long val = getStartTimeToNowLocked(now);
8613 if (which == STATS_CURRENT) {
8614 val -= mLoadedStartTime;
8615 } else if (which == STATS_SINCE_UNPLUGGED) {
8616 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008618 return val;
8619 }
8620
8621 @Override
8622 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008623 int val = mStarts;
8624 if (which == STATS_CURRENT) {
8625 val -= mLoadedStarts;
8626 } else if (which == STATS_SINCE_UNPLUGGED) {
8627 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 }
8629
8630 return val;
8631 }
8632 }
8633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08008635 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 }
8637 }
8638
8639 /**
8640 * Retrieve the statistics object for a particular process, creating
8641 * if needed.
8642 */
8643 public Proc getProcessStatsLocked(String name) {
8644 Proc ps = mProcessStats.get(name);
8645 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008646 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 mProcessStats.put(name, ps);
8648 }
8649
8650 return ps;
8651 }
8652
Dianne Hackborna8d10942015-11-19 17:55:19 -08008653 public void updateUidProcessStateLocked(int procState) {
8654 int uidRunningState;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008655 // Make special note of Foreground Services
8656 final boolean userAwareService =
8657 (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008658 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
8659 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
8660 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
8661 uidRunningState = PROCESS_STATE_TOP;
8662 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
8663 // Persistent and other foreground states go here.
8664 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
8665 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
8666 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
8667 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
8668 // Persistent and other foreground states go here.
8669 uidRunningState = PROCESS_STATE_FOREGROUND;
8670 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
8671 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008672 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08008673 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008674 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008675
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008676 if (mProcessState == uidRunningState && userAwareService == mInForegroundService) {
8677 return;
8678 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08008679
Bookatz867c0d72017-03-07 18:23:42 -08008680 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008681 if (mProcessState != uidRunningState) {
8682 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08008683
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008684 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8685 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008686 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008687 mProcessState = uidRunningState;
8688 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8689 if (mProcessStateTimer[uidRunningState] == null) {
8690 makeProcessState(uidRunningState, null);
8691 }
8692 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
8693 }
8694
8695 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
8696 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008697 }
Bookatz867c0d72017-03-07 18:23:42 -08008698
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008699 if (userAwareService != mInForegroundService) {
8700 if (userAwareService) {
8701 noteForegroundServiceResumedLocked(elapsedRealtimeMs);
8702 } else {
8703 noteForegroundServicePausedLocked(elapsedRealtimeMs);
8704 }
8705 mInForegroundService = userAwareService;
8706 }
Bookatz867c0d72017-03-07 18:23:42 -08008707 }
8708
Bookatzc8c44962017-05-11 12:12:54 -07008709 /** Whether to consider Uid to be in the background for background timebase purposes. */
8710 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08008711 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
8712 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07008713 return mProcessState >= PROCESS_STATE_BACKGROUND;
8714 }
8715
8716 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
8717 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
8718 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
8719 }
8720
8721 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
8722 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
8723 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008724 }
8725
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008726 public SparseArray<? extends Pid> getPidStats() {
8727 return mPids;
8728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008729
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008730 public Pid getPidStatsLocked(int pid) {
8731 Pid p = mPids.get(pid);
8732 if (p == null) {
8733 p = new Pid();
8734 mPids.put(pid, p);
8735 }
8736 return p;
8737 }
8738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 /**
8740 * Retrieve the statistics object for a particular service, creating
8741 * if needed.
8742 */
8743 public Pkg getPackageStatsLocked(String name) {
8744 Pkg ps = mPackageStats.get(name);
8745 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008746 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 mPackageStats.put(name, ps);
8748 }
8749
8750 return ps;
8751 }
8752
8753 /**
8754 * Retrieve the statistics object for a particular service, creating
8755 * if needed.
8756 */
8757 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
8758 Pkg ps = getPackageStatsLocked(pkg);
8759 Pkg.Serv ss = ps.mServiceStats.get(serv);
8760 if (ss == null) {
8761 ss = ps.newServiceStatsLocked();
8762 ps.mServiceStats.put(serv, ss);
8763 }
8764
8765 return ss;
8766 }
8767
Dianne Hackbornd953c532014-08-16 18:17:38 -07008768 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008769 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008770 timer.readSummaryFromParcelLocked(in);
8771 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008772 }
8773
Dianne Hackbornd953c532014-08-16 18:17:38 -07008774 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008775 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008776 timer.readSummaryFromParcelLocked(in);
8777 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008778 }
8779
Dianne Hackbornd953c532014-08-16 18:17:38 -07008780 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008781 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008782 mWakelockStats.add(wlName, wl);
8783 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008784 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008785 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008786 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008787 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008788 }
8789 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008790 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008792 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008793 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 }
8796
Bookatz867c0d72017-03-07 18:23:42 -08008797 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 Sensor se = mSensorStats.get(sensor);
8799 if (se == null) {
8800 if (!create) {
8801 return null;
8802 }
Joe Onoratoabded112016-02-08 16:49:39 -08008803 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 mSensorStats.put(sensor, se);
8805 }
Bookatz867c0d72017-03-07 18:23:42 -08008806 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 if (t != null) {
8808 return t;
8809 }
Joe Onoratoabded112016-02-08 16:49:39 -08008810 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008812 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008813 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 }
Bookatz867c0d72017-03-07 18:23:42 -08008815 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8816 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008817 se.mTimer = t;
8818 return t;
8819 }
8820
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008821 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008822 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008823 if (t != null) {
8824 t.startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008825 // TODO(statsd): Possibly use a worksource instead of a uid.
8826 StatsLog.write(StatsLog.SYNC_STATE_CHANGED, getUid(), name, 1);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008827 }
8828 }
8829
8830 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008831 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008832 if (t != null) {
8833 t.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008834 if (!t.isRunningLocked()) { // only tell statsd if truly stopped
8835 // TODO(statsd): Possibly use a worksource instead of a uid.
8836 StatsLog.write(StatsLog.SYNC_STATE_CHANGED, getUid(), name, 0);
8837 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008838 }
8839 }
8840
8841 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008842 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008843 if (t != null) {
8844 t.startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008845 // TODO(statsd): Possibly use a worksource instead of a uid.
8846 StatsLog.write(StatsLog.SCHEDULED_JOB_STATE_CHANGED, getUid(), name, 1);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008847 }
8848 }
8849
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008850 public void noteStopJobLocked(String name, long elapsedRealtimeMs, int stopReason) {
Bookatzaa4594a2017-03-24 12:39:56 -07008851 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008852 if (t != null) {
8853 t.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008854 if (!t.isRunningLocked()) { // only tell statsd if truly stopped
8855 // TODO(statsd): Possibly use a worksource instead of a uid.
8856 StatsLog.write(StatsLog.SCHEDULED_JOB_STATE_CHANGED, getUid(), name, 0);
8857 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008858 }
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008859 if (mBsi.mOnBatteryTimeBase.isRunning()) {
8860 SparseIntArray types = mJobCompletions.get(name);
8861 if (types == null) {
8862 types = new SparseIntArray();
8863 mJobCompletions.put(name, types);
8864 }
8865 int last = types.get(stopReason, 0);
8866 types.put(stopReason, last + 1);
8867 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008868 }
8869
Bookatz5b5ec322017-05-26 09:40:38 -07008870 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
8871 if (wl == null) {
8872 return null;
8873 }
8874 switch (type) {
8875 case WAKE_TYPE_PARTIAL: {
8876 DualTimer t = wl.mTimerPartial;
8877 if (t == null) {
8878 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
8879 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
8880 mOnBatteryScreenOffBackgroundTimeBase);
8881 wl.mTimerPartial = t;
8882 }
8883 return t;
8884 }
8885 case WAKE_TYPE_FULL: {
8886 StopwatchTimer t = wl.mTimerFull;
8887 if (t == null) {
8888 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
8889 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
8890 wl.mTimerFull = t;
8891 }
8892 return t;
8893 }
8894 case WAKE_TYPE_WINDOW: {
8895 StopwatchTimer t = wl.mTimerWindow;
8896 if (t == null) {
8897 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
8898 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
8899 wl.mTimerWindow = t;
8900 }
8901 return t;
8902 }
8903 case WAKE_TYPE_DRAW: {
8904 StopwatchTimer t = wl.mTimerDraw;
8905 if (t == null) {
8906 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
8907 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
8908 wl.mTimerDraw = t;
8909 }
8910 return t;
8911 }
8912 default:
8913 throw new IllegalArgumentException("type=" + type);
8914 }
8915 }
8916
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008917 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008918 Wakelock wl = mWakelockStats.startObject(name);
8919 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008920 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
Bookatzd6746242017-10-24 18:39:35 -07008921 // TODO(statsd): Hopefully use a worksource instead of a uid (so move elsewhere)
8922 StatsLog.write(StatsLog.WAKELOCK_STATE_CHANGED, getUid(), type, name, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 }
Bookatzc8c44962017-05-11 12:12:54 -07008924 if (type == WAKE_TYPE_PARTIAL) {
8925 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008926 // TODO(statsd): Possibly use a worksource instead of a uid.
8927 StatsLog.write(StatsLog.UID_WAKELOCK_STATE_CHANGED, getUid(), type, 1);
Bookatzc8c44962017-05-11 12:12:54 -07008928 if (pid >= 0) {
8929 Pid p = getPidStatsLocked(pid);
8930 if (p.mWakeNesting++ == 0) {
8931 p.mWakeStartMs = elapsedRealtimeMs;
8932 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008933 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 }
8936
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008937 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008938 Wakelock wl = mWakelockStats.stopObject(name);
8939 if (wl != null) {
Bookatzd6746242017-10-24 18:39:35 -07008940 StopwatchTimer wlt = getWakelockTimerLocked(wl, type);
8941 wlt.stopRunningLocked(elapsedRealtimeMs);
8942 if (!wlt.isRunningLocked()) { // only tell statsd if truly stopped
8943 // TODO(statsd): Possibly use a worksource instead of a uid.
8944 StatsLog.write(StatsLog.WAKELOCK_STATE_CHANGED, getUid(), type, name, 0);
8945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 }
Bookatzc8c44962017-05-11 12:12:54 -07008947 if (type == WAKE_TYPE_PARTIAL) {
8948 if (mAggregatedPartialWakelockTimer != null) {
8949 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008950 if (!mAggregatedPartialWakelockTimer.isRunningLocked()) {
8951 // TODO(statsd): Possibly use a worksource instead of a uid.
8952 StatsLog.write(StatsLog.UID_WAKELOCK_STATE_CHANGED, getUid(), type,
8953 0);
8954 }
Bookatzc8c44962017-05-11 12:12:54 -07008955 }
8956 if (pid >= 0) {
8957 Pid p = mPids.get(pid);
8958 if (p != null && p.mWakeNesting > 0) {
8959 if (p.mWakeNesting-- == 1) {
8960 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8961 p.mWakeStartMs = 0;
8962 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008963 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008964 }
8965 }
8966 }
8967
Dianne Hackborn287952c2010-09-22 22:34:31 -07008968 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8969 Proc p = getProcessStatsLocked(proc);
8970 if (p != null) {
8971 p.addExcessiveCpu(overTime, usedTime);
8972 }
8973 }
8974
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008975 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008976 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008977 t.startRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008978 // TODO(statsd): Possibly use a worksource instead of a uid.
8979 if (sensor == Sensor.GPS) {
8980 StatsLog.write(StatsLog.GPS_SCAN_STATE_CHANGED, getUid(), 1);
8981 } else {
8982 StatsLog.write(StatsLog.SENSOR_STATE_CHANGED, getUid(), sensor, 1);
8983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008984 }
8985
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008986 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008988 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008990 t.stopRunningLocked(elapsedRealtimeMs);
Bookatzc1a050a2017-10-10 15:49:28 -07008991 if (!t.isRunningLocked()) { // only tell statsd if truly stopped
8992 // TODO(statsd): Possibly use a worksource instead of a uid.
8993 if (sensor == Sensor.GPS) {
8994 StatsLog.write(StatsLog.GPS_SCAN_STATE_CHANGED, getUid(), 0);
8995 } else {
8996 StatsLog.write(StatsLog.SENSOR_STATE_CHANGED, getUid(), sensor, 0);
8997 }
8998 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009001
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009002 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08009003 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009005
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009006 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08009007 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 }
9009
9010 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08009011 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009012 }
9013 }
9014
Sudheer Shanka9b735c52017-05-09 18:26:18 -07009015 public long[] getCpuFreqs() {
9016 return mCpuFreqs;
9017 }
9018
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07009019 public BatteryStatsImpl(File systemDir, Handler handler, PlatformIdleStateCallback cb,
9020 UserInfoProvider userInfoProvider) {
9021 this(new SystemClocks(), systemDir, handler, cb, userInfoProvider);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07009022 }
9023
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07009024 private BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
9025 PlatformIdleStateCallback cb,
9026 UserInfoProvider userInfoProvider) {
Joe Onoratoabded112016-02-08 16:49:39 -08009027 init(clocks);
9028
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009029 if (systemDir != null) {
9030 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
9031 new File(systemDir, "batterystats.bin.tmp"));
9032 } else {
9033 mFile = null;
9034 }
9035 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009036 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -08009037 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08009039 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -07009040 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009041 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08009042 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009043 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009044 }
Joe Onoratoabded112016-02-08 16:49:39 -08009045 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
9046 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
9047 mOnBatteryTimeBase);
9048 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
9049 mOnBatteryTimeBase);
9050 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
9051 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
9052 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
9053 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
9054 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
9055 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
9056 mOnBatteryTimeBase);
9057 }
9058 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
9059 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009060 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08009061 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009062 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009063 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009064 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009065 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
9066 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009067 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009068 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
9069 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
9070 NUM_BT_TX_LEVELS);
9071 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
9072 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08009073 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
9074 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
9075 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009076 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009077 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
9078 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08009079 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
9080 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009081 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08009082 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07009083 mOnBatteryTimeBase);
9084 }
Joe Onoratoabded112016-02-08 16:49:39 -08009085 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9086 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
9087 mOnBatteryTimeBase);
9088 }
9089 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9090 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
9091 mOnBatteryTimeBase);
9092 }
9093 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
9094 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
9095 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
9096 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
9097 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009098 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -07009099 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
Mike Ma15313c92017-11-15 17:58:21 -08009100 mDischargeLightDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
9101 mDischargeDeepDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009102 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08009104 long uptime = mClocks.uptimeMillis() * 1000;
9105 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009106 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009107 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07009108 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009109 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009110 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07009111 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009112 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009113 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009114 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009115 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07009116 mPlatformIdleStateCallback = cb;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07009117 mUserInfoProvider = userInfoProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118 }
9119
9120 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08009121 this(new SystemClocks(), p);
9122 }
9123
9124 public BatteryStatsImpl(Clocks clocks, Parcel p) {
9125 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009126 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009127 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009128 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07009129 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009130 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009131 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009132 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07009133 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 }
9135
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009136 public void setPowerProfileLocked(PowerProfile profile) {
9137 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07009138
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009139 // We need to initialize the KernelCpuSpeedReaders to read from
9140 // the first cpu of each core. Once we have the PowerProfile, we have access to this
9141 // information.
9142 final int numClusters = mPowerProfile.getNumCpuClusters();
9143 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
9144 int firstCpuOfCluster = 0;
9145 for (int i = 0; i < numClusters; i++) {
9146 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
9147 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
9148 numSpeedSteps);
9149 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
9150 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009151
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009152 if (mEstimatedBatteryCapacity == -1) {
9153 // Initialize the estimated battery capacity to a known preset one.
9154 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -07009155 }
9156 }
9157
Dianne Hackborn0d903a82010-09-07 23:51:03 -07009158 public void setCallback(BatteryCallback cb) {
9159 mCallback = cb;
9160 }
9161
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009162 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07009163 if (mPhoneSignalScanningTimer != null) {
9164 mPhoneSignalScanningTimer.setTimeout(timeout);
9165 }
9166 }
9167
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07009168 public void setExternalStatsSyncLocked(ExternalStatsSync sync) {
9169 mExternalSync = sync;
9170 }
9171
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009172 public void updateDailyDeadlineLocked() {
9173 // Get the current time.
9174 long currentTime = mDailyStartTime = System.currentTimeMillis();
9175 Calendar calDeadline = Calendar.getInstance();
9176 calDeadline.setTimeInMillis(currentTime);
9177
9178 // Move time up to the next day, ranging from 1am to 3pm.
9179 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
9180 calDeadline.set(Calendar.MILLISECOND, 0);
9181 calDeadline.set(Calendar.SECOND, 0);
9182 calDeadline.set(Calendar.MINUTE, 0);
9183 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
9184 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
9185 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
9186 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
9187 }
9188
9189 public void recordDailyStatsIfNeededLocked(boolean settled) {
9190 long currentTime = System.currentTimeMillis();
9191 if (currentTime >= mNextMaxDailyDeadline) {
9192 recordDailyStatsLocked();
9193 } else if (settled && currentTime >= mNextMinDailyDeadline) {
9194 recordDailyStatsLocked();
9195 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
9196 recordDailyStatsLocked();
9197 }
9198 }
9199
9200 public void recordDailyStatsLocked() {
9201 DailyItem item = new DailyItem();
9202 item.mStartTime = mDailyStartTime;
9203 item.mEndTime = System.currentTimeMillis();
9204 boolean hasData = false;
9205 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
9206 hasData = true;
9207 item.mDischargeSteps = new LevelStepTracker(
9208 mDailyDischargeStepTracker.mNumStepDurations,
9209 mDailyDischargeStepTracker.mStepDurations);
9210 }
9211 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
9212 hasData = true;
9213 item.mChargeSteps = new LevelStepTracker(
9214 mDailyChargeStepTracker.mNumStepDurations,
9215 mDailyChargeStepTracker.mStepDurations);
9216 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009217 if (mDailyPackageChanges != null) {
9218 hasData = true;
9219 item.mPackageChanges = mDailyPackageChanges;
9220 mDailyPackageChanges = null;
9221 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009222 mDailyDischargeStepTracker.init();
9223 mDailyChargeStepTracker.init();
9224 updateDailyDeadlineLocked();
9225
9226 if (hasData) {
9227 mDailyItems.add(item);
9228 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
9229 mDailyItems.remove(0);
9230 }
9231 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
9232 try {
9233 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01009234 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009235 writeDailyItemsLocked(out);
9236 BackgroundThread.getHandler().post(new Runnable() {
9237 @Override
9238 public void run() {
9239 synchronized (mCheckinFile) {
9240 FileOutputStream stream = null;
9241 try {
9242 stream = mDailyFile.startWrite();
9243 memStream.writeTo(stream);
9244 stream.flush();
9245 FileUtils.sync(stream);
9246 stream.close();
9247 mDailyFile.finishWrite(stream);
9248 } catch (IOException e) {
9249 Slog.w("BatteryStats",
9250 "Error writing battery daily items", e);
9251 mDailyFile.failWrite(stream);
9252 }
9253 }
9254 }
9255 });
9256 } catch (IOException e) {
9257 }
9258 }
9259 }
9260
9261 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
9262 StringBuilder sb = new StringBuilder(64);
9263 out.startDocument(null, true);
9264 out.startTag(null, "daily-items");
9265 for (int i=0; i<mDailyItems.size(); i++) {
9266 final DailyItem dit = mDailyItems.get(i);
9267 out.startTag(null, "item");
9268 out.attribute(null, "start", Long.toString(dit.mStartTime));
9269 out.attribute(null, "end", Long.toString(dit.mEndTime));
9270 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
9271 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009272 if (dit.mPackageChanges != null) {
9273 for (int j=0; j<dit.mPackageChanges.size(); j++) {
9274 PackageChange pc = dit.mPackageChanges.get(j);
9275 if (pc.mUpdate) {
9276 out.startTag(null, "upd");
9277 out.attribute(null, "pkg", pc.mPackageName);
9278 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
9279 out.endTag(null, "upd");
9280 } else {
9281 out.startTag(null, "rem");
9282 out.attribute(null, "pkg", pc.mPackageName);
9283 out.endTag(null, "rem");
9284 }
9285 }
9286 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009287 out.endTag(null, "item");
9288 }
9289 out.endTag(null, "daily-items");
9290 out.endDocument();
9291 }
9292
9293 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
9294 StringBuilder tmpBuilder) throws IOException {
9295 if (steps != null) {
9296 out.startTag(null, tag);
9297 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
9298 for (int i=0; i<steps.mNumStepDurations; i++) {
9299 out.startTag(null, "s");
9300 tmpBuilder.setLength(0);
9301 steps.encodeEntryAt(i, tmpBuilder);
9302 out.attribute(null, "v", tmpBuilder.toString());
9303 out.endTag(null, "s");
9304 }
9305 out.endTag(null, tag);
9306 }
9307 }
9308
9309 public void readDailyStatsLocked() {
9310 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
9311 mDailyItems.clear();
9312 FileInputStream stream;
9313 try {
9314 stream = mDailyFile.openRead();
9315 } catch (FileNotFoundException e) {
9316 return;
9317 }
9318 try {
9319 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01009320 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009321 readDailyItemsLocked(parser);
9322 } catch (XmlPullParserException e) {
9323 } finally {
9324 try {
9325 stream.close();
9326 } catch (IOException e) {
9327 }
9328 }
9329 }
9330
9331 private void readDailyItemsLocked(XmlPullParser parser) {
9332 try {
9333 int type;
9334 while ((type = parser.next()) != XmlPullParser.START_TAG
9335 && type != XmlPullParser.END_DOCUMENT) {
9336 ;
9337 }
9338
9339 if (type != XmlPullParser.START_TAG) {
9340 throw new IllegalStateException("no start tag found");
9341 }
9342
9343 int outerDepth = parser.getDepth();
9344 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9345 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9346 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9347 continue;
9348 }
9349
9350 String tagName = parser.getName();
9351 if (tagName.equals("item")) {
9352 readDailyItemTagLocked(parser);
9353 } else {
9354 Slog.w(TAG, "Unknown element under <daily-items>: "
9355 + parser.getName());
9356 XmlUtils.skipCurrentTag(parser);
9357 }
9358 }
9359
9360 } catch (IllegalStateException e) {
9361 Slog.w(TAG, "Failed parsing daily " + e);
9362 } catch (NullPointerException e) {
9363 Slog.w(TAG, "Failed parsing daily " + e);
9364 } catch (NumberFormatException e) {
9365 Slog.w(TAG, "Failed parsing daily " + e);
9366 } catch (XmlPullParserException e) {
9367 Slog.w(TAG, "Failed parsing daily " + e);
9368 } catch (IOException e) {
9369 Slog.w(TAG, "Failed parsing daily " + e);
9370 } catch (IndexOutOfBoundsException e) {
9371 Slog.w(TAG, "Failed parsing daily " + e);
9372 }
9373 }
9374
9375 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
9376 XmlPullParserException, IOException {
9377 DailyItem dit = new DailyItem();
9378 String attr = parser.getAttributeValue(null, "start");
9379 if (attr != null) {
9380 dit.mStartTime = Long.parseLong(attr);
9381 }
9382 attr = parser.getAttributeValue(null, "end");
9383 if (attr != null) {
9384 dit.mEndTime = Long.parseLong(attr);
9385 }
9386 int outerDepth = parser.getDepth();
9387 int type;
9388 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9389 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9390 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9391 continue;
9392 }
9393
9394 String tagName = parser.getName();
9395 if (tagName.equals("dis")) {
9396 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
9397 } else if (tagName.equals("chg")) {
9398 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009399 } else if (tagName.equals("upd")) {
9400 if (dit.mPackageChanges == null) {
9401 dit.mPackageChanges = new ArrayList<>();
9402 }
9403 PackageChange pc = new PackageChange();
9404 pc.mUpdate = true;
9405 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9406 String verStr = parser.getAttributeValue(null, "ver");
9407 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
9408 dit.mPackageChanges.add(pc);
9409 XmlUtils.skipCurrentTag(parser);
9410 } else if (tagName.equals("rem")) {
9411 if (dit.mPackageChanges == null) {
9412 dit.mPackageChanges = new ArrayList<>();
9413 }
9414 PackageChange pc = new PackageChange();
9415 pc.mUpdate = false;
9416 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9417 dit.mPackageChanges.add(pc);
9418 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009419 } else {
9420 Slog.w(TAG, "Unknown element under <item>: "
9421 + parser.getName());
9422 XmlUtils.skipCurrentTag(parser);
9423 }
9424 }
9425 mDailyItems.add(dit);
9426 }
9427
9428 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
9429 String tag)
9430 throws NumberFormatException, XmlPullParserException, IOException {
9431 final String numAttr = parser.getAttributeValue(null, "n");
9432 if (numAttr == null) {
9433 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
9434 XmlUtils.skipCurrentTag(parser);
9435 return;
9436 }
9437 final int num = Integer.parseInt(numAttr);
9438 LevelStepTracker steps = new LevelStepTracker(num);
9439 if (isCharge) {
9440 dit.mChargeSteps = steps;
9441 } else {
9442 dit.mDischargeSteps = steps;
9443 }
9444 int i = 0;
9445 int outerDepth = parser.getDepth();
9446 int type;
9447 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9448 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9449 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9450 continue;
9451 }
9452
9453 String tagName = parser.getName();
9454 if ("s".equals(tagName)) {
9455 if (i < num) {
9456 String valueAttr = parser.getAttributeValue(null, "v");
9457 if (valueAttr != null) {
9458 steps.decodeEntryAt(i, valueAttr);
9459 i++;
9460 }
9461 }
9462 } else {
9463 Slog.w(TAG, "Unknown element under <" + tag + ">: "
9464 + parser.getName());
9465 XmlUtils.skipCurrentTag(parser);
9466 }
9467 }
9468 steps.mNumStepDurations = i;
9469 }
9470
9471 @Override
9472 public DailyItem getDailyItemLocked(int daysAgo) {
9473 int index = mDailyItems.size()-1-daysAgo;
9474 return index >= 0 ? mDailyItems.get(index) : null;
9475 }
9476
9477 @Override
9478 public long getCurrentDailyStartTime() {
9479 return mDailyStartTime;
9480 }
9481
9482 @Override
9483 public long getNextMinDailyDeadline() {
9484 return mNextMinDailyDeadline;
9485 }
9486
9487 @Override
9488 public long getNextMaxDailyDeadline() {
9489 return mNextMaxDailyDeadline;
9490 }
9491
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009492 @Override
9493 public boolean startIteratingOldHistoryLocked() {
9494 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9495 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009496 if ((mHistoryIterator = mHistory) == null) {
9497 return false;
9498 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009499 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009500 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009501 mReadOverflow = false;
9502 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009503 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009504 }
9505
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009506 @Override
9507 public boolean getNextOldHistoryLocked(HistoryItem out) {
9508 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
9509 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009510 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009511 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009512 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009513 HistoryItem cur = mHistoryIterator;
9514 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009515 if (!mReadOverflow && !end) {
9516 Slog.w(TAG, "Old history ends before new history!");
9517 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009518 return false;
9519 }
9520 out.setTo(cur);
9521 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009522 if (!mReadOverflow) {
9523 if (end) {
9524 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009525 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07009526 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009527 pw.println("Histories differ!");
9528 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009529 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009530 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009531 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
9532 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07009533 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009534 }
9535 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009536 return true;
9537 }
9538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009540 public void finishIteratingOldHistoryLocked() {
9541 mIteratingHistory = false;
9542 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009543 mHistoryIterator = null;
9544 }
9545
9546 public int getHistoryTotalSize() {
9547 return MAX_HISTORY_BUFFER;
9548 }
9549
9550 public int getHistoryUsedSize() {
9551 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009552 }
9553
9554 @Override
9555 public boolean startIteratingHistoryLocked() {
9556 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9557 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009558 if (mHistoryBuffer.dataSize() <= 0) {
9559 return false;
9560 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009561 mHistoryBuffer.setDataPosition(0);
9562 mReadOverflow = false;
9563 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009564 mReadHistoryStrings = new String[mHistoryTagPool.size()];
9565 mReadHistoryUids = new int[mHistoryTagPool.size()];
9566 mReadHistoryChars = 0;
9567 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9568 final HistoryTag tag = ent.getKey();
9569 final int idx = ent.getValue();
9570 mReadHistoryStrings[idx] = tag.string;
9571 mReadHistoryUids[idx] = tag.uid;
9572 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009573 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009574 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009575 }
9576
9577 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009578 public int getHistoryStringPoolSize() {
9579 return mReadHistoryStrings.length;
9580 }
9581
9582 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009583 public int getHistoryStringPoolBytes() {
9584 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
9585 // Each string character is 2 bytes.
9586 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
9587 }
9588
9589 @Override
9590 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009591 return mReadHistoryStrings[index];
9592 }
9593
9594 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009595 public int getHistoryTagPoolUid(int index) {
9596 return mReadHistoryUids[index];
9597 }
9598
9599 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009600 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009601 final int pos = mHistoryBuffer.dataPosition();
9602 if (pos == 0) {
9603 out.clear();
9604 }
9605 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009606 if (end) {
9607 return false;
9608 }
9609
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009610 final long lastRealtime = out.time;
9611 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009612 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07009613 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
9614 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009615 out.currentTime = lastWalltime + (out.time - lastRealtime);
9616 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009617 return true;
9618 }
9619
9620 @Override
9621 public void finishIteratingHistoryLocked() {
9622 mIteratingHistory = false;
9623 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08009624 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009625 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009626
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009627 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009628 public long getHistoryBaseTime() {
9629 return mHistoryBaseTime;
9630 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009631
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009632 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 public int getStartCount() {
9634 return mStartCount;
9635 }
9636
9637 public boolean isOnBattery() {
9638 return mOnBattery;
9639 }
9640
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009641 public boolean isCharging() {
9642 return mCharging;
9643 }
9644
Mike Mac2f518a2017-09-19 16:06:03 -07009645 public boolean isScreenOn(int state) {
Chris Phoenix10a4a642017-09-25 13:21:00 -07009646 return state == Display.STATE_ON || state == Display.STATE_VR
9647 || state == Display.STATE_ON_SUSPEND;
Mike Mac2f518a2017-09-19 16:06:03 -07009648 }
9649
9650 public boolean isScreenOff(int state) {
9651 return state == Display.STATE_OFF;
9652 }
9653
9654 public boolean isScreenDoze(int state) {
9655 return state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009656 }
9657
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009658 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009659 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009660 mOnBatteryTimeBase.init(uptime, realtime);
9661 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009662 mRealtime = 0;
9663 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009664 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07009665 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009666 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009667
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009668 void initDischarge() {
9669 mLowDischargeAmountSinceCharge = 0;
9670 mHighDischargeAmountSinceCharge = 0;
9671 mDischargeAmountScreenOn = 0;
9672 mDischargeAmountScreenOnSinceCharge = 0;
9673 mDischargeAmountScreenOff = 0;
9674 mDischargeAmountScreenOffSinceCharge = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009675 mDischargeAmountScreenDoze = 0;
9676 mDischargeAmountScreenDozeSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009677 mDischargeStepTracker.init();
9678 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009679 mDischargeScreenOffCounter.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -07009680 mDischargeScreenDozeCounter.reset(false);
Mike Ma15313c92017-11-15 17:58:21 -08009681 mDischargeLightDozeCounter.reset(false);
9682 mDischargeDeepDozeCounter.reset(false);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009683 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009684 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009685
9686 public void resetAllStatsCmdLocked() {
9687 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08009688 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009689 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08009690 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009691 long realtime = mSecRealtime * 1000;
9692 mDischargeStartLevel = mHistoryCur.batteryLevel;
9693 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009694 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009695 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
9696 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009697 mOnBatteryTimeBase.reset(uptime, realtime);
9698 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
9699 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Mike Mac2f518a2017-09-19 16:06:03 -07009700 if (isScreenOn(mScreenState)) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009701 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
Mike Mac2f518a2017-09-19 16:06:03 -07009702 mDischargeScreenDozeUnplugLevel = 0;
9703 mDischargeScreenOffUnplugLevel = 0;
9704 } else if (isScreenDoze(mScreenState)) {
9705 mDischargeScreenOnUnplugLevel = 0;
9706 mDischargeScreenDozeUnplugLevel = mHistoryCur.batteryLevel;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009707 mDischargeScreenOffUnplugLevel = 0;
9708 } else {
9709 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009710 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009711 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
9712 }
9713 mDischargeAmountScreenOn = 0;
9714 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009715 mDischargeAmountScreenDoze = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009716 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009717 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009718 }
9719
9720 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07009721 final long uptimeMillis = mClocks.uptimeMillis();
9722 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009723 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07009724 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009725 mScreenOnTimer.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -07009726 mScreenDozeTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009727 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009728 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009729 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07009730
9731 if (mPowerProfile != null) {
9732 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
9733 } else {
9734 mEstimatedBatteryCapacity = -1;
9735 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07009736 mMinLearnedBatteryCapacity = -1;
9737 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009738 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009739 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07009740 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009741 mLongestLightIdleTime = 0;
9742 mLongestFullIdleTime = 0;
9743 mDeviceIdleModeLightTimer.reset(false);
9744 mDeviceIdleModeFullTimer.reset(false);
9745 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009746 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009747 mPhoneOnTimer.reset(false);
9748 mAudioOnTimer.reset(false);
9749 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009750 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009751 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009752 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08009753 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009754 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009755 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009756 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009757 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009758 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009759 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009760 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009761 mNetworkByteActivityCounters[i].reset(false);
9762 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009763 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009764 mMobileRadioActiveTimer.reset(false);
9765 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009766 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009767 mMobileRadioActiveUnknownTime.reset(false);
9768 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009769 mWifiOnTimer.reset(false);
9770 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009771 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009772 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009773 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009774 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9775 mWifiSupplStateTimer[i].reset(false);
9776 }
9777 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9778 mWifiSignalStrengthsTimer[i].reset(false);
9779 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009780 mWifiActivity.reset(false);
9781 mBluetoothActivity.reset(false);
9782 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009783 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009784
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009785 for (int i=0; i<mUidStats.size(); i++) {
Bookatz993a0be2017-07-21 09:03:23 -07009786 if (mUidStats.valueAt(i).reset(uptimeMillis * 1000, elapsedRealtimeMillis * 1000)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009787 mUidStats.remove(mUidStats.keyAt(i));
9788 i--;
9789 }
9790 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009791
Bookatz50df7112017-08-04 14:53:26 -07009792 if (mRpmStats.size() > 0) {
9793 for (SamplingTimer timer : mRpmStats.values()) {
9794 mOnBatteryTimeBase.remove(timer);
9795 }
9796 mRpmStats.clear();
9797 }
9798 if (mScreenOffRpmStats.size() > 0) {
9799 for (SamplingTimer timer : mScreenOffRpmStats.values()) {
9800 mOnBatteryScreenOffTimeBase.remove(timer);
9801 }
9802 mScreenOffRpmStats.clear();
9803 }
9804
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009805 if (mKernelWakelockStats.size() > 0) {
9806 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009807 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009808 }
9809 mKernelWakelockStats.clear();
9810 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009811
James Carr3a226052016-07-01 14:49:52 -07009812 if (mKernelMemoryStats.size() > 0) {
9813 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
9814 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
9815 }
9816 mKernelMemoryStats.clear();
9817 }
9818
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009819 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009820 for (SamplingTimer timer : mWakeupReasonStats.values()) {
9821 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009822 }
9823 mWakeupReasonStats.clear();
9824 }
9825
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009826 mLastHistoryStepDetails = null;
9827 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
9828 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
9829 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
9830 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
9831 mLastStepStatUserTime = mCurStepStatUserTime = 0;
9832 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
9833 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
9834 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
9835 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
9836 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
9837
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009838 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009839
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009840 clearHistoryLocked();
9841 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009842
Dianne Hackborn40c87252014-03-19 16:55:40 -07009843 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009844 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009845 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
9846 // Not recording process starts/stops.
9847 continue;
9848 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009849 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009850 if (active == null) {
9851 continue;
9852 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009853 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
9854 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009855 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07009856 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
9857 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009858 }
9859 }
9860 }
9861 }
9862
Mike Mac2f518a2017-09-19 16:06:03 -07009863 void updateDischargeScreenLevelsLocked(int oldState, int newState) {
9864 updateOldDischargeScreenLevelLocked(oldState);
9865 updateNewDischargeScreenLevelLocked(newState);
9866 }
9867
9868 private void updateOldDischargeScreenLevelLocked(int state) {
9869 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009870 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
9871 if (diff > 0) {
9872 mDischargeAmountScreenOn += diff;
9873 mDischargeAmountScreenOnSinceCharge += diff;
9874 }
Mike Mac2f518a2017-09-19 16:06:03 -07009875 } else if (isScreenDoze(state)) {
9876 int diff = mDischargeScreenDozeUnplugLevel - mDischargeCurrentLevel;
9877 if (diff > 0) {
9878 mDischargeAmountScreenDoze += diff;
9879 mDischargeAmountScreenDozeSinceCharge += diff;
9880 }
9881 } else if (isScreenOff(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009882 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
9883 if (diff > 0) {
9884 mDischargeAmountScreenOff += diff;
9885 mDischargeAmountScreenOffSinceCharge += diff;
9886 }
9887 }
Mike Mac2f518a2017-09-19 16:06:03 -07009888 }
9889
9890 private void updateNewDischargeScreenLevelLocked(int state) {
9891 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009892 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
9893 mDischargeScreenOffUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009894 mDischargeScreenDozeUnplugLevel = 0;
9895 } else if (isScreenDoze(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009896 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009897 mDischargeScreenDozeUnplugLevel = mDischargeCurrentLevel;
9898 mDischargeScreenOffUnplugLevel = 0;
9899 } else if (isScreenOff(state)) {
9900 mDischargeScreenOnUnplugLevel = 0;
9901 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009902 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
9903 }
9904 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009905
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009906 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009907 if (mOnBatteryInternal) {
Mike Mac2f518a2017-09-19 16:06:03 -07009908 updateDischargeScreenLevelsLocked(mScreenState, mScreenState);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009909 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009910 }
9911
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009912 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
9913 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
9914
9915 private final Object mWifiNetworkLock = new Object();
9916
9917 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009918 private String[] mWifiIfaces = EmptyArray.STRING;
9919
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009920 @GuardedBy("mWifiNetworkLock")
9921 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009922
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009923 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009924
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009925 @GuardedBy("mModemNetworkLock")
9926 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009927
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009928 @GuardedBy("mModemNetworkLock")
9929 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
9930
9931 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
9932 try {
9933 if (!ArrayUtils.isEmpty(ifaces)) {
9934 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
9935 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
9936 }
9937 } catch (IOException e) {
9938 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009939 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009940 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009941 }
9942
9943 /**
9944 * Distribute WiFi energy info and network traffic to apps.
9945 * @param info The energy information from the WiFi controller.
9946 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009947 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009948 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009949 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009950 }
9951
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009952 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009953 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009954 synchronized (mWifiNetworkLock) {
9955 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
9956 if (latestStats != null) {
9957 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
9958 mNetworkStatsPool.acquire());
9959 mNetworkStatsPool.release(mLastWifiNetworkStats);
9960 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009961 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009962 }
9963
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009964 synchronized (this) {
9965 if (!mOnBatteryInternal) {
9966 if (delta != null) {
9967 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009968 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009969 return;
9970 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009971
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009972 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
9973 SparseLongArray rxPackets = new SparseLongArray();
9974 SparseLongArray txPackets = new SparseLongArray();
9975 long totalTxPackets = 0;
9976 long totalRxPackets = 0;
9977 if (delta != null) {
9978 NetworkStats.Entry entry = new NetworkStats.Entry();
9979 final int size = delta.size();
9980 for (int i = 0; i < size; i++) {
9981 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009982
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009983 if (DEBUG_ENERGY) {
9984 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
9985 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9986 + " txPackets=" + entry.txPackets);
9987 }
9988
9989 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9990 // Skip the lookup below since there is no work to do.
9991 continue;
9992 }
9993
9994 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9995 if (entry.rxBytes != 0) {
9996 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009997 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009998 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9999 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
10000 entry.rxPackets);
10001 }
10002 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
10003 entry.rxBytes);
10004 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
10005 entry.rxPackets);
10006
10007 rxPackets.put(u.getUid(), entry.rxPackets);
10008
10009 // Sum the total number of packets so that the Rx Power can
10010 // be evenly distributed amongst the apps.
10011 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -080010012 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010013
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010014 if (entry.txBytes != 0) {
10015 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -080010016 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010017 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
10018 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
10019 entry.txPackets);
10020 }
10021 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
10022 entry.txBytes);
10023 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
10024 entry.txPackets);
10025
10026 txPackets.put(u.getUid(), entry.txPackets);
10027
10028 // Sum the total number of packets so that the Tx Power can
10029 // be evenly distributed amongst the apps.
10030 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -080010031 }
Adam Lesinskiba88e682015-12-08 12:06:55 -080010032 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010033 mNetworkStatsPool.release(delta);
10034 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -070010035 }
10036
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010037 if (info != null) {
10038 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -070010039
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010040 // Measured in mAms
10041 final long txTimeMs = info.getControllerTxTimeMillis();
10042 final long rxTimeMs = info.getControllerRxTimeMillis();
10043 final long idleTimeMs = info.getControllerIdleTimeMillis();
10044 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -070010045
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010046 long leftOverRxTimeMs = rxTimeMs;
10047 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -070010048
Adam Lesinskie08af192015-03-25 16:42:59 -070010049 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010050 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
10051 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
10052 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
10053 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
10054 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -070010055 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010056
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010057 long totalWifiLockTimeMs = 0;
10058 long totalScanTimeMs = 0;
10059
10060 // On the first pass, collect some totals so that we can normalize power
10061 // calculations if we need to.
10062 final int uidStatsSize = mUidStats.size();
10063 for (int i = 0; i < uidStatsSize; i++) {
10064 final Uid uid = mUidStats.valueAt(i);
10065
10066 // Sum the total scan power for all apps.
10067 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
10068 elapsedRealtimeMs * 1000) / 1000;
10069
10070 // Sum the total time holding wifi lock for all apps.
10071 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
10072 elapsedRealtimeMs * 1000) / 1000;
10073 }
10074
10075 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
10076 Slog.d(TAG,
10077 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
10078 + rxTimeMs + " ms). Normalizing scan time.");
10079 }
10080 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
10081 Slog.d(TAG,
10082 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
10083 + txTimeMs + " ms). Normalizing scan time.");
10084 }
10085
10086 // Actually assign and distribute power usage to apps.
10087 for (int i = 0; i < uidStatsSize; i++) {
10088 final Uid uid = mUidStats.valueAt(i);
10089
10090 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
10091 elapsedRealtimeMs * 1000) / 1000;
10092 if (scanTimeSinceMarkMs > 0) {
10093 // Set the new mark so that next time we get new data since this point.
10094 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
10095
10096 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
10097 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
10098
10099 // Our total scan time is more than the reported Tx/Rx time.
10100 // This is possible because the cost of a scan is approximate.
10101 // Let's normalize the result so that we evenly blame each app
10102 // scanning.
10103 //
10104 // This means that we may have apps that transmitted/received packets not be
10105 // blamed for this, but this is fine as scans are relatively more expensive.
10106 if (totalScanTimeMs > rxTimeMs) {
10107 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
10108 totalScanTimeMs;
10109 }
10110 if (totalScanTimeMs > txTimeMs) {
10111 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
10112 totalScanTimeMs;
10113 }
10114
10115 if (DEBUG_ENERGY) {
10116 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
10117 + scanRxTimeSinceMarkMs + " ms Tx:"
10118 + scanTxTimeSinceMarkMs + " ms)");
10119 }
10120
10121 ControllerActivityCounterImpl activityCounter =
10122 uid.getOrCreateWifiControllerActivityLocked();
10123 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
10124 activityCounter.getTxTimeCounters()[0].addCountLocked(
10125 scanTxTimeSinceMarkMs);
10126 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
10127 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
10128 }
10129
10130 // Distribute evenly the power consumed while Idle to each app holding a WiFi
10131 // lock.
10132 final long wifiLockTimeSinceMarkMs =
10133 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
10134 elapsedRealtimeMs * 1000) / 1000;
10135 if (wifiLockTimeSinceMarkMs > 0) {
10136 // Set the new mark so that next time we get new data since this point.
10137 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
10138
10139 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
10140 / totalWifiLockTimeMs;
10141 if (DEBUG_ENERGY) {
10142 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
10143 + myIdleTimeMs + " ms");
10144 }
10145 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
10146 .addCountLocked(myIdleTimeMs);
10147 }
10148 }
10149
Adam Lesinskie08af192015-03-25 16:42:59 -070010150 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010151 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
10152 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -070010153 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010154
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010155 // Distribute the remaining Tx power appropriately between all apps that transmitted
10156 // packets.
10157 for (int i = 0; i < txPackets.size(); i++) {
10158 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
10159 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
10160 / totalTxPackets;
10161 if (DEBUG_ENERGY) {
10162 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
10163 }
10164 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
10165 .addCountLocked(myTxTimeMs);
10166 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010167
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010168 // Distribute the remaining Rx power appropriately between all apps that received
10169 // packets.
10170 for (int i = 0; i < rxPackets.size(); i++) {
10171 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
10172 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
10173 / totalRxPackets;
10174 if (DEBUG_ENERGY) {
10175 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
10176 }
10177 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
10178 .addCountLocked(myRxTimeMs);
10179 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010180
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010181 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
10182
10183
10184 // Update WiFi controller stats.
10185 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
10186 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
10187 info.getControllerTxTimeMillis());
10188 mWifiActivity.getIdleTimeCounter().addCountLocked(
10189 info.getControllerIdleTimeMillis());
10190
10191 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10192 final double opVolt = mPowerProfile.getAveragePower(
10193 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10194 if (opVolt != 0) {
10195 // We store the power drain as mAms.
10196 mWifiActivity.getPowerCounter().addCountLocked(
10197 (long) (info.getControllerEnergyUsed() / opVolt));
10198 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010199 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010200 }
10201 }
10202
10203 /**
10204 * Distribute Cell radio energy info and network traffic to apps.
10205 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010206 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010207 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010208 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010209 }
10210
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010211 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -070010212 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010213 synchronized (mModemNetworkLock) {
10214 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
10215 if (latestStats != null) {
10216 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
10217 mNetworkStatsPool.acquire());
10218 mNetworkStatsPool.release(mLastModemNetworkStats);
10219 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -070010220 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010221 }
10222
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010223 synchronized (this) {
10224 if (!mOnBatteryInternal) {
10225 if (delta != null) {
10226 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010227 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010228 return;
Adam Lesinskie08af192015-03-25 16:42:59 -070010229 }
10230
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070010231 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010232 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
10233 elapsedRealtimeMs * 1000);
10234 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
10235
10236 long totalRxPackets = 0;
10237 long totalTxPackets = 0;
10238 if (delta != null) {
10239 NetworkStats.Entry entry = new NetworkStats.Entry();
10240 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010241 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010242 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010243 if (entry.rxPackets == 0 && entry.txPackets == 0) {
10244 continue;
10245 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010246
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010247 if (DEBUG_ENERGY) {
10248 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
10249 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
10250 + " txPackets=" + entry.txPackets);
10251 }
10252
10253 totalRxPackets += entry.rxPackets;
10254 totalTxPackets += entry.txPackets;
10255
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010256 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010257 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
10258 entry.rxPackets);
10259 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
10260 entry.txPackets);
10261 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
10262 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
10263 entry.rxBytes, entry.rxPackets);
10264 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
10265 entry.txBytes, entry.txPackets);
10266 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010267
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010268 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
10269 entry.rxBytes);
10270 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
10271 entry.txBytes);
10272 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
10273 entry.rxPackets);
10274 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
10275 entry.txPackets);
10276 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010277
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010278 // Now distribute proportional blame to the apps that did networking.
10279 long totalPackets = totalRxPackets + totalTxPackets;
10280 if (totalPackets > 0) {
10281 for (int i = 0; i < size; i++) {
10282 entry = delta.getValues(i, entry);
10283 if (entry.rxPackets == 0 && entry.txPackets == 0) {
10284 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010285 }
10286
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010287 final Uid u = getUidStatsLocked(mapUid(entry.uid));
10288
10289 // Distribute total radio active time in to this app.
10290 final long appPackets = entry.rxPackets + entry.txPackets;
10291 final long appRadioTime = (radioTime * appPackets) / totalPackets;
10292 u.noteMobileRadioActiveTimeLocked(appRadioTime);
10293
10294 // Remove this app from the totals, so that we don't lose any time
10295 // due to rounding.
10296 radioTime -= appRadioTime;
10297 totalPackets -= appPackets;
10298
10299 if (activityInfo != null) {
10300 ControllerActivityCounterImpl activityCounter =
10301 u.getOrCreateModemControllerActivityLocked();
10302 if (totalRxPackets > 0 && entry.rxPackets > 0) {
10303 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
10304 / totalRxPackets;
10305 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
10306 }
10307
10308 if (totalTxPackets > 0 && entry.txPackets > 0) {
10309 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
10310 long txMs =
10311 entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
10312 txMs /= totalTxPackets;
10313 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
10314 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010315 }
10316 }
10317 }
10318 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010319
10320 if (radioTime > 0) {
10321 // Whoops, there is some radio time we can't blame on an app!
10322 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
10323 mMobileRadioActiveUnknownCount.addCountLocked(1);
10324 }
10325
10326 mNetworkStatsPool.release(delta);
10327 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010328 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010329
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010330 if (activityInfo != null) {
10331 mHasModemReporting = true;
10332 mModemActivity.getIdleTimeCounter().addCountLocked(
10333 activityInfo.getIdleTimeMillis());
10334 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
10335 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
10336 mModemActivity.getTxTimeCounters()[lvl]
10337 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
10338 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010339
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010340 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10341 final double opVolt = mPowerProfile.getAveragePower(
10342 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10343 if (opVolt != 0) {
10344 // We store the power drain as mAms.
10345 mModemActivity.getPowerCounter().addCountLocked(
10346 (long) (activityInfo.getEnergyUsed() / opVolt));
10347 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010348 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010349 }
10350 }
10351
10352 /**
10353 * Distribute Bluetooth energy info and network traffic to apps.
10354 * @param info The energy information from the bluetooth controller.
10355 */
10356 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010357 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010358 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010359 }
10360
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010361 if (info == null || !mOnBatteryInternal) {
10362 return;
10363 }
Adam Lesinskie283d332015-04-16 12:29:25 -070010364
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010365 mHasBluetoothReporting = true;
10366
Bookatz867c0d72017-03-07 18:23:42 -080010367 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010368 final long rxTimeMs = info.getControllerRxTimeMillis();
10369 final long txTimeMs = info.getControllerTxTimeMillis();
10370
10371 if (DEBUG_ENERGY) {
10372 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
10373 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
10374 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
10375 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
10376 }
10377
10378 long totalScanTimeMs = 0;
10379
10380 final int uidCount = mUidStats.size();
10381 for (int i = 0; i < uidCount; i++) {
10382 final Uid u = mUidStats.valueAt(i);
10383 if (u.mBluetoothScanTimer == null) {
10384 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -070010385 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010386
Bookatzaa4594a2017-03-24 12:39:56 -070010387 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010388 elapsedRealtimeMs * 1000) / 1000;
10389 }
10390
10391 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
10392 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
10393
10394 if (DEBUG_ENERGY) {
10395 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
10396 + " TX=" + normalizeScanTxTime);
10397 }
10398
10399 long leftOverRxTimeMs = rxTimeMs;
10400 long leftOverTxTimeMs = txTimeMs;
10401
10402 for (int i = 0; i < uidCount; i++) {
10403 final Uid u = mUidStats.valueAt(i);
10404 if (u.mBluetoothScanTimer == null) {
10405 continue;
10406 }
10407
Bookatzaa4594a2017-03-24 12:39:56 -070010408 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010409 elapsedRealtimeMs * 1000) / 1000;
10410 if (scanTimeSinceMarkMs > 0) {
10411 // Set the new mark so that next time we get new data since this point.
10412 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
10413
10414 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
10415 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
10416
10417 if (normalizeScanRxTime) {
10418 // Scan time is longer than the total rx time in the controller,
10419 // so distribute the scan time proportionately. This means regular traffic
10420 // will not blamed, but scans are more expensive anyways.
10421 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
10422 }
10423
10424 if (normalizeScanTxTime) {
10425 // Scan time is longer than the total tx time in the controller,
10426 // so distribute the scan time proportionately. This means regular traffic
10427 // will not blamed, but scans are more expensive anyways.
10428 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
10429 }
10430
10431 final ControllerActivityCounterImpl counter =
10432 u.getOrCreateBluetoothControllerActivityLocked();
10433 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
10434 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
10435
10436 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
10437 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
10438 }
10439 }
10440
10441 if (DEBUG_ENERGY) {
10442 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
10443 + " TX=" + leftOverTxTimeMs);
10444 }
10445
10446 //
10447 // Now distribute blame to apps that did bluetooth traffic.
10448 //
10449
10450 long totalTxBytes = 0;
10451 long totalRxBytes = 0;
10452
10453 final UidTraffic[] uidTraffic = info.getUidTraffic();
10454 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
10455 for (int i = 0; i < numUids; i++) {
10456 final UidTraffic traffic = uidTraffic[i];
10457
10458 // Add to the global counters.
10459 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
10460 traffic.getRxBytes());
10461 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
10462 traffic.getTxBytes());
10463
10464 // Add to the UID counters.
10465 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
10466 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
10467 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
10468
10469 // Calculate the total traffic.
10470 totalTxBytes += traffic.getTxBytes();
10471 totalRxBytes += traffic.getRxBytes();
10472 }
10473
10474 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
10475 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010476 for (int i = 0; i < numUids; i++) {
10477 final UidTraffic traffic = uidTraffic[i];
10478
Adam Lesinski50e47602015-12-04 17:04:54 -080010479 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010480 final ControllerActivityCounterImpl counter =
10481 u.getOrCreateBluetoothControllerActivityLocked();
10482
10483 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
10484 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
10485
10486 if (DEBUG_ENERGY) {
10487 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
10488 + " rx_time=" + timeRxMs);
10489 }
10490 counter.getRxTimeCounter().addCountLocked(timeRxMs);
10491 leftOverRxTimeMs -= timeRxMs;
10492 }
10493
10494 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
10495 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
10496
10497 if (DEBUG_ENERGY) {
10498 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
10499 + " tx_time=" + timeTxMs);
10500 }
10501
10502 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
10503 leftOverTxTimeMs -= timeTxMs;
10504 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010505 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010506 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010507
10508 mBluetoothActivity.getRxTimeCounter().addCountLocked(
10509 info.getControllerRxTimeMillis());
10510 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
10511 info.getControllerTxTimeMillis());
10512 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
10513 info.getControllerIdleTimeMillis());
10514
10515 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10516 final double opVolt = mPowerProfile.getAveragePower(
10517 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10518 if (opVolt != 0) {
10519 // We store the power drain as mAms.
10520 mBluetoothActivity.getPowerCounter().addCountLocked(
10521 (long) (info.getControllerEnergyUsed() / opVolt));
10522 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010523 }
10524
10525 /**
Bookatz0b8a0502017-09-13 11:51:52 -070010526 * Read and record Resource Power Manager (RPM) state and voter times.
10527 * If RPM stats were fetched more recently than RPM_STATS_UPDATE_FREQ_MS ago, uses the old data
10528 * instead of fetching it anew.
Bookatz50df7112017-08-04 14:53:26 -070010529 */
10530 public void updateRpmStatsLocked() {
10531 if (mPlatformIdleStateCallback == null) return;
Bookatz0b8a0502017-09-13 11:51:52 -070010532 long now = SystemClock.elapsedRealtime();
10533 if (now - mLastRpmStatsUpdateTimeMs >= RPM_STATS_UPDATE_FREQ_MS) {
10534 mPlatformIdleStateCallback.fillLowPowerStats(mTmpRpmStats);
10535 mLastRpmStatsUpdateTimeMs = now;
10536 }
Bookatz50df7112017-08-04 14:53:26 -070010537
10538 for (Map.Entry<String, RpmStats.PowerStatePlatformSleepState> pstate
10539 : mTmpRpmStats.mPlatformLowPowerStats.entrySet()) {
10540
10541 // Update values for this platform state.
10542 final String pName = pstate.getKey();
10543 final long pTimeUs = pstate.getValue().mTimeMs * 1000;
10544 final int pCount = pstate.getValue().mCount;
10545 getRpmTimerLocked(pName).update(pTimeUs, pCount);
Bookatz82b341172017-09-07 19:06:08 -070010546 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10547 getScreenOffRpmTimerLocked(pName).update(pTimeUs, pCount);
10548 }
Bookatz50df7112017-08-04 14:53:26 -070010549
10550 // Update values for each voter of this platform state.
10551 for (Map.Entry<String, RpmStats.PowerStateElement> voter
10552 : pstate.getValue().mVoters.entrySet()) {
10553 final String vName = pName + "." + voter.getKey();
10554 final long vTimeUs = voter.getValue().mTimeMs * 1000;
10555 final int vCount = voter.getValue().mCount;
10556 getRpmTimerLocked(vName).update(vTimeUs, vCount);
Bookatz82b341172017-09-07 19:06:08 -070010557 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10558 getScreenOffRpmTimerLocked(vName).update(vTimeUs, vCount);
10559 }
Bookatz50df7112017-08-04 14:53:26 -070010560 }
10561 }
10562
10563 for (Map.Entry<String, RpmStats.PowerStateSubsystem> subsys
10564 : mTmpRpmStats.mSubsystemLowPowerStats.entrySet()) {
10565
10566 final String subsysName = subsys.getKey();
10567 for (Map.Entry<String, RpmStats.PowerStateElement> sstate
10568 : subsys.getValue().mStates.entrySet()) {
10569 final String name = subsysName + "." + sstate.getKey();
10570 final long timeUs = sstate.getValue().mTimeMs * 1000;
10571 final int count = sstate.getValue().mCount;
10572 getRpmTimerLocked(name).update(timeUs, count);
Bookatz82b341172017-09-07 19:06:08 -070010573 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10574 getScreenOffRpmTimerLocked(name).update(timeUs, count);
10575 }
Bookatz50df7112017-08-04 14:53:26 -070010576 }
10577 }
10578 }
10579
10580 /**
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010581 * Read and distribute kernel wake lock use across apps.
10582 */
10583 public void updateKernelWakelocksLocked() {
10584 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
10585 mTmpWakelockStats);
10586 if (wakelockStats == null) {
10587 // Not crashing might make board bringup easier.
10588 Slog.w(TAG, "Couldn't get kernel wake lock stats");
10589 return;
10590 }
10591
10592 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
10593 String name = ent.getKey();
10594 KernelWakelockStats.Entry kws = ent.getValue();
10595
10596 SamplingTimer kwlt = mKernelWakelockStats.get(name);
10597 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010598 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010599 mKernelWakelockStats.put(name, kwlt);
10600 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070010601
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010602 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010603 kwlt.setUpdateVersion(kws.mVersion);
10604 }
10605
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010606 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070010607 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
10608 // this time.
10609 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10610 SamplingTimer st = ent.getValue();
10611 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
10612 st.endSample();
10613 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010614 }
10615 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010616
Adam Lesinskid84ad302016-05-17 18:31:02 -070010617 // Record whether we've seen a non-zero time (for debugging b/22716723).
10618 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010619 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
10620 }
10621
10622 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
10623 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
10624 wakelockStats.kernelWakelockVersion);
10625 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010626 }
10627
Adam Lesinski72478f02015-06-17 15:39:43 -070010628 // We use an anonymous class to access these variables,
10629 // so they can't live on the stack or they'd have to be
10630 // final MutableLong objects (more allocations).
10631 // Used in updateCpuTimeLocked().
10632 long mTempTotalCpuUserTimeUs;
10633 long mTempTotalCpuSystemTimeUs;
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010634 long[][] mWakeLockAllocationsUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070010635
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010636 /**
James Carr3a226052016-07-01 14:49:52 -070010637 * Reads the newest memory stats from the kernel.
10638 */
10639 public void updateKernelMemoryBandwidthLocked() {
10640 mKernelMemoryBandwidthStats.updateStats();
10641 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
10642 final int bandwidthEntryCount = bandwidthEntries.size();
10643 int index;
10644 for (int i = 0; i < bandwidthEntryCount; i++) {
10645 SamplingTimer timer;
10646 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
10647 timer = mKernelMemoryStats.valueAt(index);
10648 } else {
10649 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
10650 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
10651 }
10652 timer.update(bandwidthEntries.valueAt(i), 1);
10653 if (DEBUG_MEMORY) {
10654 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
10655 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
10656 mKernelMemoryStats.get(
10657 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
10658 mKernelMemoryStats.size()));
10659 }
10660 }
10661 }
10662
10663 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070010664 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
10665 * and we are on battery with screen off, we give more of the cpu time to those apps holding
10666 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010667 */
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010668 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010669 if (mPowerProfile == null) {
10670 return;
10671 }
10672
Adam Lesinski72478f02015-06-17 15:39:43 -070010673 if (DEBUG_ENERGY_CPU) {
10674 Slog.d(TAG, "!Cpu updating!");
10675 }
10676
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010677 if (mCpuFreqs == null) {
10678 mCpuFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile);
10679 }
10680
Sudheer Shanka38383232017-07-25 09:55:03 -070010681 // Calculate the wakelocks we have to distribute amongst. The system is excluded as it is
10682 // usually holding the wakelock on behalf of an app.
10683 // And Only distribute cpu power to wakelocks if the screen is off and we're on battery.
10684 ArrayList<StopwatchTimer> partialTimersToConsider = null;
Adam Lesinski72478f02015-06-17 15:39:43 -070010685 if (mOnBatteryScreenOffTimeBase.isRunning()) {
Sudheer Shanka38383232017-07-25 09:55:03 -070010686 partialTimersToConsider = new ArrayList<>();
10687 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070010688 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070010689 // Since the collection and blaming of wakelocks can be scheduled to run after
10690 // some delay, the mPartialTimers list may have new entries. We can't blame
10691 // the newly added timer for past cpu time, so we only consider timers that
10692 // were present for one round of collection. Once a timer has gone through
10693 // a round of collection, its mInList field is set to true.
Adam Lesinski72478f02015-06-17 15:39:43 -070010694 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
Sudheer Shanka38383232017-07-25 09:55:03 -070010695 partialTimersToConsider.add(timer);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010696 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010697 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010698 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010699 markPartialTimersAsEligible();
Adam Lesinski72478f02015-06-17 15:39:43 -070010700
Sudheer Shanka38383232017-07-25 09:55:03 -070010701 // When the battery is not on, we don't attribute the cpu times to any timers but we still
10702 // need to take the snapshots.
10703 if (!mOnBatteryInternal) {
10704 mKernelUidCpuTimeReader.readDelta(null);
10705 mKernelUidCpuFreqTimeReader.readDelta(null);
10706 for (int cluster = mKernelCpuSpeedReaders.length - 1; cluster >= 0; --cluster) {
10707 mKernelCpuSpeedReaders[cluster].readDelta();
10708 }
10709 return;
10710 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010711
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010712 mUserInfoProvider.refreshUserIds();
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010713 final SparseLongArray updatedUids = mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()
10714 ? null : new SparseLongArray();
Sudheer Shanka38383232017-07-25 09:55:03 -070010715 readKernelUidCpuTimesLocked(partialTimersToConsider, updatedUids);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010716 // updatedUids=null means /proc/uid_time_in_state provides snapshots of per-cluster cpu
10717 // freqs, so no need to approximate these values.
10718 if (updatedUids != null) {
10719 updateClusterSpeedTimes(updatedUids);
Sudheer Shanka671985f2017-05-19 11:33:42 -070010720 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010721 readKernelUidCpuFreqTimesLocked(partialTimersToConsider);
Sudheer Shanka38383232017-07-25 09:55:03 -070010722 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010723
Sudheer Shanka38383232017-07-25 09:55:03 -070010724 /**
10725 * Mark the current partial timers as gone through a collection so that they will be
10726 * considered in the next cpu times distribution to wakelock holders.
10727 */
10728 @VisibleForTesting
10729 public void markPartialTimersAsEligible() {
10730 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
10731 // No difference, so each timer is now considered for the next collection.
10732 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
10733 mPartialTimers.get(i).mInList = true;
10734 }
10735 } else {
10736 // The lists are different, meaning we added (or removed a timer) since the last
10737 // collection.
10738 for (int i = mLastPartialTimers.size() - 1; i >= 0; --i) {
10739 mLastPartialTimers.get(i).mInList = false;
10740 }
10741 mLastPartialTimers.clear();
Adam Lesinski72478f02015-06-17 15:39:43 -070010742
Sudheer Shanka38383232017-07-25 09:55:03 -070010743 // Mark the current timers as gone through a collection.
10744 final int numPartialTimers = mPartialTimers.size();
10745 for (int i = 0; i < numPartialTimers; ++i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070010746 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070010747 timer.mInList = true;
10748 mLastPartialTimers.add(timer);
Adam Lesinski72478f02015-06-17 15:39:43 -070010749 }
10750 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010751 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010752
Sudheer Shanka38383232017-07-25 09:55:03 -070010753 /**
10754 * Take snapshot of cpu times (aggregated over all uids) at different frequencies and
10755 * calculate cpu times spent by each uid at different frequencies.
10756 *
10757 * @param updatedUids The uids for which times spent at different frequencies are calculated.
10758 */
10759 @VisibleForTesting
10760 public void updateClusterSpeedTimes(@NonNull SparseLongArray updatedUids) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010761 long totalCpuClustersTimeMs = 0;
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010762 // Read the time spent for each cluster at various cpu frequencies.
Sudheer Shankaaf857412017-07-21 00:14:24 -070010763 final long[][] clusterSpeedTimesMs = new long[mKernelCpuSpeedReaders.length][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010764 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010765 clusterSpeedTimesMs[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
10766 if (clusterSpeedTimesMs[cluster] != null) {
10767 for (int speed = clusterSpeedTimesMs[cluster].length - 1; speed >= 0; --speed) {
10768 totalCpuClustersTimeMs += clusterSpeedTimesMs[cluster][speed];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010769 }
10770 }
10771 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070010772 if (totalCpuClustersTimeMs != 0) {
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010773 // We have cpu times per freq aggregated over all uids but we need the times per uid.
10774 // So, we distribute total time spent by an uid to different cpu freqs based on the
10775 // amount of time cpu was running at that freq.
10776 final int updatedUidsCount = updatedUids.size();
10777 for (int i = 0; i < updatedUidsCount; ++i) {
10778 final Uid u = getUidStatsLocked(updatedUids.keyAt(i));
Sudheer Shankaaf857412017-07-21 00:14:24 -070010779 final long appCpuTimeUs = updatedUids.valueAt(i);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010780 // Add the cpu speeds to this UID.
10781 final int numClusters = mPowerProfile.getNumCpuClusters();
Sudheer Shanka38383232017-07-25 09:55:03 -070010782 if (u.mCpuClusterSpeedTimesUs == null ||
10783 u.mCpuClusterSpeedTimesUs.length != numClusters) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010784 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010785 }
10786
Sudheer Shankaaf857412017-07-21 00:14:24 -070010787 for (int cluster = 0; cluster < clusterSpeedTimesMs.length; cluster++) {
10788 final int speedsInCluster = clusterSpeedTimesMs[cluster].length;
10789 if (u.mCpuClusterSpeedTimesUs[cluster] == null || speedsInCluster !=
10790 u.mCpuClusterSpeedTimesUs[cluster].length) {
10791 u.mCpuClusterSpeedTimesUs[cluster]
10792 = new LongSamplingCounter[speedsInCluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010793 }
10794
Sudheer Shankaaf857412017-07-21 00:14:24 -070010795 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeedTimesUs[cluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010796 for (int speed = 0; speed < speedsInCluster; speed++) {
10797 if (cpuSpeeds[speed] == null) {
10798 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10799 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070010800 cpuSpeeds[speed].addCountLocked(appCpuTimeUs
10801 * clusterSpeedTimesMs[cluster][speed]
10802 / totalCpuClustersTimeMs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010803 }
10804 }
10805 }
10806 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010807 }
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010808
Sudheer Shanka38383232017-07-25 09:55:03 -070010809 /**
10810 * Take a snapshot of the cpu times spent by each uid and update the corresponding counters.
10811 * If {@param partialTimers} is not null and empty, then we assign a portion of cpu times to
10812 * wakelock holders.
10813 *
10814 * @param partialTimers The wakelock holders among which the cpu times will be distributed.
10815 * @param updatedUids If not null, then the uids found in the snapshot will be added to this.
10816 */
10817 @VisibleForTesting
10818 public void readKernelUidCpuTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
10819 @Nullable SparseLongArray updatedUids) {
10820 mTempTotalCpuUserTimeUs = mTempTotalCpuSystemTimeUs = 0;
10821 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
10822 final long startTimeMs = mClocks.uptimeMillis();
Adam Lesinski72478f02015-06-17 15:39:43 -070010823
Sudheer Shanka38383232017-07-25 09:55:03 -070010824 mKernelUidCpuTimeReader.readDelta((uid, userTimeUs, systemTimeUs) -> {
10825 uid = mapUid(uid);
10826 if (Process.isIsolated(uid)) {
10827 // This could happen if the isolated uid mapping was removed before that process
10828 // was actually killed.
10829 mKernelUidCpuTimeReader.removeUid(uid);
10830 Slog.d(TAG, "Got readings for an isolated uid with no mapping: " + uid);
10831 return;
10832 }
10833 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10834 Slog.d(TAG, "Got readings for an invalid user's uid " + uid);
10835 mKernelUidCpuTimeReader.removeUid(uid);
10836 return;
10837 }
10838 final Uid u = getUidStatsLocked(uid);
10839
10840 // Accumulate the total system and user time.
10841 mTempTotalCpuUserTimeUs += userTimeUs;
10842 mTempTotalCpuSystemTimeUs += systemTimeUs;
10843
10844 StringBuilder sb = null;
10845 if (DEBUG_ENERGY_CPU) {
10846 sb = new StringBuilder();
10847 sb.append(" got time for uid=").append(u.mUid).append(": u=");
10848 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10849 sb.append(" s=");
10850 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10851 sb.append("\n");
10852 }
10853
10854 if (numWakelocks > 0) {
10855 // We have wakelocks being held, so only give a portion of the
10856 // time to the process. The rest will be distributed among wakelock
10857 // holders.
10858 userTimeUs = (userTimeUs * WAKE_LOCK_WEIGHT) / 100;
10859 systemTimeUs = (systemTimeUs * WAKE_LOCK_WEIGHT) / 100;
10860 }
10861
10862 if (sb != null) {
10863 sb.append(" adding to uid=").append(u.mUid).append(": u=");
10864 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10865 sb.append(" s=");
10866 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10867 Slog.d(TAG, sb.toString());
10868 }
10869
10870 u.mUserCpuTime.addCountLocked(userTimeUs);
10871 u.mSystemCpuTime.addCountLocked(systemTimeUs);
10872 if (updatedUids != null) {
10873 updatedUids.put(u.getUid(), userTimeUs + systemTimeUs);
10874 }
10875 });
10876
10877 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
10878 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
10879 Slog.d(TAG, "Reading cpu stats took " + elapsedTimeMs + "ms");
10880 }
10881
10882 if (numWakelocks > 0) {
10883 // Distribute a portion of the total cpu time to wakelock holders.
10884 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
10885 mTempTotalCpuSystemTimeUs =
10886 (mTempTotalCpuSystemTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
10887
10888 for (int i = 0; i < numWakelocks; ++i) {
10889 final StopwatchTimer timer = partialTimers.get(i);
10890 final int userTimeUs = (int) (mTempTotalCpuUserTimeUs / (numWakelocks - i));
10891 final int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / (numWakelocks - i));
10892
10893 if (DEBUG_ENERGY_CPU) {
10894 final StringBuilder sb = new StringBuilder();
10895 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
10896 .append(": u=");
10897 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10898 sb.append(" s=");
10899 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10900 Slog.d(TAG, sb.toString());
10901 }
10902
10903 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
10904 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
10905 if (updatedUids != null) {
10906 final int uid = timer.mUid.getUid();
10907 updatedUids.put(uid, updatedUids.get(uid, 0) + userTimeUs + systemTimeUs);
10908 }
10909
10910 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
10911 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
10912
10913 mTempTotalCpuUserTimeUs -= userTimeUs;
10914 mTempTotalCpuSystemTimeUs -= systemTimeUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070010915 }
10916 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010917 }
10918
Sudheer Shanka38383232017-07-25 09:55:03 -070010919 /**
10920 * Take a snapshot of the cpu times spent by each uid in each freq and update the
10921 * corresponding counters.
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010922 *
10923 * @param partialTimers The wakelock holders among which the cpu freq times will be distributed.
Sudheer Shanka38383232017-07-25 09:55:03 -070010924 */
10925 @VisibleForTesting
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010926 public void readKernelUidCpuFreqTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers) {
10927 final boolean perClusterTimesAvailable =
10928 mKernelUidCpuFreqTimeReader.perClusterTimesAvailable();
10929 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
10930 final int numClusters = mPowerProfile.getNumCpuClusters();
10931 mWakeLockAllocationsUs = null;
10932 mKernelUidCpuFreqTimeReader.readDelta((uid, cpuFreqTimeMs) -> {
10933 uid = mapUid(uid);
10934 if (Process.isIsolated(uid)) {
10935 mKernelUidCpuFreqTimeReader.removeUid(uid);
10936 Slog.d(TAG, "Got freq readings for an isolated uid with no mapping: " + uid);
10937 return;
Sudheer Shanka38383232017-07-25 09:55:03 -070010938 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010939 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10940 Slog.d(TAG, "Got freq readings for an invalid user's uid " + uid);
10941 mKernelUidCpuFreqTimeReader.removeUid(uid);
10942 return;
10943 }
10944 final Uid u = getUidStatsLocked(uid);
10945 if (u.mCpuFreqTimeMs == null || u.mCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
10946 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
10947 }
10948 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10949 if (u.mScreenOffCpuFreqTimeMs == null ||
10950 u.mScreenOffCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
10951 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
10952 mOnBatteryScreenOffTimeBase);
10953 }
10954 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010955
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010956 if (perClusterTimesAvailable) {
10957 if (u.mCpuClusterSpeedTimesUs == null ||
10958 u.mCpuClusterSpeedTimesUs.length != numClusters) {
10959 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070010960 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010961 if (numWakelocks > 0 && mWakeLockAllocationsUs == null) {
10962 mWakeLockAllocationsUs = new long[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070010963 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010964
10965 int freqIndex = 0;
10966 for (int cluster = 0; cluster < numClusters; ++cluster) {
10967 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
10968 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
10969 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
10970 u.mCpuClusterSpeedTimesUs[cluster]
10971 = new LongSamplingCounter[speedsInCluster];
10972 }
10973 if (numWakelocks > 0 && mWakeLockAllocationsUs[cluster] == null) {
10974 mWakeLockAllocationsUs[cluster] = new long[speedsInCluster];
10975 }
10976 final LongSamplingCounter[] cpuTimesUs = u.mCpuClusterSpeedTimesUs[cluster];
10977 for (int speed = 0; speed < speedsInCluster; ++speed) {
10978 if (cpuTimesUs[speed] == null) {
10979 cpuTimesUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10980 }
10981 final long appAllocationUs;
10982 if (mWakeLockAllocationsUs != null) {
10983 appAllocationUs =
10984 (cpuFreqTimeMs[freqIndex] * 1000 * WAKE_LOCK_WEIGHT) / 100;
10985 mWakeLockAllocationsUs[cluster][speed] +=
10986 (cpuFreqTimeMs[freqIndex] * 1000 - appAllocationUs);
10987 } else {
10988 appAllocationUs = cpuFreqTimeMs[freqIndex] * 1000;
10989 }
10990 cpuTimesUs[speed].addCountLocked(appAllocationUs);
10991 freqIndex++;
10992 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010993 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010994 }
10995 });
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010996
10997 if (mWakeLockAllocationsUs != null) {
10998 for (int i = 0; i < numWakelocks; ++i) {
10999 final Uid u = partialTimers.get(i).mUid;
11000 if (u.mCpuClusterSpeedTimesUs == null ||
11001 u.mCpuClusterSpeedTimesUs.length != numClusters) {
11002 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
11003 }
11004
11005 for (int cluster = 0; cluster < numClusters; ++cluster) {
11006 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
11007 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
11008 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
11009 u.mCpuClusterSpeedTimesUs[cluster]
11010 = new LongSamplingCounter[speedsInCluster];
11011 }
11012 final LongSamplingCounter[] cpuTimeUs = u.mCpuClusterSpeedTimesUs[cluster];
11013 for (int speed = 0; speed < speedsInCluster; ++speed) {
11014 if (cpuTimeUs[speed] == null) {
11015 cpuTimeUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
11016 }
11017 final long allocationUs =
11018 mWakeLockAllocationsUs[cluster][speed] / (numWakelocks - i);
11019 cpuTimeUs[speed].addCountLocked(allocationUs);
11020 mWakeLockAllocationsUs[cluster][speed] -= allocationUs;
11021 }
11022 }
11023 }
11024 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011025 }
11026
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011027 boolean setChargingLocked(boolean charging) {
11028 if (mCharging != charging) {
11029 mCharging = charging;
11030 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011031 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011032 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011033 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011034 }
11035 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
11036 return true;
11037 }
11038 return false;
11039 }
11040
Mike Mac2f518a2017-09-19 16:06:03 -070011041 protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime,
11042 final boolean onBattery, final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011043 boolean doWrite = false;
11044 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
11045 m.arg1 = onBattery ? 1 : 0;
11046 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011047
Dianne Hackborn40c87252014-03-19 16:55:40 -070011048 final long uptime = mSecUptime * 1000;
11049 final long realtime = mSecRealtime * 1000;
Mike Mac2f518a2017-09-19 16:06:03 -070011050 final int screenState = mScreenState;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011051 if (onBattery) {
11052 // We will reset our status if we are unplugging after the
11053 // battery was last full, or the level is at 100, or
11054 // we have gone through a significant charge (from a very low
11055 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011056 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070011057 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011058 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070011059 || (mDischargeCurrentLevel < 20 && level >= 80)
11060 || (getHighDischargeAmountSinceCharge() >= 200
11061 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070011062 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070011063 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070011064 + " lowAmount=" + getLowDischargeAmountSinceCharge()
11065 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011066 // Before we write, collect a snapshot of the final aggregated
11067 // stats to be reported in the next checkin. Only do this if we have
11068 // a sufficient amount of data to make it interesting.
11069 if (getLowDischargeAmountSinceCharge() >= 20) {
11070 final Parcel parcel = Parcel.obtain();
11071 writeSummaryToParcel(parcel, true);
11072 BackgroundThread.getHandler().post(new Runnable() {
11073 @Override public void run() {
11074 synchronized (mCheckinFile) {
11075 FileOutputStream stream = null;
11076 try {
11077 stream = mCheckinFile.startWrite();
11078 stream.write(parcel.marshall());
11079 stream.flush();
11080 FileUtils.sync(stream);
11081 stream.close();
11082 mCheckinFile.finishWrite(stream);
11083 } catch (IOException e) {
11084 Slog.w("BatteryStats",
11085 "Error writing checkin battery statistics", e);
11086 mCheckinFile.failWrite(stream);
11087 } finally {
11088 parcel.recycle();
11089 }
11090 }
11091 }
11092 });
11093 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011094 doWrite = true;
11095 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080011096 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011097 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080011098 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011099 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011100 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080011101 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011102 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011103 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011104 if (mCharging) {
11105 setChargingLocked(false);
11106 }
11107 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080011108 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011109 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070011110 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011111 mDischargeStepTracker.clearTime();
11112 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011113 mInitStepMode = mCurStepMode;
11114 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011115 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011116 mHistoryCur.batteryLevel = (byte)level;
11117 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
11118 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
11119 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011120 if (reset) {
11121 mRecordingHistory = true;
11122 startRecordingHistory(mSecRealtime, mSecUptime, reset);
11123 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011124 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011125 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070011126 if (isScreenOn(screenState)) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011127 mDischargeScreenOnUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070011128 mDischargeScreenDozeUnplugLevel = 0;
11129 mDischargeScreenOffUnplugLevel = 0;
11130 } else if (isScreenDoze(screenState)) {
11131 mDischargeScreenOnUnplugLevel = 0;
11132 mDischargeScreenDozeUnplugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011133 mDischargeScreenOffUnplugLevel = 0;
11134 } else {
11135 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070011136 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011137 mDischargeScreenOffUnplugLevel = level;
11138 }
11139 mDischargeAmountScreenOn = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070011140 mDischargeAmountScreenDoze = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011141 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070011142 updateTimeBasesLocked(true, screenState, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011143 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011144 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080011145 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011146 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011147 mHistoryCur.batteryLevel = (byte)level;
11148 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
11149 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
11150 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070011151 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011152 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011153 if (level < mDischargeUnplugLevel) {
11154 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
11155 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
11156 }
Mike Mac2f518a2017-09-19 16:06:03 -070011157 updateDischargeScreenLevelsLocked(screenState, screenState);
11158 updateTimeBasesLocked(false, screenState, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011159 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011160 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070011161 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011162 mInitStepMode = mCurStepMode;
11163 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080011164 }
11165 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
11166 if (mFile != null) {
11167 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 }
11169 }
11170 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011171
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011172 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
11173 boolean reset) {
11174 mRecordingHistory = true;
11175 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070011176 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
11177 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011178 mHistoryCur);
11179 mHistoryCur.currentTime = 0;
11180 if (reset) {
11181 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
11182 }
11183 }
11184
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011185 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
11186 final long uptimeMs) {
11187 if (mRecordingHistory) {
11188 mHistoryCur.currentTime = currentTime;
11189 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
11190 mHistoryCur);
11191 mHistoryCur.currentTime = 0;
11192 }
11193 }
11194
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080011195 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
11196 if (mRecordingHistory) {
11197 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080011198 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
11199 mHistoryCur);
11200 mHistoryCur.currentTime = 0;
11201 }
11202 }
11203
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011204 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011205 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011206 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011207 }
11208 }
11209
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011210 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011211 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011212
Bookatz8c6571b2017-10-24 15:04:41 -070011213 public void setBatteryStateLocked(final int status, final int health, final int plugType,
11214 final int level, /* not final */ int temp, final int volt, final int chargeUAh,
11215 final int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070011216 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
11217 temp = Math.max(0, temp);
11218
Bookatz8c6571b2017-10-24 15:04:41 -070011219 reportChangesToStatsLog(mHaveBatteryLevel ? mHistoryCur : null,
11220 status, plugType, level, temp);
11221
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011222 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080011223 final long uptime = mClocks.uptimeMillis();
11224 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011225 if (!mHaveBatteryLevel) {
11226 mHaveBatteryLevel = true;
11227 // We start out assuming that the device is plugged in (not
11228 // on battery). If our first report is now that we are indeed
11229 // plugged in, then twiddle our state to correctly reflect that
11230 // since we won't be going through the full setOnBattery().
11231 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011232 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011233 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011234 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011235 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011236 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011237 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011238 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011239 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011240 mHistoryCur.batteryStatus = (byte)status;
11241 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011242 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011243 mMaxChargeStepLevel = mMinDischargeStepLevel =
11244 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011245 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011246 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
11247 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
11248 }
11249 int oldStatus = mHistoryCur.batteryStatus;
11250 if (onBattery) {
11251 mDischargeCurrentLevel = level;
11252 if (!mRecordingHistory) {
11253 mRecordingHistory = true;
11254 startRecordingHistory(elapsedRealtime, uptime, true);
11255 }
11256 } else if (level < 96) {
11257 if (!mRecordingHistory) {
11258 mRecordingHistory = true;
11259 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011260 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011261 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011262 mCurrentBatteryLevel = level;
11263 if (mDischargePlugLevel < 0) {
11264 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070011265 }
Adam Lesinski926969b2016-04-28 17:31:12 -070011266
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011267 if (onBattery != mOnBattery) {
11268 mHistoryCur.batteryLevel = (byte)level;
11269 mHistoryCur.batteryStatus = (byte)status;
11270 mHistoryCur.batteryHealth = (byte)health;
11271 mHistoryCur.batteryPlugType = (byte)plugType;
11272 mHistoryCur.batteryTemperature = (short)temp;
11273 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011274 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
11275 // Only record discharges
11276 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
11277 mDischargeCounter.addCountLocked(chargeDiff);
11278 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070011279 if (isScreenDoze(mScreenState)) {
11280 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
11281 }
Mike Ma15313c92017-11-15 17:58:21 -080011282 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
11283 mDischargeLightDozeCounter.addCountLocked(chargeDiff);
11284 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
11285 mDischargeDeepDozeCounter.addCountLocked(chargeDiff);
11286 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011287 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011288 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011289 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011290 } else {
11291 boolean changed = false;
11292 if (mHistoryCur.batteryLevel != level) {
11293 mHistoryCur.batteryLevel = (byte)level;
11294 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070011295
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011296 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
11297 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011298 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070011299 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011300 if (mHistoryCur.batteryStatus != status) {
11301 mHistoryCur.batteryStatus = (byte)status;
11302 changed = true;
11303 }
11304 if (mHistoryCur.batteryHealth != health) {
11305 mHistoryCur.batteryHealth = (byte)health;
11306 changed = true;
11307 }
11308 if (mHistoryCur.batteryPlugType != plugType) {
11309 mHistoryCur.batteryPlugType = (byte)plugType;
11310 changed = true;
11311 }
11312 if (temp >= (mHistoryCur.batteryTemperature+10)
11313 || temp <= (mHistoryCur.batteryTemperature-10)) {
11314 mHistoryCur.batteryTemperature = (short)temp;
11315 changed = true;
11316 }
11317 if (volt > (mHistoryCur.batteryVoltage+20)
11318 || volt < (mHistoryCur.batteryVoltage-20)) {
11319 mHistoryCur.batteryVoltage = (char)volt;
11320 changed = true;
11321 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011322 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
11323 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011324 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
11325 // Only record discharges
11326 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
11327 mDischargeCounter.addCountLocked(chargeDiff);
11328 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070011329 if (isScreenDoze(mScreenState)) {
11330 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
11331 }
Mike Ma15313c92017-11-15 17:58:21 -080011332 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
11333 mDischargeLightDozeCounter.addCountLocked(chargeDiff);
11334 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
11335 mDischargeDeepDozeCounter.addCountLocked(chargeDiff);
11336 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011337 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011338 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070011339 changed = true;
11340 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011341 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
11342 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
11343 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
11344 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011345 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011346 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
11347 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
11348 modeBits, elapsedRealtime);
11349 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
11350 modeBits, elapsedRealtime);
11351 mLastDischargeStepLevel = level;
11352 mMinDischargeStepLevel = level;
11353 mInitStepMode = mCurStepMode;
11354 mModStepMode = 0;
11355 }
11356 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011357 if (level >= 90) {
11358 // If the battery level is at least 90%, always consider the device to be
11359 // charging even if it happens to go down a level.
11360 changed |= setChargingLocked(true);
11361 mLastChargeStepLevel = level;
11362 } if (!mCharging) {
11363 if (mLastChargeStepLevel < level) {
11364 // We have not reporting that we are charging, but the level has now
11365 // gone up, so consider the state to be charging.
11366 changed |= setChargingLocked(true);
11367 mLastChargeStepLevel = level;
11368 }
11369 } else {
11370 if (mLastChargeStepLevel > level) {
11371 // We had reported that the device was charging, but here we are with
11372 // power connected and the level going down. Looks like the current
11373 // power supplied isn't enough, so consider the device to now be
11374 // discharging.
11375 changed |= setChargingLocked(false);
11376 mLastChargeStepLevel = level;
11377 }
11378 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011379 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
11380 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
11381 modeBits, elapsedRealtime);
11382 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
11383 modeBits, elapsedRealtime);
11384 mLastChargeStepLevel = level;
11385 mMaxChargeStepLevel = level;
11386 mInitStepMode = mCurStepMode;
11387 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070011388 }
11389 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011390 if (changed) {
11391 addHistoryRecordLocked(elapsedRealtime, uptime);
11392 }
Evan Millarc64edde2009-04-18 12:26:32 -070011393 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011394 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
11395 // We don't record history while we are plugged in and fully charged.
11396 // The next time we are unplugged, history will be cleared.
11397 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080011398 }
Adam Lesinski041d9172016-12-12 12:03:56 -080011399
Jocelyn Dangc627d102017-04-14 13:15:14 -070011400 if (mMinLearnedBatteryCapacity == -1) {
11401 mMinLearnedBatteryCapacity = chargeFullUAh;
11402 } else {
11403 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080011404 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070011405 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070011406 }
11407
Bookatz8c6571b2017-10-24 15:04:41 -070011408 // Inform StatsLog of setBatteryState changes.
11409 // If this is the first reporting, pass in recentPast == null.
11410 private void reportChangesToStatsLog(HistoryItem recentPast,
11411 final int status, final int plugType, final int level, final int temp) {
11412
11413 if (recentPast == null || recentPast.batteryStatus != status) {
11414 StatsLog.write(StatsLog.CHARGING_STATE_CHANGED, status);
11415 }
11416 if (recentPast == null || recentPast.batteryPlugType != plugType) {
11417 StatsLog.write(StatsLog.PLUGGED_STATE_CHANGED, plugType);
11418 }
11419 if (recentPast == null || recentPast.batteryLevel != level) {
11420 StatsLog.write(StatsLog.BATTERY_LEVEL_CHANGED, level);
11421 }
11422 // Let's just always print the temperature, regardless of whether it changed.
11423 StatsLog.write(StatsLog.DEVICE_TEMPERATURE_REPORTED, temp);
11424 }
11425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426 public long getAwakeTimeBattery() {
11427 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
11428 }
11429
11430 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080011431 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011432 }
11433
11434 @Override
11435 public long computeUptime(long curTime, int which) {
11436 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011437 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011438 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070011439 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011440 }
11441 return 0;
11442 }
11443
11444 @Override
11445 public long computeRealtime(long curTime, int which) {
11446 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011447 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070011449 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 }
11451 return 0;
11452 }
11453
11454 @Override
11455 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011456 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011457 }
11458
11459 @Override
11460 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011461 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 }
11463
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011464 @Override
11465 public long computeBatteryScreenOffUptime(long curTime, int which) {
11466 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
11467 }
11468
11469 @Override
11470 public long computeBatteryScreenOffRealtime(long curTime, int which) {
11471 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011472 }
11473
Dianne Hackborn260c5022014-04-29 11:23:16 -070011474 private long computeTimePerLevel(long[] steps, int numSteps) {
11475 // For now we'll do a simple average across all steps.
11476 if (numSteps <= 0) {
11477 return -1;
11478 }
11479 long total = 0;
11480 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011481 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011482 }
11483 return total / numSteps;
11484 /*
11485 long[] buckets = new long[numSteps];
11486 int numBuckets = 0;
11487 int numToAverage = 4;
11488 int i = 0;
11489 while (i < numSteps) {
11490 long totalTime = 0;
11491 int num = 0;
11492 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011493 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011494 num++;
11495 }
11496 buckets[numBuckets] = totalTime / num;
11497 numBuckets++;
11498 numToAverage *= 2;
11499 i += num;
11500 }
11501 if (numBuckets < 1) {
11502 return -1;
11503 }
11504 long averageTime = buckets[numBuckets-1];
11505 for (i=numBuckets-2; i>=0; i--) {
11506 averageTime = (averageTime + buckets[i]) / 2;
11507 }
11508 return averageTime;
11509 */
11510 }
11511
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011512 @Override
11513 public long computeBatteryTimeRemaining(long curTime) {
11514 if (!mOnBattery) {
11515 return -1;
11516 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070011517 /* Simple implementation just looks at the average discharge per level across the
11518 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011519 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
11520 if (discharge < 2) {
11521 return -1;
11522 }
11523 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
11524 if (duration < 1000*1000) {
11525 return -1;
11526 }
11527 long usPerLevel = duration/discharge;
11528 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011529 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011530 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011531 return -1;
11532 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011533 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011534 if (msPerLevel <= 0) {
11535 return -1;
11536 }
11537 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011538 }
11539
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011540 @Override
11541 public LevelStepTracker getDischargeLevelStepTracker() {
11542 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011543 }
11544
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011545 @Override
11546 public LevelStepTracker getDailyDischargeLevelStepTracker() {
11547 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011548 }
11549
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011550 @Override
11551 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011552 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011553 // Not yet working.
11554 return -1;
11555 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070011556 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011557 int curLevel = mCurrentBatteryLevel;
11558 int plugLevel = mDischargePlugLevel;
11559 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
11560 return -1;
11561 }
11562 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
11563 if (duration < 1000*1000) {
11564 return -1;
11565 }
11566 long usPerLevel = duration/(curLevel-plugLevel);
11567 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070011568 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011569 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011570 return -1;
11571 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011572 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011573 if (msPerLevel <= 0) {
11574 return -1;
11575 }
11576 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011577 }
11578
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011579 @Override
11580 public LevelStepTracker getChargeLevelStepTracker() {
11581 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011582 }
11583
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011584 @Override
11585 public LevelStepTracker getDailyChargeLevelStepTracker() {
11586 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011587 }
11588
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011589 @Override
11590 public ArrayList<PackageChange> getDailyPackageChanges() {
11591 return mDailyPackageChanges;
11592 }
11593
Joe Onoratoe1acd632016-02-23 13:25:10 -080011594 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011595 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 }
11597
11598 @Override
11599 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011600 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011601 }
11602
11603 @Override
11604 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011605 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011606 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070011607
The Android Open Source Project10592532009-03-18 17:39:46 -070011608 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011609 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011610 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011611 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011612 }
11613 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011614
Evan Millar633a1742009-04-02 16:36:33 -070011615 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011616 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011617 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011618
The Android Open Source Project10592532009-03-18 17:39:46 -070011619 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011620 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011621 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011622 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011623 }
11624 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011625
Evan Millar633a1742009-04-02 16:36:33 -070011626 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011627 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011629
Amith Yamasanie43530a2009-08-21 13:11:37 -070011630 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011631 public int getLowDischargeAmountSinceCharge() {
11632 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011633 int val = mLowDischargeAmountSinceCharge;
11634 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11635 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
11636 }
11637 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011638 }
11639 }
11640
11641 @Override
11642 public int getHighDischargeAmountSinceCharge() {
11643 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011644 int val = mHighDischargeAmountSinceCharge;
11645 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11646 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
11647 }
11648 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011649 }
11650 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011651
11652 @Override
11653 public int getDischargeAmount(int which) {
11654 int dischargeAmount = which == STATS_SINCE_CHARGED
11655 ? getHighDischargeAmountSinceCharge()
11656 : (getDischargeStartLevel() - getDischargeCurrentLevel());
11657 if (dischargeAmount < 0) {
11658 dischargeAmount = 0;
11659 }
11660 return dischargeAmount;
11661 }
11662
Mike Mac2f518a2017-09-19 16:06:03 -070011663 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011664 public int getDischargeAmountScreenOn() {
11665 synchronized(this) {
11666 int val = mDischargeAmountScreenOn;
Mike Mac2f518a2017-09-19 16:06:03 -070011667 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011668 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11669 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11670 }
11671 return val;
11672 }
11673 }
11674
Mike Mac2f518a2017-09-19 16:06:03 -070011675 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011676 public int getDischargeAmountScreenOnSinceCharge() {
11677 synchronized(this) {
11678 int val = mDischargeAmountScreenOnSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -070011679 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011680 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11681 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11682 }
11683 return val;
11684 }
11685 }
11686
Mike Mac2f518a2017-09-19 16:06:03 -070011687 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011688 public int getDischargeAmountScreenOff() {
11689 synchronized(this) {
11690 int val = mDischargeAmountScreenOff;
Mike Mac2f518a2017-09-19 16:06:03 -070011691 if (mOnBattery && isScreenOff(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011692 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11693 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11694 }
Mike Mac2f518a2017-09-19 16:06:03 -070011695 // For backward compatibility, doze discharge is counted into screen off.
11696 return val + getDischargeAmountScreenDoze();
11697 }
11698 }
11699
11700 @Override
11701 public int getDischargeAmountScreenOffSinceCharge() {
11702 synchronized(this) {
11703 int val = mDischargeAmountScreenOffSinceCharge;
11704 if (mOnBattery && isScreenOff(mScreenState)
11705 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11706 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11707 }
11708 // For backward compatibility, doze discharge is counted into screen off.
11709 return val + getDischargeAmountScreenDozeSinceCharge();
11710 }
11711 }
11712
11713 @Override
11714 public int getDischargeAmountScreenDoze() {
11715 synchronized(this) {
11716 int val = mDischargeAmountScreenDoze;
11717 if (mOnBattery && isScreenDoze(mScreenState)
11718 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
11719 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
11720 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011721 return val;
11722 }
11723 }
11724
Mike Mac2f518a2017-09-19 16:06:03 -070011725 @Override
11726 public int getDischargeAmountScreenDozeSinceCharge() {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011727 synchronized(this) {
Mike Mac2f518a2017-09-19 16:06:03 -070011728 int val = mDischargeAmountScreenDozeSinceCharge;
11729 if (mOnBattery && isScreenDoze(mScreenState)
11730 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
11731 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011732 }
11733 return val;
11734 }
11735 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 /**
11738 * Retrieve the statistics object for a particular uid, creating if needed.
11739 */
11740 public Uid getUidStatsLocked(int uid) {
11741 Uid u = mUidStats.get(uid);
11742 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080011743 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011744 mUidStats.put(uid, u);
11745 }
11746 return u;
11747 }
11748
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070011749 public void onCleanupUserLocked(int userId) {
11750 final int firstUidForUser = UserHandle.getUid(userId, 0);
11751 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11752 mKernelUidCpuFreqTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11753 mKernelUidCpuTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11754 }
11755
11756 public void onUserRemovedLocked(int userId) {
11757 final int firstUidForUser = UserHandle.getUid(userId, 0);
11758 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11759 mUidStats.put(firstUidForUser, null);
11760 mUidStats.put(lastUidForUser, null);
11761 final int firstIndex = mUidStats.indexOfKey(firstUidForUser);
11762 final int lastIndex = mUidStats.indexOfKey(lastUidForUser);
11763 mUidStats.removeAtRange(firstIndex, lastIndex - firstIndex + 1);
11764 }
11765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011766 /**
11767 * Remove the statistics object for a particular uid.
11768 */
11769 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070011770 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070011771 mKernelUidCpuFreqTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 mUidStats.remove(uid);
11773 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070011774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775 /**
11776 * Retrieve the statistics object for a particular process, creating
11777 * if needed.
11778 */
11779 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011780 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 Uid u = getUidStatsLocked(uid);
11782 return u.getProcessStatsLocked(name);
11783 }
11784
11785 /**
11786 * Retrieve the statistics object for a particular process, creating
11787 * if needed.
11788 */
11789 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011790 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011791 Uid u = getUidStatsLocked(uid);
11792 return u.getPackageStatsLocked(pkg);
11793 }
11794
11795 /**
11796 * Retrieve the statistics object for a particular service, creating
11797 * if needed.
11798 */
11799 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011800 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011801 Uid u = getUidStatsLocked(uid);
11802 return u.getServiceStatsLocked(pkg, name);
11803 }
11804
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011805 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011806 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011807 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011808 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011809 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011810
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011811 Parcel mPendingWrite = null;
11812 final ReentrantLock mWriteLock = new ReentrantLock();
11813
11814 public void writeAsyncLocked() {
11815 writeLocked(false);
11816 }
11817
11818 public void writeSyncLocked() {
11819 writeLocked(true);
11820 }
11821
11822 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011823 if (mFile == null) {
11824 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 return;
11826 }
11827
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011828 if (mShuttingDown) {
11829 return;
11830 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011831
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011832 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011833 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080011834 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011835
11836 if (mPendingWrite != null) {
11837 mPendingWrite.recycle();
11838 }
11839 mPendingWrite = out;
11840
11841 if (sync) {
11842 commitPendingDataToDisk();
11843 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011844 BackgroundThread.getHandler().post(new Runnable() {
11845 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011846 commitPendingDataToDisk();
11847 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011848 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011849 }
11850 }
11851
11852 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011853 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011854 synchronized (this) {
11855 next = mPendingWrite;
11856 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011857 if (next == null) {
11858 return;
11859 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011860 }
11861
Amith Yamasanid2450862017-02-07 15:58:24 -080011862 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011863 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011864 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011865 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070011867 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011869 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011871 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011872 mFile.rollback();
11873 } finally {
11874 next.recycle();
11875 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070011876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 }
11878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011880 if (mDailyFile != null) {
11881 readDailyStatsLocked();
11882 }
11883
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011884 if (mFile == null) {
11885 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 return;
11887 }
11888
11889 mUidStats.clear();
11890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011892 File file = mFile.chooseForRead();
11893 if (!file.exists()) {
11894 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011896 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011898 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011899 Parcel in = Parcel.obtain();
11900 in.unmarshall(raw, 0, raw.length);
11901 in.setDataPosition(0);
11902 stream.close();
11903
11904 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080011905 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011906 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011907 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011908 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011909
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011910 mEndPlatformVersion = Build.ID;
11911
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011912 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011913 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080011914 final long elapsedRealtime = mClocks.elapsedRealtime();
11915 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011916 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011917 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011918 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011919 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
11920 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011921 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011922
11923 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011924 }
11925
11926 public int describeContents() {
11927 return 0;
11928 }
11929
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011930 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070011931 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011932
11933 mHistoryBuffer.setDataSize(0);
11934 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011935 mHistoryTagPool.clear();
11936 mNextHistoryTagIdx = 0;
11937 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011938
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011939 int numTags = in.readInt();
11940 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080011941 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011942 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011943 if (str == null) {
11944 throw new ParcelFormatException("null history tag string");
11945 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011946 int uid = in.readInt();
11947 HistoryTag tag = new HistoryTag();
11948 tag.string = str;
11949 tag.uid = uid;
11950 tag.poolIdx = idx;
11951 mHistoryTagPool.put(tag, idx);
11952 if (idx >= mNextHistoryTagIdx) {
11953 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011954 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011955 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011956 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011957
11958 int bufSize = in.readInt();
11959 int curPos = in.dataPosition();
11960 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011961 throw new ParcelFormatException("File corrupt: history data buffer too large " +
11962 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011963 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011964 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
11965 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011966 } else {
11967 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
11968 + " bytes at " + curPos);
11969 mHistoryBuffer.appendFrom(in, curPos, bufSize);
11970 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011971 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011972
Dianne Hackbornae384452011-06-28 12:33:48 -070011973 if (andOldHistory) {
11974 readOldHistory(in);
11975 }
11976
11977 if (DEBUG_HISTORY) {
11978 StringBuilder sb = new StringBuilder(128);
11979 sb.append("****************** OLD mHistoryBaseTime: ");
11980 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11981 Slog.i(TAG, sb.toString());
11982 }
11983 mHistoryBaseTime = historyBaseTime;
11984 if (DEBUG_HISTORY) {
11985 StringBuilder sb = new StringBuilder(128);
11986 sb.append("****************** NEW mHistoryBaseTime: ");
11987 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11988 Slog.i(TAG, sb.toString());
11989 }
11990
11991 // We are just arbitrarily going to insert 1 minute from the sample of
11992 // the last run until samples in this run.
11993 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080011994 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011995 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070011996 if (DEBUG_HISTORY) {
11997 StringBuilder sb = new StringBuilder(128);
11998 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
11999 TimeUtils.formatDuration(mHistoryBaseTime, sb);
12000 Slog.i(TAG, sb.toString());
12001 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070012002 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070012003 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012004
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012005 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070012006 if (!USE_OLD_HISTORY) {
12007 return;
12008 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012009 mHistory = mHistoryEnd = mHistoryCache = null;
12010 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070012011 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012012 HistoryItem rec = new HistoryItem(time, in);
12013 addHistoryRecordLocked(rec);
12014 }
12015 }
12016
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012017 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070012018 if (DEBUG_HISTORY) {
12019 StringBuilder sb = new StringBuilder(128);
12020 sb.append("****************** WRITING mHistoryBaseTime: ");
12021 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070012022 sb.append(" mLastHistoryElapsedRealtime: ");
12023 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070012024 Slog.i(TAG, sb.toString());
12025 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070012026 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012027 if (!inclData) {
12028 out.writeInt(0);
12029 out.writeInt(0);
12030 return;
12031 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080012032 out.writeInt(mHistoryTagPool.size());
12033 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
12034 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080012035 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080012036 out.writeString(tag.string);
12037 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080012038 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012039 out.writeInt(mHistoryBuffer.dataSize());
12040 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
12041 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
12042 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070012043
12044 if (andOldHistory) {
12045 writeOldHistory(out);
12046 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012047 }
12048
12049 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070012050 if (!USE_OLD_HISTORY) {
12051 return;
12052 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012053 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070012054 while (rec != null) {
12055 if (rec.time >= 0) rec.writeToParcel(out, 0);
12056 rec = rec.next;
12057 }
12058 out.writeLong(-1);
12059 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012060
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012061 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012062 final int version = in.readInt();
12063 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012064 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 + ", expected " + VERSION + "; erasing old stats");
12066 return;
12067 }
12068
Dianne Hackbornae384452011-06-28 12:33:48 -070012069 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012072 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012074 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012075 mStartPlatformVersion = in.readString();
12076 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012077 mOnBatteryTimeBase.readSummaryFromParcel(in);
12078 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012079 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012080 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070012081 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012082 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012083 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012084 mMinLearnedBatteryCapacity = in.readInt();
12085 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012086 mLowDischargeAmountSinceCharge = in.readInt();
12087 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012088 mDischargeAmountScreenOnSinceCharge = in.readInt();
12089 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070012090 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012091 mDischargeStepTracker.readFromParcel(in);
12092 mChargeStepTracker.readFromParcel(in);
12093 mDailyDischargeStepTracker.readFromParcel(in);
12094 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012095 mDischargeCounter.readSummaryFromParcelLocked(in);
12096 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070012097 mDischargeScreenDozeCounter.readSummaryFromParcelLocked(in);
Mike Ma15313c92017-11-15 17:58:21 -080012098 mDischargeLightDozeCounter.readSummaryFromParcelLocked(in);
12099 mDischargeDeepDozeCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012100 int NPKG = in.readInt();
12101 if (NPKG > 0) {
12102 mDailyPackageChanges = new ArrayList<>(NPKG);
12103 while (NPKG > 0) {
12104 NPKG--;
12105 PackageChange pc = new PackageChange();
12106 pc.mPackageName = in.readString();
12107 pc.mUpdate = in.readInt() != 0;
12108 pc.mVersionCode = in.readInt();
12109 mDailyPackageChanges.add(pc);
12110 }
12111 } else {
12112 mDailyPackageChanges = null;
12113 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012114 mDailyStartTime = in.readLong();
12115 mNextMinDailyDeadline = in.readLong();
12116 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012119
Jeff Browne95c3cd2014-05-02 16:59:26 -070012120 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012121 mScreenOnTimer.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070012122 mScreenDozeTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070012123 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
12124 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
12125 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012126 mInteractive = false;
12127 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012128 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012129 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012130 mLongestLightIdleTime = in.readLong();
12131 mLongestFullIdleTime = in.readLong();
12132 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
12133 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
12134 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012135 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012136 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080012137 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070012138 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
12139 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070012140 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012141 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
12142 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
12143 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012144 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012145 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
12146 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012147 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012148 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080012149 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080012150 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012151 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012152 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
12153 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012154 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070012155 mWifiOn = false;
12156 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012157 mGlobalWifiRunning = false;
12158 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012159 for (int i=0; i<NUM_WIFI_STATES; i++) {
12160 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
12161 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012162 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12163 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
12164 }
12165 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12166 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
12167 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012168 mWifiActivity.readSummaryFromParcel(in);
12169 mBluetoothActivity.readSummaryFromParcel(in);
12170 mModemActivity.readSummaryFromParcel(in);
12171 mHasWifiReporting = in.readInt() != 0;
12172 mHasBluetoothReporting = in.readInt() != 0;
12173 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070012174
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012175 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012176 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012177 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012178 mCameraOnNesting = 0;
12179 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012180 mBluetoothScanNesting = 0;
12181 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012182
Bookatz50df7112017-08-04 14:53:26 -070012183 int NRPMS = in.readInt();
12184 if (NRPMS > 10000) {
12185 throw new ParcelFormatException("File corrupt: too many rpm stats " + NRPMS);
12186 }
12187 for (int irpm = 0; irpm < NRPMS; irpm++) {
12188 if (in.readInt() != 0) {
12189 String rpmName = in.readString();
12190 getRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
12191 }
12192 }
12193 int NSORPMS = in.readInt();
12194 if (NSORPMS > 10000) {
12195 throw new ParcelFormatException("File corrupt: too many screen-off rpm stats " + NSORPMS);
12196 }
12197 for (int irpm = 0; irpm < NSORPMS; irpm++) {
12198 if (in.readInt() != 0) {
12199 String rpmName = in.readString();
12200 getScreenOffRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
12201 }
12202 }
12203
Evan Millarc64edde2009-04-18 12:26:32 -070012204 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012205 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012206 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012207 }
Evan Millarc64edde2009-04-18 12:26:32 -070012208 for (int ikw = 0; ikw < NKW; ikw++) {
12209 if (in.readInt() != 0) {
12210 String kwltName = in.readString();
12211 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
12212 }
12213 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070012214
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012215 int NWR = in.readInt();
12216 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012217 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012218 }
12219 for (int iwr = 0; iwr < NWR; iwr++) {
12220 if (in.readInt() != 0) {
12221 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012222 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012223 }
12224 }
12225
James Carr3a226052016-07-01 14:49:52 -070012226 int NMS = in.readInt();
12227 for (int ims = 0; ims < NMS; ims++) {
12228 if (in.readInt() != 0) {
12229 long kmstName = in.readLong();
12230 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
12231 }
12232 }
12233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012234 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012235 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012236 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238 for (int iu = 0; iu < NU; iu++) {
12239 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012240 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 mUidStats.put(uid, u);
12242
Bookatz867c0d72017-03-07 18:23:42 -080012243 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070012244 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080012245
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012246 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012247 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012248 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012249 }
The Android Open Source Project10592532009-03-18 17:39:46 -070012250 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012251 if (in.readInt() != 0) {
12252 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
12253 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070012254 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012255 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070012256 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012257 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070012258 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
12259 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
12260 if (in.readInt() != 0) {
12261 u.makeWifiBatchedScanBin(i, null);
12262 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
12263 }
12264 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070012265 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012266 if (in.readInt() != 0) {
12267 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
12268 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012269 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012270 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012271 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012272 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012273 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12274 }
12275 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012276 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12277 }
12278 if (in.readInt() != 0) {
12279 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12280 }
12281 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012282 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
12283 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012284 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070012285 u.createForegroundServiceTimerLocked().readSummaryFromParcelLocked(in);
12286 }
12287 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070012288 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
12289 }
12290 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012291 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
12292 }
Bookatz956f36bf2017-04-28 09:48:17 -070012293 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070012294 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
12295 }
12296 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070012297 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
12298 }
Bookatzb1f04f32017-05-19 13:57:32 -070012299 if (in.readInt() != 0) {
12300 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
12301 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080012302 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070012303 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
12304 if (in.readInt() != 0) {
12305 u.makeProcessState(i, null);
12306 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
12307 }
12308 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012309 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012310 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012311 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070012312
Dianne Hackborn617f8772009-03-31 15:04:46 -070012313 if (in.readInt() != 0) {
12314 if (u.mUserActivityCounters == null) {
12315 u.initUserActivityLocked();
12316 }
12317 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
12318 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
12319 }
12320 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012321
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012322 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012323 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012324 u.initNetworkActivityLocked();
12325 }
12326 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012327 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
12328 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012329 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012330 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
12331 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012332 }
12333
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012334 u.mUserCpuTime.readSummaryFromParcelLocked(in);
12335 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
12336
Adam Lesinski6832f392015-09-05 18:05:40 -070012337 if (in.readInt() != 0) {
12338 final int numClusters = in.readInt();
12339 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
12340 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012341 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012342
Sudheer Shankaaf857412017-07-21 00:14:24 -070012343 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070012344 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012345 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070012346 final int NSB = in.readInt();
12347 if (mPowerProfile != null &&
12348 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
12349 throw new ParcelFormatException("File corrupt: too many speed bins " +
12350 NSB);
12351 }
12352
Sudheer Shankaaf857412017-07-21 00:14:24 -070012353 u.mCpuClusterSpeedTimesUs[cluster] = new LongSamplingCounter[NSB];
Adam Lesinski6832f392015-09-05 18:05:40 -070012354 for (int speed = 0; speed < NSB; speed++) {
12355 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012356 u.mCpuClusterSpeedTimesUs[cluster][speed] = new LongSamplingCounter(
Adam Lesinski6832f392015-09-05 18:05:40 -070012357 mOnBatteryTimeBase);
Sudheer Shankaaf857412017-07-21 00:14:24 -070012358 u.mCpuClusterSpeedTimesUs[cluster][speed].readSummaryFromParcelLocked(in);
Adam Lesinski6832f392015-09-05 18:05:40 -070012359 }
12360 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070012361 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012362 u.mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070012363 }
12364 }
12365 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012366 u.mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012367 }
12368
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012369 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
12370 in, mOnBatteryTimeBase);
12371 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
12372 in, mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012373
12374 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070012375 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
12376 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
12377 } else {
12378 u.mMobileRadioApWakeupCount = null;
12379 }
12380
12381 if (in.readInt() != 0) {
12382 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
12383 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
12384 } else {
12385 u.mWifiRadioApWakeupCount = null;
12386 }
12387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012389 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012390 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012392 for (int iw = 0; iw < NW; iw++) {
12393 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012394 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395 }
12396
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012397 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012398 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012399 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012400 }
12401 for (int is = 0; is < NS; is++) {
12402 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012403 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012404 }
12405
12406 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012407 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012408 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012409 }
12410 for (int ij = 0; ij < NJ; ij++) {
12411 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012412 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012413 }
12414
Dianne Hackborn94326cb2017-06-28 16:17:20 -070012415 u.readJobCompletionsFromParcelLocked(in);
12416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012418 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012419 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 for (int is = 0; is < NP; is++) {
12422 int seNumber = in.readInt();
12423 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080012424 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 }
12426 }
12427
12428 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012429 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012430 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012432 for (int ip = 0; ip < NP; ip++) {
12433 String procName = in.readString();
12434 Uid.Proc p = u.getProcessStatsLocked(procName);
12435 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012436 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012437 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012438 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012439 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
12440 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012441 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012442 }
12443
12444 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012445 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012446 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012448 for (int ip = 0; ip < NP; ip++) {
12449 String pkgName = in.readString();
12450 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012451 final int NWA = in.readInt();
12452 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012453 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012454 }
12455 p.mWakeupAlarms.clear();
12456 for (int iwa=0; iwa<NWA; iwa++) {
12457 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -070012458 Counter c = new Counter(mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012459 c.readSummaryFromParcelLocked(in);
12460 p.mWakeupAlarms.put(tag, c);
12461 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012462 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012463 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012464 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012466 for (int is = 0; is < NS; is++) {
12467 String servName = in.readString();
12468 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
12469 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012470 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012472 }
12473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012474 }
12475 }
12476
12477 /**
12478 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
12479 * disk. This format does not allow a lossless round-trip.
12480 *
12481 * @param out the Parcel to be written to.
12482 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012483 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012484 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012485
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012486 // Pull the clock time. This may update the time and make a new history entry
12487 // if we had originally pulled a time before the RTC was set.
12488 long startClockTime = getStartClockTime();
12489
Joe Onoratoabded112016-02-08 16:49:39 -080012490 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
12491 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012492
12493 out.writeInt(VERSION);
12494
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012495 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012497 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012498 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012499 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012500 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012501 out.writeString(mStartPlatformVersion);
12502 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012503 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
12504 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012505 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012506 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070012507 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012508 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012509 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012510 out.writeInt(mMinLearnedBatteryCapacity);
12511 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080012512 out.writeInt(getLowDischargeAmountSinceCharge());
12513 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012514 out.writeInt(getDischargeAmountScreenOnSinceCharge());
12515 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Mike Mac2f518a2017-09-19 16:06:03 -070012516 out.writeInt(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012517 mDischargeStepTracker.writeToParcel(out);
12518 mChargeStepTracker.writeToParcel(out);
12519 mDailyDischargeStepTracker.writeToParcel(out);
12520 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070012521 mDischargeCounter.writeSummaryFromParcelLocked(out);
12522 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Mike Mac2f518a2017-09-19 16:06:03 -070012523 mDischargeScreenDozeCounter.writeSummaryFromParcelLocked(out);
Mike Ma15313c92017-11-15 17:58:21 -080012524 mDischargeLightDozeCounter.writeSummaryFromParcelLocked(out);
12525 mDischargeDeepDozeCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012526 if (mDailyPackageChanges != null) {
12527 final int NPKG = mDailyPackageChanges.size();
12528 out.writeInt(NPKG);
12529 for (int i=0; i<NPKG; i++) {
12530 PackageChange pc = mDailyPackageChanges.get(i);
12531 out.writeString(pc.mPackageName);
12532 out.writeInt(pc.mUpdate ? 1 : 0);
12533 out.writeInt(pc.mVersionCode);
12534 }
12535 } else {
12536 out.writeInt(0);
12537 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012538 out.writeLong(mDailyStartTime);
12539 out.writeLong(mNextMinDailyDeadline);
12540 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012541
12542 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Mike Mac2f518a2017-09-19 16:06:03 -070012543 mScreenDozeTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070012544 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012545 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070012546 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012547 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012548 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012549 out.writeLong(mLongestLightIdleTime);
12550 out.writeLong(mLongestFullIdleTime);
12551 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12552 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12553 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012554 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012555 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080012556 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012557 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012558 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012559 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012560 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012561 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012562 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012563 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012564 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
12565 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012566 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012567 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12568 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012569 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012570 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
12571 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012572 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12573 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012574 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012575 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012576 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012577 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12578 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12579 }
12580 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12581 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12582 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012583 mWifiActivity.writeSummaryToParcel(out);
12584 mBluetoothActivity.writeSummaryToParcel(out);
12585 mModemActivity.writeSummaryToParcel(out);
12586 out.writeInt(mHasWifiReporting ? 1 : 0);
12587 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12588 out.writeInt(mHasModemReporting ? 1 : 0);
12589
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012590 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012591 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012592 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012593 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012594
Bookatz50df7112017-08-04 14:53:26 -070012595 out.writeInt(mRpmStats.size());
12596 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
12597 Timer rpmt = ent.getValue();
12598 if (rpmt != null) {
12599 out.writeInt(1);
12600 out.writeString(ent.getKey());
12601 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12602 } else {
12603 out.writeInt(0);
12604 }
12605 }
12606 out.writeInt(mScreenOffRpmStats.size());
12607 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
12608 Timer rpmt = ent.getValue();
12609 if (rpmt != null) {
12610 out.writeInt(1);
12611 out.writeString(ent.getKey());
12612 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12613 } else {
12614 out.writeInt(0);
12615 }
12616 }
12617
Evan Millarc64edde2009-04-18 12:26:32 -070012618 out.writeInt(mKernelWakelockStats.size());
12619 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12620 Timer kwlt = ent.getValue();
12621 if (kwlt != null) {
12622 out.writeInt(1);
12623 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012624 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12625 } else {
12626 out.writeInt(0);
12627 }
12628 }
12629
12630 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012631 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12632 SamplingTimer timer = ent.getValue();
12633 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012634 out.writeInt(1);
12635 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012636 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070012637 } else {
12638 out.writeInt(0);
12639 }
12640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012641
James Carr3a226052016-07-01 14:49:52 -070012642 out.writeInt(mKernelMemoryStats.size());
12643 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12644 Timer kmt = mKernelMemoryStats.valueAt(i);
12645 if (kmt != null) {
12646 out.writeInt(1);
12647 out.writeLong(mKernelMemoryStats.keyAt(i));
12648 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12649 } else {
12650 out.writeInt(0);
12651 }
12652 }
12653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012654 final int NU = mUidStats.size();
12655 out.writeInt(NU);
12656 for (int iu = 0; iu < NU; iu++) {
12657 out.writeInt(mUidStats.keyAt(iu));
12658 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012659
Bookatz867c0d72017-03-07 18:23:42 -080012660 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070012661 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080012662
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012663 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012664 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012665 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012666 } else {
12667 out.writeInt(0);
12668 }
12669 if (u.mFullWifiLockTimer != null) {
12670 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012671 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012672 } else {
12673 out.writeInt(0);
12674 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070012675 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012676 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012677 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012678 } else {
12679 out.writeInt(0);
12680 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070012681 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
12682 if (u.mWifiBatchedScanTimer[i] != null) {
12683 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012684 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070012685 } else {
12686 out.writeInt(0);
12687 }
12688 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012689 if (u.mWifiMulticastTimer != null) {
12690 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012691 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012692 } else {
12693 out.writeInt(0);
12694 }
12695 if (u.mAudioTurnedOnTimer != null) {
12696 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012697 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012698 } else {
12699 out.writeInt(0);
12700 }
12701 if (u.mVideoTurnedOnTimer != null) {
12702 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012703 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012704 } else {
12705 out.writeInt(0);
12706 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012707 if (u.mFlashlightTurnedOnTimer != null) {
12708 out.writeInt(1);
12709 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12710 } else {
12711 out.writeInt(0);
12712 }
12713 if (u.mCameraTurnedOnTimer != null) {
12714 out.writeInt(1);
12715 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12716 } else {
12717 out.writeInt(0);
12718 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012719 if (u.mForegroundActivityTimer != null) {
12720 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012721 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012722 } else {
12723 out.writeInt(0);
12724 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070012725 if (u.mForegroundServiceTimer != null) {
12726 out.writeInt(1);
12727 u.mForegroundServiceTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12728 } else {
12729 out.writeInt(0);
12730 }
Bookatzc8c44962017-05-11 12:12:54 -070012731 if (u.mAggregatedPartialWakelockTimer != null) {
12732 out.writeInt(1);
12733 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12734 } else {
12735 out.writeInt(0);
12736 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012737 if (u.mBluetoothScanTimer != null) {
12738 out.writeInt(1);
12739 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12740 } else {
12741 out.writeInt(0);
12742 }
Bookatzb1f04f32017-05-19 13:57:32 -070012743 if (u.mBluetoothUnoptimizedScanTimer != null) {
12744 out.writeInt(1);
12745 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12746 } else {
12747 out.writeInt(0);
12748 }
Bookatz956f36bf2017-04-28 09:48:17 -070012749 if (u.mBluetoothScanResultCounter != null) {
12750 out.writeInt(1);
12751 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
12752 } else {
12753 out.writeInt(0);
12754 }
Bookatzb1f04f32017-05-19 13:57:32 -070012755 if (u.mBluetoothScanResultBgCounter != null) {
12756 out.writeInt(1);
12757 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
12758 } else {
12759 out.writeInt(0);
12760 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070012761 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
12762 if (u.mProcessStateTimer[i] != null) {
12763 out.writeInt(1);
12764 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12765 } else {
12766 out.writeInt(0);
12767 }
12768 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012769 if (u.mVibratorOnTimer != null) {
12770 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012771 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012772 } else {
12773 out.writeInt(0);
12774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012775
Dianne Hackborn617f8772009-03-31 15:04:46 -070012776 if (u.mUserActivityCounters == null) {
12777 out.writeInt(0);
12778 } else {
12779 out.writeInt(1);
12780 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
12781 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
12782 }
12783 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012784
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012785 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012786 out.writeInt(0);
12787 } else {
12788 out.writeInt(1);
12789 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012790 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
12791 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012792 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012793 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
12794 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012795 }
12796
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012797 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
12798 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
12799
Sudheer Shankaaf857412017-07-21 00:14:24 -070012800 if (u.mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012801 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -070012802 out.writeInt(u.mCpuClusterSpeedTimesUs.length);
12803 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012804 if (cpuSpeeds != null) {
12805 out.writeInt(1);
12806 out.writeInt(cpuSpeeds.length);
12807 for (LongSamplingCounter c : cpuSpeeds) {
12808 if (c != null) {
12809 out.writeInt(1);
12810 c.writeSummaryFromParcelLocked(out);
12811 } else {
12812 out.writeInt(0);
12813 }
12814 }
12815 } else {
12816 out.writeInt(0);
12817 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012818 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012819 } else {
12820 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012821 }
12822
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012823 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
12824 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012825
Adam Lesinski5f056f62016-07-14 16:56:08 -070012826 if (u.mMobileRadioApWakeupCount != null) {
12827 out.writeInt(1);
12828 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12829 } else {
12830 out.writeInt(0);
12831 }
12832
12833 if (u.mWifiRadioApWakeupCount != null) {
12834 out.writeInt(1);
12835 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12836 } else {
12837 out.writeInt(0);
12838 }
12839
Dianne Hackbornd953c532014-08-16 18:17:38 -070012840 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
12841 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012843 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012844 out.writeString(wakeStats.keyAt(iw));
12845 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012846 if (wl.mTimerFull != null) {
12847 out.writeInt(1);
12848 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12849 } else {
12850 out.writeInt(0);
12851 }
12852 if (wl.mTimerPartial != null) {
12853 out.writeInt(1);
12854 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12855 } else {
12856 out.writeInt(0);
12857 }
12858 if (wl.mTimerWindow != null) {
12859 out.writeInt(1);
12860 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12861 } else {
12862 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012864 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070012865 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012866 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070012867 } else {
12868 out.writeInt(0);
12869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012870 }
12871
Bookatz2bffb5b2017-04-13 11:59:33 -070012872 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012873 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012874 out.writeInt(NS);
12875 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012876 out.writeString(syncStats.keyAt(is));
12877 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012878 }
12879
Bookatzaa4594a2017-03-24 12:39:56 -070012880 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012881 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012882 out.writeInt(NJ);
12883 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012884 out.writeString(jobStats.keyAt(ij));
12885 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012886 }
12887
Dianne Hackborn94326cb2017-06-28 16:17:20 -070012888 u.writeJobCompletionsToParcelLocked(out);
12889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012890 int NSE = u.mSensorStats.size();
12891 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012892 for (int ise=0; ise<NSE; ise++) {
12893 out.writeInt(u.mSensorStats.keyAt(ise));
12894 Uid.Sensor se = u.mSensorStats.valueAt(ise);
12895 if (se.mTimer != null) {
12896 out.writeInt(1);
12897 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12898 } else {
12899 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 }
12901 }
12902
12903 int NP = u.mProcessStats.size();
12904 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012905 for (int ip=0; ip<NP; ip++) {
12906 out.writeString(u.mProcessStats.keyAt(ip));
12907 Uid.Proc ps = u.mProcessStats.valueAt(ip);
12908 out.writeLong(ps.mUserTime);
12909 out.writeLong(ps.mSystemTime);
12910 out.writeLong(ps.mForegroundTime);
12911 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012912 out.writeInt(ps.mNumCrashes);
12913 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012914 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 }
12916
12917 NP = u.mPackageStats.size();
12918 out.writeInt(NP);
12919 if (NP > 0) {
12920 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
12921 : u.mPackageStats.entrySet()) {
12922 out.writeString(ent.getKey());
12923 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012924 final int NWA = ps.mWakeupAlarms.size();
12925 out.writeInt(NWA);
12926 for (int iwa=0; iwa<NWA; iwa++) {
12927 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
12928 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
12929 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012930 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012931 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012932 for (int is=0; is<NS; is++) {
12933 out.writeString(ps.mServiceStats.keyAt(is));
12934 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
12935 long time = ss.getStartTimeToNowLocked(
12936 mOnBatteryTimeBase.getUptime(NOW_SYS));
12937 out.writeLong(time);
12938 out.writeInt(ss.mStarts);
12939 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012940 }
12941 }
12942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943 }
12944 }
12945
12946 public void readFromParcel(Parcel in) {
12947 readFromParcelLocked(in);
12948 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012950 void readFromParcelLocked(Parcel in) {
12951 int magic = in.readInt();
12952 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012953 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954 }
12955
Dianne Hackbornae384452011-06-28 12:33:48 -070012956 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012959 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012960 mStartPlatformVersion = in.readString();
12961 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012962 mUptime = in.readLong();
12963 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964 mRealtime = in.readLong();
12965 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012966 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012967 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012968 mMinLearnedBatteryCapacity = in.readInt();
12969 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012970 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012971 mOnBatteryTimeBase.readFromParcel(in);
12972 mOnBatteryScreenOffTimeBase.readFromParcel(in);
12973
Jeff Browne95c3cd2014-05-02 16:59:26 -070012974 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080012975 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070012976 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012977 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012978 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
12979 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012980 }
Dianne Hackborn29325132014-05-21 15:01:03 -070012981 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012982 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012983 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012984 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
12985 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012986 mLongestLightIdleTime = in.readLong();
12987 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080012988 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
12989 mOnBatteryTimeBase, in);
12990 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
12991 mOnBatteryTimeBase, in);
12992 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
12993 mOnBatteryTimeBase, in);
12994 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
12995 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012996 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012997 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012998 null, mOnBatteryTimeBase, in);
12999 }
Joe Onoratoabded112016-02-08 16:49:39 -080013000 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
13001 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013002 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080013003 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013004 null, mOnBatteryTimeBase, in);
13005 }
13006 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
13007 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
13008 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
13009 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013010 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080013011 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
13012 mOnBatteryTimeBase, in);
Kweku Adams87b19ec2017-10-09 12:40:03 -070013013 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
Joe Onoratoabded112016-02-08 16:49:39 -080013014 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013015 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013016 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
13017 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070013018 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013019 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080013020 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013021 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080013022 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
13023 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013024 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080013025 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013026 null, mOnBatteryTimeBase, in);
13027 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013028 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080013029 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070013030 null, mOnBatteryTimeBase, in);
13031 }
13032 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080013033 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070013034 null, mOnBatteryTimeBase, in);
13035 }
Adam Lesinski33dac552015-03-09 15:24:48 -070013036
Adam Lesinski21f76aa2016-01-25 12:27:06 -080013037 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
13038 NUM_WIFI_TX_LEVELS, in);
13039 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
13040 NUM_BT_TX_LEVELS, in);
13041 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
13042 ModemActivityInfo.TX_POWER_LEVELS, in);
13043 mHasWifiReporting = in.readInt() != 0;
13044 mHasBluetoothReporting = in.readInt() != 0;
13045 mHasModemReporting = in.readInt() != 0;
13046
Dianne Hackborn1e01d162014-12-04 17:46:42 -080013047 mNumConnectivityChange = in.readInt();
13048 mLoadedNumConnectivityChange = in.readInt();
13049 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070013050 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080013051 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070013052 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080013053 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013054 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080013055 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013056 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080013057 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013058 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080013059 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013060 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013061 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070013062 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013063 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013064 mLowDischargeAmountSinceCharge = in.readInt();
13065 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013066 mDischargeAmountScreenOn = in.readInt();
13067 mDischargeAmountScreenOnSinceCharge = in.readInt();
13068 mDischargeAmountScreenOff = in.readInt();
13069 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070013070 mDischargeAmountScreenDoze = in.readInt();
13071 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013072 mDischargeStepTracker.readFromParcel(in);
13073 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070013074 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070013075 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase, in);
13076 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
Mike Ma15313c92017-11-15 17:58:21 -080013077 mDischargeLightDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
13078 mDischargeDeepDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079 mLastWriteTime = in.readLong();
13080
Bookatz50df7112017-08-04 14:53:26 -070013081 mRpmStats.clear();
13082 int NRPMS = in.readInt();
13083 for (int irpm = 0; irpm < NRPMS; irpm++) {
13084 if (in.readInt() != 0) {
13085 String rpmName = in.readString();
13086 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
13087 mRpmStats.put(rpmName, rpmt);
13088 }
13089 }
13090 mScreenOffRpmStats.clear();
13091 int NSORPMS = in.readInt();
13092 for (int irpm = 0; irpm < NSORPMS; irpm++) {
13093 if (in.readInt() != 0) {
13094 String rpmName = in.readString();
13095 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
13096 mScreenOffRpmStats.put(rpmName, rpmt);
13097 }
13098 }
13099
Evan Millarc64edde2009-04-18 12:26:32 -070013100 mKernelWakelockStats.clear();
13101 int NKW = in.readInt();
13102 for (int ikw = 0; ikw < NKW; ikw++) {
13103 if (in.readInt() != 0) {
13104 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080013105 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070013106 mKernelWakelockStats.put(wakelockName, kwlt);
13107 }
13108 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013109
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013110 mWakeupReasonStats.clear();
13111 int NWR = in.readInt();
13112 for (int iwr = 0; iwr < NWR; iwr++) {
13113 if (in.readInt() != 0) {
13114 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080013115 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013116 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013117 }
13118 }
13119
James Carr3a226052016-07-01 14:49:52 -070013120 mKernelMemoryStats.clear();
13121 int nmt = in.readInt();
13122 for (int imt = 0; imt < nmt; imt++) {
13123 if (in.readInt() != 0) {
13124 Long bucket = in.readLong();
13125 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
13126 mKernelMemoryStats.put(bucket, kmt);
13127 }
13128 }
13129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013130 mPartialTimers.clear();
13131 mFullTimers.clear();
13132 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013133 mWifiRunningTimers.clear();
13134 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070013135 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070013136 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013137 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070013138 mAudioTurnedOnTimers.clear();
13139 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013140 mFlashlightTurnedOnTimers.clear();
13141 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013142
13143 int numUids = in.readInt();
13144 mUidStats.clear();
13145 for (int i = 0; i < numUids; i++) {
13146 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080013147 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013148 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013149 mUidStats.append(uid, u);
13150 }
13151 }
13152
13153 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013154 writeToParcelLocked(out, true, flags);
13155 }
13156
13157 public void writeToParcelWithoutUids(Parcel out, int flags) {
13158 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013159 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013160
13161 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013162 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013163 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080013164 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013165
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070013166 // Pull the clock time. This may update the time and make a new history entry
13167 // if we had originally pulled a time before the RTC was set.
13168 long startClockTime = getStartClockTime();
13169
Joe Onoratoabded112016-02-08 16:49:39 -080013170 final long uSecUptime = mClocks.uptimeMillis() * 1000;
13171 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013172 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
13173 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013175 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013176
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070013177 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013179 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070013180 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070013181 out.writeString(mStartPlatformVersion);
13182 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013183 out.writeLong(mUptime);
13184 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013185 out.writeLong(mRealtime);
13186 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070013188 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070013189 out.writeInt(mMinLearnedBatteryCapacity);
13190 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013191 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
13192 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
13193
13194 mScreenOnTimer.writeToParcel(out, uSecRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -070013195 mScreenDozeTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013196 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
13197 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
13198 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070013199 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070013200 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070013201 out.writeLong(mLongestLightIdleTime);
13202 out.writeLong(mLongestFullIdleTime);
13203 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
13204 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
13205 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013206 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013207 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
13208 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
13209 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
13210 }
13211 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
13212 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
13213 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
13214 }
13215 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
13216 mNetworkByteActivityCounters[i].writeToParcel(out);
13217 mNetworkPacketActivityCounters[i].writeToParcel(out);
13218 }
13219 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
13220 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013221 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013222 mMobileRadioActiveUnknownTime.writeToParcel(out);
13223 mMobileRadioActiveUnknownCount.writeToParcel(out);
13224 mWifiOnTimer.writeToParcel(out, uSecRealtime);
13225 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
13226 for (int i=0; i<NUM_WIFI_STATES; i++) {
13227 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
13228 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013229 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13230 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
13231 }
13232 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13233 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
13234 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080013235 mWifiActivity.writeToParcel(out, 0);
13236 mBluetoothActivity.writeToParcel(out, 0);
13237 mModemActivity.writeToParcel(out, 0);
13238 out.writeInt(mHasWifiReporting ? 1 : 0);
13239 out.writeInt(mHasBluetoothReporting ? 1 : 0);
13240 out.writeInt(mHasModemReporting ? 1 : 0);
13241
Dianne Hackborn1e01d162014-12-04 17:46:42 -080013242 out.writeInt(mNumConnectivityChange);
13243 out.writeInt(mLoadedNumConnectivityChange);
13244 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070013245 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013246 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013247 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013248 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013249 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070013250 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013251 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013252 out.writeInt(mLowDischargeAmountSinceCharge);
13253 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013254 out.writeInt(mDischargeAmountScreenOn);
13255 out.writeInt(mDischargeAmountScreenOnSinceCharge);
13256 out.writeInt(mDischargeAmountScreenOff);
13257 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Mike Mac2f518a2017-09-19 16:06:03 -070013258 out.writeInt(mDischargeAmountScreenDoze);
13259 out.writeInt(mDischargeAmountScreenDozeSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013260 mDischargeStepTracker.writeToParcel(out);
13261 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070013262 mDischargeCounter.writeToParcel(out);
13263 mDischargeScreenOffCounter.writeToParcel(out);
Mike Mac2f518a2017-09-19 16:06:03 -070013264 mDischargeScreenDozeCounter.writeToParcel(out);
Mike Ma15313c92017-11-15 17:58:21 -080013265 mDischargeLightDozeCounter.writeToParcel(out);
13266 mDischargeDeepDozeCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013267 out.writeLong(mLastWriteTime);
13268
Bookatz50df7112017-08-04 14:53:26 -070013269 out.writeInt(mRpmStats.size());
13270 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
13271 SamplingTimer rpmt = ent.getValue();
13272 if (rpmt != null) {
13273 out.writeInt(1);
13274 out.writeString(ent.getKey());
13275 rpmt.writeToParcel(out, uSecRealtime);
13276 } else {
13277 out.writeInt(0);
13278 }
13279 }
13280 out.writeInt(mScreenOffRpmStats.size());
13281 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
13282 SamplingTimer rpmt = ent.getValue();
13283 if (rpmt != null) {
13284 out.writeInt(1);
13285 out.writeString(ent.getKey());
13286 rpmt.writeToParcel(out, uSecRealtime);
13287 } else {
13288 out.writeInt(0);
13289 }
13290 }
13291
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013292 if (inclUids) {
13293 out.writeInt(mKernelWakelockStats.size());
13294 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
13295 SamplingTimer kwlt = ent.getValue();
13296 if (kwlt != null) {
13297 out.writeInt(1);
13298 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013299 kwlt.writeToParcel(out, uSecRealtime);
13300 } else {
13301 out.writeInt(0);
13302 }
13303 }
13304 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013305 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
13306 SamplingTimer timer = ent.getValue();
13307 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013308 out.writeInt(1);
13309 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013310 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013311 } else {
13312 out.writeInt(0);
13313 }
Evan Millarc64edde2009-04-18 12:26:32 -070013314 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013315 } else {
Bookatzba78b2b2017-10-10 11:06:33 -070013316 out.writeInt(0);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013317 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070013318 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070013319
James Carr3a226052016-07-01 14:49:52 -070013320 out.writeInt(mKernelMemoryStats.size());
13321 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
13322 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
13323 if (kmt != null) {
13324 out.writeInt(1);
13325 out.writeLong(mKernelMemoryStats.keyAt(i));
13326 kmt.writeToParcel(out, uSecRealtime);
13327 } else {
13328 out.writeInt(0);
13329 }
13330 }
13331
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013332 if (inclUids) {
13333 int size = mUidStats.size();
13334 out.writeInt(size);
13335 for (int i = 0; i < size; i++) {
13336 out.writeInt(mUidStats.keyAt(i));
13337 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338
Bookatz867c0d72017-03-07 18:23:42 -080013339 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013340 }
13341 } else {
13342 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343 }
13344 }
13345
13346 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
13347 new Parcelable.Creator<BatteryStatsImpl>() {
13348 public BatteryStatsImpl createFromParcel(Parcel in) {
13349 return new BatteryStatsImpl(in);
13350 }
13351
13352 public BatteryStatsImpl[] newArray(int size) {
13353 return new BatteryStatsImpl[size];
13354 }
13355 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013356
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013357 public void prepareForDumpLocked() {
13358 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080013359 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070013360
13361 // Pull the clock time. This may update the time and make a new history entry
13362 // if we had originally pulled a time before the RTC was set.
13363 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013364 }
13365
Dianne Hackbornc51cf032014-03-02 19:08:15 -080013366 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013367 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013368 pw.println("mOnBatteryTimeBase:");
13369 mOnBatteryTimeBase.dump(pw, " ");
13370 pw.println("mOnBatteryScreenOffTimeBase:");
13371 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013372 Printer pr = new PrintWriterPrinter(pw);
Mike Mac2f518a2017-09-19 16:06:03 -070013373 pr.println("*** Screen on timer:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013374 mScreenOnTimer.logState(pr, " ");
Mike Mac2f518a2017-09-19 16:06:03 -070013375 pr.println("*** Screen doze timer:");
13376 mScreenDozeTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070013377 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013378 pr.println("*** Screen brightness #" + i + ":");
13379 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070013380 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070013381 pr.println("*** Interactive timer:");
13382 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070013383 pr.println("*** Power save mode timer:");
13384 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070013385 pr.println("*** Device idle mode light timer:");
13386 mDeviceIdleModeLightTimer.logState(pr, " ");
13387 pr.println("*** Device idle mode full timer:");
13388 mDeviceIdleModeFullTimer.logState(pr, " ");
13389 pr.println("*** Device light idling timer:");
13390 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013391 pr.println("*** Device idling timer:");
13392 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013393 pr.println("*** Phone timer:");
13394 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080013395 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070013396 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013397 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013398 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070013399 pr.println("*** Signal scanning :");
13400 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013401 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013402 pr.println("*** Data connection type #" + i + ":");
13403 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013404 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013405 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080013406 pr.println("*** Mobile network active timer:");
13407 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013408 pr.println("*** Mobile network active adjusted timer:");
13409 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070013410 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013411 pr.println("*** Wifi timer:");
13412 mWifiOnTimer.logState(pr, " ");
13413 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013414 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080013415 for (int i=0; i<NUM_WIFI_STATES; i++) {
13416 pr.println("*** Wifi state #" + i + ":");
13417 mWifiStateTimer[i].logState(pr, " ");
13418 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013419 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13420 pr.println("*** Wifi suppl state #" + i + ":");
13421 mWifiSupplStateTimer[i].logState(pr, " ");
13422 }
13423 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13424 pr.println("*** Wifi signal strength #" + i + ":");
13425 mWifiSignalStrengthsTimer[i].logState(pr, " ");
13426 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070013427 pr.println("*** Flashlight timer:");
13428 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013429 pr.println("*** Camera timer:");
13430 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013431 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080013432 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013434}